forked from domtes/once
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (55 loc) · 2.08 KB
/
app.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
#!/usr/bin/env python3
import os
import base64
import configparser
from typing import Optional
from aws_cdk import core
from once.once_stack import OnceStack, CustomDomainStack
ONCE_CONFIG_FILE = os.getenv('ONCE_CONFIG_FILE', os.path.expanduser('~/.once'))
SECRET_KEY = os.getenv('SECRET_KEY')
CUSTOM_DOMAIN = os.getenv('CUSTOM_DOMAIN')
HOSTED_ZONE_NAME = os.getenv('HOSTED_ZONE_NAME')
HOSTED_ZONE_ID = os.getenv('HOSTED_ZONE_ID')
def generate_random_key() -> str:
return base64.b64encode(os.urandom(128)).decode('utf-8')
def generate_config(secret_key: Optional[str] = None,
custom_domain: str = None,
hosted_zone_name: str = None,
hosted_zone_id: str = None) -> configparser.ConfigParser:
config = configparser.ConfigParser()
config['once'] = {
'secret_key': secret_key or generate_random_key(),
}
config['deployment'] = {}
if all([custom_domain, hosted_zone_name, hosted_zone_id]):
config['once']['base_url'] = f'https://{custom_domain}'
config['deployment'] = {
'custom_domain': custom_domain,
'hosted_zone_name': hosted_zone_name,
'hosted_zone_id': hosted_zone_id
}
return config
def get_config(config_gile: str = ONCE_CONFIG_FILE) -> configparser.ConfigParser:
if not os.path.exists(ONCE_CONFIG_FILE):
print(f'Generating configuration file at {ONCE_CONFIG_FILE}')
with open(ONCE_CONFIG_FILE, 'w') as config_file:
config = generate_config(
secret_key=SECRET_KEY,
custom_domain=CUSTOM_DOMAIN,
hosted_zone_name=HOSTED_ZONE_NAME,
hosted_zone_id=HOSTED_ZONE_ID)
config.write(config_file)
else:
config = configparser.ConfigParser()
config.read(ONCE_CONFIG_FILE)
return config
def main():
config = get_config()
kwargs = {'secret_key': config['once']['secret_key']}
if config.has_section('deployment'):
kwargs.update(config['deployment'])
app = core.App()
once = OnceStack(app, 'once', **kwargs)
app.synth()
if __name__ == '__main__':
main()