Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 1 | import logging |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 2 | |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 3 | logger = logging.getLogger(__name__) |
| 4 | |
| 5 | config_global_libs_groovy = """\ |
| 6 | import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever; |
| 7 | import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration; |
| 8 | import jenkins.plugins.git.GitSCMSource; |
| 9 | |
| 10 | def globalLibsDesc = Jenkins.getInstance().getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 11 | def existingLib = globalLibsDesc.get().getLibraries().find{ |
Jakub Josef | 07678b3 | 2017-09-07 14:29:46 +0200 | [diff] [blame] | 12 | (!it.retriever.class.name.equals("org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever") || |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 13 | 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 Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 17 | it.implicit == true |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 18 | } |
| 19 | if(existingLib){ |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 20 | print("EXISTS") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 21 | }else{ |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 22 | SCMSourceRetriever retriever = new SCMSourceRetriever(new GitSCMSource( |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 23 | "${lib_name}", |
| 24 | "${url}", |
| 25 | "${credential_id}", |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 26 | "*", |
| 27 | "", |
| 28 | false)) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 29 | LibraryConfiguration library = new LibraryConfiguration("${lib_name}", retriever) |
| 30 | library.setDefaultVersion("${branch}") |
Jakub Josef | 07678b3 | 2017-09-07 14:29:46 +0200 | [diff] [blame] | 31 | library.setImplicit(${implicit}) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 32 | if(globalLibsDesc.get().getLibraries().isEmpty()){ |
Jakub Josef | 691fb37 | 2017-05-25 15:36:34 +0200 | [diff] [blame] | 33 | globalLibsDesc.get().setLibraries([library]) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 34 | }else{ |
| 35 | globalLibsDesc.get().getLibraries().removeIf{ it.name.equals("${lib_name}")} |
Jakub Josef | 691fb37 | 2017-05-25 15:36:34 +0200 | [diff] [blame] | 36 | globalLibsDesc.get().getLibraries().add(library) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 37 | } |
Jakub Josef | b07ce1d | 2017-05-29 14:26:22 +0200 | [diff] [blame] | 38 | globalLibsDesc.save() |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 39 | print("SUCCESS") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 40 | } |
| 41 | """ # noqa |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 42 | |
| 43 | remove_global_libs_groovy = """\ |
| 44 | def globalLibsDesc = Jenkins.getInstance().getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 45 | def existingLib = globalLibsDesc.get().getLibraries().removeIf{it.name.equals("${lib_name}")} |
| 46 | if(existingLib){ |
Jakub Josef | b07ce1d | 2017-05-29 14:26:22 +0200 | [diff] [blame] | 47 | globalLibsDesc.save() |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 48 | print("DELETED") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 49 | }else{ |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 50 | print("NOT PRESENT") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 51 | } |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 52 | """ |
| 53 | |
Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 54 | |
| 55 | def __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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 67 | def present(name, url, branch="master", |
| 68 | credential_id="", implicit=True, **kwargs): |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 69 | """ |
| 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 90 | ret['comment'] = 'Jenkins pipeline lib config %s %s' % ( |
| 91 | name, status.lower()) |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 92 | else: |
| 93 | call_result = __salt__['jenkins_common.call_groovy_script']( |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 94 | 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 Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 102 | status = call_result["msg"] |
| 103 | if status == "SUCCESS": |
| 104 | ret['changes'][name] = status |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 105 | ret['comment'] = 'Jenkins pipeline lib config %s %s' % ( |
| 106 | name, status.lower()) |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 107 | 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 113 | "msg"]) |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 114 | ret['result'] = None if test else result |
| 115 | return ret |
| 116 | |
| 117 | |
| 118 | def 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 136 | ret['comment'] = 'Jenkins pipeline lib config %s %s' % ( |
| 137 | name, status.lower()) |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 138 | else: |
| 139 | call_result = __salt__['jenkins_common.call_groovy_script']( |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 140 | remove_global_libs_groovy, {"lib_name": name}) |
| 141 | if call_result["code"] == 200 and call_result["msg"] in [ |
| 142 | "DELETED", "NOT PRESENT"]: |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 143 | status = call_result["msg"] |
| 144 | if status == "DELETED": |
| 145 | ret['changes'][name] = status |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 146 | ret['comment'] = 'Jenkins pipeline lib config %s %s' % ( |
| 147 | name, status.lower()) |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 148 | 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 154 | "msg"]) |
Jakub Josef | 6e0cda9 | 2017-02-14 18:01:58 +0100 | [diff] [blame] | 155 | ret['result'] = None if test else result |
| 156 | return ret |