| 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: codepipeline |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Create or delete AWS CodePipelines |
| 12 |
notes: |
| 13 |
- For details of the parameters and returns see U(http://boto3.readthedocs.io/en/latest/reference/services/codepipeline.html). |
| 14 |
description: |
| 15 |
- Create or delete a CodePipeline on AWS. |
| 16 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_codepipeline). |
| 17 |
The usage did not change. |
| 18 |
author: |
| 19 |
- Stefan Horning (@stefanhorning) <[email protected]> |
| 20 |
options: |
| 21 |
name: |
| 22 |
description: |
| 23 |
- Name of the CodePipeline. |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
role_arn: |
| 27 |
description: |
| 28 |
- ARN of the IAM role to use when executing the CodePipeline. |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
artifact_store: |
| 32 |
description: |
| 33 |
- Location information where artifacts are stored (on S3). Dictionary with fields type and location. |
| 34 |
required: true |
| 35 |
suboptions: |
| 36 |
type: |
| 37 |
description: |
| 38 |
- Type of the artifacts storage (only 'S3' is currently supported). |
| 39 |
type: str |
| 40 |
location: |
| 41 |
description: |
| 42 |
- Bucket name for artifacts. |
| 43 |
type: str |
| 44 |
type: dict |
| 45 |
stages: |
| 46 |
description: |
| 47 |
- List of stages to perform in the CodePipeline. List of dictionaries containing name and actions for each stage. |
| 48 |
required: true |
| 49 |
suboptions: |
| 50 |
name: |
| 51 |
description: |
| 52 |
- Name of the stage (step) in the CodePipeline. |
| 53 |
type: str |
| 54 |
actions: |
| 55 |
description: |
| 56 |
- List of action configurations for that stage. |
| 57 |
- 'See the boto3 documentation for full documentation of suboptions:' |
| 58 |
- 'U(https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/codepipeline.html#CodePipeline.Client.create_pipeline)' |
| 59 |
type: list |
| 60 |
elements: dict |
| 61 |
elements: dict |
| 62 |
type: list |
| 63 |
version: |
| 64 |
description: |
| 65 |
- Version number of the CodePipeline. This number is automatically incremented when a CodePipeline is updated. |
| 66 |
required: false |
| 67 |
type: int |
| 68 |
state: |
| 69 |
description: |
| 70 |
- Create or remove CodePipeline. |
| 71 |
default: 'present' |
| 72 |
choices: ['present', 'absent'] |
| 73 |
type: str |
| 74 |
extends_documentation_fragment: |
| 75 |
- amazon.aws.common.modules |
| 76 |
- amazon.aws.region.modules |
| 77 |
- amazon.aws.boto3 |
| 78 |
""" |
| 79 |
|
| 80 |
EXAMPLES = r""" |
| 81 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 82 |
|
| 83 |
# Example for creating a pipeline for continuous deploy of Github code to an ECS cluster (container) |
| 84 |
- community.aws.codepipeline: |
| 85 |
name: my_deploy_pipeline |
| 86 |
role_arn: arn:aws:iam::123456:role/AWS-CodePipeline-Service |
| 87 |
artifact_store: |
| 88 |
type: S3 |
| 89 |
location: my_s3_codepipline_bucket |
| 90 |
stages: |
| 91 |
- name: Get_source |
| 92 |
actions: |
| 93 |
- |
| 94 |
name: Git_pull |
| 95 |
actionTypeId: |
| 96 |
category: Source |
| 97 |
owner: ThirdParty |
| 98 |
provider: GitHub |
| 99 |
version: '1' |
| 100 |
outputArtifacts: |
| 101 |
- { name: my-app-source } |
| 102 |
configuration: |
| 103 |
Owner: mediapeers |
| 104 |
Repo: my_gh_repo |
| 105 |
PollForSourceChanges: 'true' |
| 106 |
Branch: master |
| 107 |
# Generate token like this: |
| 108 |
# https://docs.aws.amazon.com/codepipeline/latest/userguide/GitHub-rotate-personal-token-CLI.html |
| 109 |
# GH Link: https://github.com/settings/tokens |
| 110 |
OAuthToken: 'abc123def456' |
| 111 |
runOrder: 1 |
| 112 |
- name: Build |
| 113 |
actions: |
| 114 |
- |
| 115 |
name: CodeBuild |
| 116 |
actionTypeId: |
| 117 |
category: Build |
| 118 |
owner: AWS |
| 119 |
provider: CodeBuild |
| 120 |
version: '1' |
| 121 |
inputArtifacts: |
| 122 |
- { name: my-app-source } |
| 123 |
outputArtifacts: |
| 124 |
- { name: my-app-build } |
| 125 |
configuration: |
| 126 |
# A project with that name needs to be setup on AWS CodeBuild already (use code_build module). |
| 127 |
ProjectName: codebuild-project-name |
| 128 |
runOrder: 1 |
| 129 |
- name: ECS_deploy |
| 130 |
actions: |
| 131 |
- |
| 132 |
name: ECS_deploy |
| 133 |
actionTypeId: |
| 134 |
category: Deploy |
| 135 |
owner: AWS |
| 136 |
provider: ECS |
| 137 |
version: '1' |
| 138 |
inputArtifacts: |
| 139 |
- { name: vod-api-app-build } |
| 140 |
configuration: |
| 141 |
# an ECS cluster with that name needs to be setup on AWS ECS already (use ecs_cluster and ecs_service module) |
| 142 |
ClusterName: ecs-cluster-name |
| 143 |
ServiceName: ecs-cluster-service-name |
| 144 |
FileName: imagedefinitions.json |
| 145 |
region: us-east-1 |
| 146 |
state: present |
| 147 |
""" |
| 148 |
|
| 149 |
RETURN = r""" |
| 150 |
pipeline: |
| 151 |
description: Returns the dictionary describing the CodePipeline configuration. |
| 152 |
returned: success |
| 153 |
type: complex |
| 154 |
contains: |
| 155 |
name: |
| 156 |
description: Name of the CodePipeline |
| 157 |
returned: always |
| 158 |
type: str |
| 159 |
sample: my_deploy_pipeline |
| 160 |
role_arn: |
| 161 |
description: ARN of the IAM role attached to the CodePipeline |
| 162 |
returned: always |
| 163 |
type: str |
| 164 |
sample: arn:aws:iam::123123123:role/codepipeline-service-role |
| 165 |
artifact_store: |
| 166 |
description: Information about where the build artifacts are stored |
| 167 |
returned: always |
| 168 |
type: complex |
| 169 |
contains: |
| 170 |
type: |
| 171 |
description: The type of the artifacts store, such as S3 |
| 172 |
returned: always |
| 173 |
type: str |
| 174 |
sample: S3 |
| 175 |
location: |
| 176 |
description: The location of the artifacts storage (s3 bucket name) |
| 177 |
returned: always |
| 178 |
type: str |
| 179 |
sample: my_s3_codepipline_bucket |
| 180 |
encryption_key: |
| 181 |
description: The encryption key used to encrypt the artifacts store, such as an AWS KMS key. |
| 182 |
returned: when configured |
| 183 |
type: str |
| 184 |
stages: |
| 185 |
description: List of stages configured for this CodePipeline |
| 186 |
returned: always |
| 187 |
type: list |
| 188 |
version: |
| 189 |
description: |
| 190 |
- The version number of the CodePipeline. |
| 191 |
- This number is auto incremented when CodePipeline params are changed. |
| 192 |
returned: always |
| 193 |
type: int |
| 194 |
""" |
| 195 |
|
| 196 |
import copy |
| 197 |
|
| 198 |
try: |
| 199 |
import botocore |
| 200 |
except ImportError: |
| 201 |
pass # caught by AnsibleAWSModule |
| 202 |
|
| 203 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 204 |
|
| 205 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 206 |
from ansible_collections.amazon.aws.plugins.module_utils.policy import compare_policies |
| 207 |
|
| 208 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 209 |
|
| 210 |
|
| 211 |
def create_pipeline(client, name, role_arn, artifact_store, stages, version, module): |
| 212 |
pipeline_dict = {"name": name, "roleArn": role_arn, "artifactStore": artifact_store, "stages": stages} |
| 213 |
if version: |
| 214 |
pipeline_dict["version"] = version |
| 215 |
try: |
| 216 |
resp = client.create_pipeline(pipeline=pipeline_dict) |
| 217 |
return resp |
| 218 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 219 |
module.fail_json_aws(e, msg=f"Unable create pipeline {pipeline_dict['name']}") |
| 220 |
|
| 221 |
|
| 222 |
def update_pipeline(client, pipeline_dict, module): |
| 223 |
try: |
| 224 |
resp = client.update_pipeline(pipeline=pipeline_dict) |
| 225 |
return resp |
| 226 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 227 |
module.fail_json_aws(e, msg=f"Unable update pipeline {pipeline_dict['name']}") |
| 228 |
|
| 229 |
|
| 230 |
def delete_pipeline(client, name, module): |
| 231 |
try: |
| 232 |
resp = client.delete_pipeline(name=name) |
| 233 |
return resp |
| 234 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 235 |
module.fail_json_aws(e, msg=f"Unable delete pipeline {name}") |
| 236 |
|
| 237 |
|
| 238 |
def describe_pipeline(client, name, version, module): |
| 239 |
pipeline = {} |
| 240 |
try: |
| 241 |
if version is not None: |
| 242 |
pipeline = client.get_pipeline(name=name, version=version) |
| 243 |
return pipeline |
| 244 |
else: |
| 245 |
pipeline = client.get_pipeline(name=name) |
| 246 |
return pipeline |
| 247 |
except is_boto3_error_code("PipelineNotFoundException"): |
| 248 |
return pipeline |
| 249 |
except ( |
| 250 |
botocore.exceptions.BotoCoreError, |
| 251 |
botocore.exceptions.ClientError, |
| 252 |
) as e: # pylint: disable=duplicate-except |
| 253 |
module.fail_json_aws(e) |
| 254 |
|
| 255 |
|
| 256 |
def main(): |
| 257 |
argument_spec = dict( |
| 258 |
name=dict(required=True, type="str"), |
| 259 |
role_arn=dict(required=True, type="str"), |
| 260 |
artifact_store=dict(required=True, type="dict"), |
| 261 |
stages=dict(required=True, type="list", elements="dict"), |
| 262 |
version=dict(type="int"), |
| 263 |
state=dict(choices=["present", "absent"], default="present"), |
| 264 |
) |
| 265 |
|
| 266 |
module = AnsibleAWSModule(argument_spec=argument_spec) |
| 267 |
client_conn = module.client("codepipeline") |
| 268 |
|
| 269 |
state = module.params.get("state") |
| 270 |
changed = False |
| 271 |
|
| 272 |
# Determine if the CodePipeline exists |
| 273 |
found_code_pipeline = describe_pipeline( |
| 274 |
client=client_conn, name=module.params["name"], version=module.params["version"], module=module |
| 275 |
) |
| 276 |
pipeline_result = {} |
| 277 |
|
| 278 |
if state == "present": |
| 279 |
if "pipeline" in found_code_pipeline: |
| 280 |
pipeline_dict = copy.deepcopy(found_code_pipeline["pipeline"]) |
| 281 |
# Update dictionary with provided module params: |
| 282 |
pipeline_dict["roleArn"] = module.params["role_arn"] |
| 283 |
pipeline_dict["artifactStore"] = module.params["artifact_store"] |
| 284 |
pipeline_dict["stages"] = module.params["stages"] |
| 285 |
if module.params["version"] is not None: |
| 286 |
pipeline_dict["version"] = module.params["version"] |
| 287 |
|
| 288 |
pipeline_result = update_pipeline(client=client_conn, pipeline_dict=pipeline_dict, module=module) |
| 289 |
|
| 290 |
if compare_policies(found_code_pipeline["pipeline"], pipeline_result["pipeline"]): |
| 291 |
changed = True |
| 292 |
else: |
| 293 |
pipeline_result = create_pipeline( |
| 294 |
client=client_conn, |
| 295 |
name=module.params["name"], |
| 296 |
role_arn=module.params["role_arn"], |
| 297 |
artifact_store=module.params["artifact_store"], |
| 298 |
stages=module.params["stages"], |
| 299 |
version=module.params["version"], |
| 300 |
module=module, |
| 301 |
) |
| 302 |
changed = True |
| 303 |
elif state == "absent": |
| 304 |
if found_code_pipeline: |
| 305 |
pipeline_result = delete_pipeline(client=client_conn, name=module.params["name"], module=module) |
| 306 |
changed = True |
| 307 |
|
| 308 |
module.exit_json(changed=changed, **camel_dict_to_snake_dict(pipeline_result)) |
| 309 |
|
| 310 |
|
| 311 |
if __name__ == "__main__": |
| 312 |
main() |