-
Notifications
You must be signed in to change notification settings - Fork 33
/
run.py
executable file
·102 lines (82 loc) · 2.96 KB
/
run.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
#!/usr/bin/env python3
import os
import time
from pathlib import Path
import click
from deploy.aws import aws, resource_details
from deploy.settings import PROJECT_NAME
from deploy.utils import run, timing
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
@click.group()
def cli():
pass
@cli.command(help='Deploy app to production')
@timing
def deploy_app():
# Upload website static content
s3_bucket = resource_details(
PROJECT_NAME,
'WebsiteBucket',
)['PhysicalResourceId']
dist_folder = os.path.join(BASE_DIR, 'public')
aws(f's3 sync {dist_folder} s3://{s3_bucket} --delete', parse_output=False)
# Invalidate CDN
cloudfront_id = resource_details(
PROJECT_NAME,
'WebsiteDistribution',
)['PhysicalResourceId']
aws(f'cloudfront create-invalidation --distribution-id {cloudfront_id} '
f'--paths "/*"')
@cli.command(help='Deploy lambda function to production')
@timing
def deploy_lambda():
function_name = resource_details(
PROJECT_NAME,
'JobApplicationLambda',
)['PhysicalResourceId']
package_path = Path('lambda-package')
package_deps_path = package_path / 'dependencies'
code_archive_name = 'lambda-package.zip'
run(f'zip -r ../{code_archive_name} .', cwd=package_deps_path)
run(f'zip -g {code_archive_name} lambda_function.py', cwd=package_path)
code_archive_path = f'fileb://{package_path}/{code_archive_name}'
aws(
f'lambda update-function-code '
f'--function-name {function_name} '
f'--zip-file {code_archive_path} '
f'--publish',
)
def get_function_config():
return aws(
f'lambda get-function --function-name {function_name}',
)['Configuration']
function_config = get_function_config()
status = function_config['LastUpdateStatus']
while status == 'InProgress':
time.sleep(1)
function_config = get_function_config()
status = function_config['LastUpdateStatus']
if status == 'Failed':
status_reason = function_config['LastUpdateStatusReason']
status_reason_code = function_config['LastUpdateStatusReasonCode']
raise RuntimeError(
f'Failed to update lambda function. '
f'Reason: {status_reason_code} | {status_reason}.',
)
info = os.environ['GOOGLE_API_SERVICE_ACCOUNT_INFO']
info = info.replace('"', '\\"') # encode quotes inside json string
spreadsheet_id = os.environ['GOOGLE_SPREADSHEET_ID']
slack_bot_token = os.environ['SLACK_BOT_TOKEN']
pipedrive_token = os.environ['PIPEDRIVE_TOKEN']
aws(
f'lambda update-function-configuration '
f'--function-name {function_name} '
f'--environment "Variables={{'
f'GOOGLE_API_SERVICE_ACCOUNT_INFO=\'{info}\','
f'GOOGLE_SPREADSHEET_ID={spreadsheet_id},'
f'PIPEDRIVE_TOKEN={pipedrive_token},'
f'SLACK_BOT_TOKEN={slack_bot_token}'
f'}}"',
)
if __name__ == '__main__':
cli()