Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 1 | import logging |
| 2 | logger = logging.getLogger(__name__) |
| 3 | |
| 4 | set_theme_groovy = """\ |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 5 | try{ |
| 6 | if(Class.forName("org.codefirst.SimpleThemeDecorator")){ |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 7 | def state; |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 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}" |
Jakub Josef | 8a5c051 | 2018-04-05 14:40:18 +0200 | [diff] [blame^] | 13 | pd.save() |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 14 | state="SUCCESS" |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 15 | }else{ |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 16 | state="EXISTS" |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 17 | } |
| 18 | } |
| 19 | } |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 20 | print(state) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 21 | } |
| 22 | }catch(ClassNotFoundException e){ |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 23 | print("Cannot user SimpleThemeDecorator, maybe Simple Theme Plugin not installed") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 24 | } |
| 25 | """ # noqa |
| 26 | |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 27 | |
Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 28 | def __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 Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 40 | def 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 64 | if call_result["code"] == 200 and call_result["msg"] in [ |
| 65 | "SUCCESS", "EXISTS"]: |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 66 | status = call_result["msg"] |
| 67 | if status == "SUCCESS": |
| 68 | ret['changes'][name] = status |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 69 | ret['comment'] = 'Jenkins theme config %s %s' % ( |
| 70 | name, status.lower()) |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 71 | 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 77 | "msg"]) |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 78 | ret['result'] = None if test else result |
| 79 | return ret |