| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: jenkins_build |
| 11 |
short_description: Manage jenkins builds |
| 12 |
version_added: 2.2.0 |
| 13 |
description: |
| 14 |
- Manage Jenkins builds with Jenkins REST API. |
| 15 |
requirements: |
| 16 |
- "python-jenkins >= 0.4.12" |
| 17 |
author: |
| 18 |
- Brett Milford (@brettmilford) |
| 19 |
- Tong He (@unnecessary-username) |
| 20 |
- Juan Casanova (@juanmcasanova) |
| 21 |
extends_documentation_fragment: |
| 22 |
- community.general._attributes |
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: none |
| 26 |
diff_mode: |
| 27 |
support: none |
| 28 |
options: |
| 29 |
args: |
| 30 |
description: |
| 31 |
- A list of parameters to pass to the build. |
| 32 |
type: dict |
| 33 |
name: |
| 34 |
description: |
| 35 |
- Name of the Jenkins job to build. |
| 36 |
required: true |
| 37 |
type: str |
| 38 |
build_number: |
| 39 |
description: |
| 40 |
- An integer which specifies a build of a job. Is required to remove a build from the queue. |
| 41 |
type: int |
| 42 |
password: |
| 43 |
description: |
| 44 |
- Password to authenticate with the Jenkins server. |
| 45 |
type: str |
| 46 |
state: |
| 47 |
description: |
| 48 |
- Attribute that specifies if the build is to be created, deleted or stopped. |
| 49 |
- The V(stopped) state has been added in community.general 3.3.0. |
| 50 |
default: present |
| 51 |
choices: ['present', 'absent', 'stopped'] |
| 52 |
type: str |
| 53 |
token: |
| 54 |
description: |
| 55 |
- API token used to authenticate with the Jenkins server. |
| 56 |
type: str |
| 57 |
url: |
| 58 |
description: |
| 59 |
- URL of the Jenkins server. |
| 60 |
default: http://localhost:8080 |
| 61 |
type: str |
| 62 |
user: |
| 63 |
description: |
| 64 |
- User to authenticate with the Jenkins server. |
| 65 |
type: str |
| 66 |
detach: |
| 67 |
description: |
| 68 |
- Enable detached mode to not wait for the build end. |
| 69 |
default: false |
| 70 |
type: bool |
| 71 |
version_added: 7.4.0 |
| 72 |
time_between_checks: |
| 73 |
description: |
| 74 |
- Time in seconds to wait between requests to the Jenkins server. |
| 75 |
- This times must be higher than the configured quiet time for the job. |
| 76 |
default: 10 |
| 77 |
type: int |
| 78 |
version_added: 7.4.0 |
| 79 |
""" |
| 80 |
|
| 81 |
EXAMPLES = r""" |
| 82 |
- name: Create a jenkins build using basic authentication |
| 83 |
community.general.jenkins_build: |
| 84 |
name: "test-check" |
| 85 |
args: |
| 86 |
cloud: "test" |
| 87 |
availability_zone: "test_az" |
| 88 |
state: present |
| 89 |
user: admin |
| 90 |
password: asdfg |
| 91 |
url: http://localhost:8080 |
| 92 |
|
| 93 |
- name: Stop a running jenkins build anonymously |
| 94 |
community.general.jenkins_build: |
| 95 |
name: "stop-check" |
| 96 |
build_number: 3 |
| 97 |
state: stopped |
| 98 |
url: http://localhost:8080 |
| 99 |
|
| 100 |
- name: Trigger Jenkins build in detached mode |
| 101 |
community.general.jenkins_build: |
| 102 |
name: "detached-build" |
| 103 |
state: present |
| 104 |
user: admin |
| 105 |
token: abcdefghijklmnopqrstuvwxyz123456 |
| 106 |
url: http://localhost:8080 |
| 107 |
detach: true |
| 108 |
time_between_checks: 20 |
| 109 |
|
| 110 |
- name: Delete a jenkins build using token authentication |
| 111 |
community.general.jenkins_build: |
| 112 |
name: "delete-experiment" |
| 113 |
build_number: 30 |
| 114 |
state: absent |
| 115 |
user: Jenkins |
| 116 |
token: abcdefghijklmnopqrstuvwxyz123456 |
| 117 |
url: http://localhost:8080 |
| 118 |
""" |
| 119 |
|
| 120 |
RETURN = r""" |
| 121 |
name: |
| 122 |
description: Name of the jenkins job. |
| 123 |
returned: success |
| 124 |
type: str |
| 125 |
sample: "test-job" |
| 126 |
state: |
| 127 |
description: State of the jenkins job. |
| 128 |
returned: success |
| 129 |
type: str |
| 130 |
sample: present |
| 131 |
user: |
| 132 |
description: User used for authentication. |
| 133 |
returned: success |
| 134 |
type: str |
| 135 |
sample: admin |
| 136 |
url: |
| 137 |
description: URL to connect to the Jenkins server. |
| 138 |
returned: success |
| 139 |
type: str |
| 140 |
sample: https://jenkins.mydomain.com |
| 141 |
build_info: |
| 142 |
description: Build info of the jenkins job. |
| 143 |
returned: success |
| 144 |
type: dict |
| 145 |
""" |
| 146 |
|
| 147 |
import traceback |
| 148 |
from time import sleep |
| 149 |
|
| 150 |
JENKINS_IMP_ERR = None |
| 151 |
try: |
| 152 |
import jenkins |
| 153 |
|
| 154 |
python_jenkins_installed = True |
| 155 |
except ImportError: |
| 156 |
JENKINS_IMP_ERR = traceback.format_exc() |
| 157 |
python_jenkins_installed = False |
| 158 |
|
| 159 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 160 |
|
| 161 |
|
| 162 |
class JenkinsBuild: |
| 163 |
def __init__(self, module): |
| 164 |
self.module = module |
| 165 |
|
| 166 |
self.name = module.params.get("name") |
| 167 |
self.password = module.params.get("password") |
| 168 |
self.args = module.params.get("args") |
| 169 |
self.state = module.params.get("state") |
| 170 |
self.token = module.params.get("token") |
| 171 |
self.user = module.params.get("user") |
| 172 |
self.jenkins_url = module.params.get("url") |
| 173 |
self.build_number = module.params.get("build_number") |
| 174 |
self.detach = module.params.get("detach") |
| 175 |
self.time_between_checks = module.params.get("time_between_checks") |
| 176 |
self.server = self.get_jenkins_connection() |
| 177 |
|
| 178 |
self.result = { |
| 179 |
"changed": False, |
| 180 |
"url": self.jenkins_url, |
| 181 |
"name": self.name, |
| 182 |
"user": self.user, |
| 183 |
"state": self.state, |
| 184 |
} |
| 185 |
|
| 186 |
self.EXCL_STATE = "excluded state" |
| 187 |
|
| 188 |
def get_jenkins_connection(self): |
| 189 |
try: |
| 190 |
if self.user and self.password: |
| 191 |
return jenkins.Jenkins(self.jenkins_url, self.user, self.password) |
| 192 |
elif self.user and self.token: |
| 193 |
return jenkins.Jenkins(self.jenkins_url, self.user, self.token) |
| 194 |
elif self.user and not (self.password or self.token): |
| 195 |
return jenkins.Jenkins(self.jenkins_url, self.user) |
| 196 |
else: |
| 197 |
return jenkins.Jenkins(self.jenkins_url) |
| 198 |
except Exception as e: |
| 199 |
self.module.fail_json(msg=f"Unable to connect to Jenkins server, {e}") |
| 200 |
|
| 201 |
def get_next_build(self): |
| 202 |
try: |
| 203 |
build_number = self.server.get_job_info(self.name)["nextBuildNumber"] |
| 204 |
except Exception as e: |
| 205 |
self.module.fail_json( |
| 206 |
msg=f"Unable to get job info from Jenkins server, {e}", exception=traceback.format_exc() |
| 207 |
) |
| 208 |
|
| 209 |
return build_number |
| 210 |
|
| 211 |
def get_build_status(self): |
| 212 |
try: |
| 213 |
response = self.server.get_build_info(self.name, self.build_number) |
| 214 |
return response |
| 215 |
except jenkins.JenkinsException: |
| 216 |
response = {} |
| 217 |
response["result"] = "ABSENT" |
| 218 |
return response |
| 219 |
except Exception as e: |
| 220 |
self.module.fail_json(msg=f"Unable to fetch build information, {e}", exception=traceback.format_exc()) |
| 221 |
|
| 222 |
def present_build(self): |
| 223 |
self.build_number = self.get_next_build() |
| 224 |
|
| 225 |
try: |
| 226 |
if self.args is None: |
| 227 |
self.server.build_job(self.name) |
| 228 |
else: |
| 229 |
self.server.build_job(self.name, self.args) |
| 230 |
except Exception as e: |
| 231 |
self.module.fail_json( |
| 232 |
msg=f"Unable to create build for {self.jenkins_url}: {e}", exception=traceback.format_exc() |
| 233 |
) |
| 234 |
|
| 235 |
def stopped_build(self): |
| 236 |
build_info = None |
| 237 |
try: |
| 238 |
build_info = self.server.get_build_info(self.name, self.build_number) |
| 239 |
if build_info["building"] is True: |
| 240 |
self.server.stop_build(self.name, self.build_number) |
| 241 |
except Exception as e: |
| 242 |
self.module.fail_json( |
| 243 |
msg=f"Unable to stop build for {self.jenkins_url}: {e}", exception=traceback.format_exc() |
| 244 |
) |
| 245 |
else: |
| 246 |
if build_info["building"] is False: |
| 247 |
self.module.exit_json(**self.result) |
| 248 |
|
| 249 |
def absent_build(self): |
| 250 |
try: |
| 251 |
self.server.delete_build(self.name, self.build_number) |
| 252 |
except Exception as e: |
| 253 |
self.module.fail_json( |
| 254 |
msg=f"Unable to delete build for {self.jenkins_url}: {e}", exception=traceback.format_exc() |
| 255 |
) |
| 256 |
|
| 257 |
def get_result(self): |
| 258 |
result = self.result |
| 259 |
build_status = self.get_build_status() |
| 260 |
|
| 261 |
if build_status["result"] is None: |
| 262 |
# If detached mode is active mark as success, we wouldn't be able to get here if it didn't exist |
| 263 |
if self.detach: |
| 264 |
result["changed"] = True |
| 265 |
result["build_info"] = build_status |
| 266 |
|
| 267 |
return result |
| 268 |
|
| 269 |
sleep(self.time_between_checks) |
| 270 |
self.get_result() |
| 271 |
else: |
| 272 |
if self.state == "stopped" and build_status["result"] == "ABORTED": |
| 273 |
result["changed"] = True |
| 274 |
result["build_info"] = build_status |
| 275 |
elif self.state == "absent" and build_status["result"] == "ABSENT": |
| 276 |
result["changed"] = True |
| 277 |
result["build_info"] = build_status |
| 278 |
elif self.state != "absent" and build_status["result"] == "SUCCESS": |
| 279 |
result["changed"] = True |
| 280 |
result["build_info"] = build_status |
| 281 |
else: |
| 282 |
result["failed"] = True |
| 283 |
result["build_info"] = build_status |
| 284 |
|
| 285 |
return result |
| 286 |
|
| 287 |
|
| 288 |
def test_dependencies(module): |
| 289 |
if not python_jenkins_installed: |
| 290 |
module.fail_json( |
| 291 |
msg=missing_required_lib( |
| 292 |
"python-jenkins", url="https://python-jenkins.readthedocs.io/en/latest/install.html" |
| 293 |
), |
| 294 |
exception=JENKINS_IMP_ERR, |
| 295 |
) |
| 296 |
|
| 297 |
|
| 298 |
def main(): |
| 299 |
module = AnsibleModule( |
| 300 |
argument_spec=dict( |
| 301 |
args=dict(type="dict"), |
| 302 |
build_number=dict(type="int"), |
| 303 |
name=dict(required=True), |
| 304 |
password=dict(no_log=True), |
| 305 |
state=dict(choices=["present", "absent", "stopped"], default="present"), |
| 306 |
token=dict(no_log=True), |
| 307 |
url=dict(default="http://localhost:8080"), |
| 308 |
user=dict(), |
| 309 |
detach=dict(type="bool", default=False), |
| 310 |
time_between_checks=dict(type="int", default=10), |
| 311 |
), |
| 312 |
mutually_exclusive=[["password", "token"]], |
| 313 |
required_if=[["state", "absent", ["build_number"], True], ["state", "stopped", ["build_number"], True]], |
| 314 |
) |
| 315 |
|
| 316 |
test_dependencies(module) |
| 317 |
jenkins_build = JenkinsBuild(module) |
| 318 |
|
| 319 |
if module.params.get("state") == "present": |
| 320 |
jenkins_build.present_build() |
| 321 |
elif module.params.get("state") == "stopped": |
| 322 |
jenkins_build.stopped_build() |
| 323 |
else: |
| 324 |
jenkins_build.absent_build() |
| 325 |
|
| 326 |
sleep(jenkins_build.time_between_checks) |
| 327 |
result = jenkins_build.get_result() |
| 328 |
module.exit_json(**result) |
| 329 |
|
| 330 |
|
| 331 |
if __name__ == "__main__": |
| 332 |
main() |