blob: daccc2e53bd7ab459f60846239b6d6ad243d1802 [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")
Jakub Josef9d9b0ed2017-07-26 16:12:57 +020011// empty artifactory servers is not empty list but null, but find can be called on null
Adam Tengler70763e02017-08-21 16:50:32 +000012def server = desc.getArtifactoryServers().find{it -> it.name.equals("${name}")}
Jakub Josef01940252017-05-10 14:01:22 +020013if(server &&
Adam Tengler70763e02017-08-21 16:50:32 +000014 server.getName().equals("${name}") &&
15 server.getUrl().equals("${serverUrl}") &&
16 (server.getDeployerCredentialsConfig() == null || server.getDeployerCredentialsConfig().getCredentialsId().equals("${credentialsId}")) &&
17 (server.getResolverCredentialsConfig() == null || server.getResolverCredentialsConfig().getCredentialsId().equals("${credentialsId}"))){
Jakub Josef01940252017-05-10 14:01:22 +020018 print("EXISTS")
Adam Tengler70763e02017-08-21 16:50:32 +000019}else{
Jakub Josef9d9b0ed2017-07-26 16:12:57 +020020 // we must care about null here
Adam Tengler70763e02017-08-21 16:50:32 +000021 if(desc.getArtifactoryServers() != null && !desc.getArtifactoryServers().isEmpty()){
22 desc.getArtifactoryServers().removeIf{it -> it.name.equals("${name}")}
23 }else{
Jakub Josef9d9b0ed2017-07-26 16:12:57 +020024 desc.setArtifactoryServers([])
Adam Tengler70763e02017-08-21 16:50:32 +000025 }
Jakub Josef01940252017-05-10 14:01:22 +020026 def newServer = new ArtifactoryServer(
Adam Tengler70763e02017-08-21 16:50:32 +000027 "${name}",
28 "${serverUrl}",
29 new CredentialsConfig("", "", "${credentialsId}"),
30 new CredentialsConfig("", "", "${credentialsId}"),
Jakub Josef01940252017-05-10 14:01:22 +020031 300,
32 false,
33 null)
34 desc.getArtifactoryServers().add(newServer)
35 desc.save()
36 print("ADDED/CHANGED")
Adam Tengler70763e02017-08-21 16:50:32 +000037}
Jakub Josef01940252017-05-10 14:01:22 +020038""" # noqa
39
40delete_artifactory_groovy = u"""\
41def inst = Jenkins.getInstance()
42def desc = inst.getDescriptor("org.jfrog.hudson.ArtifactoryBuilder")
Adam Tengler70763e02017-08-21 16:50:32 +000043if(desc.getArtifactoryServers().removeIf{it -> it.name.equals("${name}")}){
Jakub Josef01940252017-05-10 14:01:22 +020044 print("REMOVED")
Adam Tengler70763e02017-08-21 16:50:32 +000045}else{
Jakub Josef01940252017-05-10 14:01:22 +020046 print("NOT PRESENT")
Adam Tengler70763e02017-08-21 16:50:32 +000047}
Jakub Josef01940252017-05-10 14:01:22 +020048""" # noqa
49
50
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040051def __virtual__():
52 '''
53 Only load if jenkins_common module exist.
54 '''
55 if 'jenkins_common.call_groovy_script' not in __salt__:
56 return (
57 False,
58 'The jenkins_artifactory state module cannot be loaded: '
59 'jenkins_common not found')
60 return True
61
62
Jakub Josef01940252017-05-10 14:01:22 +020063def present(name, url, credential_id, **kwargs):
64 """
65 Jenkins artifactory present state method
66
67 :param name: artifactory server name
68 :param url: artifactory server url
69 :param credential_id: artifactory server credential id
70 :returns: salt-specified state dict
71 """
Adam Tengler70763e02017-08-21 16:50:32 +000072 return _plugin_call(name, url, credential_id, add_artifactory_groovy, [
73 "ADDED/CHANGED", "EXISTS"], **kwargs)
Jakub Josef01940252017-05-10 14:01:22 +020074
75
76def absent(name, **kwargs):
77 """
78 Jenkins artifactory present state method
79
80 :param name: artifactory server name
81 :returns: salt-specified state dict
82 """
Adam Tengler70763e02017-08-21 16:50:32 +000083 return _plugin_call(name, None, None, delete_artifactory_groovy, [
84 "REMOVED", "NOT PRESENT"], **kwargs)
Jakub Josef01940252017-05-10 14:01:22 +020085
86
87def _plugin_call(name, url, credentialsId, template, success_msgs, **kwargs):
88 test = __opts__['test'] # noqa
89 ret = {
90 'name': name,
91 'changes': {},
92 'result': False,
93 'comment': '',
94 }
95 result = False
96 if test:
97 status = success_msgs[0]
98 ret['changes'][name] = status
99 ret['comment'] = 'Jenkins artifactory server %s %s' % (
100 name, status.lower())
101 else:
102 call_result = __salt__['jenkins_common.call_groovy_script'](
103 template, {"name": name, "serverUrl": url, "credentialsId": credentialsId})
104 if call_result["code"] == 200 and call_result["msg"] in success_msgs:
105 status = call_result["msg"]
106 if status == success_msgs[0]:
107 ret['changes'][name] = status
108 ret['comment'] = 'Jenkins artifactory server %s %s' % (
109 name, status.lower())
110 result = True
111 else:
112 status = 'FAILED'
113 logger.error(
114 "Jenkins artifactory API call failure: %s", call_result["msg"])
115 ret['comment'] = 'Jenkins artifactory API call failure: %s' % (call_result[
116 "msg"])
117 ret['result'] = None if test else result
118 return ret