blob: 5e41e79fb7da89530162dd63526db02929f06448 [file] [log] [blame]
import logging
logger = logging.getLogger(__name__)
test_timestamper_groovy = """\
import hudson.plugins.timestamper.TimestamperConfig
def timestamper = TimestamperConfig.get()
if (timestamper.isAllPipelines()) {
print("ENABLED")
} else {
print("DISABLED")
}
"""
enable_timestamper_groovy = """\
import hudson.plugins.timestamper.TimestamperConfig
def timestamper = TimestamperConfig.get()
if (!timestamper.isAllPipelines()) {
timestamper.setAllPipelines(true)
print("ENABLED")
} else {
print("ALREADY ENABLED")
}
"""
disable_timestamper_groovy = """\
import hudson.plugins.timestamper.TimestamperConfig
def timestamper = TimestamperConfig.get()
if (timestamper.isAllPipelines()) {
timestamper.setAllPipelines(false)
print("DISABLED")
} else {
print("ALREADY DISABLED")
}
"""
def __virtual__():
'''
Only load if jenkins_common module exist.
'''
if 'jenkins_common.call_groovy_script' not in __salt__:
return (
False,
'The jenkins_timestamper state module cannot be loaded: '
'jenkins_common not found')
return True
def present(name='timestamper'):
test = __opts__['test'] # noqa
ret = {
'name': name,
'changes': {},
'result': False,
'comment': '',
}
result = False
if test:
call_result = __salt__['jenkins_common.call_groovy_script'](
test_timestamper_groovy, {})
if call_result["code"] == 200 and call_result["msg"] in [
"DISABLED"]:
ret['comment'] = 'Jenkins timestamper will be enabled'
else:
call_result = __salt__['jenkins_common.call_groovy_script'](
enable_timestamper_groovy, {})
if call_result["code"] == 200 and call_result["msg"] in [
"ENABLED", "ALREADY ENABLED"]:
status = call_result["msg"]
if status == "ENABLED":
ret['changes'][name] = status
ret['comment'] = 'Jenkins timestamper config %s %s' % (
name, status.lower())
result = True
else:
status = 'FAILED'
logger.error(
"Jenkins timestamper config API call failure: %s", call_result["msg"])
ret['comment'] = 'Jenkins timestamper config API call failure: %s' % (call_result[
"msg"])
ret['result'] = None if test else result
return ret
def absent(name='timestamper'):
test = __opts__['test'] # noqa
ret = {
'name': name,
'changes': {},
'result': False,
'comment': '',
}
result = False
if test:
call_result = __salt__['jenkins_common.call_groovy_script'](
test_timestamper_groovy, {})
if call_result["code"] == 200 and call_result["msg"] in [
"ENABLED"]:
ret['comment'] = 'Jenkins timestamper will be disabled'
else:
call_result = __salt__['jenkins_common.call_groovy_script'](
disable_timestamper_groovy, {})
if call_result["code"] == 200 and call_result["msg"] in [
"DISABLED", "ALREADY DISABLED"]:
status = call_result["msg"]
if status == "DISABLED":
ret['changes'][name] = status
ret['comment'] = 'Jenkins timestamper config %s %s' % (
name, status.lower())
result = True
else:
status = 'FAILED'
logger.error(
"Jenkins timestamper config API call failure: %s", call_result["msg"])
ret['comment'] = 'Jenkins timestamper config API call failure: %s' % (call_result[
"msg"])
ret['result'] = None if test else result
return ret