Merge pull request #2892 from resmo/fix/streamline_jenkins_job
streamline jenkins job module
This commit is contained in:
commit
afd0b23836
1 changed files with 136 additions and 104 deletions
|
@ -18,20 +18,25 @@ DOCUMENTATION = '''
|
|||
module: jenkins_job
|
||||
short_description: Manage jenkins jobs
|
||||
description:
|
||||
- Manage Jenkins jobs by using Jenkins REST API
|
||||
- Manage Jenkins jobs by using Jenkins REST API.
|
||||
requirements:
|
||||
- "python-jenkins >= 0.4.12"
|
||||
- "lxml >= 3.3.3"
|
||||
version_added: "2.2"
|
||||
author: "Sergio Millan Rodriguez"
|
||||
author: "Sergio Millan Rodriguez (@sermilrod)"
|
||||
options:
|
||||
config:
|
||||
description:
|
||||
- config.xml file to use as job config within your Ansible repo.
|
||||
- config in XML format.
|
||||
- Required if job does not yet exist.
|
||||
- Mututally exclusive with C(enabled).
|
||||
- Considered if C(state=present).
|
||||
required: false
|
||||
enable:
|
||||
enabled:
|
||||
description:
|
||||
- Action to take with the Jenkins job (enable/disable).
|
||||
- Whether the job should be enabled or disabled.
|
||||
- Mututally exclusive with C(config).
|
||||
- Considered if C(state=present).
|
||||
required: false
|
||||
name:
|
||||
description:
|
||||
|
@ -44,7 +49,8 @@ options:
|
|||
state:
|
||||
description:
|
||||
- Attribute that specifies if the job has to be created or deleted.
|
||||
required: true
|
||||
required: false
|
||||
default: present
|
||||
choices: ['present', 'absent']
|
||||
token:
|
||||
description:
|
||||
|
@ -67,8 +73,6 @@ EXAMPLES = '''
|
|||
config: "{{ lookup('file', 'templates/test.xml') }}"
|
||||
name: test
|
||||
password: admin
|
||||
state: present
|
||||
enable: True
|
||||
url: "http://localhost:8080"
|
||||
user: admin
|
||||
|
||||
|
@ -77,8 +81,6 @@ EXAMPLES = '''
|
|||
config: "{{ lookup('template', 'templates/test.xml.j2') }}"
|
||||
name: test
|
||||
token: asdfasfasfasdfasdfadfasfasdfasdfc
|
||||
state: present
|
||||
enable: yes
|
||||
url: "http://localhost:8080"
|
||||
user: admin
|
||||
|
||||
|
@ -102,8 +104,7 @@ EXAMPLES = '''
|
|||
- jenkins_job:
|
||||
name: test
|
||||
password: admin
|
||||
state: present
|
||||
enable: False
|
||||
enabled: false
|
||||
url: "http://localhost:8080"
|
||||
user: admin
|
||||
|
||||
|
@ -111,8 +112,7 @@ EXAMPLES = '''
|
|||
- jenkins_job:
|
||||
name: test
|
||||
token: asdfasfasfasdfasdfadfasfasdfasdfc
|
||||
state: present
|
||||
enable: no
|
||||
enabled: false
|
||||
url: "http://localhost:8080"
|
||||
user: admin
|
||||
'''
|
||||
|
@ -129,6 +129,16 @@ state:
|
|||
returned: success
|
||||
type: string
|
||||
sample: present
|
||||
enabled:
|
||||
description: Whether the jenkins job is enabled or not.
|
||||
returned: success
|
||||
type: bool
|
||||
sample: true
|
||||
user:
|
||||
description: User used for authentication.
|
||||
returned: success
|
||||
type: string
|
||||
sample: admin
|
||||
url:
|
||||
description: Url to connect to the Jenkins server.
|
||||
returned: success
|
||||
|
@ -148,18 +158,32 @@ try:
|
|||
except ImportError:
|
||||
python_lxml_installed = False
|
||||
|
||||
class Jenkins:
|
||||
def __init__(self, config, name, password, state, enable, token, url, user):
|
||||
self.config = config
|
||||
self.name = name
|
||||
self.password = password
|
||||
self.state = state
|
||||
self.enable = enable
|
||||
self.token = token
|
||||
self.user = user
|
||||
self.jenkins_url = url
|
||||
class JenkinsJob:
|
||||
def __init__(self, module):
|
||||
self.module = module
|
||||
|
||||
self.config = module.params.get('config')
|
||||
self.name = module.params.get('name')
|
||||
self.password = module.params.get('password')
|
||||
self.state = module.params.get('state')
|
||||
self.enabled = module.params.get('enabled')
|
||||
self.token = module.params.get('token')
|
||||
self.user = module.params.get('user')
|
||||
self.jenkins_url = module.params.get('url')
|
||||
self.server = self.get_jenkins_connection()
|
||||
|
||||
self.result = {
|
||||
'changed': False,
|
||||
'url': self.jenkins_url,
|
||||
'name': self.name,
|
||||
'user': self.user,
|
||||
'state': self.state,
|
||||
'diff': {
|
||||
'before': "",
|
||||
'after': ""
|
||||
}
|
||||
}
|
||||
|
||||
def get_jenkins_connection(self):
|
||||
try:
|
||||
if (self.user and self.password):
|
||||
|
@ -172,106 +196,119 @@ class Jenkins:
|
|||
return jenkins.Jenkins(self.jenkins_url)
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg='Unable to connect to Jenkins server, %s' % str(e))
|
||||
self.module.fail_json(msg='Unable to connect to Jenkins server, %s' % str(e))
|
||||
|
||||
def get_job_status(self, module):
|
||||
def get_job_status(self):
|
||||
try:
|
||||
return self.server.get_job_info(self.name)['color'].encode('utf-8')
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg='Unable to fetch job information, %s' % str(e))
|
||||
self.module.fail_json(msg='Unable to fetch job information, %s' % str(e))
|
||||
|
||||
def job_exists(self, module):
|
||||
def job_exists(self):
|
||||
try:
|
||||
return bool(self.server.job_exists(self.name))
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg='Unable to validate if job exists, %s for %s' % (str(e), self.jenkins_url))
|
||||
|
||||
def build(self, module):
|
||||
if self.state == 'present':
|
||||
self.update_job(module)
|
||||
else:
|
||||
self.delete_job(module)
|
||||
self.module.fail_json(msg='Unable to validate if job exists, %s for %s' % (str(e), self.jenkins_url))
|
||||
|
||||
def get_config(self):
|
||||
return job_config_to_string(self.config)
|
||||
|
||||
def configuration_changed(self):
|
||||
changed = False
|
||||
def get_current_config(self):
|
||||
return job_config_to_string(self.server.get_job_config(self.name).encode('utf-8'))
|
||||
|
||||
def has_config_changed(self):
|
||||
# config is optional, if not provided we keep the current config as is
|
||||
if self.config is None:
|
||||
return False
|
||||
|
||||
config_file = self.get_config()
|
||||
machine_file = job_config_to_string(self.server.get_job_config(self.name).encode('utf-8'))
|
||||
if not machine_file == config_file:
|
||||
changed = True
|
||||
machine_file = self.get_current_config()
|
||||
|
||||
return changed
|
||||
self.result['diff']['after'] = config_file
|
||||
self.result['diff']['before'] = machine_file
|
||||
|
||||
def update_job(self, module):
|
||||
if not self.job_exists(module):
|
||||
self.create_job(module)
|
||||
if machine_file != config_file:
|
||||
return True
|
||||
return False
|
||||
|
||||
def present_job(self):
|
||||
if self.config is None and self.enabled is None:
|
||||
module.fail_json(msg='one of the following params is required on state=present: config,enabled')
|
||||
|
||||
if not self.job_exists():
|
||||
self.create_job()
|
||||
else:
|
||||
self.reconfig_job(module)
|
||||
self.update_job()
|
||||
|
||||
def state_changed(self, status):
|
||||
changed = False
|
||||
if ( (self.enable == False and status != "disabled") or (self.enable == True and status == "disabled") ):
|
||||
changed = True
|
||||
def has_state_changed(self, status):
|
||||
# Keep in current state if enabled arg_spec is not given
|
||||
if self.enabled is None:
|
||||
return False
|
||||
|
||||
return changed
|
||||
if ( (self.enabled == False and status != "disabled") or (self.enabled == True and status == "disabled") ):
|
||||
return True
|
||||
return False
|
||||
|
||||
def change_state(self):
|
||||
if self.enable == False:
|
||||
def switch_state(self):
|
||||
if self.enabled == False:
|
||||
self.server.disable_job(self.name)
|
||||
else:
|
||||
self.server.enable_job(self.name)
|
||||
|
||||
def reconfig_job(self, module):
|
||||
changed = False
|
||||
def update_job(self):
|
||||
try:
|
||||
status = self.get_job_status(module)
|
||||
if self.enable == True:
|
||||
if ( self.configuration_changed() or self.state_changed(status) ):
|
||||
changed = True
|
||||
if not module.check_mode:
|
||||
self.server.reconfig_job(self.name, self.get_config())
|
||||
self.change_state()
|
||||
else:
|
||||
if self.state_changed(status):
|
||||
changed = True
|
||||
if not module.check_mode:
|
||||
self.change_state()
|
||||
status = self.get_job_status()
|
||||
|
||||
# Handle job config
|
||||
if self.has_config_changed():
|
||||
self.result['changed'] = True
|
||||
if not self.module.check_mode:
|
||||
self.server.reconfig_job(self.name, self.get_config())
|
||||
|
||||
# Handle job disable/enable
|
||||
elif self.has_state_changed(status):
|
||||
self.result['changed'] = True
|
||||
if not self.module.check_mode:
|
||||
self.switch_state()
|
||||
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg='Unable to reconfigure job, %s for %s' % (str(e), self.jenkins_url))
|
||||
self.module.fail_json(msg='Unable to reconfigure job, %s for %s' % (str(e), self.jenkins_url))
|
||||
|
||||
module.exit_json(changed=changed, name=self.name, state=self.state, url=self.jenkins_url)
|
||||
def create_job(self):
|
||||
if self.config is None:
|
||||
self.module.fail_json(msg='missing required param: config')
|
||||
|
||||
def create_job(self, module):
|
||||
changed = False
|
||||
self.result['changed'] = True
|
||||
try:
|
||||
changed = True
|
||||
if not module.check_mode:
|
||||
self.server.create_job(self.name, self.get_config())
|
||||
self.change_state()
|
||||
config_file = self.get_config()
|
||||
self.result['diff']['after'] = config_file
|
||||
if not self.module.check_mode:
|
||||
self.server.create_job(self.name, config_file)
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg='Unable to create job, %s for %s' % (str(e), self.jenkins_url))
|
||||
self.module.fail_json(msg='Unable to create job, %s for %s' % (str(e), self.jenkins_url))
|
||||
|
||||
module.exit_json(changed=changed, name=self.name, state=self.state, url=self.jenkins_url)
|
||||
|
||||
def delete_job(self, module):
|
||||
changed = False
|
||||
if self.job_exists(module):
|
||||
changed = True
|
||||
if not module.check_mode:
|
||||
def absent_job(self):
|
||||
if self.job_exists():
|
||||
self.result['changed'] = True
|
||||
self.result['diff']['before'] = self.get_current_config()
|
||||
if not self.module.check_mode:
|
||||
try:
|
||||
self.server.delete_job(self.name)
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg='Unable to delete job, %s for %s' % (str(e), self.jenkins_url))
|
||||
self.module.fail_json(msg='Unable to delete job, %s for %s' % (str(e), self.jenkins_url))
|
||||
|
||||
module.exit_json(changed=changed, name=self.name, state=self.state, url=self.jenkins_url)
|
||||
def get_result(self):
|
||||
result = self.result
|
||||
if self.job_exists():
|
||||
result['enabled'] = self.get_job_status() != "disabled"
|
||||
else:
|
||||
result['enabled'] = None
|
||||
return result
|
||||
|
||||
def test_dependencies(module):
|
||||
if not python_jenkins_installed:
|
||||
|
@ -285,41 +322,36 @@ def test_dependencies(module):
|
|||
def job_config_to_string(xml_str):
|
||||
return ET.tostring(ET.fromstring(xml_str))
|
||||
|
||||
def jenkins_builder(module):
|
||||
return Jenkins(
|
||||
module.params.get('config'),
|
||||
module.params.get('name'),
|
||||
module.params.get('password'),
|
||||
module.params.get('state'),
|
||||
module.params.get('enable'),
|
||||
module.params.get('token'),
|
||||
module.params.get('url'),
|
||||
module.params.get('user')
|
||||
)
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
config = dict(required=False),
|
||||
name = dict(required=True),
|
||||
password = dict(required=False, no_log=True),
|
||||
state = dict(required=True, choices=['present', 'absent']),
|
||||
enable = dict(required=False, type='bool'),
|
||||
state = dict(required=False, choices=['present', 'absent'], default="present"),
|
||||
enabled = dict(required=False, type='bool'),
|
||||
token = dict(required=False, no_log=True),
|
||||
url = dict(required=False, default="http://localhost:8080"),
|
||||
user = dict(required=False)
|
||||
),
|
||||
required_if = [
|
||||
('state', 'present', ['enable']),
|
||||
('enable', True, ['config'])
|
||||
mutually_exclusive = [
|
||||
['password', 'token'],
|
||||
['config', 'enabled'],
|
||||
],
|
||||
mutually_exclusive = [['password', 'token']],
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
test_dependencies(module)
|
||||
jenkins = jenkins_builder(module)
|
||||
jenkins.build(module)
|
||||
jenkins_job = JenkinsJob(module)
|
||||
|
||||
if module.params.get('state') == "present":
|
||||
jenkins_job.present_job()
|
||||
else:
|
||||
jenkins_job.absent_job()
|
||||
|
||||
result = jenkins_job.get_result()
|
||||
module.exit_json(**result)
|
||||
|
||||
|
||||
from ansible.module_utils.basic import *
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in a new issue