You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.2 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import kpm.deploy
  4. from ansible.module_utils.basic import *
  5. DOCUMENTATION = """
  6. ---
  7. module: kpm
  8. short_description: Application deployment on kubernetes with kpm registry
  9. description:
  10. - Create, remove, and update resources within a Kubernetes Cluster
  11. version_added: "2.0"
  12. options:
  13. name:
  14. required: true
  15. default: null
  16. description:
  17. - The name of the kpm package
  18. namespace:
  19. required: false
  20. default: 'default'
  21. description:
  22. - The namespace to deploy package. It will be created if doesn't exist
  23. force:
  24. required: false
  25. default: false
  26. description:
  27. - A flag to indicate to force delete, replace.
  28. registry:
  29. required: false
  30. default: 'https://api.kpm.sh'
  31. description:
  32. - The registry url to fetch packages
  33. version:
  34. required: false
  35. default: 'None'
  36. description:
  37. - The package version
  38. variables:
  39. required: false
  40. default: 'None'
  41. description:
  42. - Set package variables
  43. state:
  44. required: false
  45. choices: ['present', 'absent']
  46. default: present
  47. description:
  48. - present handles checking existence or creating resources,
  49. absent handles deleting resource(s).
  50. requirements:
  51. - kubectl
  52. - kpm
  53. author: "Antoine Legrand (ant31_2t@msn.com)"
  54. """
  55. EXAMPLES = """
  56. - name: check presence or install ghost
  57. kpm: name=ghost/ghost state=present
  58. - name: check absence or remove rocketchat
  59. kpm: name=ant31/rocketchat state=absent
  60. """
  61. RETURN = """
  62. """
  63. def check_changed(result, state='present'):
  64. no_change = ["ok", 'protected', 'absent']
  65. for r in result:
  66. if r['status'] not in no_change:
  67. return True
  68. return False
  69. def main():
  70. module = AnsibleModule(
  71. supports_check_mode=True,
  72. argument_spec = dict(
  73. version = dict(default=None, required=False),
  74. state = dict(default='present', choices=['present', 'absent']),
  75. name = dict(required=True),
  76. force = dict(required=False, default=False, type='bool'),
  77. variables = dict(required=False, default=None, type='dict'),
  78. registry = dict(required=False, default="https://api.kpm.sh"),
  79. namespace=dict(default='default', required=False)))
  80. params = {"version": module.params.get("version"),
  81. "namespace": module.params.get('namespace'),
  82. "variables": module.params.get('variables'),
  83. "endpoint": module.params.get('registry'),
  84. "dry": module.check_mode,
  85. "proxy": None,
  86. "fmt": "json"}
  87. state = module.params.get("state")
  88. try:
  89. if state == 'present':
  90. r = kpm.deploy.deploy(module.params.get('name'), **params)
  91. elif state == 'absent':
  92. r = kpm.deploy.delete(module.params.get('name'), **params)
  93. except Exception as e:
  94. module.fail_json(msg=e.message)
  95. res = {}
  96. res['kpm'] = r
  97. res['changed'] = check_changed(r, state)
  98. module.exit_json(**res)
  99. if __name__ == '__main__':
  100. main()