Module and states code refactor
Change-Id: Ia1c468d0b55603b6274ca0d275b2720602e5319e
diff --git a/_states/jenkins_lib.py b/_states/jenkins_lib.py
index 88f1a47..46970f5 100644
--- a/_states/jenkins_lib.py
+++ b/_states/jenkins_lib.py
@@ -1,4 +1,5 @@
import logging
+
logger = logging.getLogger(__name__)
config_global_libs_groovy = """\
@@ -7,47 +8,47 @@
import jenkins.plugins.git.GitSCMSource;
def globalLibsDesc = Jenkins.getInstance().getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries")
-def existingLib = globalLibsDesc.get().getLibraries().find{{
+def existingLib = globalLibsDesc.get().getLibraries().find{
(!it.retriever.class.name.equals("org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever") ||
- it.retriever.scm.remote.equals("{url}") &&
- it.retriever.scm.credentialsId.equals("{credential_id}")) &&
- it.name.equals("{lib_name}") &&
- it.defaultVersion.equals("{branch}") &&
+ it.retriever.scm.remote.equals("${url}") &&
+ it.retriever.scm.credentialsId.equals("${credential_id}")) &&
+ it.name.equals("${lib_name}") &&
+ it.defaultVersion.equals("${branch}") &&
it.implicit == true
-}}
-if(existingLib){{
+}
+if(existingLib){
print("EXISTS")
-}}else{{
+}else{
SCMSourceRetriever retriever = new SCMSourceRetriever(new GitSCMSource(
- "{lib_name}",
- "{url}",
- "{credential_id}",
+ "${lib_name}",
+ "${url}",
+ "${credential_id}",
"*",
"",
false))
- LibraryConfiguration library = new LibraryConfiguration("{lib_name}", retriever)
- library.setDefaultVersion("{branch}")
+ LibraryConfiguration library = new LibraryConfiguration("${lib_name}", retriever)
+ library.setDefaultVersion("${branch}")
library.setImplicit({implicit})
- if(globalLibsDesc.get().getLibraries().isEmpty()){{
+ if(globalLibsDesc.get().getLibraries().isEmpty()){
globalLibsDesc.get().setLibraries([library])
- }}else{{
- globalLibsDesc.get().getLibraries().removeIf{{ it.name.equals("{lib_name}")}}
+ }else{
+ globalLibsDesc.get().getLibraries().removeIf{ it.name.equals("${lib_name}")}
globalLibsDesc.get().getLibraries().add(library)
- }}
+ }
globalLibsDesc.save()
print("SUCCESS")
-}}
-""" # noqa
+}
+""" # noqa
remove_global_libs_groovy = """\
def globalLibsDesc = Jenkins.getInstance().getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries")
-def existingLib = globalLibsDesc.get().getLibraries().removeIf{{it.name.equals("{lib_name}")}}
-if(existingLib){{
+def existingLib = globalLibsDesc.get().getLibraries().removeIf{it.name.equals("${lib_name}")}
+if(existingLib){
globalLibsDesc.save()
print("DELETED")
-}}else{{
+}else{
print("NOT PRESENT")
-}}
+}
"""
@@ -63,7 +64,8 @@
return True
-def present(name, url, branch="master", credential_id="", implicit=True, **kwargs):
+def present(name, url, branch="master",
+ credential_id="", implicit=True, **kwargs):
"""
Jenkins Global pipeline library present state method
@@ -85,27 +87,30 @@
if test:
status = "SUCCESS"
ret['changes'][name] = status
- ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
+ ret['comment'] = 'Jenkins pipeline lib config %s %s' % (
+ name, status.lower())
else:
call_result = __salt__['jenkins_common.call_groovy_script'](
- config_global_libs_groovy, {"lib_name":name,
- "url": url,
- "branch": branch,
- "credential_id": credential_id if credential_id else "",
- "implicit": "true" if implicit else "false"
- })
- if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
+ config_global_libs_groovy, {"lib_name": name,
+ "url": url,
+ "branch": branch,
+ "credential_id": credential_id if credential_id else "",
+ "implicit": "true" if implicit else "false"
+ })
+ if call_result["code"] == 200 and call_result["msg"] in [
+ "SUCCESS", "EXISTS"]:
status = call_result["msg"]
if status == "SUCCESS":
ret['changes'][name] = status
- ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
+ ret['comment'] = 'Jenkins pipeline lib config %s %s' % (
+ name, status.lower())
result = True
else:
status = 'FAILED'
logger.error(
"Jenkins pipeline lib API call failure: %s", call_result["msg"])
ret['comment'] = 'Jenkins pipeline lib API call failure: %s' % (call_result[
- "msg"])
+ "msg"])
ret['result'] = None if test else result
return ret
@@ -128,21 +133,24 @@
if test:
status = "SUCCESS"
ret['changes'][name] = status
- ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
+ ret['comment'] = 'Jenkins pipeline lib config %s %s' % (
+ name, status.lower())
else:
call_result = __salt__['jenkins_common.call_groovy_script'](
- remove_global_libs_groovy, {"lib_name":name})
- if call_result["code"] == 200 and call_result["msg"] in ["DELETED", "NOT PRESENT"]:
+ remove_global_libs_groovy, {"lib_name": name})
+ if call_result["code"] == 200 and call_result["msg"] in [
+ "DELETED", "NOT PRESENT"]:
status = call_result["msg"]
if status == "DELETED":
ret['changes'][name] = status
- ret['comment'] = 'Jenkins pipeline lib config %s %s' % (name, status.lower())
+ ret['comment'] = 'Jenkins pipeline lib config %s %s' % (
+ name, status.lower())
result = True
else:
status = 'FAILED'
logger.error(
"Jenkins pipeline lib API call failure: %s", call_result["msg"])
ret['comment'] = 'Jenkins pipeline lib API call failure: %s' % (call_result[
- "msg"])
+ "msg"])
ret['result'] = None if test else result
return ret