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