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