blob: 2f0208bcdb64bb62a22288c8f79c7381c21c0008 [file] [log] [blame]
Jakub Josefe01cf3c2017-03-16 13:27:16 +01001import logging
2logger = logging.getLogger(__name__)
3
4set_theme_groovy = """\
5try{{
6 if(Class.forName("org.codefirst.SimpleThemeDecorator")){{
7 def state;
8 for (pd in PageDecorator.all()) {{
9 if (pd instanceof org.codefirst.SimpleThemeDecorator) {{
10 if(!pd.cssUrl.equals("{css_url}") || !pd.jsUrl.equals("{js_url}")){{
11 pd.cssUrl = "{css_url}"
12 pd.jsUrl = "{js_url}"
13 state="SUCCESS"
14 }}else{{
15 state="EXISTS"
16 }}
17 }}
18 }}
19 print(state)
20 }}
21}}catch(ClassNotFoundException e){{
22 print("Cannot user SimpleThemeDecorator, maybe Simple Theme Plugin not installed")
23}}
24""" # noqa
25
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040026def __virtual__():
27 '''
28 Only load if jenkins_common module exist.
29 '''
30 if 'jenkins_common.call_groovy_script' not in __salt__:
31 return (
32 False,
33 'The jenkins_theme state module cannot be loaded: '
34 'jenkins_common not found')
35 return True
36
37
Jakub Josefe01cf3c2017-03-16 13:27:16 +010038def config(name, css_url, js_url):
39 """
40 Jenkins theme config state method
41
42 :param name: configuration name
43 :param css_url: URL to theme CSS
44 :param js_url: URL to theme JS
45 :returns: salt-specified state dict
46 """
47 test = __opts__['test'] # noqa
48 ret = {
49 'name': name,
50 'changes': {},
51 'result': False,
52 'comment': '',
53 }
54 result = False
55 if test:
56 status = "SUCCESS"
57 ret['changes'][name] = status
58 ret['comment'] = 'Jenkins Theme config %s %s' % (name, status.lower())
59 else:
60 call_result = __salt__['jenkins_common.call_groovy_script'](
61 set_theme_groovy, {"css_url": css_url, "js_url": js_url})
62 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
63 status = call_result["msg"]
64 if status == "SUCCESS":
65 ret['changes'][name] = status
66 ret['comment'] = 'Jenkins theme config %s %s' % (name, status.lower())
67 result = True
68 else:
69 status = 'FAILED'
70 logger.error(
71 "Jenkins theme API call failure: %s", call_result["msg"])
72 ret['comment'] = 'Jenkins theme API call failure: %s' % (call_result[
73 "msg"])
74 ret['result'] = None if test else result
75 return ret