blob: 2f0208bcdb64bb62a22288c8f79c7381c21c0008 [file] [log] [blame]
import logging
logger = logging.getLogger(__name__)
set_theme_groovy = """\
try{{
if(Class.forName("org.codefirst.SimpleThemeDecorator")){{
def state;
for (pd in PageDecorator.all()) {{
if (pd instanceof org.codefirst.SimpleThemeDecorator) {{
if(!pd.cssUrl.equals("{css_url}") || !pd.jsUrl.equals("{js_url}")){{
pd.cssUrl = "{css_url}"
pd.jsUrl = "{js_url}"
state="SUCCESS"
}}else{{
state="EXISTS"
}}
}}
}}
print(state)
}}
}}catch(ClassNotFoundException e){{
print("Cannot user SimpleThemeDecorator, maybe Simple Theme Plugin not installed")
}}
""" # noqa
def __virtual__():
'''
Only load if jenkins_common module exist.
'''
if 'jenkins_common.call_groovy_script' not in __salt__:
return (
False,
'The jenkins_theme state module cannot be loaded: '
'jenkins_common not found')
return True
def config(name, css_url, js_url):
"""
Jenkins theme config state method
:param name: configuration name
:param css_url: URL to theme CSS
:param js_url: URL to theme JS
:returns: salt-specified state dict
"""
test = __opts__['test'] # noqa
ret = {
'name': name,
'changes': {},
'result': False,
'comment': '',
}
result = False
if test:
status = "SUCCESS"
ret['changes'][name] = status
ret['comment'] = 'Jenkins Theme config %s %s' % (name, status.lower())
else:
call_result = __salt__['jenkins_common.call_groovy_script'](
set_theme_groovy, {"css_url": css_url, "js_url": js_url})
if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
status = call_result["msg"]
if status == "SUCCESS":
ret['changes'][name] = status
ret['comment'] = 'Jenkins theme config %s %s' % (name, status.lower())
result = True
else:
status = 'FAILED'
logger.error(
"Jenkins theme API call failure: %s", call_result["msg"])
ret['comment'] = 'Jenkins theme API call failure: %s' % (call_result[
"msg"])
ret['result'] = None if test else result
return ret