blob: 38ce6c22f390cf1ec58095d6170b45f5d31ff2c5 [file] [log] [blame]
Jakub Josef01940252017-05-10 14:01:22 +02001import logging
2logger = logging.getLogger(__name__)
3
4add_artifactory_groovy = u"""\
5import jenkins.model.*
6import org.jfrog.*
7import org.jfrog.hudson.*
8def inst = Jenkins.getInstance()
9def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder")
Jakub Josef9d9b0ed2017-07-26 16:12:57 +020010// empty artifactory servers is not empty list but null, but find can be called on null
Jakub Josef01940252017-05-10 14:01:22 +020011def server = desc.getArtifactoryServers().find{{it -> it.name.equals("{name}")}}
12if(server &&
13 server.getName().equals("{name}") &&
14 server.getUrl().equals("{serverUrl}") &&
Jakub Josefff4059c2017-08-15 18:52:36 +020015 (server.getDeployerCredentialsConfig() == null || server.getDeployerCredentialsConfig().getCredentialsId().equals("{credentialsId}")) &&
Jakub Josef431a6942017-08-16 14:21:55 +020016 (server.getResolverCredentialsConfig() == null || server.getResolverCredentialsConfig().getCredentialsId().equals("{credentialsId}"))){{
Jakub Josef01940252017-05-10 14:01:22 +020017 print("EXISTS")
18}}else{{
Jakub Josef9d9b0ed2017-07-26 16:12:57 +020019 // we must care about null here
Jakub Josef86686e92017-07-26 15:03:43 +020020 if(desc.getArtifactoryServers() != null && !desc.getArtifactoryServers().isEmpty()){{
21 desc.getArtifactoryServers().removeIf{{it -> it.name.equals("{name}")}}
Jakub Josef9d9b0ed2017-07-26 16:12:57 +020022 }}else{{
23 desc.setArtifactoryServers([])
Jakub Josef86686e92017-07-26 15:03:43 +020024 }}
Jakub Josef01940252017-05-10 14:01:22 +020025 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
39delete_artifactory_groovy = u"""\
40def inst = Jenkins.getInstance()
41def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder")
42if(desc.getArtifactoryServers().removeIf{{it -> it.name.equals("{name}")}}){{
43 print("REMOVED")
44}}else{{
45 print("NOT PRESENT")
46}}
47""" # noqa
48
49
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040050def __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 Josef01940252017-05-10 14:01:22 +020062def 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
74def 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
84def _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