blob: 88f1a470a9cee91faf7aea353cbf01fb34273fe1 [file] [log] [blame]
Jakub Josef6e0cda92017-02-14 18:01:58 +01001import logging
2logger = logging.getLogger(__name__)
3
4config_global_libs_groovy = """\
5import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever;
6import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration;
7import jenkins.plugins.git.GitSCMSource;
8
9def globalLibsDesc = Jenkins.getInstance().getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries")
10def existingLib = globalLibsDesc.get().getLibraries().find{{
11 (!it.retriever.class.name.equals("org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever") ||
12 it.retriever.scm.remote.equals("{url}") &&
13 it.retriever.scm.credentialsId.equals("{credential_id}")) &&
14 it.name.equals("{lib_name}") &&
15 it.defaultVersion.equals("{branch}") &&
16 it.implicit == true
17}}
18if(existingLib){{
19 print("EXISTS")
20}}else{{
21 SCMSourceRetriever retriever = new SCMSourceRetriever(new GitSCMSource(
22 "{lib_name}",
23 "{url}",
24 "{credential_id}",
25 "*",
26 "",
27 false))
28 LibraryConfiguration library = new LibraryConfiguration("{lib_name}", retriever)
29 library.setDefaultVersion("{branch}")
30 library.setImplicit({implicit})
Jakub Josef691fb372017-05-25 15:36:34 +020031 if(globalLibsDesc.get().getLibraries().isEmpty()){{
32 globalLibsDesc.get().setLibraries([library])
33 }}else{{
34 globalLibsDesc.get().getLibraries().removeIf{{ it.name.equals("{lib_name}")}}
35 globalLibsDesc.get().getLibraries().add(library)
36 }}
Jakub Josefb07ce1d2017-05-29 14:26:22 +020037 globalLibsDesc.save()
Jakub Josef6e0cda92017-02-14 18:01:58 +010038 print("SUCCESS")
39}}
40""" # noqa
41
42remove_global_libs_groovy = """\
43def globalLibsDesc = Jenkins.getInstance().getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries")
44def existingLib = globalLibsDesc.get().getLibraries().removeIf{{it.name.equals("{lib_name}")}}
45if(existingLib){{
Jakub Josefb07ce1d2017-05-29 14:26:22 +020046 globalLibsDesc.save()
Jakub Josef6e0cda92017-02-14 18:01:58 +010047 print("DELETED")
48}}else{{
49 print("NOT PRESENT")
50}}
51"""
52
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040053
54def __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_lib state module cannot be loaded: '
62 'jenkins_common not found')
63 return True
64
65
Jakub Josef6e0cda92017-02-14 18:01:58 +010066def present(name, url, branch="master", credential_id="", implicit=True, **kwargs):
67 """
68 Jenkins Global pipeline library present state method
69
70 :param name: pipeline library name
71 :param url: url to remote repo
72 :param branch: remote branch
73 :param credential_id: credential id for repo
74 :param implicit: implicit load boolean switch
75 :returns: salt-specified state dict
76 """
77 test = __opts__['test'] # noqa
78 ret = {
79 'name': name,
80 'changes': {},
81 'result': False,
82 'comment': '',
83 }
84 result = False
85 if test:
86 status = "SUCCESS"
87 ret['changes'][name] = status
88 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
89 else:
90 call_result = __salt__['jenkins_common.call_groovy_script'](
91 config_global_libs_groovy, {"lib_name":name,
92 "url": url,
93 "branch": branch,
94 "credential_id": credential_id if credential_id else "",
95 "implicit": "true" if implicit else "false"
96 })
97 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
98 status = call_result["msg"]
99 if status == "SUCCESS":
100 ret['changes'][name] = status
101 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
102 result = True
103 else:
104 status = 'FAILED'
105 logger.error(
106 "Jenkins pipeline lib API call failure: %s", call_result["msg"])
107 ret['comment'] = 'Jenkins pipeline lib API call failure: %s' % (call_result[
108 "msg"])
109 ret['result'] = None if test else result
110 return ret
111
112
113def absent(name, **kwargs):
114 """
115 Jenkins Global pipeline library absent state method
116
117 :param name: pipeline library name
118 :returns: salt-specified state dict
119 """
120 test = __opts__['test'] # noqa
121 ret = {
122 'name': name,
123 'changes': {},
124 'result': False,
125 'comment': '',
126 }
127 result = False
128 if test:
129 status = "SUCCESS"
130 ret['changes'][name] = status
131 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
132 else:
133 call_result = __salt__['jenkins_common.call_groovy_script'](
134 remove_global_libs_groovy, {"lib_name":name})
135 if call_result["code"] == 200 and call_result["msg"] in ["DELETED", "NOT PRESENT"]:
136 status = call_result["msg"]
137 if status == "DELETED":
138 ret['changes'][name] = status
139 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
140 result = True
141 else:
142 status = 'FAILED'
143 logger.error(
144 "Jenkins pipeline lib API call failure: %s", call_result["msg"])
145 ret['comment'] = 'Jenkins pipeline lib API call failure: %s' % (call_result[
146 "msg"])
147 ret['result'] = None if test else result
148 return ret