-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·94 lines (79 loc) · 2.66 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
#!/usr/bin/env -S uv run
import json
import os
import time
from pathlib import Path
from shlex import quote
import click
from deploy import settings
from deploy.aws import aws, resource_details
from deploy.utils import run, timing
@click.group()
def cli():
pass
@cli.command(help='Deploy the lambda functions to production')
@timing
def deploy_lambda():
package_path = Path('lambda-functions')
run(
'uv pip install '
f'--target {package_path / "dependencies"} '
f'-r {package_path / "pyproject.toml"}'
)
package_path = Path('lambda-functions')
package_deps_path = package_path / 'dependencies'
code_archive_name = 'lambda-functions.zip'
run(f'zip -qr ../{code_archive_name} .', cwd=package_deps_path)
run(
f'zip -qg {code_archive_name} pipedrive.py handler.py '
'contact_form.py newsletter_sign_up.py',
cwd=package_path,
)
code_archive_path = f'fileb://{package_path}/{code_archive_name}'
function_name = resource_details(
settings.PROJECT_NAME,
'LandingLambda',
)['PhysicalResourceId']
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']
while True:
function_config = get_function_config()
status = function_config['LastUpdateStatus']
if status != 'InProgress':
break
time.sleep(1)
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}.',
)
environment = {
'Variables': {
'CONTACT_FORM_FROM_EMAIL': settings.CONTACT_FORM_FROM_EMAIL,
'CONTACT_FORM_TO_EMAIL': settings.CONTACT_FORM_TO_EMAIL,
'PIPE_DRIVE_API_TOKEN': os.environ['PIPE_DRIVE_API_TOKEN'],
'PIPE_DRIVE_LEAD_CUSTOM_DATA':
os.environ['PIPE_DRIVE_LEAD_CUSTOM_DATA'],
'CRISP_WEBSITE_ID': settings.CRISP_WEBSITE_ID,
'CRISP_TOKEN_ID': os.environ['CRISP_TOKEN_ID'],
'CRISP_TOKEN_KEY': os.environ['CRISP_TOKEN_KEY'],
},
}
aws(
f'lambda update-function-configuration '
f'--function-name {function_name} '
'--timeout 50 '
f'--environment {quote(json.dumps(environment))}',
)
if __name__ == '__main__':
cli()