blob: c27b0a2e158e37dc44549273a93a8c660afa50f6 [file] [log] [blame]
Jakub Josefe01cf3c2017-03-16 13:27:16 +01001import logging
2logger = logging.getLogger(__name__)
3
4set_theme_groovy = """\
Adam Tengler70763e02017-08-21 16:50:32 +00005try{
6 if(Class.forName("org.codefirst.SimpleThemeDecorator")){
Jakub Josefe01cf3c2017-03-16 13:27:16 +01007 def state;
Adam Tengler70763e02017-08-21 16:50:32 +00008 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}"
Jakub Josefe01cf3c2017-03-16 13:27:16 +010013 state="SUCCESS"
Adam Tengler70763e02017-08-21 16:50:32 +000014 }else{
Jakub Josefe01cf3c2017-03-16 13:27:16 +010015 state="EXISTS"
Adam Tengler70763e02017-08-21 16:50:32 +000016 }
17 }
18 }
Jakub Josefe01cf3c2017-03-16 13:27:16 +010019 print(state)
Adam Tengler70763e02017-08-21 16:50:32 +000020 }
21}catch(ClassNotFoundException e){
Jakub Josefe01cf3c2017-03-16 13:27:16 +010022 print("Cannot user SimpleThemeDecorator, maybe Simple Theme Plugin not installed")
Adam Tengler70763e02017-08-21 16:50:32 +000023}
24""" # noqa
25
Jakub Josefe01cf3c2017-03-16 13:27:16 +010026
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040027def __virtual__():
28 '''
29 Only load if jenkins_common module exist.
30 '''
31 if 'jenkins_common.call_groovy_script' not in __salt__:
32 return (
33 False,
34 'The jenkins_theme state module cannot be loaded: '
35 'jenkins_common not found')
36 return True
37
38
Jakub Josefe01cf3c2017-03-16 13:27:16 +010039def config(name, css_url, js_url):
40 """
41 Jenkins theme config state method
42
43 :param name: configuration name
44 :param css_url: URL to theme CSS
45 :param js_url: URL to theme JS
46 :returns: salt-specified state dict
47 """
48 test = __opts__['test'] # noqa
49 ret = {
50 'name': name,
51 'changes': {},
52 'result': False,
53 'comment': '',
54 }
55 result = False
56 if test:
57 status = "SUCCESS"
58 ret['changes'][name] = status
59 ret['comment'] = 'Jenkins Theme config %s %s' % (name, status.lower())
60 else:
61 call_result = __salt__['jenkins_common.call_groovy_script'](
62 set_theme_groovy, {"css_url": css_url, "js_url": js_url})
Adam Tengler70763e02017-08-21 16:50:32 +000063 if call_result["code"] == 200 and call_result["msg"] in [
64 "SUCCESS", "EXISTS"]:
Jakub Josefe01cf3c2017-03-16 13:27:16 +010065 status = call_result["msg"]
66 if status == "SUCCESS":
67 ret['changes'][name] = status
Adam Tengler70763e02017-08-21 16:50:32 +000068 ret['comment'] = 'Jenkins theme config %s %s' % (
69 name, status.lower())
Jakub Josefe01cf3c2017-03-16 13:27:16 +010070 result = True
71 else:
72 status = 'FAILED'
73 logger.error(
74 "Jenkins theme API call failure: %s", call_result["msg"])
75 ret['comment'] = 'Jenkins theme API call failure: %s' % (call_result[
Adam Tengler70763e02017-08-21 16:50:32 +000076 "msg"])
Jakub Josefe01cf3c2017-03-16 13:27:16 +010077 ret['result'] = None if test else result
78 return ret