blob: 7af839e155c4c6acf700c55e132af0c73447b80c [file] [log] [blame]
Jakub Josef6e0cda92017-02-14 18:01:58 +01001import logging
Adam Tengler70763e02017-08-21 16:50:32 +00002
Jakub Josef6e0cda92017-02-14 18:01:58 +01003logger = logging.getLogger(__name__)
4
5config_global_libs_groovy = """\
6import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever;
7import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration;
8import jenkins.plugins.git.GitSCMSource;
9
10def globalLibsDesc = Jenkins.getInstance().getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries")
Adam Tengler70763e02017-08-21 16:50:32 +000011def existingLib = globalLibsDesc.get().getLibraries().find{
Jakub Josef07678b32017-09-07 14:29:46 +020012 (!it.retriever.class.name.equals("org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever") ||
Adam Tengler70763e02017-08-21 16:50:32 +000013 it.retriever.scm.remote.equals("${url}") &&
14 it.retriever.scm.credentialsId.equals("${credential_id}")) &&
15 it.name.equals("${lib_name}") &&
16 it.defaultVersion.equals("${branch}") &&
Jakub Josef6e0cda92017-02-14 18:01:58 +010017 it.implicit == true
Adam Tengler70763e02017-08-21 16:50:32 +000018}
19if(existingLib){
Jakub Josef6e0cda92017-02-14 18:01:58 +010020 print("EXISTS")
Adam Tengler70763e02017-08-21 16:50:32 +000021}else{
Jakub Josef6e0cda92017-02-14 18:01:58 +010022 SCMSourceRetriever retriever = new SCMSourceRetriever(new GitSCMSource(
Adam Tengler70763e02017-08-21 16:50:32 +000023 "${lib_name}",
24 "${url}",
25 "${credential_id}",
Jakub Josef6e0cda92017-02-14 18:01:58 +010026 "*",
27 "",
28 false))
Adam Tengler70763e02017-08-21 16:50:32 +000029 LibraryConfiguration library = new LibraryConfiguration("${lib_name}", retriever)
30 library.setDefaultVersion("${branch}")
Jakub Josef07678b32017-09-07 14:29:46 +020031 library.setImplicit(${implicit})
Adam Tengler70763e02017-08-21 16:50:32 +000032 if(globalLibsDesc.get().getLibraries().isEmpty()){
Jakub Josef691fb372017-05-25 15:36:34 +020033 globalLibsDesc.get().setLibraries([library])
Adam Tengler70763e02017-08-21 16:50:32 +000034 }else{
35 globalLibsDesc.get().getLibraries().removeIf{ it.name.equals("${lib_name}")}
Jakub Josef691fb372017-05-25 15:36:34 +020036 globalLibsDesc.get().getLibraries().add(library)
Adam Tengler70763e02017-08-21 16:50:32 +000037 }
Jakub Josefb07ce1d2017-05-29 14:26:22 +020038 globalLibsDesc.save()
Jakub Josef6e0cda92017-02-14 18:01:58 +010039 print("SUCCESS")
Adam Tengler70763e02017-08-21 16:50:32 +000040}
41""" # noqa
Jakub Josef6e0cda92017-02-14 18:01:58 +010042
43remove_global_libs_groovy = """\
44def globalLibsDesc = Jenkins.getInstance().getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries")
Adam Tengler70763e02017-08-21 16:50:32 +000045def existingLib = globalLibsDesc.get().getLibraries().removeIf{it.name.equals("${lib_name}")}
46if(existingLib){
Jakub Josefb07ce1d2017-05-29 14:26:22 +020047 globalLibsDesc.save()
Jakub Josef6e0cda92017-02-14 18:01:58 +010048 print("DELETED")
Adam Tengler70763e02017-08-21 16:50:32 +000049}else{
Jakub Josef6e0cda92017-02-14 18:01:58 +010050 print("NOT PRESENT")
Adam Tengler70763e02017-08-21 16:50:32 +000051}
Jakub Josef6e0cda92017-02-14 18:01:58 +010052"""
53
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040054
55def __virtual__():
56 '''
57 Only load if jenkins_common module exist.
58 '''
59 if 'jenkins_common.call_groovy_script' not in __salt__:
60 return (
61 False,
62 'The jenkins_lib state module cannot be loaded: '
63 'jenkins_common not found')
64 return True
65
66
Adam Tengler70763e02017-08-21 16:50:32 +000067def present(name, url, branch="master",
68 credential_id="", implicit=True, **kwargs):
Jakub Josef6e0cda92017-02-14 18:01:58 +010069 """
70 Jenkins Global pipeline library present state method
71
72 :param name: pipeline library name
73 :param url: url to remote repo
74 :param branch: remote branch
75 :param credential_id: credential id for repo
76 :param implicit: implicit load boolean switch
77 :returns: salt-specified state dict
78 """
79 test = __opts__['test'] # noqa
80 ret = {
81 'name': name,
82 'changes': {},
83 'result': False,
84 'comment': '',
85 }
86 result = False
87 if test:
88 status = "SUCCESS"
89 ret['changes'][name] = status
Adam Tengler70763e02017-08-21 16:50:32 +000090 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (
91 name, status.lower())
Jakub Josef6e0cda92017-02-14 18:01:58 +010092 else:
93 call_result = __salt__['jenkins_common.call_groovy_script'](
Adam Tengler70763e02017-08-21 16:50:32 +000094 config_global_libs_groovy, {"lib_name": name,
95 "url": url,
96 "branch": branch,
97 "credential_id": credential_id if credential_id else "",
98 "implicit": "true" if implicit else "false"
99 })
100 if call_result["code"] == 200 and call_result["msg"] in [
101 "SUCCESS", "EXISTS"]:
Jakub Josef6e0cda92017-02-14 18:01:58 +0100102 status = call_result["msg"]
103 if status == "SUCCESS":
104 ret['changes'][name] = status
Adam Tengler70763e02017-08-21 16:50:32 +0000105 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (
106 name, status.lower())
Jakub Josef6e0cda92017-02-14 18:01:58 +0100107 result = True
108 else:
109 status = 'FAILED'
110 logger.error(
111 "Jenkins pipeline lib API call failure: %s", call_result["msg"])
112 ret['comment'] = 'Jenkins pipeline lib API call failure: %s' % (call_result[
Adam Tengler70763e02017-08-21 16:50:32 +0000113 "msg"])
Jakub Josef6e0cda92017-02-14 18:01:58 +0100114 ret['result'] = None if test else result
115 return ret
116
117
118def absent(name, **kwargs):
119 """
120 Jenkins Global pipeline library absent state method
121
122 :param name: pipeline library name
123 :returns: salt-specified state dict
124 """
125 test = __opts__['test'] # noqa
126 ret = {
127 'name': name,
128 'changes': {},
129 'result': False,
130 'comment': '',
131 }
132 result = False
133 if test:
134 status = "SUCCESS"
135 ret['changes'][name] = status
Adam Tengler70763e02017-08-21 16:50:32 +0000136 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (
137 name, status.lower())
Jakub Josef6e0cda92017-02-14 18:01:58 +0100138 else:
139 call_result = __salt__['jenkins_common.call_groovy_script'](
Adam Tengler70763e02017-08-21 16:50:32 +0000140 remove_global_libs_groovy, {"lib_name": name})
141 if call_result["code"] == 200 and call_result["msg"] in [
142 "DELETED", "NOT PRESENT"]:
Jakub Josef6e0cda92017-02-14 18:01:58 +0100143 status = call_result["msg"]
144 if status == "DELETED":
145 ret['changes'][name] = status
Adam Tengler70763e02017-08-21 16:50:32 +0000146 ret['comment'] = 'Jenkins pipeline lib config %s %s' % (
147 name, status.lower())
Jakub Josef6e0cda92017-02-14 18:01:58 +0100148 result = True
149 else:
150 status = 'FAILED'
151 logger.error(
152 "Jenkins pipeline lib API call failure: %s", call_result["msg"])
153 ret['comment'] = 'Jenkins pipeline lib API call failure: %s' % (call_result[
Adam Tengler70763e02017-08-21 16:50:32 +0000154 "msg"])
Jakub Josef6e0cda92017-02-14 18:01:58 +0100155 ret['result'] = None if test else result
156 return ret