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}) |
Jakub Josef | 691fb37 | 2017-05-25 15:36:34 +0200 | [diff] [blame] | 31 | 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 Josef | b07ce1d | 2017-05-29 14:26:22 +0200 | [diff] [blame^] | 37 | globalLibsDesc.save() |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 38 | print("SUCCESS") |
| 39 | }} |
| 40 | """ # noqa |
| 41 | |
| 42 | remove_global_libs_groovy = """\ |
| 43 | def globalLibsDesc = Jenkins.getInstance().getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries") |
| 44 | def existingLib = globalLibsDesc.get().getLibraries().removeIf{{it.name.equals("{lib_name}")}} |
| 45 | if(existingLib){{ |
Jakub Josef | b07ce1d | 2017-05-29 14:26:22 +0200 | [diff] [blame^] | 46 | globalLibsDesc.save() |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 47 | print("DELETED") |
| 48 | }}else{{ |
| 49 | print("NOT PRESENT") |
| 50 | }} |
| 51 | """ |
| 52 | |
| 53 | def 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 | |
| 100 | def 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 |