blob: 7683cd79088516391fdb386d30b797d6894fd4ba [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")
10def server = desc.getArtifactoryServers().find{{it -> it.name.equals("{name}")}}
11if(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 Josef86686e92017-07-26 15:03:43 +020018 if(desc.getArtifactoryServers() != null && !desc.getArtifactoryServers().isEmpty()){{
19 desc.getArtifactoryServers().removeIf{{it -> it.name.equals("{name}")}}
20 }}
Jakub Josef01940252017-05-10 14:01:22 +020021 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
35delete_artifactory_groovy = u"""\
36def inst = Jenkins.getInstance()
37def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder")
38if(desc.getArtifactoryServers().removeIf{{it -> it.name.equals("{name}")}}){{
39 print("REMOVED")
40}}else{{
41 print("NOT PRESENT")
42}}
43""" # noqa
44
45
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040046def __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 Josef01940252017-05-10 14:01:22 +020058def 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
70def 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
80def _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