-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
dokku_service_link.py
183 lines (155 loc) · 4.6 KB
/
dokku_service_link.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.dokku_app import dokku_apps_exists
import subprocess
DOCUMENTATION = """
---
module: dokku_service_link
short_description: Links and unlinks a given service to an application
options:
app:
description:
- The name of the app
required: True
default: null
aliases: []
name:
description:
- The name of the service
required: True
default: null
aliases: []
service:
description:
- The type of service to link
required: True
default: null
aliases: []
state:
description:
- The state of the service link
required: False
default: present
choices: [ "present", "absent" ]
aliases: []
author: Jose Diaz-Gonzalez
requirements: [ ]
"""
EXAMPLES = """
- name: redis:link default hello-world
dokku_service_link:
app: hello-world
name: default
service: redis
- name: postgres:link default hello-world
dokku_service_link:
app: hello-world
name: default
service: postgres
- name: redis:unlink default hello-world
dokku_service_link:
app: hello-world
name: default
service: redis
state: absent
"""
def dokku_service_exists(service, name):
exists = False
error = None
command = "dokku --quiet {0}:exists {1}".format(service, name)
try:
subprocess.check_call(command, shell=True)
exists = True
except subprocess.CalledProcessError as e:
error = str(e)
return exists, error
def dokku_service_linked(service, name, app):
linked = False
error = None
command = "dokku --quiet {0}:linked {1} {2}".format(service, name, app)
try:
subprocess.check_call(command, shell=True)
linked = True
except subprocess.CalledProcessError as e:
error = str(e)
return linked, error
def dokku_service_link_absent(data):
is_error = True
has_changed = False
meta = {"present": False}
exists, error = dokku_service_exists(data["service"], data["name"])
if not exists:
meta["error"] = error
return (is_error, has_changed, meta)
app_exists, error = dokku_apps_exists(data["app"])
if not app_exists:
meta["error"] = error
return (is_error, has_changed, meta)
linked, error = dokku_service_linked(data["service"], data["name"], data["app"])
if not linked:
is_error = False
return (is_error, has_changed, meta)
command = "dokku --quiet {0}:unlink {1} {2}".format(
data["service"], data["name"], 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_service_link_present(data):
is_error = True
has_changed = False
meta = {"present": False}
exists, error = dokku_service_exists(data["service"], data["name"])
if not exists:
meta["error"] = error
return (is_error, has_changed, meta)
app_exists, error = dokku_apps_exists(data["app"])
if not app_exists:
meta["error"] = error
return (is_error, has_changed, meta)
linked, error = dokku_service_linked(data["service"], data["name"], data["app"])
if linked:
is_error = False
return (is_error, has_changed, meta)
command = "dokku --quiet {0}:link {1} {2}".format(
data["service"], data["name"], 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 main():
fields = {
"app": {"required": True, "type": "str"},
"name": {"required": True, "type": "str"},
"service": {"required": True, "type": "str"},
"state": {
"required": False,
"default": "present",
"choices": ["present", "absent"],
"type": "str",
},
}
choice_map = {
"present": dokku_service_link_present,
"absent": dokku_service_link_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()