From 3f98e1ff4ea49c9dea2631a608b50796027d185d Mon Sep 17 00:00:00 2001 From: Marcel Date: Mon, 6 Jan 2020 20:49:48 +0100 Subject: [PATCH] =?UTF-8?q?docker=5Fcontainer=20-=20#65993=20-=20update=20?= =?UTF-8?q?restart=20policy=20(restart=20policy=20&=20restart=20retries)?= =?UTF-8?q?=20wit=E2=80=A6=20(#66192)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * #65993 - update restart policy (restart policy & restart retries) without restarting the container * - proper indentation on the continuation-line - set restart_policy to the correct value independent from the api version * - move restart_policy definitions into the if block - add a new variable for the restart_policy configuration value * add changelog fragment * typo; minus -> underscore * rename changelog fragment to contain the correct module name * rename restart_policy_config_value to just restart_policy and refer to the correct dict values (cherry picked from commit 02c126f5ee451a7dc1b442d15a883bc3f86505d1) --- ...r_container-on-restart-policy-updates.yaml | 2 ++ .../modules/cloud/docker/docker_container.py | 25 +++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/65993-restart-docker_container-on-restart-policy-updates.yaml diff --git a/changelogs/fragments/65993-restart-docker_container-on-restart-policy-updates.yaml b/changelogs/fragments/65993-restart-docker_container-on-restart-policy-updates.yaml new file mode 100644 index 00000000000000..101f57207c9663 --- /dev/null +++ b/changelogs/fragments/65993-restart-docker_container-on-restart-policy-updates.yaml @@ -0,0 +1,2 @@ +minor_changes: + - docker_container.py - update a containers restart_policy without restarting the container (https://github.com/ansible/ansible/issues/65993) diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index 35aa926afd2f1d..201679f8bb1d28 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -1255,12 +1255,17 @@ def update_parameters(self): mem_reservation='memory_reservation', memswap_limit='memory_swap', kernel_memory='kernel_memory', + restart_policy='restart_policy', ) result = dict() for key, value in update_parameters.items(): if getattr(self, value, None) is not None: - if self.client.option_minimal_versions[value]['supported']: + if key == 'restart_policy' and self.client.option_minimal_versions[value]['supported']: + restart_policy = dict(Name=self.restart_policy, + MaximumRetryCount=self.restart_retries) + result[key] = restart_policy + elif self.client.option_minimal_versions[value]['supported']: result[key] = getattr(self, value) return result @@ -1819,7 +1824,6 @@ def has_different_configuration(self, image): host_config = self.container['HostConfig'] log_config = host_config.get('LogConfig', dict()) - restart_policy = host_config.get('RestartPolicy', dict()) config = self.container['Config'] network = self.container['NetworkSettings'] @@ -1866,7 +1870,6 @@ def has_different_configuration(self, image): privileged=host_config.get('Privileged'), expected_ports=host_config.get('PortBindings'), read_only=host_config.get('ReadonlyRootfs'), - restart_policy=restart_policy.get('Name'), runtime=host_config.get('Runtime'), shm_size=host_config.get('ShmSize'), security_opts=host_config.get("SecurityOpt"), @@ -1891,8 +1894,6 @@ def has_different_configuration(self, image): pids_limit=host_config.get('PidsLimit'), ) # Options which don't make sense without their accompanying option - if self.parameters.restart_policy: - config_mapping['restart_retries'] = restart_policy.get('MaximumRetryCount') if self.parameters.log_driver: config_mapping['log_driver'] = log_config.get('Type') config_mapping['log_options'] = log_config.get('Config') @@ -1914,6 +1915,12 @@ def has_different_configuration(self, image): # we need to handle all limits which are usually handled by # update_container() as configuration changes which require a container # restart. + restart_policy = host_config.get('RestartPolicy', dict()) + + # Options which don't make sense without their accompanying option + if self.parameters.restart_policy: + config_mapping['restart_retries'] = restart_policy.get('MaximumRetryCount') + config_mapping.update(dict( blkio_weight=host_config.get('BlkioWeight'), cpu_period=host_config.get('CpuPeriod'), @@ -1925,6 +1932,7 @@ def has_different_configuration(self, image): memory=host_config.get('Memory'), memory_reservation=host_config.get('MemoryReservation'), memory_swap=host_config.get('MemorySwap'), + restart_policy=restart_policy.get('Name') )) differences = DifferenceTracker() @@ -1971,6 +1979,8 @@ def has_different_resource_limits(self): host_config = self.container['HostConfig'] + restart_policy = host_config.get('RestartPolicy') or dict() + config_mapping = dict( blkio_weight=host_config.get('BlkioWeight'), cpu_period=host_config.get('CpuPeriod'), @@ -1982,8 +1992,13 @@ def has_different_resource_limits(self): memory=host_config.get('Memory'), memory_reservation=host_config.get('MemoryReservation'), memory_swap=host_config.get('MemorySwap'), + restart_policy=restart_policy.get('Name') ) + # Options which don't make sense without their accompanying option + if self.parameters.restart_policy: + config_mapping['restart_retries'] = restart_policy.get('MaximumRetryCount') + differences = DifferenceTracker() for key, value in config_mapping.items(): if getattr(self.parameters, key, None):