Browse code

docker_container - #65993 - update restart policy (restart policy & restart retries) wit… (#66192)

* #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)

Marcel authored on 2020/01/07 04:49:48
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,2 @@
0
+minor_changes:
1
+  - docker_container.py - update a containers restart_policy without restarting the container (https://github.com/ansible/ansible/issues/65993)
... ...
@@ -1255,12 +1255,17 @@ class TaskParameters(DockerBaseClass):
1255 1255
             mem_reservation='memory_reservation',
1256 1256
             memswap_limit='memory_swap',
1257 1257
             kernel_memory='kernel_memory',
1258
+            restart_policy='restart_policy',
1258 1259
         )
1259 1260
 
1260 1261
         result = dict()
1261 1262
         for key, value in update_parameters.items():
1262 1263
             if getattr(self, value, None) is not None:
1263
-                if self.client.option_minimal_versions[value]['supported']:
1264
+                if key == 'restart_policy' and self.client.option_minimal_versions[value]['supported']:
1265
+                    restart_policy = dict(Name=self.restart_policy,
1266
+                                          MaximumRetryCount=self.restart_retries)
1267
+                    result[key] = restart_policy
1268
+                elif self.client.option_minimal_versions[value]['supported']:
1264 1269
                     result[key] = getattr(self, value)
1265 1270
         return result
1266 1271
 
... ...
@@ -1819,7 +1824,6 @@ class Container(DockerBaseClass):
1819 1819
 
1820 1820
         host_config = self.container['HostConfig']
1821 1821
         log_config = host_config.get('LogConfig', dict())
1822
-        restart_policy = host_config.get('RestartPolicy', dict())
1823 1822
         config = self.container['Config']
1824 1823
         network = self.container['NetworkSettings']
1825 1824
 
... ...
@@ -1866,7 +1870,6 @@ class Container(DockerBaseClass):
1866 1866
             privileged=host_config.get('Privileged'),
1867 1867
             expected_ports=host_config.get('PortBindings'),
1868 1868
             read_only=host_config.get('ReadonlyRootfs'),
1869
-            restart_policy=restart_policy.get('Name'),
1870 1869
             runtime=host_config.get('Runtime'),
1871 1870
             shm_size=host_config.get('ShmSize'),
1872 1871
             security_opts=host_config.get("SecurityOpt"),
... ...
@@ -1891,8 +1894,6 @@ class Container(DockerBaseClass):
1891 1891
             pids_limit=host_config.get('PidsLimit'),
1892 1892
         )
1893 1893
         # Options which don't make sense without their accompanying option
1894
-        if self.parameters.restart_policy:
1895
-            config_mapping['restart_retries'] = restart_policy.get('MaximumRetryCount')
1896 1894
         if self.parameters.log_driver:
1897 1895
             config_mapping['log_driver'] = log_config.get('Type')
1898 1896
             config_mapping['log_options'] = log_config.get('Config')
... ...
@@ -1914,6 +1915,12 @@ class Container(DockerBaseClass):
1914 1914
             # we need to handle all limits which are usually handled by
1915 1915
             # update_container() as configuration changes which require a container
1916 1916
             # restart.
1917
+            restart_policy = host_config.get('RestartPolicy', dict())
1918
+
1919
+            # Options which don't make sense without their accompanying option
1920
+            if self.parameters.restart_policy:
1921
+                config_mapping['restart_retries'] = restart_policy.get('MaximumRetryCount')
1922
+
1917 1923
             config_mapping.update(dict(
1918 1924
                 blkio_weight=host_config.get('BlkioWeight'),
1919 1925
                 cpu_period=host_config.get('CpuPeriod'),
... ...
@@ -1925,6 +1932,7 @@ class Container(DockerBaseClass):
1925 1925
                 memory=host_config.get('Memory'),
1926 1926
                 memory_reservation=host_config.get('MemoryReservation'),
1927 1927
                 memory_swap=host_config.get('MemorySwap'),
1928
+                restart_policy=restart_policy.get('Name')
1928 1929
             ))
1929 1930
 
1930 1931
         differences = DifferenceTracker()
... ...
@@ -1971,6 +1979,8 @@ class Container(DockerBaseClass):
1971 1971
 
1972 1972
         host_config = self.container['HostConfig']
1973 1973
 
1974
+        restart_policy = host_config.get('RestartPolicy') or dict()
1975
+
1974 1976
         config_mapping = dict(
1975 1977
             blkio_weight=host_config.get('BlkioWeight'),
1976 1978
             cpu_period=host_config.get('CpuPeriod'),
... ...
@@ -1982,8 +1992,13 @@ class Container(DockerBaseClass):
1982 1982
             memory=host_config.get('Memory'),
1983 1983
             memory_reservation=host_config.get('MemoryReservation'),
1984 1984
             memory_swap=host_config.get('MemorySwap'),
1985
+            restart_policy=restart_policy.get('Name')
1985 1986
         )
1986 1987
 
1988
+        # Options which don't make sense without their accompanying option
1989
+        if self.parameters.restart_policy:
1990
+            config_mapping['restart_retries'] = restart_policy.get('MaximumRetryCount')
1991
+
1987 1992
         differences = DifferenceTracker()
1988 1993
         for key, value in config_mapping.items():
1989 1994
             if getattr(self.parameters, key, None):