Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 1 | import logging |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 2 | |
Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 3 | logger = logging.getLogger(__name__) |
| 4 | |
| 5 | add_artifactory_groovy = u"""\ |
| 6 | import jenkins.model.* |
| 7 | import org.jfrog.* |
| 8 | import org.jfrog.hudson.* |
| 9 | def inst = Jenkins.getInstance() |
| 10 | def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder") |
Dmitry Burmistrov | d55e3d4 | 2018-07-10 17:17:12 +0400 | [diff] [blame] | 11 | if (! desc.useCredentialsPlugin ) { |
| 12 | desc.useCredentialsPlugin = true |
| 13 | } |
Jakub Josef | 9d9b0ed | 2017-07-26 16:12:57 +0200 | [diff] [blame] | 14 | // empty artifactory servers is not empty list but null, but find can be called on null |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 15 | def server = desc.getArtifactoryServers().find{it -> it.name.equals("${name}")} |
Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 16 | if(server && |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 17 | server.getName().equals("${name}") && |
| 18 | server.getUrl().equals("${serverUrl}") && |
| 19 | (server.getDeployerCredentialsConfig() == null || server.getDeployerCredentialsConfig().getCredentialsId().equals("${credentialsId}")) && |
| 20 | (server.getResolverCredentialsConfig() == null || server.getResolverCredentialsConfig().getCredentialsId().equals("${credentialsId}"))){ |
Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 21 | print("EXISTS") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 22 | }else{ |
Jakub Josef | 9d9b0ed | 2017-07-26 16:12:57 +0200 | [diff] [blame] | 23 | // we must care about null here |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 24 | if(desc.getArtifactoryServers() != null && !desc.getArtifactoryServers().isEmpty()){ |
| 25 | desc.getArtifactoryServers().removeIf{it -> it.name.equals("${name}")} |
| 26 | }else{ |
Jakub Josef | 9d9b0ed | 2017-07-26 16:12:57 +0200 | [diff] [blame] | 27 | desc.setArtifactoryServers([]) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 28 | } |
Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 29 | def newServer = new ArtifactoryServer( |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 30 | "${name}", |
| 31 | "${serverUrl}", |
| 32 | new CredentialsConfig("", "", "${credentialsId}"), |
| 33 | new CredentialsConfig("", "", "${credentialsId}"), |
Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 34 | 300, |
| 35 | false, |
| 36 | null) |
| 37 | desc.getArtifactoryServers().add(newServer) |
| 38 | desc.save() |
| 39 | print("ADDED/CHANGED") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 40 | } |
Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 41 | """ # noqa |
| 42 | |
| 43 | delete_artifactory_groovy = u"""\ |
| 44 | def inst = Jenkins.getInstance() |
| 45 | def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 46 | if(desc.getArtifactoryServers().removeIf{it -> it.name.equals("${name}")}){ |
Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 47 | print("REMOVED") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 48 | }else{ |
Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 49 | print("NOT PRESENT") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 50 | } |
Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 51 | """ # noqa |
| 52 | |
| 53 | |
Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 54 | def __virtual__(): |
| 55 | ''' |
| 56 | Only load if jenkins_common module exist. |
| 57 | ''' |
| 58 | if 'jenkins_common.call_groovy_script' not in __salt__: |
| 59 | return ( |
| 60 | False, |
| 61 | 'The jenkins_artifactory state module cannot be loaded: ' |
| 62 | 'jenkins_common not found') |
| 63 | return True |
| 64 | |
| 65 | |
Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 66 | def present(name, url, credential_id, **kwargs): |
| 67 | """ |
| 68 | Jenkins artifactory present state method |
| 69 | |
| 70 | :param name: artifactory server name |
| 71 | :param url: artifactory server url |
| 72 | :param credential_id: artifactory server credential id |
| 73 | :returns: salt-specified state dict |
| 74 | """ |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 75 | return _plugin_call(name, url, credential_id, add_artifactory_groovy, [ |
| 76 | "ADDED/CHANGED", "EXISTS"], **kwargs) |
Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 77 | |
| 78 | |
| 79 | def absent(name, **kwargs): |
| 80 | """ |
| 81 | Jenkins artifactory present state method |
| 82 | |
| 83 | :param name: artifactory server name |
| 84 | :returns: salt-specified state dict |
| 85 | """ |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 86 | return _plugin_call(name, None, None, delete_artifactory_groovy, [ |
| 87 | "REMOVED", "NOT PRESENT"], **kwargs) |
Jakub Josef | 0194025 | 2017-05-10 14:01:22 +0200 | [diff] [blame] | 88 | |
| 89 | |
| 90 | def _plugin_call(name, url, credentialsId, template, success_msgs, **kwargs): |
| 91 | test = __opts__['test'] # noqa |
| 92 | ret = { |
| 93 | 'name': name, |
| 94 | 'changes': {}, |
| 95 | 'result': False, |
| 96 | 'comment': '', |
| 97 | } |
| 98 | result = False |
| 99 | if test: |
| 100 | status = success_msgs[0] |
| 101 | ret['changes'][name] = status |
| 102 | ret['comment'] = 'Jenkins artifactory server %s %s' % ( |
| 103 | name, status.lower()) |
| 104 | else: |
| 105 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 106 | template, {"name": name, "serverUrl": url, "credentialsId": credentialsId}) |
| 107 | if call_result["code"] == 200 and call_result["msg"] in success_msgs: |
| 108 | status = call_result["msg"] |
| 109 | if status == success_msgs[0]: |
| 110 | ret['changes'][name] = status |
| 111 | ret['comment'] = 'Jenkins artifactory server %s %s' % ( |
| 112 | name, status.lower()) |
| 113 | result = True |
| 114 | else: |
| 115 | status = 'FAILED' |
| 116 | logger.error( |
| 117 | "Jenkins artifactory API call failure: %s", call_result["msg"]) |
| 118 | ret['comment'] = 'Jenkins artifactory API call failure: %s' % (call_result[ |
| 119 | "msg"]) |
| 120 | ret['result'] = None if test else result |
| 121 | return ret |