mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #43023 from terminalmage/fix-jenkins-xml-caching
Fixes/improvements to Jenkins state/module
This commit is contained in:
commit
ad89ff3104
2 changed files with 84 additions and 50 deletions
|
@ -35,7 +35,7 @@ import salt.utils
|
|||
|
||||
# Import 3rd-party libs
|
||||
# pylint: disable=import-error,no-name-in-module,redefined-builtin
|
||||
from salt.exceptions import SaltInvocationError
|
||||
from salt.exceptions import CommandExecutionError, SaltInvocationError
|
||||
# pylint: enable=import-error,no-name-in-module
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
@ -87,6 +87,19 @@ def _connect():
|
|||
password=jenkins_password)
|
||||
|
||||
|
||||
def _retrieve_config_xml(config_xml, saltenv):
|
||||
'''
|
||||
Helper to cache the config XML and raise a CommandExecutionError if we fail
|
||||
to do so. If we successfully cache the file, return the cached path.
|
||||
'''
|
||||
ret = __salt__['cp.cache_file'](config_xml, saltenv)
|
||||
|
||||
if not ret:
|
||||
raise CommandExecutionError('Failed to retrieve {0}'.format(config_xml))
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def get_version():
|
||||
'''
|
||||
Return version of Jenkins
|
||||
|
@ -144,7 +157,7 @@ def job_exists(name=None):
|
|||
|
||||
'''
|
||||
if not name:
|
||||
raise SaltInvocationError('Required parameter `name` is missing.')
|
||||
raise SaltInvocationError('Required parameter \'name\' is missing')
|
||||
|
||||
server = _connect()
|
||||
if server.job_exists(name):
|
||||
|
@ -168,12 +181,12 @@ def get_job_info(name=None):
|
|||
|
||||
'''
|
||||
if not name:
|
||||
raise SaltInvocationError('Required parameter `name` is missing.')
|
||||
raise SaltInvocationError('Required parameter \'name\' is missing')
|
||||
|
||||
server = _connect()
|
||||
|
||||
if not job_exists(name):
|
||||
raise SaltInvocationError('Job `{0}` does not exist.'.format(name))
|
||||
raise CommandExecutionError('Job \'{0}\' does not exist'.format(name))
|
||||
|
||||
job_info = server.get_job_info(name)
|
||||
if job_info:
|
||||
|
@ -197,17 +210,19 @@ def build_job(name=None, parameters=None):
|
|||
|
||||
'''
|
||||
if not name:
|
||||
raise SaltInvocationError('Required parameter `name` is missing.')
|
||||
raise SaltInvocationError('Required parameter \'name\' is missing')
|
||||
|
||||
server = _connect()
|
||||
|
||||
if not job_exists(name):
|
||||
raise SaltInvocationError('Job `{0}` does not exist.'.format(name))
|
||||
raise CommandExecutionError('Job \'{0}\' does not exist.'.format(name))
|
||||
|
||||
try:
|
||||
server.build_job(name, parameters)
|
||||
except jenkins.JenkinsException as err:
|
||||
raise SaltInvocationError('Something went wrong {0}.'.format(err))
|
||||
raise CommandExecutionError(
|
||||
'Encountered error building job \'{0}\': {1}'.format(name, err)
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
|
@ -232,15 +247,15 @@ def create_job(name=None,
|
|||
|
||||
'''
|
||||
if not name:
|
||||
raise SaltInvocationError('Required parameter `name` is missing.')
|
||||
raise SaltInvocationError('Required parameter \'name\' is missing')
|
||||
|
||||
if job_exists(name):
|
||||
raise SaltInvocationError('Job `{0}` already exists.'.format(name))
|
||||
raise CommandExecutionError('Job \'{0}\' already exists'.format(name))
|
||||
|
||||
if not config_xml:
|
||||
config_xml = jenkins.EMPTY_CONFIG_XML
|
||||
else:
|
||||
config_xml_file = __salt__['cp.cache_file'](config_xml, saltenv)
|
||||
config_xml_file = _retrieve_config_xml(config_xml, saltenv)
|
||||
|
||||
with salt.utils.fopen(config_xml_file) as _fp:
|
||||
config_xml = _fp.read()
|
||||
|
@ -249,7 +264,9 @@ def create_job(name=None,
|
|||
try:
|
||||
server.create_job(name, config_xml)
|
||||
except jenkins.JenkinsException as err:
|
||||
raise SaltInvocationError('Something went wrong {0}.'.format(err))
|
||||
raise CommandExecutionError(
|
||||
'Encountered error creating job \'{0}\': {1}'.format(name, err)
|
||||
)
|
||||
return config_xml
|
||||
|
||||
|
||||
|
@ -274,12 +291,12 @@ def update_job(name=None,
|
|||
|
||||
'''
|
||||
if not name:
|
||||
raise SaltInvocationError('Required parameter `name` is missing.')
|
||||
raise SaltInvocationError('Required parameter \'name\' is missing')
|
||||
|
||||
if not config_xml:
|
||||
config_xml = jenkins.EMPTY_CONFIG_XML
|
||||
else:
|
||||
config_xml_file = __salt__['cp.cache_file'](config_xml, saltenv)
|
||||
config_xml_file = _retrieve_config_xml(config_xml, saltenv)
|
||||
|
||||
with salt.utils.fopen(config_xml_file) as _fp:
|
||||
config_xml = _fp.read()
|
||||
|
@ -288,7 +305,9 @@ def update_job(name=None,
|
|||
try:
|
||||
server.reconfig_job(name, config_xml)
|
||||
except jenkins.JenkinsException as err:
|
||||
raise SaltInvocationError('Something went wrong {0}.'.format(err))
|
||||
raise CommandExecutionError(
|
||||
'Encountered error updating job \'{0}\': {1}'.format(name, err)
|
||||
)
|
||||
return config_xml
|
||||
|
||||
|
||||
|
@ -307,17 +326,19 @@ def delete_job(name=None):
|
|||
|
||||
'''
|
||||
if not name:
|
||||
raise SaltInvocationError('Required parameter `name` is missing.')
|
||||
raise SaltInvocationError('Required parameter \'name\' is missing')
|
||||
|
||||
server = _connect()
|
||||
|
||||
if not job_exists(name):
|
||||
raise SaltInvocationError('Job `{0}` does not exists.'.format(name))
|
||||
raise CommandExecutionError('Job \'{0}\' does not exist'.format(name))
|
||||
|
||||
try:
|
||||
server.delete_job(name)
|
||||
except jenkins.JenkinsException as err:
|
||||
raise SaltInvocationError('Something went wrong {0}.'.format(err))
|
||||
raise CommandExecutionError(
|
||||
'Encountered error deleting job \'{0}\': {1}'.format(name, err)
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
|
@ -336,17 +357,19 @@ def enable_job(name=None):
|
|||
|
||||
'''
|
||||
if not name:
|
||||
raise SaltInvocationError('Required parameter `name` is missing.')
|
||||
raise SaltInvocationError('Required parameter \'name\' is missing')
|
||||
|
||||
server = _connect()
|
||||
|
||||
if not job_exists(name):
|
||||
raise SaltInvocationError('Job `{0}` does not exists.'.format(name))
|
||||
raise CommandExecutionError('Job \'{0}\' does not exist'.format(name))
|
||||
|
||||
try:
|
||||
server.enable_job(name)
|
||||
except jenkins.JenkinsException as err:
|
||||
raise SaltInvocationError('Something went wrong {0}.'.format(err))
|
||||
raise CommandExecutionError(
|
||||
'Encountered error enabling job \'{0}\': {1}'.format(name, err)
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
|
@ -366,17 +389,19 @@ def disable_job(name=None):
|
|||
'''
|
||||
|
||||
if not name:
|
||||
raise SaltInvocationError('Required parameter `name` is missing.')
|
||||
raise SaltInvocationError('Required parameter \'name\' is missing')
|
||||
|
||||
server = _connect()
|
||||
|
||||
if not job_exists(name):
|
||||
raise SaltInvocationError('Job `{0}` does not exists.'.format(name))
|
||||
raise CommandExecutionError('Job \'{0}\' does not exist'.format(name))
|
||||
|
||||
try:
|
||||
server.disable_job(name)
|
||||
except jenkins.JenkinsException as err:
|
||||
raise SaltInvocationError('Something went wrong {0}.'.format(err))
|
||||
raise CommandExecutionError(
|
||||
'Encountered error disabling job \'{0}\': {1}'.format(name, err)
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
|
@ -396,12 +421,12 @@ def job_status(name=None):
|
|||
'''
|
||||
|
||||
if not name:
|
||||
raise SaltInvocationError('Required parameter `name` is missing.')
|
||||
raise SaltInvocationError('Required parameter \'name\' is missing')
|
||||
|
||||
server = _connect()
|
||||
|
||||
if not job_exists(name):
|
||||
raise SaltInvocationError('Job `{0}` does not exists.'.format(name))
|
||||
raise CommandExecutionError('Job \'{0}\' does not exist'.format(name))
|
||||
|
||||
return server.get_job_info('empty')['buildable']
|
||||
|
||||
|
@ -422,12 +447,12 @@ def get_job_config(name=None):
|
|||
'''
|
||||
|
||||
if not name:
|
||||
raise SaltInvocationError('Required parameter `name` is missing.')
|
||||
raise SaltInvocationError('Required parameter \'name\' is missing')
|
||||
|
||||
server = _connect()
|
||||
|
||||
if not job_exists(name):
|
||||
raise SaltInvocationError('Job `{0}` does not exists.'.format(name))
|
||||
raise CommandExecutionError('Job \'{0}\' does not exist'.format(name))
|
||||
|
||||
job_info = server.get_job_config(name)
|
||||
return job_info
|
||||
|
|
|
@ -15,23 +15,28 @@ import logging
|
|||
# Import Salt libs
|
||||
import salt.ext.six as six
|
||||
import salt.utils
|
||||
from salt.exceptions import CommandExecutionError
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _fail(ret, msg):
|
||||
ret['comment'] = msg
|
||||
ret['result'] = False
|
||||
return ret
|
||||
|
||||
|
||||
def present(name,
|
||||
config=None,
|
||||
**kwargs):
|
||||
'''
|
||||
Ensure the job is present in the Jenkins
|
||||
configured jobs
|
||||
Ensure the job is present in the Jenkins configured jobs
|
||||
|
||||
name
|
||||
The unique name for the Jenkins job
|
||||
|
||||
config
|
||||
The Salt URL for the file to use for
|
||||
configuring the job.
|
||||
The Salt URL for the file to use for configuring the job
|
||||
'''
|
||||
|
||||
ret = {'name': name,
|
||||
|
@ -39,9 +44,7 @@ def present(name,
|
|||
'changes': {},
|
||||
'comment': ['Job {0} is up to date.'.format(name)]}
|
||||
|
||||
_job_exists = __salt__['jenkins.job_exists'](name)
|
||||
|
||||
if _job_exists:
|
||||
if __salt__['jenkins.job_exists'](name):
|
||||
_current_job_config = __salt__['jenkins.get_job_config'](name)
|
||||
buf = six.moves.StringIO(_current_job_config)
|
||||
_current_job_config = buf.readlines()
|
||||
|
@ -52,23 +55,30 @@ def present(name,
|
|||
|
||||
if _current_job_config != new_config_xml:
|
||||
diff = difflib.unified_diff(_current_job_config, new_config_xml, lineterm='')
|
||||
__salt__['jenkins.update_job'](name, config, __env__)
|
||||
ret['changes'] = ''.join(diff)
|
||||
ret['comment'].append('Job {0} updated.'.format(name))
|
||||
try:
|
||||
__salt__['jenkins.update_job'](name, config, __env__)
|
||||
except CommandExecutionError as exc:
|
||||
return _fail(ret, exc.strerror)
|
||||
else:
|
||||
ret['changes'] = ''.join(diff)
|
||||
ret['comment'].append('Job \'{0}\' updated.'.format(name))
|
||||
|
||||
else:
|
||||
cached_source_path = __salt__['cp.cache_file'](config, __env__)
|
||||
with salt.utils.fopen(cached_source_path) as _fp:
|
||||
new_config_xml = _fp.read()
|
||||
|
||||
__salt__['jenkins.create_job'](name, config, __env__)
|
||||
try:
|
||||
__salt__['jenkins.create_job'](name, config, __env__)
|
||||
except CommandExecutionError as exc:
|
||||
return _fail(ret, exc.strerror)
|
||||
|
||||
buf = six.moves.StringIO(new_config_xml)
|
||||
_current_job_config = buf.readlines()
|
||||
|
||||
diff = difflib.unified_diff('', buf, lineterm='')
|
||||
ret['changes'] = ''.join(diff)
|
||||
ret['comment'].append('Job {0} added.'.format(name))
|
||||
ret['comment'].append('Job \'{0}\' added.'.format(name))
|
||||
|
||||
ret['comment'] = '\n'.join(ret['comment'])
|
||||
return ret
|
||||
|
@ -77,24 +87,23 @@ def present(name,
|
|||
def absent(name,
|
||||
**kwargs):
|
||||
'''
|
||||
Ensure the job is present in the Jenkins
|
||||
configured jobs
|
||||
Ensure the job is absent from the Jenkins configured jobs
|
||||
|
||||
name
|
||||
The name of the Jenkins job to remove.
|
||||
|
||||
The name of the Jenkins job to remove
|
||||
'''
|
||||
|
||||
ret = {'name': name,
|
||||
'result': True,
|
||||
'changes': {},
|
||||
'comment': []}
|
||||
|
||||
_job_exists = __salt__['jenkins.job_exists'](name)
|
||||
|
||||
if _job_exists:
|
||||
__salt__['jenkins.delete_job'](name)
|
||||
ret['comment'] = 'Job {0} deleted.'.format(name)
|
||||
if __salt__['jenkins.job_exists'](name):
|
||||
try:
|
||||
__salt__['jenkins.delete_job'](name)
|
||||
except CommandExecutionError as exc:
|
||||
return _fail(ret, exc.strerror)
|
||||
else:
|
||||
ret['comment'] = 'Job \'{0}\' deleted.'.format(name)
|
||||
else:
|
||||
ret['comment'] = 'Job {0} already absent.'.format(name)
|
||||
ret['comment'] = 'Job \'{0}\' already absent.'.format(name)
|
||||
return ret
|
||||
|
|
Loading…
Add table
Reference in a new issue