new module nomad_job & nomad_job_info (#867)

* nomad_job module

* Delete nomad_job.py

* new module nomad_job

* fix symlink

* disable test with centos6 , not supported

* fix centos unsupported

* fix

* requested changes doc

* disable freebsd ci

* requested change docs + check_mode

* lint

* fix syntax

* update docs

* doc fix

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update nomad_job.py

fix docs + ssl true default

* Update nomad_job.yml

disable ssl ci

* nomad_job_info

* Update nomad_job_info.py

fix token nomad job info

* Update nomad_job.py

idempotence + check_mode plan result

* Update nomad_job.py

fail if no id with json content

* Update nomad_job.yml

ci idempotence + check_mode , nomad_job and nomad_job_info

* Update nomad_job.yml

fix ci

* Update main.yml

add kill nomad ci

* Update main.yml

always kill

* fix check mode delete job

* ci with delete and check_mode

* lint

* force start in first deploy

* 12.4 nomad

* fix version nomad

* fix ci assert

* fix ci

* fix ci

* lint

* fix version job id None, import os unused

* lint job_info

* Update aliases

* docs frag + info refacto

* lint

lint

* ci

* jmespath

* fix ci

Co-authored-by: FERREIRA Christophe <christophe.ferreira@cnaf.fr>
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
chris93111 2020-10-19 13:40:07 +02:00 committed by GitHub
commit b2e075e6d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 1255 additions and 0 deletions

View file

@ -0,0 +1,255 @@
#!/usr/bin/python
# coding: utf-8 -*-
# (c) 2020, FERREIRA Christophe <christophe.ferreira@cnaf.fr>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
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
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 I(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 I(name) must be specified.
type: str
content_format:
description:
- Type of content of Nomad job.
choices: ["hcl", "json"]
default: hcl
type: str
notes:
- C(check_mode) is supported.
seealso:
- name: Nomad jobs documentation
description: Complete documentation for Nomad API jobs.
link: https://www.nomadproject.io/api-docs/jobs/
'''
EXAMPLES = '''
- name: Create job
community.general.nomad_job:
host: localhost
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._text 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'),
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', default=None),
client_key=dict(type='path', default=None),
namespace=dict(type='str', default=None),
name=dict(type='str', default=None),
content_format=dict(choices=['hcl', 'json'], default='hcl'),
content=dict(type='str', default=None),
force_start=dict(type='bool', default=False),
token=dict(type='str', default=None, 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'),
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()

View file

@ -0,0 +1,345 @@
#!/usr/bin/python
# coding: utf-8 -*-
# (c) 2020, FERREIRA Christophe <christophe.ferreira@cnaf.fr>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: nomad_job_info
author: FERREIRA Christophe (@chris93111)
version_added: "1.3.0"
short_description: Get Nomad Jobs info
description:
- Get info for one Nomad job.
- List Nomad jobs.
requirements:
- python-nomad
extends_documentation_fragment:
- community.general.nomad
options:
name:
description:
- Name of job for Get info.
- If not specified, lists all jobs.
type: str
notes:
- C(check_mode) is supported.
seealso:
- name: Nomad jobs documentation
description: Complete documentation for Nomad API jobs.
link: https://www.nomadproject.io/api-docs/jobs/
'''
EXAMPLES = '''
- name: Get info for job awx
community.general.nomad_job:
host: localhost
name: awx
register: result
- name: List Nomad jobs
community.general.nomad_job:
host: localhost
register: result
'''
RETURN = '''
result:
description: List with dictionary contains jobs info
returned: success
type: list
sample: [
{
"Affinities": null,
"AllAtOnce": false,
"Constraints": null,
"ConsulToken": "",
"CreateIndex": 13,
"Datacenters": [
"dc1"
],
"Dispatched": false,
"ID": "example",
"JobModifyIndex": 13,
"Meta": null,
"ModifyIndex": 13,
"Multiregion": null,
"Name": "example",
"Namespace": "default",
"NomadTokenID": "",
"ParameterizedJob": null,
"ParentID": "",
"Payload": null,
"Periodic": null,
"Priority": 50,
"Region": "global",
"Spreads": null,
"Stable": false,
"Status": "pending",
"StatusDescription": "",
"Stop": false,
"SubmitTime": 1602244370615307000,
"TaskGroups": [
{
"Affinities": null,
"Constraints": null,
"Count": 1,
"EphemeralDisk": {
"Migrate": false,
"SizeMB": 300,
"Sticky": false
},
"Meta": null,
"Migrate": {
"HealthCheck": "checks",
"HealthyDeadline": 300000000000,
"MaxParallel": 1,
"MinHealthyTime": 10000000000
},
"Name": "cache",
"Networks": null,
"ReschedulePolicy": {
"Attempts": 0,
"Delay": 30000000000,
"DelayFunction": "exponential",
"Interval": 0,
"MaxDelay": 3600000000000,
"Unlimited": true
},
"RestartPolicy": {
"Attempts": 3,
"Delay": 15000000000,
"Interval": 1800000000000,
"Mode": "fail"
},
"Scaling": null,
"Services": null,
"ShutdownDelay": null,
"Spreads": null,
"StopAfterClientDisconnect": null,
"Tasks": [
{
"Affinities": null,
"Artifacts": null,
"CSIPluginConfig": null,
"Config": {
"image": "redis:3.2",
"port_map": [
{
"db": 6379.0
}
]
},
"Constraints": null,
"DispatchPayload": null,
"Driver": "docker",
"Env": null,
"KillSignal": "",
"KillTimeout": 5000000000,
"Kind": "",
"Leader": false,
"Lifecycle": null,
"LogConfig": {
"MaxFileSizeMB": 10,
"MaxFiles": 10
},
"Meta": null,
"Name": "redis",
"Resources": {
"CPU": 500,
"Devices": null,
"DiskMB": 0,
"IOPS": 0,
"MemoryMB": 256,
"Networks": [
{
"CIDR": "",
"DNS": null,
"Device": "",
"DynamicPorts": [
{
"HostNetwork": "default",
"Label": "db",
"To": 0,
"Value": 0
}
],
"IP": "",
"MBits": 10,
"Mode": "",
"ReservedPorts": null
}
]
},
"RestartPolicy": {
"Attempts": 3,
"Delay": 15000000000,
"Interval": 1800000000000,
"Mode": "fail"
},
"Services": [
{
"AddressMode": "auto",
"CanaryMeta": null,
"CanaryTags": null,
"Checks": [
{
"AddressMode": "",
"Args": null,
"CheckRestart": null,
"Command": "",
"Expose": false,
"FailuresBeforeCritical": 0,
"GRPCService": "",
"GRPCUseTLS": false,
"Header": null,
"InitialStatus": "",
"Interval": 10000000000,
"Method": "",
"Name": "alive",
"Path": "",
"PortLabel": "",
"Protocol": "",
"SuccessBeforePassing": 0,
"TLSSkipVerify": false,
"TaskName": "",
"Timeout": 2000000000,
"Type": "tcp"
}
],
"Connect": null,
"EnableTagOverride": false,
"Meta": null,
"Name": "redis-cache",
"PortLabel": "db",
"Tags": [
"global",
"cache"
],
"TaskName": ""
}
],
"ShutdownDelay": 0,
"Templates": null,
"User": "",
"Vault": null,
"VolumeMounts": null
}
],
"Update": {
"AutoPromote": false,
"AutoRevert": false,
"Canary": 0,
"HealthCheck": "checks",
"HealthyDeadline": 180000000000,
"MaxParallel": 1,
"MinHealthyTime": 10000000000,
"ProgressDeadline": 600000000000,
"Stagger": 30000000000
},
"Volumes": null
}
],
"Type": "service",
"Update": {
"AutoPromote": false,
"AutoRevert": false,
"Canary": 0,
"HealthCheck": "",
"HealthyDeadline": 0,
"MaxParallel": 1,
"MinHealthyTime": 0,
"ProgressDeadline": 0,
"Stagger": 30000000000
},
"VaultNamespace": "",
"VaultToken": "",
"Version": 0
}
]
'''
import os
import json
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text 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'),
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', default=None),
client_key=dict(type='path', default=None),
namespace=dict(type='str', default=None),
name=dict(type='str', default=None),
token=dict(type='str', default=None, no_log=True)
),
supports_check_mode=True
)
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'),
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')
)
changed = False
nomad_jobs = list()
try:
job_list = nomad_client.jobs.get_jobs()
for job in job_list:
nomad_jobs.append(nomad_client.job.get_job(job.get('ID')))
result = nomad_jobs
except Exception as e:
module.fail_json(msg=to_native(e))
if module.params.get('name'):
filter = list()
try:
for job in result:
if job.get('ID') == module.params.get('name'):
filter.append(job)
result = filter
if not filter:
module.fail_json(msg="Couldn't find Job with id " + str(module.params.get('name')))
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()