blob: d4dc5be6ef2aa7f4afcb8ccb5f8234594dcba737 [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
53def present(name, url, branch="master", credential_id="", implicit=True, **kwargs):
54 """
55 Jenkins Global pipeline library present state method
56
57 :param name: pipeline library name
58 :param url: url to remote repo
59 :param branch: remote branch
60 :param credential_id: credential id for repo
61 :param implicit: implicit load boolean switch
62 :returns: salt-specified state dict
63 """
64 test = __opts__['test'] # noqa
65 ret = {
66 'name': name,
67 'changes': {},
68 'result': False,
69 'comment': '',
70 }
71 result = False
72 if test:
73 status = "SUCCESS"
74 ret['changes'][name] = status
75 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
76 else:
77 call_result = __salt__['jenkins_common.call_groovy_script'](
78 config_global_libs_groovy, {"lib_name":name,
79 "url": url,
80 "branch": branch,
81 "credential_id": credential_id if credential_id else "",
82 "implicit": "true" if implicit else "false"
83 })
84 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
85 status = call_result["msg"]
86 if status == "SUCCESS":
87 ret['changes'][name] = status
88 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
89 result = True
90 else:
91 status = 'FAILED'
92 logger.error(
93 "Jenkins pipeline lib API call failure: %s", call_result["msg"])
94 ret['comment'] = 'Jenkins pipeline lib API call failure: %s' % (call_result[
95 "msg"])
96 ret['result'] = None if test else result
97 return ret
98
99
100def absent(name, **kwargs):
101 """
102 Jenkins Global pipeline library absent state method
103
104 :param name: pipeline library name
105 :returns: salt-specified state dict
106 """
107 test = __opts__['test'] # noqa
108 ret = {
109 'name': name,
110 'changes': {},
111 'result': False,
112 'comment': '',
113 }
114 result = False
115 if test:
116 status = "SUCCESS"
117 ret['changes'][name] = status
118 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
119 else:
120 call_result = __salt__['jenkins_common.call_groovy_script'](
121 remove_global_libs_groovy, {"lib_name":name})
122 if call_result["code"] == 200 and call_result["msg"] in ["DELETED", "NOT PRESENT"]:
123 status = call_result["msg"]
124 if status == "DELETED":
125 ret['changes'][name] = status
126 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
127 result = True
128 else:
129 status = 'FAILED'
130 logger.error(
131 "Jenkins pipeline lib API call failure: %s", call_result["msg"])
132 ret['comment'] = 'Jenkins pipeline lib API call failure: %s' % (call_result[
133 "msg"])
134 ret['result'] = None if test else result
135 return ret