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 | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 13 | state="SUCCESS" |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 14 | }else{ |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 15 | state="EXISTS" |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 16 | } |
| 17 | } |
| 18 | } |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 19 | print(state) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 20 | } |
| 21 | }catch(ClassNotFoundException e){ |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 22 | print("Cannot user SimpleThemeDecorator, maybe Simple Theme Plugin not installed") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 23 | } |
| 24 | """ # noqa |
| 25 | |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 26 | |
Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 27 | def __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 Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 39 | def 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 63 | if call_result["code"] == 200 and call_result["msg"] in [ |
| 64 | "SUCCESS", "EXISTS"]: |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 65 | status = call_result["msg"] |
| 66 | if status == "SUCCESS": |
| 67 | ret['changes'][name] = status |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 68 | ret['comment'] = 'Jenkins theme config %s %s' % ( |
| 69 | name, status.lower()) |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 70 | 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 76 | "msg"]) |
Jakub Josef | e01cf3c | 2017-03-16 13:27:16 +0100 | [diff] [blame] | 77 | ret['result'] = None if test else result |
| 78 | return ret |