|
|
|
@ -5,6 +5,7 @@ from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
# FIXME: Hack to make imports work with IDE. The ansible import path is not valid for a regular python
|
|
|
|
# FIXME: Hack to make imports work with IDE. The ansible import path is not valid for a regular python
|
|
|
|
@ -95,7 +96,6 @@ docker_compose_file_path:
|
|
|
|
sample: ''
|
|
|
|
sample: ''
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
COMPOSE_STACK = 2
|
|
|
|
COMPOSE_STACK = 2
|
|
|
|
STRING_METHOD = "string"
|
|
|
|
STRING_METHOD = "string"
|
|
|
|
|
|
|
|
|
|
|
|
@ -115,29 +115,19 @@ def _create_stack(client, module, file_contents):
|
|
|
|
return client.post("stacks", body=body, query_params=query_params)
|
|
|
|
return client.post("stacks", body=body, query_params=query_params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _update_stack(client, module, stack_id):
|
|
|
|
def _update_stack(contents, client, module, stack_id):
|
|
|
|
target_stack_name = module.params["stack_name"]
|
|
|
|
target_stack_name = module.params["stack_name"]
|
|
|
|
with open(module.params["docker_compose_file_path"]) as f:
|
|
|
|
|
|
|
|
file_contents = f.read()
|
|
|
|
|
|
|
|
return client.put(
|
|
|
|
return client.put(
|
|
|
|
f"stacks/{stack_id}?&endpointId={client.endpoint}",
|
|
|
|
f"stacks/{stack_id}?&endpointId={client.endpoint}",
|
|
|
|
body={
|
|
|
|
body={
|
|
|
|
"name": target_stack_name,
|
|
|
|
"name": target_stack_name,
|
|
|
|
"stackFileContent": file_contents,
|
|
|
|
"stackFileContent": json.dumps(contents),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def handle_state_present(client, module):
|
|
|
|
def _handle_state_present(contents, stacks, result, client, module):
|
|
|
|
result = dict(changed=False, stack_name=module.params["stack_name"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
already_exists = False
|
|
|
|
already_exists = False
|
|
|
|
stacks = client.get("stacks")
|
|
|
|
|
|
|
|
result["stacks"] = stacks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with open(module.params["docker_compose_file_path"]) as f:
|
|
|
|
|
|
|
|
file_contents = f.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
target_stack_name = module.params["stack_name"]
|
|
|
|
target_stack_name = module.params["stack_name"]
|
|
|
|
for stack in stacks:
|
|
|
|
for stack in stacks:
|
|
|
|
if stack["Name"] == target_stack_name and int(stack["EndpointId"]) == module.params["endpoint_id"]:
|
|
|
|
if stack["Name"] == target_stack_name and int(stack["EndpointId"]) == module.params["endpoint_id"]:
|
|
|
|
@ -146,7 +136,7 @@ def handle_state_present(client, module):
|
|
|
|
break
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
if not already_exists:
|
|
|
|
if not already_exists:
|
|
|
|
stack = _create_stack(client, module, file_contents)
|
|
|
|
stack = _create_stack(client, module, contents)
|
|
|
|
result["changed"] = True
|
|
|
|
result["changed"] = True
|
|
|
|
result["stack_id"] = stack["Id"]
|
|
|
|
result["stack_id"] = stack["Id"]
|
|
|
|
module.exit_json(**result)
|
|
|
|
module.exit_json(**result)
|
|
|
|
@ -158,18 +148,36 @@ def handle_state_present(client, module):
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
result["are_equal"] = (
|
|
|
|
result["are_equal"] = (
|
|
|
|
current_file_contents_resp["StackFileContent"] == file_contents
|
|
|
|
current_file_contents_resp["StackFileContent"] == contents
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if result["are_equal"]:
|
|
|
|
if result["are_equal"]:
|
|
|
|
module.exit_json(**result)
|
|
|
|
module.exit_json(**result)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# the stack exists and we have a new config.
|
|
|
|
# the stack exists and we have a new config.
|
|
|
|
_update_stack(client, module, stack_id)
|
|
|
|
_update_stack(contents, client, module, stack_id)
|
|
|
|
result["changed"] = True
|
|
|
|
result["changed"] = True
|
|
|
|
module.exit_json(**result)
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def handle_state_present(client, module):
|
|
|
|
|
|
|
|
result = dict(changed=False, stack_name=module.params["stack_name"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stacks = client.get("stacks")
|
|
|
|
|
|
|
|
result["stacks"] = stacks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contents = ""
|
|
|
|
|
|
|
|
if module.params.get("docker_compose_file_path"):
|
|
|
|
|
|
|
|
with open(module.params["docker_compose_file_path"]) as f:
|
|
|
|
|
|
|
|
contents = f.read()
|
|
|
|
|
|
|
|
elif module.params.get("stack_definition"):
|
|
|
|
|
|
|
|
contents = json.dumps(module.params["stack_definition"], indent=4)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise ValueError("Should not be able to be here!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_handle_state_present(contents, stacks, result, client, module)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def handle_state_absent(client, module):
|
|
|
|
def handle_state_absent(client, module):
|
|
|
|
result = dict(changed=False, stack_name=module.params["stack_name"])
|
|
|
|
result = dict(changed=False, stack_name=module.params["stack_name"])
|
|
|
|
already_exists = False
|
|
|
|
already_exists = False
|
|
|
|
@ -198,6 +206,7 @@ def run_module():
|
|
|
|
module_args = dict(
|
|
|
|
module_args = dict(
|
|
|
|
stack_name=dict(type="str", required=True),
|
|
|
|
stack_name=dict(type="str", required=True),
|
|
|
|
docker_compose_file_path=dict(type="str"),
|
|
|
|
docker_compose_file_path=dict(type="str"),
|
|
|
|
|
|
|
|
stack_definition=dict(type=str),
|
|
|
|
username=dict(type="str", default="admin"),
|
|
|
|
username=dict(type="str", default="admin"),
|
|
|
|
password=dict(type="str", required=True, no_log=True),
|
|
|
|
password=dict(type="str", required=True, no_log=True),
|
|
|
|
endpoint_id=dict(type="int", required=True),
|
|
|
|
endpoint_id=dict(type="int", required=True),
|
|
|
|
@ -205,9 +214,9 @@ def run_module():
|
|
|
|
state=dict(type="str", default="present", choices=["present", "absent"]),
|
|
|
|
state=dict(type="str", default="present", choices=["present", "absent"]),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
required_if = [
|
|
|
|
required_one_of = [
|
|
|
|
# docker compose file is only required if we are ensuring the stack is present.
|
|
|
|
# docker compose file is only required if we are ensuring the stack is present.
|
|
|
|
["state", "present", ("docker_compose_file_path",)],
|
|
|
|
["state", "present", ("docker_compose_file_path", "stack_definition")],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
state_fns = {"present": handle_state_present, "absent": handle_state_absent}
|
|
|
|
state_fns = {"present": handle_state_present, "absent": handle_state_absent}
|
|
|
|
@ -218,7 +227,7 @@ def run_module():
|
|
|
|
# supports check mode
|
|
|
|
# supports check mode
|
|
|
|
module = AnsibleModule(
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=module_args,
|
|
|
|
argument_spec=module_args,
|
|
|
|
required_if=required_if,
|
|
|
|
required_one_of=required_one_of,
|
|
|
|
# TODO: support check mode
|
|
|
|
# TODO: support check mode
|
|
|
|
supports_check_mode=False,
|
|
|
|
supports_check_mode=False,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|