-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathnomad_job.py
269 lines (237 loc) · 8.59 KB
/
nomad_job.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2020, FERREIRA Christophe <[email protected]>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r"""
module: nomad_job
author: FERREIRA Christophe (@chris93111)
version_added: "1.3.0"
short_description: Launch a Nomad Job
description:
- Launch a Nomad job.
- Stop a Nomad job.
- Force start a Nomad job.
requirements:
- python-nomad
extends_documentation_fragment:
- community.general.nomad
- community.general.attributes
attributes:
check_mode:
support: full
diff_mode:
support: none
options:
name:
description:
- Name of job for delete, stop and start job without source.
- Name of job for delete, stop and start job without source.
- Either this or O(content) must be specified.
type: str
state:
description:
- Deploy or remove job.
choices: ["present", "absent"]
required: true
type: str
force_start:
description:
- Force job to started.
type: bool
default: false
content:
description:
- Content of Nomad job.
- Either this or O(name) must be specified.
type: str
content_format:
description:
- Type of content of Nomad job.
choices: ["hcl", "json"]
default: hcl
type: str
seealso:
- name: Nomad jobs documentation
description: Complete documentation for Nomad API jobs.
link: https://www.nomadproject.io/api-docs/jobs/
"""
EXAMPLES = r"""
- name: Create job
community.general.nomad_job:
host: localhost
state: present
content: "{{ lookup('ansible.builtin.file', 'job.hcl') }}"
timeout: 120
- name: Connect with port to create job
community.general.nomad_job:
host: localhost
port: 4645
state: present
content: "{{ lookup('ansible.builtin.file', 'job.hcl') }}"
timeout: 120
- name: Stop job
community.general.nomad_job:
host: localhost
state: absent
name: api
- name: Force job to start
community.general.nomad_job:
host: localhost
state: present
name: api
timeout: 120
force_start: true
"""
import json
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.common.text.converters import to_native
import_nomad = None
try:
import nomad
import_nomad = True
except ImportError:
import_nomad = False
def run():
module = AnsibleModule(
argument_spec=dict(
host=dict(required=True, type='str'),
port=dict(type='int', default=4646),
state=dict(required=True, choices=['present', 'absent']),
use_ssl=dict(type='bool', default=True),
timeout=dict(type='int', default=5),
validate_certs=dict(type='bool', default=True),
client_cert=dict(type='path'),
client_key=dict(type='path'),
namespace=dict(type='str'),
name=dict(type='str'),
content_format=dict(choices=['hcl', 'json'], default='hcl'),
content=dict(type='str'),
force_start=dict(type='bool', default=False),
token=dict(type='str', no_log=True)
),
supports_check_mode=True,
mutually_exclusive=[
["name", "content"]
],
required_one_of=[
['name', 'content']
]
)
if not import_nomad:
module.fail_json(msg=missing_required_lib("python-nomad"))
certificate_ssl = (module.params.get('client_cert'), module.params.get('client_key'))
nomad_client = nomad.Nomad(
host=module.params.get('host'),
port=module.params.get('port'),
secure=module.params.get('use_ssl'),
timeout=module.params.get('timeout'),
verify=module.params.get('validate_certs'),
cert=certificate_ssl,
namespace=module.params.get('namespace'),
token=module.params.get('token')
)
if module.params.get('state') == "present":
if module.params.get('name') and not module.params.get('force_start'):
module.fail_json(msg='For start job with name, force_start is needed')
changed = False
if module.params.get('content'):
if module.params.get('content_format') == 'json':
job_json = module.params.get('content')
try:
job_json = json.loads(job_json)
except ValueError as e:
module.fail_json(msg=to_native(e))
job = dict()
job['job'] = job_json
try:
job_id = job_json.get('ID')
if job_id is None:
module.fail_json(msg="Cannot retrieve job with ID None")
plan = nomad_client.job.plan_job(job_id, job, diff=True)
if not plan['Diff'].get('Type') == "None":
changed = True
if not module.check_mode:
result = nomad_client.jobs.register_job(job)
else:
result = plan
else:
result = plan
except Exception as e:
module.fail_json(msg=to_native(e))
if module.params.get('content_format') == 'hcl':
try:
job_hcl = module.params.get('content')
job_json = nomad_client.jobs.parse(job_hcl)
job = dict()
job['job'] = job_json
except nomad.api.exceptions.BadRequestNomadException as err:
msg = str(err.nomad_resp.reason) + " " + str(err.nomad_resp.text)
module.fail_json(msg=to_native(msg))
try:
job_id = job_json.get('ID')
plan = nomad_client.job.plan_job(job_id, job, diff=True)
if not plan['Diff'].get('Type') == "None":
changed = True
if not module.check_mode:
result = nomad_client.jobs.register_job(job)
else:
result = plan
else:
result = plan
except Exception as e:
module.fail_json(msg=to_native(e))
if module.params.get('force_start'):
try:
job = dict()
if module.params.get('name'):
job_name = module.params.get('name')
else:
job_name = job_json['Name']
job_json = nomad_client.job.get_job(job_name)
if job_json['Status'] == 'running':
result = job_json
else:
job_json['Status'] = 'running'
job_json['Stop'] = False
job['job'] = job_json
if not module.check_mode:
result = nomad_client.jobs.register_job(job)
else:
result = nomad_client.validate.validate_job(job)
if not result.status_code == 200:
module.fail_json(msg=to_native(result.text))
result = json.loads(result.text)
changed = True
except Exception as e:
module.fail_json(msg=to_native(e))
if module.params.get('state') == "absent":
try:
if not module.params.get('name') is None:
job_name = module.params.get('name')
else:
if module.params.get('content_format') == 'hcl':
job_json = nomad_client.jobs.parse(module.params.get('content'))
job_name = job_json['Name']
if module.params.get('content_format') == 'json':
job_json = module.params.get('content')
job_name = job_json['Name']
job = nomad_client.job.get_job(job_name)
if job['Status'] == 'dead':
changed = False
result = job
else:
if not module.check_mode:
result = nomad_client.job.deregister_job(job_name)
else:
result = job
changed = True
except Exception as e:
module.fail_json(msg=to_native(e))
module.exit_json(changed=changed, result=result)
def main():
run()
if __name__ == "__main__":
main()