-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathcdk_pipeline.py
282 lines (241 loc) · 10.8 KB
/
cdk_pipeline.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
import logging
import os
import sys
import subprocess
import boto3
from ... import db
from ...db.api import Environment, Pipeline
from ...aws.handlers.sts import SessionHelper
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
class CDKPipelineStack:
"""
Create a stack that contains CDK Continuous Integration and Delivery (CI/CD) pipeline.
The pipeline is based on AWS DDK CICD CodePipeline pipelines
- Defaults for source/synth - CodeCommit & cdk synth
- blueprint with DDK application code added in the CodeCommit repository <https://github.com/awslabs/aws-ddk>
- ability to define development stages: dev, test, prod
- Ability to connect to private artifactory to pull artifacts from at synth
- Security best practices - ensures pipeline buckets block non-SSL, and are KMS-encrypted with rotated keys
- data.all metadata as environment variables accesible at synth
"""
def get_engine(self):
envname = os.environ.get("envname", "local")
engine = db.get_engine(envname=envname)
return engine
module_name = __file__
def __init__(self, target_uri):
engine = self.get_engine()
with engine.scoped_session() as session:
self.pipeline = Pipeline.get_pipeline_by_uri(session, target_uri)
self.pipeline_environment = Environment.get_environment_by_uri(session, self.pipeline.environmentUri)
# Development environments
self.development_environments = Pipeline.query_pipeline_environments(session, target_uri)
self.env, aws = CDKPipelineStack._set_env_vars(self.pipeline_environment)
self.code_dir_path = os.path.dirname(os.path.abspath(__file__))
try:
codecommit_client = aws.client('codecommit', region_name=self.pipeline_environment.region)
repository = CDKPipelineStack._check_repository(codecommit_client, self.pipeline.repo)
if repository:
self.venv_name = None
self.code_dir_path = os.path.realpath(
os.path.abspath(
os.path.join(
__file__, "..", "..", "blueprints", "data_pipeline_blueprint"
)
)
)
CDKPipelineStack.write_ddk_json_multienvironment(path=self.code_dir_path, output_file="ddk.json", pipeline_environment=self.pipeline_environment, development_environments=self.development_environments)
CDKPipelineStack.write_ddk_app_multienvironment(path=self.code_dir_path, output_file="app.py", pipeline=self.pipeline, development_environments=self.development_environments)
logger.info(f"Pipeline Repo {self.pipeline.repo} Exists...Handling Update")
update_cmds = [
f'REPO_NAME={self.pipeline.repo}',
'COMMITID=$(aws codecommit get-branch --repository-name ${REPO_NAME} --branch-name main --query branch.commitId --output text)',
'aws codecommit put-file --repository-name ${REPO_NAME} --branch-name main --file-content file://ddk.json --file-path ddk.json --parent-commit-id ${COMMITID} --cli-binary-format raw-in-base64-out',
'COMMITID=$(aws codecommit get-branch --repository-name ${REPO_NAME} --branch-name main --query branch.commitId --output text)',
'aws codecommit put-file --repository-name ${REPO_NAME} --branch-name main --file-content file://app.py --file-path app.py --parent-commit-id ${COMMITID} --cli-binary-format raw-in-base64-out',
]
process = subprocess.run(
"; ".join(update_cmds),
text=True,
shell=True, # nosec
encoding='utf-8',
cwd=self.code_dir_path,
env=self.env
)
else:
raise Exception
except Exception as e:
self.venv_name = self.initialize_repo()
CDKPipelineStack.write_ddk_app_multienvironment(path=os.path.join(self.code_dir_path, self.pipeline.repo), output_file="app.py", pipeline=self.pipeline, development_environments=self.development_environments)
CDKPipelineStack.write_ddk_json_multienvironment(path=os.path.join(self.code_dir_path, self.pipeline.repo), output_file="ddk.json", pipeline_environment=self.pipeline_environment, development_environments=self.development_environments)
self.git_push_repo()
def initialize_repo(self):
venv_name = ".venv"
cmd_init = [
f"ddk init {self.pipeline.repo} --generate-only",
f"cd {self.pipeline.repo}",
"git init --initial-branch main",
f"ddk create-repository {self.pipeline.repo} -t application dataall -t team {self.pipeline.SamlGroupName}"
]
logger.info(f"Running Commands: {'; '.join(cmd_init)}")
process = subprocess.run(
'; '.join(cmd_init),
text=True,
shell=True, # nosec
encoding='utf-8',
cwd=self.code_dir_path,
env=self.env
)
if process.returncode == 0:
logger.info("Successfully Initialized New CDK/DDK App")
return venv_name
@staticmethod
def write_ddk_json_multienvironment(path, output_file, pipeline_environment, development_environments):
json_envs = ""
for env in development_environments:
json_env = f""",
"{env.stage}": {{
"account": "{env.AwsAccountId}",
"region": "{env.region}",
"resources": {{
"ddk-bucket": {{"versioned": false, "removal_policy": "destroy"}}
}}
}}"""
json_envs = json_envs + json_env
json = f"""{{
"environments": {{
"cicd": {{
"account": "{pipeline_environment.AwsAccountId}",
"region": "{pipeline_environment.region}"
}}{json_envs}
}}
}}"""
with open(f'{path}/{output_file}', 'w') as text_file:
print(json, file=text_file)
@staticmethod
def write_ddk_app_multienvironment(path, output_file, pipeline, development_environments):
header = f"""
# !/usr/bin/env python3
import aws_cdk as cdk
from aws_ddk_core.cicd import CICDPipelineStack
from ddk_app.ddk_app_stack import DdkApplicationStack
from aws_ddk_core.config import Config
app = cdk.App()
class ApplicationStage(cdk.Stage):
def __init__(
self,
scope,
environment_id: str,
**kwargs,
) -> None:
super().__init__(scope, f"dataall-{{environment_id.title()}}", **kwargs)
DdkApplicationStack(self, "DataPipeline-{pipeline.label}-{pipeline.DataPipelineUri}", environment_id)
id = f"dataall-cdkpipeline-{pipeline.DataPipelineUri}"
config = Config()
(
CICDPipelineStack(
app,
id=id,
environment_id="cicd",
pipeline_name="{pipeline.label}",
)
.add_source_action(repository_name="{pipeline.repo}")
.add_synth_action()
.build()"""
stages = ""
for env in sorted(development_environments, key=lambda env: env.order):
stage = f""".add_stage("{env.stage}", ApplicationStage(app, "{env.stage}", env=config.get_env("{env.stage}")))"""
stages = stages + stage
footer = """
.synth()
)
app.synth()
"""
app = header + stages + footer
with open(f'{path}/{output_file}', 'w') as text_file:
print(app, file=text_file)
def git_push_repo(self):
git_cmds = [
'git config user.email "[email protected]"',
'git config user.name "CodeBuild"',
'git config --local credential.helper "!aws codecommit credential-helper $@"',
"git config --local credential.UseHttpPath true",
"git add .",
"git commit -a -m 'Initial Commit' ",
"git push -u origin main"
]
logger.info(f"Running Commands: {'; '.join(git_cmds)}")
process = subprocess.run(
'; '.join(git_cmds),
text=True,
shell=True, # nosec
encoding='utf-8',
cwd=os.path.join(self.code_dir_path, self.pipeline.repo),
env=self.env
)
if process.returncode == 0:
logger.info("Successfully Pushed DDK App Code")
@staticmethod
def clean_up_repo(path):
if path:
precmd = [
'deactivate;',
'rm',
'-rf',
f"{path}"
]
cwd = os.path.dirname(os.path.abspath(__file__))
logger.info(f"Running command : \n {' '.join(precmd)}")
process = subprocess.run(
' '.join(precmd),
text=True,
shell=True, # nosec
encoding='utf-8',
capture_output=True,
cwd=cwd
)
if process.returncode == 0:
print(f"Successfully cleaned cloned repo: {path}. {str(process.stdout)}")
else:
logger.error(
f'Failed clean cloned repo: {path} due to {str(process.stderr)}'
)
else:
logger.info(f"Info:Path {path} not found")
return
@staticmethod
def _check_repository(codecommit_client, repo_name):
repository = None
logger.info(f"Checking Repository Exists: {repo_name}")
try:
repository = codecommit_client.get_repository(repositoryName=repo_name)
except ClientError as e:
if e.response['Error']['Code'] == 'RepositoryDoesNotExistException':
logger.debug(f'Repository does not exists {repo_name} %s', e)
else:
raise e
return repository if repository else None
@staticmethod
def _set_env_vars(pipeline_environment):
aws = SessionHelper.remote_session(pipeline_environment.AwsAccountId)
env_creds = aws.get_credentials()
python_path = '/:'.join(sys.path)[1:] + ':/code' + os.getenv('PATH')
env = {
'AWS_REGION': pipeline_environment.region,
'AWS_DEFAULT_REGION': pipeline_environment.region,
'CURRENT_AWS_ACCOUNT': pipeline_environment.AwsAccountId,
'PYTHONPATH': python_path,
'PATH': python_path,
'envname': os.environ.get('envname', 'local'),
'COOKIECUTTER_CONFIG': "/dataall/cdkproxy/blueprints/cookiecutter_config.yaml",
}
if env_creds:
env.update(
{
'AWS_ACCESS_KEY_ID': env_creds.access_key,
'AWS_SECRET_ACCESS_KEY': env_creds.secret_key,
'AWS_SESSION_TOKEN': env_creds.token
}
)
return env, aws