Browse Source

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
changes/24/708024/4
Emilien Macchi 3 years ago
parent
commit
d6577a1590
  1. 62
      tripleo_ansible/ansible_plugins/modules/container_puppet_config.py
  2. 1
      tripleo_ansible/roles/tripleo_container_manage/defaults/main.yml
  3. 4
      tripleo_ansible/roles/tripleo_container_manage/tasks/main.yml
  4. 19
      tripleo_ansible/roles/tripleo_container_manage/tasks/puppet_config.yml

62
tripleo_ansible/ansible_plugins/modules/container_puppet_config.py

@ -79,15 +79,27 @@ options:
puppet_config:
description: Path to the puppet configs
type: str
default: ""
short_hostname:
description:
- Short hostname
type: str
default: ""
step:
description:
- Step number
default: 6
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 = """
@ -96,6 +108,10 @@ EXAMPLES = """
step: 1
puppet-config: /var/lib/container-puppet/container-puppet.json
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'
@ -121,31 +137,34 @@ class ContainerPuppetManager:
# Set parameters
puppet_config = args['puppet_config']
data = json.loads(self._slurp(puppet_config))
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']
update_config_hash_only = args['update_config_hash_only']
self.config_vol_prefix = args['config_vol_prefix']
self.hostname = args['short_hostname']
config_path = os.path.join(CONTAINER_PUPPET_CONFIG,
'step_' + str(self.step))
if not update_config_hash_only:
data = json.loads(self._slurp(puppet_config))
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.hostname = args['short_hostname']
# Cleanup old configs generated in previous versions
self._cleanup_old_configs()
config_path = os.path.join(CONTAINER_PUPPET_CONFIG,
'step_' + str(self.step))
# Make sure config_path exists
# Note: it'll cleanup old configs before creating new ones.
self._create_dir(config_path)
# Cleanup old configs generated in previous versions
self._cleanup_old_configs()
# 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)
# Make sure config_path exists
# 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
self._update_hashes()
@ -415,6 +434,7 @@ class ContainerPuppetManager:
f = open(path, 'wb')
f.write(json.dumps(config, indent=2).encode('utf-8'))
os.chmod(path, 0o600)
self.results['changed'] = True
def _get_config_hash(self, config_volume):
"""Returns a config hash from a config_volume.
@ -470,8 +490,6 @@ class ContainerPuppetManager:
def _update_hashes(self):
"""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)
for config in configs:
old_config_hash = ''

1
tripleo_ansible/roles/tripleo_container_manage/defaults/main.yml

@ -18,6 +18,7 @@
# All variables intended for modification should place placed in this file.
# 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_concurrency: 1
tripleo_container_manage_config: "/var/lib/tripleo-config/"

4
tripleo_ansible/roles/tripleo_container_manage/tasks/main.yml

@ -97,6 +97,10 @@
- tripleo_container_manage_cli == 'podman'
become: true
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 }}"
include_tasks: delete.yml
- name: "Create containers from {{ tripleo_container_manage_config }}"

19
tripleo_ansible/roles/tripleo_container_manage/tasks/puppet_config.yml

@ -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
Loading…
Cancel
Save