| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2021, Red Hat |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
# STARTREMOVE (downstream) |
| 12 |
DOCUMENTATION = r""" |
| 13 |
|
| 14 |
module: openshift_build |
| 15 |
|
| 16 |
short_description: Start a new build or Cancel running, pending, or new builds. |
| 17 |
|
| 18 |
version_added: "2.3.0" |
| 19 |
|
| 20 |
author: |
| 21 |
- Aubin Bikouo (@abikouo) |
| 22 |
|
| 23 |
description: |
| 24 |
- This module starts a new build from the provided build config or build name. |
| 25 |
- This module also cancel a new, pending or running build by requesting a graceful shutdown of the build. |
| 26 |
There may be a delay between requesting the build and the time the build is terminated. |
| 27 |
- This can also restart a new build when the current is cancelled. |
| 28 |
- Analogous to C(oc cancel-build) and C(oc start-build). |
| 29 |
|
| 30 |
extends_documentation_fragment: |
| 31 |
- kubernetes.core.k8s_auth_options |
| 32 |
|
| 33 |
options: |
| 34 |
state: |
| 35 |
description: |
| 36 |
- Determines if a Build should be started ,cancelled or restarted. |
| 37 |
- When set to C(restarted) a new build will be created after the current build is cancelled. |
| 38 |
choices: |
| 39 |
- started |
| 40 |
- cancelled |
| 41 |
- restarted |
| 42 |
default: started |
| 43 |
type: str |
| 44 |
build_name: |
| 45 |
description: |
| 46 |
- Specify the name of a build which should be re-run. |
| 47 |
- Mutually exclusive with parameter I(build_config_name). |
| 48 |
type: str |
| 49 |
build_config_name: |
| 50 |
description: |
| 51 |
- Specify the name of a build config from which a new build will be run. |
| 52 |
- Mutually exclusive with parameter I(build_name). |
| 53 |
type: str |
| 54 |
namespace: |
| 55 |
description: |
| 56 |
- Specify the namespace for the build or the build config. |
| 57 |
type: str |
| 58 |
required: True |
| 59 |
build_args: |
| 60 |
description: |
| 61 |
- Specify a list of key-value pair to pass to Docker during the build. |
| 62 |
type: list |
| 63 |
elements: dict |
| 64 |
suboptions: |
| 65 |
name: |
| 66 |
description: |
| 67 |
- docker build argument name. |
| 68 |
type: str |
| 69 |
required: true |
| 70 |
value: |
| 71 |
description: |
| 72 |
- docker build argument value. |
| 73 |
type: str |
| 74 |
required: true |
| 75 |
commit: |
| 76 |
description: |
| 77 |
- Specify the source code commit identifier the build should use; |
| 78 |
requires a build based on a Git repository. |
| 79 |
type: str |
| 80 |
env_vars: |
| 81 |
description: |
| 82 |
- Specify a list of key-value pair for an environment variable to set for the build container. |
| 83 |
type: list |
| 84 |
elements: dict |
| 85 |
suboptions: |
| 86 |
name: |
| 87 |
description: |
| 88 |
- Environment variable name. |
| 89 |
type: str |
| 90 |
required: true |
| 91 |
value: |
| 92 |
description: |
| 93 |
- Environment variable value. |
| 94 |
type: str |
| 95 |
required: true |
| 96 |
incremental: |
| 97 |
description: |
| 98 |
- Overrides the incremental setting in a source-strategy build, ignored if not specified. |
| 99 |
type: bool |
| 100 |
no_cache: |
| 101 |
description: |
| 102 |
- Overrides the noCache setting in a docker-strategy build, ignored if not specified. |
| 103 |
type: bool |
| 104 |
wait: |
| 105 |
description: |
| 106 |
- When C(state=started), specify whether to wait for a build to complete |
| 107 |
and exit with a non-zero return code if the build fails. |
| 108 |
- When I(state=cancelled), specify whether to wait for a build phase to be Cancelled. |
| 109 |
default: False |
| 110 |
type: bool |
| 111 |
wait_sleep: |
| 112 |
description: |
| 113 |
- Number of seconds to sleep between checks. |
| 114 |
- Ignored if C(wait=false). |
| 115 |
default: 5 |
| 116 |
type: int |
| 117 |
wait_timeout: |
| 118 |
description: |
| 119 |
- How long in seconds to wait for a build to complete. |
| 120 |
- Ignored if C(wait=false). |
| 121 |
default: 120 |
| 122 |
type: int |
| 123 |
build_phases: |
| 124 |
description: |
| 125 |
- List of state for build to cancel. |
| 126 |
- Ignored when C(state=started). |
| 127 |
type: list |
| 128 |
elements: str |
| 129 |
choices: |
| 130 |
- New |
| 131 |
- Pending |
| 132 |
- Running |
| 133 |
default: [] |
| 134 |
|
| 135 |
requirements: |
| 136 |
- python >= 3.6 |
| 137 |
- kubernetes >= 12.0.0 |
| 138 |
""" |
| 139 |
|
| 140 |
EXAMPLES = r""" |
| 141 |
# Starts build from build config default/hello-world |
| 142 |
- name: Starts build from build config |
| 143 |
community.okd.openshift_build: |
| 144 |
namespace: default |
| 145 |
build_config_name: hello-world |
| 146 |
|
| 147 |
# Starts build from a previous build "default/hello-world-1" |
| 148 |
- name: Starts build from a previous build |
| 149 |
community.okd.openshift_build: |
| 150 |
namespace: default |
| 151 |
build_name: hello-world-1 |
| 152 |
|
| 153 |
# Cancel the build with the given name |
| 154 |
- name: Cancel build from default namespace |
| 155 |
community.okd.openshift_build: |
| 156 |
namespace: "default" |
| 157 |
build_name: ruby-build-1 |
| 158 |
state: cancelled |
| 159 |
|
| 160 |
# Cancel the named build and create a new one with the same parameters |
| 161 |
- name: Cancel build from default namespace and create a new one |
| 162 |
community.okd.openshift_build: |
| 163 |
namespace: "default" |
| 164 |
build_name: ruby-build-1 |
| 165 |
state: restarted |
| 166 |
|
| 167 |
# Cancel all builds created from 'ruby-build' build configuration that are in 'new' state |
| 168 |
- name: Cancel build from default namespace and create a new one |
| 169 |
community.okd.openshift_build: |
| 170 |
namespace: "default" |
| 171 |
build_config_name: ruby-build |
| 172 |
build_phases: |
| 173 |
- New |
| 174 |
state: cancelled |
| 175 |
""" |
| 176 |
|
| 177 |
RETURN = r""" |
| 178 |
builds: |
| 179 |
description: |
| 180 |
- The builds that were started/cancelled. |
| 181 |
returned: success |
| 182 |
type: complex |
| 183 |
contains: |
| 184 |
api_version: |
| 185 |
description: The versioned schema of this representation of an object. |
| 186 |
returned: success |
| 187 |
type: str |
| 188 |
kind: |
| 189 |
description: Represents the REST resource this object represents. |
| 190 |
returned: success |
| 191 |
type: str |
| 192 |
metadata: |
| 193 |
description: Standard object metadata. Includes name, namespace, annotations, labels, etc. |
| 194 |
returned: success |
| 195 |
type: dict |
| 196 |
spec: |
| 197 |
description: Specific attributes of the build. |
| 198 |
returned: success |
| 199 |
type: dict |
| 200 |
status: |
| 201 |
description: Current status details for the object. |
| 202 |
returned: success |
| 203 |
type: dict |
| 204 |
""" |
| 205 |
# ENDREMOVE (downstream) |
| 206 |
|
| 207 |
import copy |
| 208 |
|
| 209 |
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( |
| 210 |
AUTH_ARG_SPEC, |
| 211 |
) |
| 212 |
|
| 213 |
|
| 214 |
def argument_spec(): |
| 215 |
args = copy.deepcopy(AUTH_ARG_SPEC) |
| 216 |
|
| 217 |
args_options = dict( |
| 218 |
name=dict(type="str", required=True), value=dict(type="str", required=True) |
| 219 |
) |
| 220 |
|
| 221 |
args.update( |
| 222 |
dict( |
| 223 |
state=dict( |
| 224 |
type="str", |
| 225 |
choices=["started", "cancelled", "restarted"], |
| 226 |
default="started", |
| 227 |
), |
| 228 |
build_args=dict(type="list", elements="dict", options=args_options), |
| 229 |
commit=dict(type="str"), |
| 230 |
env_vars=dict(type="list", elements="dict", options=args_options), |
| 231 |
build_name=dict(type="str"), |
| 232 |
build_config_name=dict(type="str"), |
| 233 |
namespace=dict(type="str", required=True), |
| 234 |
incremental=dict(type="bool"), |
| 235 |
no_cache=dict(type="bool"), |
| 236 |
wait=dict(type="bool", default=False), |
| 237 |
wait_sleep=dict(type="int", default=5), |
| 238 |
wait_timeout=dict(type="int", default=120), |
| 239 |
build_phases=dict( |
| 240 |
type="list", |
| 241 |
elements="str", |
| 242 |
default=[], |
| 243 |
choices=["New", "Pending", "Running"], |
| 244 |
), |
| 245 |
) |
| 246 |
) |
| 247 |
return args |
| 248 |
|
| 249 |
|
| 250 |
def main(): |
| 251 |
mutually_exclusive = [ |
| 252 |
("build_name", "build_config_name"), |
| 253 |
] |
| 254 |
from ansible_collections.community.okd.plugins.module_utils.openshift_builds import ( |
| 255 |
OpenShiftBuilds, |
| 256 |
) |
| 257 |
|
| 258 |
module = OpenShiftBuilds( |
| 259 |
argument_spec=argument_spec(), |
| 260 |
mutually_exclusive=mutually_exclusive, |
| 261 |
required_one_of=[ |
| 262 |
[ |
| 263 |
"build_name", |
| 264 |
"build_config_name", |
| 265 |
] |
| 266 |
], |
| 267 |
) |
| 268 |
module.run_module() |
| 269 |
|
| 270 |
|
| 271 |
if __name__ == "__main__": |
| 272 |
main() |