blob: 03a015ce50d1542b309dfcb41ae414b3ea0cbede [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{{
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
33delete_artifactory_groovy = u"""\
34def inst = Jenkins.getInstance()
35def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder")
36if(desc.getArtifactoryServers().removeIf{{it -> it.name.equals("{name}")}}){{
37 print("REMOVED")
38}}else{{
39 print("NOT PRESENT")
40}}
41""" # noqa
42
43
44def 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
56def 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
66def _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