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