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") |
| 10 | def server = desc.getArtifactoryServers().find{{it -> it.name.equals("{name}")}} |
| 11 | if(server && |
| 12 | server.getName().equals("{name}") && |
| 13 | server.getUrl().equals("{serverUrl}") && |
| 14 | server.getDeployerCredentialsConfig().getCredentialsId().equals("{credentialsId}") && |
| 15 | server.getResolverCredentialsConfig().getCredentialsId().equals("{credentialsId}")){{ |
| 16 | print("EXISTS") |
| 17 | }}else{{ |
| 18 | desc.getArtifactoryServers().removeIf{{it -> it.name.equals("{name}")}} |
| 19 | def newServer = new ArtifactoryServer( |
| 20 | "{name}", |
| 21 | "{serverUrl}", |
| 22 | new CredentialsConfig("", "", "{credentialsId}"), |
| 23 | new CredentialsConfig("", "", "{credentialsId}"), |
| 24 | 300, |
| 25 | false, |
| 26 | null) |
| 27 | desc.getArtifactoryServers().add(newServer) |
| 28 | desc.save() |
| 29 | print("ADDED/CHANGED") |
| 30 | }} |
| 31 | """ # noqa |
| 32 | |
| 33 | delete_artifactory_groovy = u"""\ |
| 34 | def inst = Jenkins.getInstance() |
| 35 | def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder") |
| 36 | if(desc.getArtifactoryServers().removeIf{{it -> it.name.equals("{name}")}}){{ |
| 37 | print("REMOVED") |
| 38 | }}else{{ |
| 39 | print("NOT PRESENT") |
| 40 | }} |
| 41 | """ # noqa |
| 42 | |
| 43 | |
| 44 | def present(name, url, credential_id, **kwargs): |
| 45 | """ |
| 46 | Jenkins artifactory present state method |
| 47 | |
| 48 | :param name: artifactory server name |
| 49 | :param url: artifactory server url |
| 50 | :param credential_id: artifactory server credential id |
| 51 | :returns: salt-specified state dict |
| 52 | """ |
| 53 | return _plugin_call(name, url, credential_id, add_artifactory_groovy, ["ADDED/CHANGED", "EXISTS"], **kwargs) |
| 54 | |
| 55 | |
| 56 | def absent(name, **kwargs): |
| 57 | """ |
| 58 | Jenkins artifactory present state method |
| 59 | |
| 60 | :param name: artifactory server name |
| 61 | :returns: salt-specified state dict |
| 62 | """ |
| 63 | return _plugin_call(name, None, None, delete_artifactory_groovy, ["REMOVED", "NOT PRESENT"], **kwargs) |
| 64 | |
| 65 | |
| 66 | def _plugin_call(name, url, credentialsId, template, success_msgs, **kwargs): |
| 67 | test = __opts__['test'] # noqa |
| 68 | ret = { |
| 69 | 'name': name, |
| 70 | 'changes': {}, |
| 71 | 'result': False, |
| 72 | 'comment': '', |
| 73 | } |
| 74 | result = False |
| 75 | if test: |
| 76 | status = success_msgs[0] |
| 77 | ret['changes'][name] = status |
| 78 | ret['comment'] = 'Jenkins artifactory server %s %s' % ( |
| 79 | name, status.lower()) |
| 80 | else: |
| 81 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 82 | template, {"name": name, "serverUrl": url, "credentialsId": credentialsId}) |
| 83 | if call_result["code"] == 200 and call_result["msg"] in success_msgs: |
| 84 | status = call_result["msg"] |
| 85 | if status == success_msgs[0]: |
| 86 | ret['changes'][name] = status |
| 87 | ret['comment'] = 'Jenkins artifactory server %s %s' % ( |
| 88 | name, status.lower()) |
| 89 | result = True |
| 90 | else: |
| 91 | status = 'FAILED' |
| 92 | logger.error( |
| 93 | "Jenkins artifactory API call failure: %s", call_result["msg"]) |
| 94 | ret['comment'] = 'Jenkins artifactory API call failure: %s' % (call_result[ |
| 95 | "msg"]) |
| 96 | ret['result'] = None if test else result |
| 97 | return ret |