forked from aiengines/codebuild_pipeline_skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodePipeline.py
executable file
·300 lines (260 loc) · 9.45 KB
/
CodePipeline.py
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Github attached AWS Code Pipeline"""
__author__ = 'Pedro Larroy'
__version__ = '0.1'
import boto3
import os
import sys
import subprocess
import logging
from troposphere import Parameter, Ref, Template, iam
from troposphere.iam import Role
from troposphere.s3 import Bucket
from troposphere.codepipeline import (
Pipeline, Stages, Actions, ActionTypeId, OutputArtifacts, InputArtifacts, Webhook,
WebhookAuthConfiguration, WebhookFilterRule,
ArtifactStore, DisableInboundStageTransitions)
import troposphere.codebuild as cb
import argparse
from awacs.aws import Allow, Statement, Principal, PolicyDocument, Policy
from awacs.sts import AssumeRole
from util import *
def create_codebuild_project(template) -> cb.Project:
from troposphere.codebuild import Project, Environment, Artifacts, Source
environment = Environment(
ComputeType='BUILD_GENERAL1_SMALL',
Image='aws/codebuild/standard:3.0',
Type='LINUX_CONTAINER',
)
codebuild_role = template.add_resource(
Role(
"CodeBuildRole",
AssumeRolePolicyDocument=Policy(
Statement=[
Statement(
Effect=Allow,
Action=[AssumeRole],
Principal=Principal("Service", ["codebuild.amazonaws.com"])
)
]
),
ManagedPolicyArns=[
'arn:aws:iam::aws:policy/AmazonS3FullAccess',
'arn:aws:iam::aws:policy/CloudWatchFullAccess',
'arn:aws:iam::aws:policy/AWSCodeBuildAdminAccess',
],
)
)
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-codebuild-project-source.html
return Project(
"ContinuousCodeBuild",
Name = "ContinuousCodeBuild",
Description = 'Continous pipeline',
Artifacts = Artifacts(Type='CODEPIPELINE'),
Environment = environment,
Source = Source(Type='CODEPIPELINE'),
ServiceRole = Ref(codebuild_role)
)
def create_pipeline_template(config) -> Template:
t = Template()
github_token = t.add_parameter(Parameter(
"GithubToken",
Type = "String"
))
github_owner = t.add_parameter(Parameter(
"GitHubOwner",
Type = 'String',
Default = 'aiengines',
AllowedPattern = "[A-Za-z0-9-_]+"
))
github_repo = t.add_parameter(Parameter(
"GitHubRepo",
Type = 'String',
Default = 'codebuild_pipeline_skeleton',
AllowedPattern = "[A-Za-z0-9-_]+"
))
github_branch = t.add_parameter(Parameter(
"GitHubBranch",
Type = 'String',
Default = 'master',
AllowedPattern = "[A-Za-z0-9-_]+"
))
artifact_store_s3_bucket = t.add_resource(Bucket(
"S3Bucket",
))
cloudformationrole = t.add_resource(Role(
"CloudformationRole",
AssumeRolePolicyDocument = PolicyDocument(
Version = "2012-10-17",
Statement = [
Statement(
Effect = Allow,
Action = [AssumeRole],
Principal = Principal("Service", ["cloudformation.amazonaws.com"])
)
]
),
ManagedPolicyArns = ['arn:aws:iam::aws:policy/AdministratorAccess']
))
codepipelinerole = t.add_resource(Role(
"CodePipelineRole",
AssumeRolePolicyDocument = PolicyDocument(
Statement = [
Statement(
Effect = Allow,
Action = [AssumeRole],
Principal = Principal("Service", ["codepipeline.amazonaws.com"])
)
]
),
ManagedPolicyArns = ['arn:aws:iam::aws:policy/AdministratorAccess']
))
codebuild_project = t.add_resource(create_codebuild_project(t))
pipeline = t.add_resource(Pipeline(
"CDPipeline",
ArtifactStore = ArtifactStore(
Type = "S3",
Location = Ref(artifact_store_s3_bucket)
),
# DisableInboundStageTransitions = [
# DisableInboundStageTransitions(
# StageName = "Release",
# Reason = "Disabling the transition until "
# "integration tests are completed"
# )
# ],
RestartExecutionOnUpdate = True,
RoleArn = codepipelinerole.GetAtt('Arn'),
Stages = [
Stages(
Name = "Source",
Actions = [
Actions(
Name = "SourceAction",
ActionTypeId = ActionTypeId(
Category = "Source",
Owner = "ThirdParty",
Provider = "GitHub",
Version = "1",
),
OutputArtifacts = [
OutputArtifacts(
Name = "GitHubSourceCode"
)
],
Configuration = {
'Owner': Ref(github_owner),
'Repo': Ref(github_repo),
'Branch': Ref(github_branch),
'PollForSourceChanges': False,
'OAuthToken': Ref(github_token)
},
RunOrder = "1"
)
]
),
Stages(
Name = "Build",
Actions = [
Actions(
Name = "BuildAction",
ActionTypeId = ActionTypeId(
Category = "Build",
Owner = "AWS",
Provider = "CodeBuild",
Version = "1"
),
InputArtifacts = [
InputArtifacts(
Name = "GitHubSourceCode"
)
],
OutputArtifacts = [
OutputArtifacts(
Name = "BuildArtifacts"
)
],
Configuration = {
'ProjectName': Ref(codebuild_project),
},
RunOrder = "1"
)
]
),
],
))
t.add_resource(Webhook(
"GitHubWebHook",
Authentication = 'GITHUB_HMAC',
AuthenticationConfiguration = WebhookAuthConfiguration(
SecretToken = Ref(github_token)
),
Filters = [
WebhookFilterRule(
JsonPath = '$.ref',
MatchEquals = 'refs/heads/{Branch}'
)
],
TargetPipeline = Ref(pipeline),
TargetAction = 'Source',
TargetPipelineVersion = pipeline.GetAtt('Version')
))
return t
def parameters_interactive(template: Template) -> List[dict]:
"""
Fill template parameters from standard input
:param template:
:return: A list of Parameter dictionary suitable to instantiate the template
"""
print("Please provide values for the Cloud Formation template parameters.")
parameter_values = []
for name, parameter in template.parameters.items():
paramdict = parameter.to_dict()
if 'Default' in paramdict:
default_value = paramdict['Default']
param_value = input(f"{name} [{default_value}]: ")
if not param_value:
param_value = default_value
else:
param_value = input(f"{name}: ")
parameter_values.append({'ParameterKey': name, 'ParameterValue': param_value})
return parameter_values
def config_logging():
import time
logging.getLogger().setLevel(os.environ.get('LOGLEVEL', logging.INFO))
logging.getLogger("requests").setLevel(logging.WARNING)
logging.basicConfig(format='{}: %(asctime)sZ %(levelname)s %(message)s'.format(script_name()))
logging.Formatter.converter = time.gmtime
def script_name() -> str:
""":returns: script name with leading paths removed"""
return os.path.split(sys.argv[0])[1]
def config_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Code pipeline",
epilog="""
""")
parser.add_argument('config', nargs='?', help='config file', default='config.yaml')
return parser
def main():
config_logging()
parser = config_argparse()
args = parser.parse_args()
with open(args.config, 'r') as fh:
config = yaml.load(fh, Loader=yaml.SafeLoader)
boto3.setup_default_session(region_name=config['aws_region'], profile_name=config['aws_profile'])
template = create_pipeline_template(config)
client = boto3.client('cloudformation')
logging.info(f"Creating stack {config['stack_name']}")
client = boto3.client('cloudformation')
delete_stack(client, config['stack_name'])
param_values_dict = parameters_interactive(template)
tparams = dict(
TemplateBody = template.to_yaml(),
Parameters = param_values_dict,
Capabilities=['CAPABILITY_IAM'],
#OnFailure = 'DELETE',
)
instantiate_CF_template(template, config['stack_name'], **tparams)
return 0
if __name__ == '__main__':
sys.exit(main())