| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: codebuild_project |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Create or delete an AWS CodeBuild project |
| 12 |
notes: |
| 13 |
- For details of the parameters and returns see U(http://boto3.readthedocs.io/en/latest/reference/services/codebuild.html). |
| 14 |
- I(tags) changed from boto3 format to standard dict format in release 6.0.0. |
| 15 |
description: |
| 16 |
- Create or delete a CodeBuild projects on AWS, used for building code artifacts from source code. |
| 17 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_codebuild). |
| 18 |
The usage did not change. |
| 19 |
author: |
| 20 |
- Stefan Horning (@stefanhorning) <[email protected]> |
| 21 |
options: |
| 22 |
name: |
| 23 |
description: |
| 24 |
- Name of the CodeBuild project. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
description: |
| 28 |
description: |
| 29 |
- Descriptive text of the CodeBuild project. |
| 30 |
type: str |
| 31 |
source: |
| 32 |
description: |
| 33 |
- Configure service and location for the build input source. |
| 34 |
- I(source) is required when creating a new project. |
| 35 |
suboptions: |
| 36 |
type: |
| 37 |
description: |
| 38 |
- "The type of the source. Allows one of these: C(CODECOMMIT), C(CODEPIPELINE), C(GITHUB), C(S3), C(BITBUCKET), C(GITHUB_ENTERPRISE)." |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
location: |
| 42 |
description: |
| 43 |
- Information about the location of the source code to be built. For I(type) C(CODEPIPELINE) location should not be specified. |
| 44 |
type: str |
| 45 |
git_clone_depth: |
| 46 |
description: |
| 47 |
- When using git you can specify the clone depth as an integer here. |
| 48 |
type: int |
| 49 |
buildspec: |
| 50 |
description: |
| 51 |
- The build spec declaration to use for the builds in this build project. Leave empty if part of the CodeBuild project. |
| 52 |
type: str |
| 53 |
insecure_ssl: |
| 54 |
description: |
| 55 |
- Enable this flag to ignore SSL warnings while connecting to the project source code. |
| 56 |
type: bool |
| 57 |
type: dict |
| 58 |
artifacts: |
| 59 |
description: |
| 60 |
- Information about the build output artifacts for the build project. |
| 61 |
- I(artifacts) is required when creating a new project. |
| 62 |
suboptions: |
| 63 |
type: |
| 64 |
description: |
| 65 |
- "The type of build output for artifacts. Can be one of the following: C(CODEPIPELINE), C(NO_ARTIFACTS), C(S3)." |
| 66 |
required: true |
| 67 |
location: |
| 68 |
description: |
| 69 |
- Information about the build output artifact location. When choosing I(type) C(S3), set the bucket name here. |
| 70 |
path: |
| 71 |
description: |
| 72 |
- Along with namespace_type and name, the pattern that AWS CodeBuild will use to name and store the output artifacts. |
| 73 |
- Used for path in S3 bucket when type is C(S3). |
| 74 |
namespace_type: |
| 75 |
description: |
| 76 |
- Along with path and name, the pattern that AWS CodeBuild will use to determine the name and location to store the output artifacts. |
| 77 |
- Accepts C(BUILD_ID) and C(NONE). |
| 78 |
- "See docs here: U(http://boto3.readthedocs.io/en/latest/reference/services/codebuild.html#CodeBuild.Client.create_project)." |
| 79 |
name: |
| 80 |
description: |
| 81 |
- Along with path and namespace_type, the pattern that AWS CodeBuild will use to name and store the output artifact. |
| 82 |
packaging: |
| 83 |
description: |
| 84 |
- The type of build output artifact to create on S3, can be NONE for creating a folder or ZIP for a ZIP file. |
| 85 |
type: dict |
| 86 |
cache: |
| 87 |
description: |
| 88 |
- Caching params to speed up following builds. |
| 89 |
suboptions: |
| 90 |
type: |
| 91 |
description: |
| 92 |
- Cache type. Can be C(NO_CACHE) or C(S3). |
| 93 |
required: true |
| 94 |
location: |
| 95 |
description: |
| 96 |
- Caching location on S3. |
| 97 |
required: true |
| 98 |
type: dict |
| 99 |
environment: |
| 100 |
description: |
| 101 |
- Information about the build environment for the build project. |
| 102 |
suboptions: |
| 103 |
type: |
| 104 |
description: |
| 105 |
- The type of build environment to use for the project. Usually C(LINUX_CONTAINER). |
| 106 |
required: true |
| 107 |
image: |
| 108 |
description: |
| 109 |
- The ID of the Docker image to use for this build project. |
| 110 |
required: true |
| 111 |
compute_type: |
| 112 |
description: |
| 113 |
- Information about the compute resources the build project will use. |
| 114 |
- "Available values include: C(BUILD_GENERAL1_SMALL), C(BUILD_GENERAL1_MEDIUM), C(BUILD_GENERAL1_LARGE)." |
| 115 |
required: true |
| 116 |
environment_variables: |
| 117 |
description: |
| 118 |
- A set of environment variables to make available to builds for the build project. List of dictionaries with name and value fields. |
| 119 |
- "Example: { name: 'MY_ENV_VARIABLE', value: 'test' }" |
| 120 |
privileged_mode: |
| 121 |
description: |
| 122 |
- Enables running the Docker daemon inside a Docker container. |
| 123 |
- Set to C(true) only if the build project is be used to build Docker images. |
| 124 |
type: dict |
| 125 |
service_role: |
| 126 |
description: |
| 127 |
- The ARN of the AWS IAM role that enables AWS CodeBuild to interact with dependent AWS services on behalf of the AWS account. |
| 128 |
type: str |
| 129 |
timeout_in_minutes: |
| 130 |
description: |
| 131 |
- How long CodeBuild should wait until timing out any build that has not been marked as completed. |
| 132 |
default: 60 |
| 133 |
type: int |
| 134 |
encryption_key: |
| 135 |
description: |
| 136 |
- The AWS Key Management Service (AWS KMS) customer master key (CMK) to be used for encrypting the build output artifacts. |
| 137 |
type: str |
| 138 |
vpc_config: |
| 139 |
description: |
| 140 |
- The VPC config enables AWS CodeBuild to access resources in an Amazon VPC. |
| 141 |
type: dict |
| 142 |
state: |
| 143 |
description: |
| 144 |
- Create or remove CodeBuild project. |
| 145 |
default: 'present' |
| 146 |
choices: ['present', 'absent'] |
| 147 |
type: str |
| 148 |
|
| 149 |
extends_documentation_fragment: |
| 150 |
- amazon.aws.boto3.modules |
| 151 |
- amazon.aws.common.modules |
| 152 |
- amazon.aws.region.modules |
| 153 |
- amazon.aws.tags.modules |
| 154 |
""" |
| 155 |
|
| 156 |
EXAMPLES = r""" |
| 157 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 158 |
|
| 159 |
- community.aws.codebuild_project: |
| 160 |
name: my_project |
| 161 |
description: My nice little project |
| 162 |
service_role: "arn:aws:iam::123123:role/service-role/code-build-service-role" |
| 163 |
source: |
| 164 |
# Possible values: BITBUCKET, CODECOMMIT, CODEPIPELINE, GITHUB, S3 |
| 165 |
type: CODEPIPELINE |
| 166 |
buildspec: '' |
| 167 |
artifacts: |
| 168 |
namespaceType: NONE |
| 169 |
packaging: NONE |
| 170 |
type: CODEPIPELINE |
| 171 |
name: my_project |
| 172 |
environment: |
| 173 |
computeType: BUILD_GENERAL1_SMALL |
| 174 |
privilegedMode: "true" |
| 175 |
image: "aws/codebuild/docker:17.09.0" |
| 176 |
type: LINUX_CONTAINER |
| 177 |
environmentVariables: |
| 178 |
- name: 'PROFILE' |
| 179 |
value: 'staging' |
| 180 |
encryption_key: "arn:aws:kms:us-east-1:123123:alias/aws/s3" |
| 181 |
region: us-east-1 |
| 182 |
state: present |
| 183 |
""" |
| 184 |
|
| 185 |
RETURN = r""" |
| 186 |
project: |
| 187 |
description: Returns the dictionary describing the code project configuration. |
| 188 |
returned: success |
| 189 |
type: complex |
| 190 |
contains: |
| 191 |
name: |
| 192 |
description: Name of the CodeBuild project. |
| 193 |
returned: always |
| 194 |
type: str |
| 195 |
sample: my_project |
| 196 |
arn: |
| 197 |
description: ARN of the CodeBuild project. |
| 198 |
returned: always |
| 199 |
type: str |
| 200 |
sample: arn:aws:codebuild:us-east-1:123123123:project/vod-api-app-builder |
| 201 |
description: |
| 202 |
description: A description of the CodeBuild project. |
| 203 |
returned: always |
| 204 |
type: str |
| 205 |
sample: My nice little project |
| 206 |
source: |
| 207 |
description: Information about the build input source code. |
| 208 |
returned: always |
| 209 |
type: complex |
| 210 |
contains: |
| 211 |
type: |
| 212 |
description: The type of the repository. |
| 213 |
returned: always |
| 214 |
type: str |
| 215 |
sample: CODEPIPELINE |
| 216 |
location: |
| 217 |
description: Location identifier, depending on the source type. |
| 218 |
returned: when configured |
| 219 |
type: str |
| 220 |
git_clone_depth: |
| 221 |
description: The git clone depth. |
| 222 |
returned: when configured |
| 223 |
type: int |
| 224 |
build_spec: |
| 225 |
description: The build spec declaration to use for the builds in this build project. |
| 226 |
returned: always |
| 227 |
type: str |
| 228 |
auth: |
| 229 |
description: Information about the authorization settings for AWS CodeBuild to access the source code to be built. |
| 230 |
returned: when configured |
| 231 |
type: complex |
| 232 |
insecure_ssl: |
| 233 |
description: True if set to ignore SSL warnings. |
| 234 |
returned: when configured |
| 235 |
type: bool |
| 236 |
artifacts: |
| 237 |
description: Information about the output of build artifacts |
| 238 |
returned: always |
| 239 |
type: complex |
| 240 |
contains: |
| 241 |
type: |
| 242 |
description: The type of build artifact. |
| 243 |
returned: always |
| 244 |
type: str |
| 245 |
sample: CODEPIPELINE |
| 246 |
location: |
| 247 |
description: Output location for build artifacts. |
| 248 |
returned: when configured |
| 249 |
type: str |
| 250 |
# and more... see http://boto3.readthedocs.io/en/latest/reference/services/codebuild.html#CodeBuild.Client.create_project |
| 251 |
cache: |
| 252 |
description: Cache settings for the build project. |
| 253 |
returned: when configured |
| 254 |
type: dict |
| 255 |
environment: |
| 256 |
description: Environment settings for the build. |
| 257 |
returned: always |
| 258 |
type: dict |
| 259 |
service_role: |
| 260 |
description: IAM role to be used during build to access other AWS services. |
| 261 |
returned: always |
| 262 |
type: str |
| 263 |
sample: arn:aws:iam::123123123:role/codebuild-service-role |
| 264 |
timeout_in_minutes: |
| 265 |
description: The timeout of a build in minutes. |
| 266 |
returned: always |
| 267 |
type: int |
| 268 |
sample: 60 |
| 269 |
tags: |
| 270 |
description: |
| 271 |
- Tags added to the project in the boto3 list of dictionaries format. |
| 272 |
- I(tags) and I(reource_tags) represent the same information in |
| 273 |
different formats. |
| 274 |
returned: when configured |
| 275 |
type: list |
| 276 |
reource_tags: |
| 277 |
description: |
| 278 |
- A simple dictionary representing the tags added to the project. |
| 279 |
- I(tags) and I(reource_tags) represent the same information in |
| 280 |
different formats. |
| 281 |
returned: when configured |
| 282 |
type: dict |
| 283 |
version_added: 4.0.0 |
| 284 |
created: |
| 285 |
description: Timestamp of the create time of the project. |
| 286 |
returned: always |
| 287 |
type: str |
| 288 |
sample: "2018-04-17T16:56:03.245000+02:00" |
| 289 |
""" |
| 290 |
|
| 291 |
try: |
| 292 |
import botocore |
| 293 |
except ImportError: |
| 294 |
pass # Handled by AnsibleAWSModule |
| 295 |
|
| 296 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 297 |
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict |
| 298 |
|
| 299 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import get_boto3_client_method_parameters |
| 300 |
from ansible_collections.amazon.aws.plugins.module_utils.exceptions import AnsibleAWSError |
| 301 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list |
| 302 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 303 |
|
| 304 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 305 |
|
| 306 |
|
| 307 |
class CodeBuildAnsibleAWSError(AnsibleAWSError): |
| 308 |
pass |
| 309 |
|
| 310 |
|
| 311 |
def do_create_project(client, params, formatted_params): |
| 312 |
if params["source"] is None or params["artifacts"] is None: |
| 313 |
raise CodeBuildAnsibleAWSError( |
| 314 |
message="The source and artifacts parameters must be provided when creating a new project. No existing project was found." |
| 315 |
) |
| 316 |
|
| 317 |
if params["tags"] is not None: |
| 318 |
formatted_params["tags"] = ansible_dict_to_boto3_tag_list( |
| 319 |
params["tags"], tag_name_key_name="key", tag_value_key_name="value" |
| 320 |
) |
| 321 |
|
| 322 |
permitted_create_params = get_boto3_client_method_parameters(client, "create_project") |
| 323 |
formatted_create_params = dict((k, v) for k, v in formatted_params.items() if k in permitted_create_params) |
| 324 |
|
| 325 |
# Or create new project: |
| 326 |
try: |
| 327 |
resp = client.create_project(**formatted_create_params) |
| 328 |
changed = True |
| 329 |
return resp, changed |
| 330 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 331 |
raise CodeBuildAnsibleAWSError( |
| 332 |
message="Unable to create CodeBuild project", |
| 333 |
exception=e, |
| 334 |
) |
| 335 |
|
| 336 |
|
| 337 |
def merge_tags(found_tags, tags, purge_tags): |
| 338 |
if purge_tags: |
| 339 |
return tags |
| 340 |
|
| 341 |
merged_tags = boto3_tag_list_to_ansible_dict(found_tags) |
| 342 |
merged_tags.update(tags) |
| 343 |
return merged_tags |
| 344 |
|
| 345 |
|
| 346 |
def format_tags(tags): |
| 347 |
return ansible_dict_to_boto3_tag_list( |
| 348 |
tags, |
| 349 |
tag_name_key_name="key", |
| 350 |
tag_value_key_name="value", |
| 351 |
) |
| 352 |
|
| 353 |
|
| 354 |
def do_update_project(client, params, formatted_params, found_project): |
| 355 |
permitted_update_params = get_boto3_client_method_parameters(client, "update_project") |
| 356 |
formatted_update_params = dict((k, v) for k, v in formatted_params.items() if k in permitted_update_params) |
| 357 |
|
| 358 |
found_tags = found_project.pop("tags", []) |
| 359 |
if params["tags"] is not None: |
| 360 |
formatted_update_params["tags"] = format_tags( |
| 361 |
merge_tags(found_tags, params["tags"], params["purge_tags"]), |
| 362 |
) |
| 363 |
|
| 364 |
resp = update_project(client=client, params=formatted_update_params) |
| 365 |
updated_project = resp["project"] |
| 366 |
|
| 367 |
# Prep both dicts for sensible change comparison: |
| 368 |
found_project.pop("lastModified") |
| 369 |
updated_project.pop("lastModified") |
| 370 |
updated_tags = updated_project.pop("tags", []) |
| 371 |
found_project["ResourceTags"] = boto3_tag_list_to_ansible_dict(found_tags) |
| 372 |
updated_project["ResourceTags"] = boto3_tag_list_to_ansible_dict(updated_tags) |
| 373 |
|
| 374 |
changed = updated_project != found_project |
| 375 |
|
| 376 |
updated_project["tags"] = updated_tags |
| 377 |
return resp, changed |
| 378 |
|
| 379 |
|
| 380 |
def create_or_update_project(client, params): |
| 381 |
name = params["name"] |
| 382 |
# clean up params |
| 383 |
formatted_params = snake_dict_to_camel_dict(dict((k, v) for k, v in params.items() if v is not None)) |
| 384 |
|
| 385 |
# Check if project with that name already exists and if so update existing: |
| 386 |
found = describe_project(client=client, name=name) |
| 387 |
|
| 388 |
if "name" not in found: |
| 389 |
return do_create_project(client, params, formatted_params) |
| 390 |
|
| 391 |
return do_update_project(client, params, formatted_params, found) |
| 392 |
|
| 393 |
|
| 394 |
def update_project(client, params): |
| 395 |
try: |
| 396 |
resp = client.update_project(**params) |
| 397 |
return resp |
| 398 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 399 |
raise CodeBuildAnsibleAWSError( |
| 400 |
message="Unable to update CodeBuild project", |
| 401 |
exception=e, |
| 402 |
) |
| 403 |
|
| 404 |
|
| 405 |
def delete_project(client, name): |
| 406 |
found = describe_project(client=client, name=name) |
| 407 |
if "name" not in found: |
| 408 |
return {}, False |
| 409 |
|
| 410 |
try: |
| 411 |
resp = client.delete_project(name=name) |
| 412 |
return resp, True |
| 413 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 414 |
raise CodeBuildAnsibleAWSError( |
| 415 |
message="Unable to update CodeBuild project", |
| 416 |
exception=e, |
| 417 |
) |
| 418 |
|
| 419 |
|
| 420 |
def describe_project(client, name): |
| 421 |
project = {} |
| 422 |
try: |
| 423 |
projects = client.batch_get_projects(names=[name])["projects"] |
| 424 |
if len(projects) > 0: |
| 425 |
project = projects[0] |
| 426 |
return project |
| 427 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 428 |
raise CodeBuildAnsibleAWSError( |
| 429 |
message="Unable to describe CodeBuild projects", |
| 430 |
exception=e, |
| 431 |
) |
| 432 |
|
| 433 |
|
| 434 |
def format_project_result(project_result): |
| 435 |
formated_result = camel_dict_to_snake_dict(project_result) |
| 436 |
project = project_result.get("project", {}) |
| 437 |
if project: |
| 438 |
tags = project.get("tags", []) |
| 439 |
formated_result["project"]["resource_tags"] = boto3_tag_list_to_ansible_dict(tags) |
| 440 |
formated_result["ORIGINAL"] = project_result |
| 441 |
return formated_result |
| 442 |
|
| 443 |
|
| 444 |
def main(): |
| 445 |
argument_spec = dict( |
| 446 |
name=dict(required=True), |
| 447 |
description=dict(), |
| 448 |
source=dict(type="dict"), |
| 449 |
artifacts=dict(type="dict"), |
| 450 |
cache=dict(type="dict"), |
| 451 |
environment=dict(type="dict"), |
| 452 |
service_role=dict(), |
| 453 |
timeout_in_minutes=dict(type="int", default=60), |
| 454 |
encryption_key=dict(no_log=False), |
| 455 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 456 |
purge_tags=dict(type="bool", default=True), |
| 457 |
vpc_config=dict(type="dict"), |
| 458 |
state=dict(choices=["present", "absent"], default="present"), |
| 459 |
) |
| 460 |
|
| 461 |
module = AnsibleAWSModule(argument_spec=argument_spec) |
| 462 |
client_conn = module.client("codebuild") |
| 463 |
|
| 464 |
state = module.params.get("state") |
| 465 |
changed = False |
| 466 |
|
| 467 |
try: |
| 468 |
if state == "present": |
| 469 |
project_result, changed = create_or_update_project( |
| 470 |
client=client_conn, |
| 471 |
params=module.params, |
| 472 |
) |
| 473 |
elif state == "absent": |
| 474 |
project_result, changed = delete_project( |
| 475 |
client=client_conn, |
| 476 |
name=module.params["name"], |
| 477 |
) |
| 478 |
except CodeBuildAnsibleAWSError as e: |
| 479 |
if e.exception: |
| 480 |
module.fail_json_aws(e.exception, msg=e.message) |
| 481 |
module.fail_json(msg=e.message) |
| 482 |
|
| 483 |
formatted_result = format_project_result(project_result) |
| 484 |
module.exit_json(changed=changed, **formatted_result) |
| 485 |
|
| 486 |
|
| 487 |
if __name__ == "__main__": |
| 488 |
main() |