blob: 64926231e54c0f4a22b968b1af94be10a2c23797 [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 Josef73bf9952017-02-15 12:26:24 +010031 globalLibsDesc.get().getLibraries().removeIf{{ it.name.equals("{lib_name}")}}
Jakub Josef6e0cda92017-02-14 18:01:58 +010032 globalLibsDesc.get().getLibraries().add(library)
33 print("SUCCESS")
34}}
35""" # noqa
36
37remove_global_libs_groovy = """\
38def globalLibsDesc = Jenkins.getInstance().getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries")
39def existingLib = globalLibsDesc.get().getLibraries().removeIf{{it.name.equals("{lib_name}")}}
40if(existingLib){{
41 print("DELETED")
42}}else{{
43 print("NOT PRESENT")
44}}
45"""
46
47def present(name, url, branch="master", credential_id="", implicit=True, **kwargs):
48 """
49 Jenkins Global pipeline library present state method
50
51 :param name: pipeline library name
52 :param url: url to remote repo
53 :param branch: remote branch
54 :param credential_id: credential id for repo
55 :param implicit: implicit load boolean switch
56 :returns: salt-specified state dict
57 """
58 test = __opts__['test'] # noqa
59 ret = {
60 'name': name,
61 'changes': {},
62 'result': False,
63 'comment': '',
64 }
65 result = False
66 if test:
67 status = "SUCCESS"
68 ret['changes'][name] = status
69 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
70 else:
71 call_result = __salt__['jenkins_common.call_groovy_script'](
72 config_global_libs_groovy, {"lib_name":name,
73 "url": url,
74 "branch": branch,
75 "credential_id": credential_id if credential_id else "",
76 "implicit": "true" if implicit else "false"
77 })
78 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
79 status = call_result["msg"]
80 if status == "SUCCESS":
81 ret['changes'][name] = status
82 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
83 result = True
84 else:
85 status = 'FAILED'
86 logger.error(
87 "Jenkins pipeline lib API call failure: %s", call_result["msg"])
88 ret['comment'] = 'Jenkins pipeline lib API call failure: %s' % (call_result[
89 "msg"])
90 ret['result'] = None if test else result
91 return ret
92
93
94def absent(name, **kwargs):
95 """
96 Jenkins Global pipeline library absent state method
97
98 :param name: pipeline library name
99 :returns: salt-specified state dict
100 """
101 test = __opts__['test'] # noqa
102 ret = {
103 'name': name,
104 'changes': {},
105 'result': False,
106 'comment': '',
107 }
108 result = False
109 if test:
110 status = "SUCCESS"
111 ret['changes'][name] = status
112 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
113 else:
114 call_result = __salt__['jenkins_common.call_groovy_script'](
115 remove_global_libs_groovy, {"lib_name":name})
116 if call_result["code"] == 200 and call_result["msg"] in ["DELETED", "NOT PRESENT"]:
117 status = call_result["msg"]
118 if status == "DELETED":
119 ret['changes'][name] = status
120 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
121 result = True
122 else:
123 status = 'FAILED'
124 logger.error(
125 "Jenkins pipeline lib API call failure: %s", call_result["msg"])
126 ret['comment'] = 'Jenkins pipeline lib API call failure: %s' % (call_result[
127 "msg"])
128 ret['result'] = None if test else result
129 return ret