summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/okd/plugins/modules/openshift_build.py
blob: b397e237c37b0ab24e81fc254839eea7f27e4bea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2021, Red Hat
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type

# STARTREMOVE (downstream)
DOCUMENTATION = r"""

module: openshift_build

short_description: Start a new build or Cancel running, pending, or new builds.

version_added: "2.3.0"

author:
  - Aubin Bikouo (@abikouo)

description:
  - This module starts a new build from the provided build config or build name.
  - This module also cancel a new, pending or running build by requesting a graceful shutdown of the build.
    There may be a delay between requesting the build and the time the build is terminated.
  - This can also restart a new build when the current is cancelled.
  - Analogous to C(oc cancel-build) and C(oc start-build).

extends_documentation_fragment:
  - kubernetes.core.k8s_auth_options

options:
  state:
    description:
      - Determines if a Build should be started ,cancelled or restarted.
      - When set to C(restarted) a new build will be created after the current build is cancelled.
    choices:
      - started
      - cancelled
      - restarted
    default: started
    type: str
  build_name:
    description:
    - Specify the name of a build which should be re-run.
    - Mutually exclusive with parameter I(build_config_name).
    type: str
  build_config_name:
    description:
    - Specify the name of a build config from which a new build will be run.
    - Mutually exclusive with parameter I(build_name).
    type: str
  namespace:
    description:
    - Specify the namespace for the build or the build config.
    type: str
    required: True
  build_args:
    description:
    - Specify a list of key-value pair to pass to Docker during the build.
    type: list
    elements: dict
    suboptions:
      name:
        description:
          - docker build argument name.
        type: str
        required: true
      value:
        description:
          - docker build argument value.
        type: str
        required: true
  commit:
    description:
    - Specify the source code commit identifier the build should use;
      requires a build based on a Git repository.
    type: str
  env_vars:
    description:
    - Specify a list of key-value pair for an environment variable to set for the build container.
    type: list
    elements: dict
    suboptions:
      name:
        description:
          - Environment variable name.
        type: str
        required: true
      value:
        description:
          - Environment variable value.
        type: str
        required: true
  incremental:
    description:
    - Overrides the incremental setting in a source-strategy build, ignored if not specified.
    type: bool
  no_cache:
    description:
    - Overrides the noCache setting in a docker-strategy build, ignored if not specified.
    type: bool
  wait:
    description:
    - When C(state=started), specify whether to wait for a build to complete
      and exit with a non-zero return code if the build fails.
    - When I(state=cancelled), specify whether to wait for a build phase to be Cancelled.
    default: False
    type: bool
  wait_sleep:
    description:
    - Number of seconds to sleep between checks.
    - Ignored if C(wait=false).
    default: 5
    type: int
  wait_timeout:
    description:
    - How long in seconds to wait for a build to complete.
    - Ignored if C(wait=false).
    default: 120
    type: int
  build_phases:
    description:
    - List of state for build to cancel.
    - Ignored when C(state=started).
    type: list
    elements: str
    choices:
      - New
      - Pending
      - Running
    default: []

requirements:
  - python >= 3.6
  - kubernetes >= 12.0.0
"""

EXAMPLES = r"""
# Starts build from build config default/hello-world
- name: Starts build from build config
  community.okd.openshift_build:
    namespace: default
    build_config_name: hello-world

# Starts build from a previous build "default/hello-world-1"
- name: Starts build from a previous build
  community.okd.openshift_build:
    namespace: default
    build_name: hello-world-1

# Cancel the build with the given name
- name: Cancel build from default namespace
  community.okd.openshift_build:
    namespace: "default"
    build_name: ruby-build-1
    state: cancelled

# Cancel the named build and create a new one with the same parameters
- name: Cancel build from default namespace and create a new one
  community.okd.openshift_build:
    namespace: "default"
    build_name: ruby-build-1
    state: restarted

# Cancel all builds created from 'ruby-build' build configuration that are in 'new' state
- name: Cancel build from default namespace and create a new one
  community.okd.openshift_build:
    namespace: "default"
    build_config_name: ruby-build
    build_phases:
      - New
    state: cancelled
"""

RETURN = r"""
builds:
  description:
  - The builds that were started/cancelled.
  returned: success
  type: complex
  contains:
    api_version:
      description: The versioned schema of this representation of an object.
      returned: success
      type: str
    kind:
      description: Represents the REST resource this object represents.
      returned: success
      type: str
    metadata:
      description: Standard object metadata. Includes name, namespace, annotations, labels, etc.
      returned: success
      type: dict
    spec:
      description: Specific attributes of the build.
      returned: success
      type: dict
    status:
      description: Current status details for the object.
      returned: success
      type: dict
"""
# ENDREMOVE (downstream)

import copy

from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (
    AUTH_ARG_SPEC,
)


def argument_spec():
    args = copy.deepcopy(AUTH_ARG_SPEC)

    args_options = dict(
        name=dict(type="str", required=True), value=dict(type="str", required=True)
    )

    args.update(
        dict(
            state=dict(
                type="str",
                choices=["started", "cancelled", "restarted"],
                default="started",
            ),
            build_args=dict(type="list", elements="dict", options=args_options),
            commit=dict(type="str"),
            env_vars=dict(type="list", elements="dict", options=args_options),
            build_name=dict(type="str"),
            build_config_name=dict(type="str"),
            namespace=dict(type="str", required=True),
            incremental=dict(type="bool"),
            no_cache=dict(type="bool"),
            wait=dict(type="bool", default=False),
            wait_sleep=dict(type="int", default=5),
            wait_timeout=dict(type="int", default=120),
            build_phases=dict(
                type="list",
                elements="str",
                default=[],
                choices=["New", "Pending", "Running"],
            ),
        )
    )
    return args


def main():
    mutually_exclusive = [
        ("build_name", "build_config_name"),
    ]
    from ansible_collections.community.okd.plugins.module_utils.openshift_builds import (
        OpenShiftBuilds,
    )

    module = OpenShiftBuilds(
        argument_spec=argument_spec(),
        mutually_exclusive=mutually_exclusive,
        required_one_of=[
            [
                "build_name",
                "build_config_name",
            ]
        ],
    )
    module.run_module()


if __name__ == "__main__":
    main()