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