Allow to update container startup config with new config hashes

1) container_puppet_config: introduce update_config_hash_only

If update_config_hash_only is set to True, the module will do the
following:

Browse the container startup configs and for each config, check if the
config_hash has changed (e.g. Puppet has run before and generated a new
config, therefore the container will need a restart); then update the
startup configs with the new hash.

This extends container_puppet_config capabilities instead of writting a
new module for that.

2) tripleo_container_manage: add tripleo_container_manage_check_puppet_config

tripleo_container_manage_check_puppet_config is a new parameter, that is
set to False by default but if set to True, we will call the
container_puppet_config module with update_config_hash_only set to True
so we get the new config hashes in the container startup configs right
before we decide if a container needs to be restarted.

Change-Id: I16b2972cdf79cd6ac927925607197ec2a969a28b
This commit is contained in:
Emilien Macchi 2020-02-16 10:38:48 -05:00
parent 3c4078cd62
commit d6577a1590
4 changed files with 64 additions and 22 deletions

View File

@ -79,15 +79,27 @@ options:
puppet_config: puppet_config:
description: Path to the puppet configs description: Path to the puppet configs
type: str type: str
default: ""
short_hostname: short_hostname:
description: description:
- Short hostname - Short hostname
type: str type: str
default: ""
step: step:
description: description:
- Step number - Step number
default: 6 default: 6
type: int type: int
update_config_hash_only:
description:
- When set to True, the module will only inspect for new config hashes
in config_vol_prefix and make sure the container-startup-configs
are updated with these hashes. This is useful to execute
before we manage the startup containers, so they will be restarted
if needed (e.g. new config has been applied, container needs
restart).
type: bool
default: False
""" """
EXAMPLES = """ EXAMPLES = """
@ -96,6 +108,10 @@ EXAMPLES = """
step: 1 step: 1
puppet-config: /var/lib/container-puppet/container-puppet.json puppet-config: /var/lib/container-puppet/container-puppet.json
short_hostname: "{{ ansible_hostname }}" short_hostname: "{{ ansible_hostname }}"
update_config_hash_only: false
- name: Update config hashes for container startup configs
container_puppet_config:
update_config_hash_only: true
""" """
CONTAINER_PUPPET_CONFIG = '/var/lib/tripleo-config/container-puppet-config' CONTAINER_PUPPET_CONFIG = '/var/lib/tripleo-config/container-puppet-config'
@ -121,31 +137,34 @@ class ContainerPuppetManager:
# Set parameters # Set parameters
puppet_config = args['puppet_config'] puppet_config = args['puppet_config']
data = json.loads(self._slurp(puppet_config)) update_config_hash_only = args['update_config_hash_only']
self.step = args['step']
self.net_host = args['net_host']
self.debug = args['debug']
self.check = args['check_mode']
self.no_archive = args['no_archive']
self.config_vol_prefix = args['config_vol_prefix'] self.config_vol_prefix = args['config_vol_prefix']
self.hostname = args['short_hostname']
config_path = os.path.join(CONTAINER_PUPPET_CONFIG, if not update_config_hash_only:
'step_' + str(self.step)) data = json.loads(self._slurp(puppet_config))
# Cleanup old configs generated in previous versions self.step = args['step']
self._cleanup_old_configs() self.net_host = args['net_host']
self.debug = args['debug']
self.check = args['check_mode']
self.no_archive = args['no_archive']
self.hostname = args['short_hostname']
# Make sure config_path exists config_path = os.path.join(CONTAINER_PUPPET_CONFIG,
# Note: it'll cleanup old configs before creating new ones. 'step_' + str(self.step))
self._create_dir(config_path)
# Generate the container configs # Cleanup old configs generated in previous versions
config = self._get_config(self._merge_volumes_configs(data)) self._cleanup_old_configs()
for k, v in config.items():
config_dest = os.path.join(config_path, k + '.json') # Make sure config_path exists
self._update_container_config(config_dest, v) # Note: it'll cleanup old configs before creating new ones.
self._create_dir(config_path)
# Generate the container configs
config = self._get_config(self._merge_volumes_configs(data))
for k, v in config.items():
config_dest = os.path.join(config_path, k + '.json')
self._update_container_config(config_dest, v)
# Update container-startup-config with new config hashes # Update container-startup-config with new config hashes
self._update_hashes() self._update_hashes()
@ -415,6 +434,7 @@ class ContainerPuppetManager:
f = open(path, 'wb') f = open(path, 'wb')
f.write(json.dumps(config, indent=2).encode('utf-8')) f.write(json.dumps(config, indent=2).encode('utf-8'))
os.chmod(path, 0o600) os.chmod(path, 0o600)
self.results['changed'] = True
def _get_config_hash(self, config_volume): def _get_config_hash(self, config_volume):
"""Returns a config hash from a config_volume. """Returns a config hash from a config_volume.
@ -470,8 +490,6 @@ class ContainerPuppetManager:
def _update_hashes(self): def _update_hashes(self):
"""Update container startup config with new config hashes if needed. """Update container startup config with new config hashes if needed.
""" """
startup_config_path = os.path.join(CONTAINER_STARTUP_CONFIG,
'step_' + str(self.step))
configs = self._find(CONTAINER_STARTUP_CONFIG) configs = self._find(CONTAINER_STARTUP_CONFIG)
for config in configs: for config in configs:
old_config_hash = '' old_config_hash = ''

View File

@ -18,6 +18,7 @@
# All variables intended for modification should place placed in this file. # All variables intended for modification should place placed in this file.
# All variables within this role should have a prefix of "tripleo_container_manage" # All variables within this role should have a prefix of "tripleo_container_manage"
tripleo_container_manage_check_puppet_config: false
tripleo_container_manage_cli: podman tripleo_container_manage_cli: podman
tripleo_container_manage_concurrency: 1 tripleo_container_manage_concurrency: 1
tripleo_container_manage_config: "/var/lib/tripleo-config/" tripleo_container_manage_config: "/var/lib/tripleo-config/"

View File

@ -97,6 +97,10 @@
- tripleo_container_manage_cli == 'podman' - tripleo_container_manage_cli == 'podman'
become: true become: true
block: block:
- name: "Update container configs with new config hashes"
include_tasks: puppet_config.yml
when:
- tripleo_container_manage_check_puppet_config|bool
- name: "Delete containers from {{ tripleo_container_manage_config }}" - name: "Delete containers from {{ tripleo_container_manage_config }}"
include_tasks: delete.yml include_tasks: delete.yml
- name: "Create containers from {{ tripleo_container_manage_config }}" - name: "Create containers from {{ tripleo_container_manage_config }}"

View File

@ -0,0 +1,19 @@
---
# Copyright 2019 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
- name: Update config hashes for container startup configs
container_puppet_config:
update_config_hash_only: true