-
Notifications
You must be signed in to change notification settings - Fork 6
/
commands.py
174 lines (135 loc) · 6.15 KB
/
commands.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
import os.path
import time
from emcee.runner.config import YAMLCommandConfiguration
from emcee.runner import command, configs, config
from emcee.runner.commands import remote
from emcee.runner.utils import confirm
from emcee.app.config import YAMLAppConfiguration
from emcee import printer
from emcee.commands.transport import *
from emcee.commands.files import copy_file
from emcee.commands.deploy import deploy, list_builds
from emcee.provision.base import provision_host, patch_host
from emcee.provision.docker import provision_docker, authenticate_ghcr
from emcee.provision.secrets import provision_secret, show_secret
from emcee.deploy.docker import publish_images
from emcee.deploy import deployer, docker, DeploymentCheckError
from emcee.backends.aws.infrastructure.commands import *
from emcee.backends.aws.provision.volumes import (provision_volume,
provision_swapfile)
configs.load(YAMLCommandConfiguration)
@command
def provision(createdb=False):
# Configure host properties and prepare host platforms
provision_host()
# Provision application services
provision_docker()
# Provision service volume
printer.header("Initializing service volume...")
provision_volume(mount_point='/vol/store', filesystem='xfs')
remote(['mkdir', '-p', '/vol/store/services'], sudo=True)
for service in [
'postgresql/data',
'postgresql/archive',
'postgresql/run',
'rabbitmq',
]:
service_path = os.path.join('/vol/store/services', service)
result = remote(['mkdir', '-p', service_path], raise_on_error=False, sudo=True)
if result.succeeded:
owner = '{}:{}'.format(config.iam.user, config.iam.user)
remote(['chown', '-R', owner, service_path], sudo=True)
# Initialize swapfile on EBS volume
provision_swapfile(1024, path='/vol/store')
# Create persistent asset directory on EBS mount
printer.header("Creating media directory on EBS volume...")
remote(('mkdir', '-p', '/vol/store/media'), sudo=True)
# Create link for log storage path
printer.header("Creating log path...")
remote(('test', '-h', config.remote.path.log_dir, '||',
'mkdir', '-p', config.remote.path.log_dir))
# Create link for static asset path
printer.header("Creating static asset path...")
remote(('test', '-h', config.remote.path.static, '||',
'mkdir', '-p', config.remote.path.static))
# Create link for stored media path
remote(('test', '-h', config.remote.path.media, '||',
'ln', '-sf', '/vol/store/media', config.remote.path.media))
# Synchronize icons and assorted media assets:
archive_path = 'media.tar'
if os.path.exists(archive_path) and \
confirm("Synchronize media from '{}'?".format(archive_path)):
copy_file(archive_path, config.remote.path.media, sudo=True)
remote(('tar', 'xvf', archive_path, '&&',
'rm', archive_path),
cd=config.remote.path.media,
sudo=True
)
# Set the correct permissions on generated assets
printer.header("Setting permissions on media assets...")
owner = '{}:{}'.format(config.iam.user, config.services.nginx.group)
remote(('chown', '-h', owner, config.remote.path.media), sudo=True)
remote(('chown', '-R', owner, '/vol/store/media'), sudo=True)
remote(('chmod', 'g+X', '/vol/store/media'), sudo=True)
# Provision application secrets
db_password = input("Enter the PostgreSQL superuser password: ")
provision_secret('DBPassword', db_password)
client_id = input("Enter the ArcGIS Client ID: ")
provision_secret('ArcGISClientID', client_id)
client_secret = input("Enter the ArcGIS Client Secret: ")
provision_secret('ArcGISClientSecret', client_secret)
oauth_secret = input("Enter the Google OAuth secret: ")
provision_secret('GoogleOAuth2Secret', oauth_secret)
# Authenticates appropriate user on remote host for
# interaction with the GitHub Container Registry.
authenticate_ghcr()
@command
def maintenance_mode():
printer.info("Activating application maintenance mode...")
remote_processor = AOLRemoteProcessor(options=None)
# remove user-facing services from stack
if remote_processor.remove_stack_services(AOL_SERVICES['frontend']):
# wait a predetermined about of time for the
# task queues to be emptied of work
printer.info("Waiting 60s for task queue to empty...")
time.sleep(60)
# remove the remaining services from stack
remote_processor.remove_stack_services(AOL_SERVICES['backend'])
class AOLLocalProcessor(docker.LocalProcessor):
include_app = True
include_uwsgi = True
include_nginx = True
class AOLRemoteProcessor(docker.RemoteProcessor):
"""
TBD
"""
@deployer()
class AOLDeployer(docker.Deployer):
local_processor_cls = AOLLocalProcessor
remote_processor_cls = AOLRemoteProcessor
app_config_cls = YAMLAppConfiguration
def bootstrap_application(self):
if not self.remote_processor.is_stack_active():
raise DeploymentCheckError("Stack is not active in remote environment.")
# enable maintenance mode before bootstrapping application
maintenance_mode()
# verify that the database has been initialized
stat_cmd = ['stat', '/vol/store/services/postgresql/data']
result = remote(stat_cmd, raise_on_error=False, run_as=config.iam.user)
if not result.succeeded:
raise DeploymentCheckError("PostgreSQL database must be initialized. Exiting.")
printer.info("Bootstrapping application...")
bootstrap_stackfile = '{}-bootstrap.yml'.format(config.env)
self.remote_processor.deploy_stack(stackfile=bootstrap_stackfile)
# force bootstrap service to run
self.remote_processor.scale_stack_service('bootstrap', 1, wait=True)
self.remote_processor.scale_stack_service('bootstrap', 0)
# force update check for all service images not uniquely tagged/versioned
self.remote_processor.pull_stack_images()
AOL_SERVICES = {}
AOL_SERVICES['frontend'] = [
'scheduler', 'app'
]
AOL_SERVICES['backend'] = [
'celery'
]