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