blob: 2a91c10c36274399928371f3e678fc580258cb02 [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 Josef8a5c0512018-04-05 14:40:18 +020013 pd.save()
Jakub Josefe01cf3c2017-03-16 13:27:16 +010014 state="SUCCESS"
Adam Tengler70763e02017-08-21 16:50:32 +000015 }else{
Jakub Josefe01cf3c2017-03-16 13:27:16 +010016 state="EXISTS"
Adam Tengler70763e02017-08-21 16:50:32 +000017 }
18 }
19 }
Jakub Josefe01cf3c2017-03-16 13:27:16 +010020 print(state)
Adam Tengler70763e02017-08-21 16:50:32 +000021 }
22}catch(ClassNotFoundException e){
Jakub Josefe01cf3c2017-03-16 13:27:16 +010023 print("Cannot user SimpleThemeDecorator, maybe Simple Theme Plugin not installed")
Adam Tengler70763e02017-08-21 16:50:32 +000024}
25""" # noqa
26
Jakub Josefe01cf3c2017-03-16 13:27:16 +010027
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040028def __virtual__():
29 '''
30 Only load if jenkins_common module exist.
31 '''
32 if 'jenkins_common.call_groovy_script' not in __salt__:
33 return (
34 False,
35 'The jenkins_theme state module cannot be loaded: '
36 'jenkins_common not found')
37 return True
38
39
Jakub Josefe01cf3c2017-03-16 13:27:16 +010040def config(name, css_url, js_url):
41 """
42 Jenkins theme config state method
43
44 :param name: configuration name
45 :param css_url: URL to theme CSS
46 :param js_url: URL to theme JS
47 :returns: salt-specified state dict
48 """
49 test = __opts__['test'] # noqa
50 ret = {
51 'name': name,
52 'changes': {},
53 'result': False,
54 'comment': '',
55 }
56 result = False
57 if test:
58 status = "SUCCESS"
59 ret['changes'][name] = status
60 ret['comment'] = 'Jenkins Theme config %s %s' % (name, status.lower())
61 else:
62 call_result = __salt__['jenkins_common.call_groovy_script'](
63 set_theme_groovy, {"css_url": css_url, "js_url": js_url})
Adam Tengler70763e02017-08-21 16:50:32 +000064 if call_result["code"] == 200 and call_result["msg"] in [
65 "SUCCESS", "EXISTS"]:
Jakub Josefe01cf3c2017-03-16 13:27:16 +010066 status = call_result["msg"]
67 if status == "SUCCESS":
68 ret['changes'][name] = status
Adam Tengler70763e02017-08-21 16:50:32 +000069 ret['comment'] = 'Jenkins theme config %s %s' % (
70 name, status.lower())
Jakub Josefe01cf3c2017-03-16 13:27:16 +010071 result = True
72 else:
73 status = 'FAILED'
74 logger.error(
75 "Jenkins theme API call failure: %s", call_result["msg"])
76 ret['comment'] = 'Jenkins theme API call failure: %s' % (call_result[
Adam Tengler70763e02017-08-21 16:50:32 +000077 "msg"])
Jakub Josefe01cf3c2017-03-16 13:27:16 +010078 ret['result'] = None if test else result
79 return ret