blob: 91dc4083d9cb2d3341bb1d08ad9941a5822eee48 [file] [log] [blame]
Jakub Josef01940252017-05-10 14:01:22 +02001import logging
Adam Tengler70763e02017-08-21 16:50:32 +00002
Jakub Josef01940252017-05-10 14:01:22 +02003logger = logging.getLogger(__name__)
4
5add_artifactory_groovy = u"""\
6import jenkins.model.*
7import org.jfrog.*
8import org.jfrog.hudson.*
9def inst = Jenkins.getInstance()
10def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder")
Dmitry Burmistrovd55e3d42018-07-10 17:17:12 +040011if (! desc.useCredentialsPlugin ) {
12 desc.useCredentialsPlugin = true
13}
Jakub Josef9d9b0ed2017-07-26 16:12:57 +020014// empty artifactory servers is not empty list but null, but find can be called on null
Adam Tengler70763e02017-08-21 16:50:32 +000015def server = desc.getArtifactoryServers().find{it -> it.name.equals("${name}")}
Jakub Josef01940252017-05-10 14:01:22 +020016if(server &&
Adam Tengler70763e02017-08-21 16:50:32 +000017 server.getName().equals("${name}") &&
18 server.getUrl().equals("${serverUrl}") &&
19 (server.getDeployerCredentialsConfig() == null || server.getDeployerCredentialsConfig().getCredentialsId().equals("${credentialsId}")) &&
20 (server.getResolverCredentialsConfig() == null || server.getResolverCredentialsConfig().getCredentialsId().equals("${credentialsId}"))){
Jakub Josef01940252017-05-10 14:01:22 +020021 print("EXISTS")
Adam Tengler70763e02017-08-21 16:50:32 +000022}else{
Jakub Josef9d9b0ed2017-07-26 16:12:57 +020023 // we must care about null here
Adam Tengler70763e02017-08-21 16:50:32 +000024 if(desc.getArtifactoryServers() != null && !desc.getArtifactoryServers().isEmpty()){
25 desc.getArtifactoryServers().removeIf{it -> it.name.equals("${name}")}
26 }else{
Jakub Josef9d9b0ed2017-07-26 16:12:57 +020027 desc.setArtifactoryServers([])
Adam Tengler70763e02017-08-21 16:50:32 +000028 }
Jakub Josef01940252017-05-10 14:01:22 +020029 def newServer = new ArtifactoryServer(
Adam Tengler70763e02017-08-21 16:50:32 +000030 "${name}",
31 "${serverUrl}",
32 new CredentialsConfig("", "", "${credentialsId}"),
33 new CredentialsConfig("", "", "${credentialsId}"),
Jakub Josef01940252017-05-10 14:01:22 +020034 300,
35 false,
36 null)
37 desc.getArtifactoryServers().add(newServer)
38 desc.save()
39 print("ADDED/CHANGED")
Adam Tengler70763e02017-08-21 16:50:32 +000040}
Jakub Josef01940252017-05-10 14:01:22 +020041""" # noqa
42
43delete_artifactory_groovy = u"""\
44def inst = Jenkins.getInstance()
45def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder")
Ivan Berezovskiy348ab9f2018-10-19 16:05:33 +040046if(desc && desc.getArtifactoryServers().removeIf{it -> it.name.equals("${name}")}){
Jakub Josef01940252017-05-10 14:01:22 +020047 print("REMOVED")
Adam Tengler70763e02017-08-21 16:50:32 +000048}else{
Jakub Josef01940252017-05-10 14:01:22 +020049 print("NOT PRESENT")
Adam Tengler70763e02017-08-21 16:50:32 +000050}
Jakub Josef01940252017-05-10 14:01:22 +020051""" # noqa
52
53
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040054def __virtual__():
55 '''
56 Only load if jenkins_common module exist.
57 '''
58 if 'jenkins_common.call_groovy_script' not in __salt__:
59 return (
60 False,
61 'The jenkins_artifactory state module cannot be loaded: '
62 'jenkins_common not found')
63 return True
64
65
Jakub Josef01940252017-05-10 14:01:22 +020066def present(name, url, credential_id, **kwargs):
67 """
68 Jenkins artifactory present state method
69
70 :param name: artifactory server name
71 :param url: artifactory server url
72 :param credential_id: artifactory server credential id
73 :returns: salt-specified state dict
74 """
Adam Tengler70763e02017-08-21 16:50:32 +000075 return _plugin_call(name, url, credential_id, add_artifactory_groovy, [
76 "ADDED/CHANGED", "EXISTS"], **kwargs)
Jakub Josef01940252017-05-10 14:01:22 +020077
78
79def absent(name, **kwargs):
80 """
81 Jenkins artifactory present state method
82
83 :param name: artifactory server name
84 :returns: salt-specified state dict
85 """
Adam Tengler70763e02017-08-21 16:50:32 +000086 return _plugin_call(name, None, None, delete_artifactory_groovy, [
87 "REMOVED", "NOT PRESENT"], **kwargs)
Jakub Josef01940252017-05-10 14:01:22 +020088
89
90def _plugin_call(name, url, credentialsId, template, success_msgs, **kwargs):
91 test = __opts__['test'] # noqa
92 ret = {
93 'name': name,
94 'changes': {},
95 'result': False,
96 'comment': '',
97 }
98 result = False
99 if test:
100 status = success_msgs[0]
101 ret['changes'][name] = status
102 ret['comment'] = 'Jenkins artifactory server %s %s' % (
103 name, status.lower())
104 else:
105 call_result = __salt__['jenkins_common.call_groovy_script'](
106 template, {"name": name, "serverUrl": url, "credentialsId": credentialsId})
107 if call_result["code"] == 200 and call_result["msg"] in success_msgs:
108 status = call_result["msg"]
109 if status == success_msgs[0]:
110 ret['changes'][name] = status
111 ret['comment'] = 'Jenkins artifactory server %s %s' % (
112 name, status.lower())
113 result = True
114 else:
115 status = 'FAILED'
116 logger.error(
117 "Jenkins artifactory API call failure: %s", call_result["msg"])
118 ret['comment'] = 'Jenkins artifactory API call failure: %s' % (call_result[
119 "msg"])
120 ret['result'] = None if test else result
121 return ret