Jakub Josef | a6d4c83 | 2017-01-19 14:06:12 +0100 | [diff] [blame] | 1 | import logging |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 2 | |
Jakub Josef | a6d4c83 | 2017-01-19 14:06:12 +0100 | [diff] [blame] | 3 | logger = logging.getLogger(__name__) |
| 4 | |
| 5 | config_slack_groovy = """\ |
| 6 | jenkins = jenkins.model.Jenkins.getInstance() |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 7 | try{ |
Jakub Josef | a6d4c83 | 2017-01-19 14:06:12 +0100 | [diff] [blame] | 8 | slack = jenkins.getDescriptorByType(jenkins.plugins.slack.SlackNotifier.DescriptorImpl) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 9 | if(slack.teamDomain.equals("${team_domain}") && |
| 10 | slack.token.equals("${token}") && |
| 11 | slack.tokenCredentialId.equals("${token_credential_id}") && |
| 12 | slack.room.equals("${room}") && |
| 13 | slack.sendAs.equals("${send_as}")){ |
Jakub Josef | a6d4c83 | 2017-01-19 14:06:12 +0100 | [diff] [blame] | 14 | print("EXISTS") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 15 | }else{ |
| 16 | slack.teamDomain = "${team_domain}" |
| 17 | slack.token = "${token}" |
| 18 | slack.tokenCredentialId = "${token_credential_id}" |
| 19 | slack.room = "${room}" |
| 20 | slack.sendAs = "${send_as}" |
Jakub Josef | a6d4c83 | 2017-01-19 14:06:12 +0100 | [diff] [blame] | 21 | slack.save() |
| 22 | print("SUCCESS") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 23 | } |
| 24 | }catch(all){ |
Jakub Josef | a6d4c83 | 2017-01-19 14:06:12 +0100 | [diff] [blame] | 25 | print("Cannot instantiate Jenkins Slack plugin, maybe plugin is not installed") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 26 | } |
| 27 | """ # noqa |
Jakub Josef | a6d4c83 | 2017-01-19 14:06:12 +0100 | [diff] [blame] | 28 | |
Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 29 | |
| 30 | def __virtual__(): |
| 31 | ''' |
| 32 | Only load if jenkins_common module exist. |
| 33 | ''' |
| 34 | if 'jenkins_common.call_groovy_script' not in __salt__: |
| 35 | return ( |
| 36 | False, |
| 37 | 'The jenkins_slack state module cannot be loaded: ' |
| 38 | 'jenkins_common not found') |
| 39 | return True |
| 40 | |
| 41 | |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 42 | def config(name, team_domain, token, |
| 43 | token_credential_id="", room="", send_as=None): |
Jakub Josef | a6d4c83 | 2017-01-19 14:06:12 +0100 | [diff] [blame] | 44 | """ |
| 45 | Jenkins Slack config state method |
| 46 | |
| 47 | :param name: configuration name |
| 48 | :param team_domain: slack team domain |
| 49 | :param token: slack token |
| 50 | :param token_credential_id: slack token credential id |
| 51 | :param room: slack room |
| 52 | :param send_as: slack send as param |
| 53 | :returns: salt-specified state dict |
| 54 | """ |
| 55 | test = __opts__['test'] # noqa |
| 56 | ret = { |
| 57 | 'name': name, |
| 58 | 'changes': {}, |
| 59 | 'result': False, |
| 60 | 'comment': '', |
| 61 | } |
| 62 | result = False |
| 63 | if test: |
| 64 | status = "SUCCESS" |
| 65 | ret['changes'][name] = status |
| 66 | ret['comment'] = 'Jenkins Slack config %s %s' % (name, status.lower()) |
| 67 | else: |
| 68 | call_result = __salt__['jenkins_common.call_groovy_script']( |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 69 | config_slack_groovy, {"team_domain": team_domain, |
| 70 | "token": token, |
Jakub Josef | a6d4c83 | 2017-01-19 14:06:12 +0100 | [diff] [blame] | 71 | "token_credential_id": token_credential_id if token_credential_id else "", |
| 72 | "room": room if room else "", |
| 73 | "send_as": send_as if send_as else ""}) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 74 | if call_result["code"] == 200 and call_result["msg"] in [ |
| 75 | "SUCCESS", "EXISTS"]: |
Jakub Josef | a6d4c83 | 2017-01-19 14:06:12 +0100 | [diff] [blame] | 76 | status = call_result["msg"] |
| 77 | if status == "SUCCESS": |
| 78 | ret['changes'][name] = status |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 79 | ret['comment'] = 'Jenkins Slack config %s %s' % ( |
| 80 | name, status.lower()) |
Jakub Josef | a6d4c83 | 2017-01-19 14:06:12 +0100 | [diff] [blame] | 81 | result = True |
| 82 | else: |
| 83 | status = 'FAILED' |
| 84 | logger.error( |
| 85 | "Jenkins slack API call failure: %s", call_result["msg"]) |
| 86 | ret['comment'] = 'Jenkins slack API call failure: %s' % (call_result[ |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 87 | "msg"]) |
Jakub Josef | a6d4c83 | 2017-01-19 14:06:12 +0100 | [diff] [blame] | 88 | ret['result'] = None if test else result |
| 89 | return ret |