-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
dokku_proxy.py
121 lines (100 loc) · 2.87 KB
/
dokku_proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.dokku_utils import subprocess_check_output
import subprocess
DOCUMENTATION = """
---
module: dokku_proxy
short_description: Enable or disable the proxy for a dokku app
options:
app:
description:
- The name of the app
required: True
default: null
aliases: []
state:
description:
- The state of the proxy
required: False
default: present
choices: [ "present", "absent" ]
aliases: []
author: Jose Diaz-Gonzalez
requirements: [ ]
"""
EXAMPLES = """
- name: Enable the default proxy
dokku_proxy:
app: hello-world
- name: Disable the default proxy
dokku_proxy:
app: hello-world
state: absent
"""
def dokku_proxy(data):
command = "dokku --quiet config:get {0} DOKKU_DISABLE_PROXY"
response, error = subprocess_check_output(command.format(data["app"]))
if error:
return "0", error
return response[0], error
def dokku_proxy_present(data):
is_error = True
has_changed = False
meta = {"present": False}
disabled, error = dokku_proxy(data)
if disabled == "0":
is_error = False
meta["present"] = True
return (is_error, has_changed, meta)
command = "dokku --quiet proxy:enable {0}".format(data["app"])
try:
subprocess.check_call(command, shell=True)
is_error = False
has_changed = True
meta["present"] = True
except subprocess.CalledProcessError as e:
meta["error"] = str(e)
return (is_error, has_changed, meta)
def dokku_proxy_absent(data=None):
is_error = True
has_changed = False
meta = {"present": True}
disabled, error = dokku_proxy(data)
if disabled == "1":
is_error = False
meta["present"] = False
return (is_error, has_changed, meta)
command = "dokku --force proxy:disable {0}".format(data["app"])
try:
subprocess.check_call(command, shell=True)
is_error = False
has_changed = True
meta["present"] = False
except subprocess.CalledProcessError as e:
meta["error"] = str(e)
return (is_error, has_changed, meta)
def main():
fields = {
"app": {"required": True, "type": "str"},
"state": {
"required": False,
"default": "present",
"choices": ["present", "absent"],
"type": "str",
},
}
choice_map = {
"present": dokku_proxy_present,
"absent": dokku_proxy_absent,
}
module = AnsibleModule(argument_spec=fields, supports_check_mode=False)
is_error, has_changed, result = choice_map.get(module.params["state"])(
module.params
)
if is_error:
module.fail_json(msg=result["error"], meta=result)
module.exit_json(changed=has_changed, meta=result)
if __name__ == "__main__":
main()