TripleO Ansible project repository. Contains playbooks for use with TripleO OpenStack deployments.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
7.6KB

  1. # Copyright 2019 Red Hat, Inc.
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. __metaclass__ = type
  16. from ansible.module_utils.basic import AnsibleModule
  17. import json
  18. import os
  19. import paunch as p
  20. import re
  21. import yaml
  22. from paunch import runner as prunner
  23. from paunch.builder import compose1 as pcompose1
  24. from paunch.builder import podman as ppodman
  25. from paunch.utils import common as putils_common
  26. ANSIBLE_METADATA = {
  27. 'metadata_version': '1.0',
  28. 'status': ['preview'],
  29. 'supported_by': 'community'
  30. }
  31. DOCUMENTATION = """
  32. ---
  33. module: paunch
  34. author:
  35. - OpenStack TripleO Contributors
  36. version_added: '1.0'
  37. short_description: Manage containers with Paunch
  38. notes: []
  39. requirements:
  40. - "paunch module"
  41. - "podman or docker"
  42. description:
  43. - Start or stop containers with Paunch
  44. options:
  45. config:
  46. description:
  47. - JSON file or directory of JSON files containing configuration data
  48. config_id:
  49. description:
  50. - ID to assign to containers
  51. required: True
  52. type: list
  53. action:
  54. description:
  55. - The desired action to apply for the container.
  56. default: apply
  57. choices:
  58. - apply
  59. - cleanup
  60. config_overrides:
  61. description:
  62. - Dictionary to override containers configs
  63. default: {}
  64. type: dict
  65. container_cli:
  66. description:
  67. - The container CLI.
  68. default: podman
  69. choices:
  70. - podman
  71. - docker
  72. container_log_stdout_path:
  73. description:
  74. - Absolute path to a directory where container stdout will be stored.
  75. default: /var/log/containers/stdouts
  76. healthcheck_disabled:
  77. description:
  78. - Whether or not we disable the Containers Healthchecks
  79. type: bool
  80. default: False
  81. managed_by:
  82. description:
  83. - Name of the tool managing the containers. Only containers labelled with
  84. this will be modified
  85. default: paunch
  86. debug:
  87. description:
  88. - Whether or not we enable Debug
  89. type: bool
  90. default: True
  91. log_file:
  92. description:
  93. - Absolute path for the paunch log file.
  94. default: /var/log/paunch.log
  95. cleanup:
  96. description:
  97. - Whether or not we cleanup containers not in config. Useful only for apply action.
  98. type: bool
  99. default: True
  100. """
  101. EXAMPLES = """
  102. # Paunch apply example
  103. - name: Start containers for step 1
  104. paunch:
  105. config: /var/lib/tripleo-config/hashed-container-startup-config-step_1.json
  106. config_overrides:
  107. haproxy:
  108. image: docker.io/tripleomaster/centos-binary-haproxy:current-tripleo-hotfix
  109. config_id: tripleo_step1
  110. cleanup: false
  111. action: apply
  112. # Paunch cleanup example
  113. - name: Cleanup containers for step 1 and step 2
  114. paunch:
  115. config_id:
  116. - tripleo_step1
  117. - tripleo_step2
  118. action: cleanup
  119. """
  120. class PaunchManager:
  121. def __init__(self, module, results):
  122. super(PaunchManager, self).__init__()
  123. self.module = module
  124. self.results = results
  125. # Fail early if containers were not deployed by Paunch before.
  126. if os.path.isfile('/var/lib/tripleo-config/.ansible-managed'):
  127. msg = ('Containers were previously deployed with '
  128. 'tripleo-ansible, paunch module can not be used. '
  129. 'Make sure EnablePaunch is set to False.')
  130. self.module.fail_json(
  131. msg=msg,
  132. stdout='',
  133. stderr='',
  134. rc=1)
  135. self.config = self.module.params['config']
  136. if (isinstance(self.module.params['config_id'], list)
  137. and len(self.module.params['config_id']) == 1):
  138. self.config_id = self.module.params['config_id'][0]
  139. else:
  140. self.config_id = self.module.params['config_id']
  141. self.action = self.module.params['action']
  142. self.healthcheck_disabled = \
  143. self.module.params['healthcheck_disabled']
  144. self.container_cli = self.module.params['container_cli']
  145. self.cleanup = self.module.params['cleanup']
  146. self.config_overrides = self.module.params['config_overrides']
  147. self.container_log_stdout_path = \
  148. self.module.params['container_log_stdout_path']
  149. self.managed_by = self.module.params['managed_by']
  150. self.debug = self.module.params['debug']
  151. self.log_file = self.module.params['log_file']
  152. if self.debug:
  153. self.log_level = 3
  154. else:
  155. # if debug is disabled, only show WARNING level
  156. self.log_level = 1
  157. self.log = putils_common.configure_logging('paunch-ansible',
  158. level=self.log_level,
  159. log_file=self.log_file)
  160. if self.config:
  161. if self.config.endswith('.json'):
  162. self.module.warn('Only one config was given, cleanup disabled')
  163. self.cleanup = False
  164. self.config_yaml = putils_common.load_config(
  165. self.config,
  166. overrides=self.config_overrides)
  167. if self.action == 'apply':
  168. self.paunch_apply()
  169. elif self.action == 'cleanup':
  170. self.paunch_cleanup()
  171. def paunch_apply(self):
  172. self.results['action'].append('Applying config_id %s' % self.config_id)
  173. if not self.config:
  174. self.module.fail_json(
  175. msg="Paunch apply requires 'config' parameter",
  176. stdout='',
  177. stderr='',
  178. rc=1)
  179. stdout_list, stderr_list, rc = p.apply(
  180. self.config_id,
  181. self.config_yaml,
  182. managed_by=self.managed_by,
  183. labels=[],
  184. cont_cmd=self.container_cli,
  185. log_level=self.log_level,
  186. log_file=self.log_file,
  187. cont_log_path=self.container_log_stdout_path,
  188. healthcheck_disabled=self.healthcheck_disabled,
  189. cleanup=self.cleanup
  190. )
  191. stdout, stderr = ["\n".join(i) for i in (stdout_list, stderr_list)]
  192. # Test paunch idempotency how we can.
  193. changed_strings = ['rm -f', 'Completed', 'Created']
  194. if any(s in stdout for s in changed_strings):
  195. self.results['changed'] = True
  196. self.results.update({"stdout": stdout, "stderr": stderr, "rc": rc})
  197. if rc != 0:
  198. self.module.fail_json(
  199. msg="Paunch failed with config_id %s" % self.config_id,
  200. stdout=stdout,
  201. stderr=stderr,
  202. rc=rc)
  203. self.module.exit_json(**self.results)
  204. def paunch_cleanup(self):
  205. self.results['action'].append('Cleaning-up config_id(s) '
  206. '%s' % self.config_id)
  207. p.cleanup(
  208. self.config_id,
  209. managed_by=self.managed_by,
  210. cont_cmd=self.container_cli,
  211. log_level=self.log_level,
  212. log_file=self.log_file
  213. )
  214. self.module.exit_json(**self.results)
  215. def main():
  216. module = AnsibleModule(
  217. argument_spec=yaml.safe_load(DOCUMENTATION)['options'],
  218. supports_check_mode=False,
  219. )
  220. results = dict(
  221. changed=False,
  222. action=[]
  223. )
  224. PaunchManager(module, results)
  225. if __name__ == '__main__':
  226. main()