forked from dssg/aequitas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
202 lines (157 loc) · 5.34 KB
/
manage.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
from pathlib import Path
from argparse import REMAINDER
from argcmdr import CacheDict, Local, LocalRoot, localmethod
from plumbum import local
ROOT_PATH = Path(__file__).parent
VENV = '.manage'
VENV_BIN_PATH = ROOT_PATH / VENV / 'bin'
def get_project_local(exe):
(head, path) = local.env['PATH'].split(':', 1)
assert VENV_BIN_PATH.samefile(head)
with local.env(PATH=path):
return local[exe]
project_local = CacheDict(get_project_local)
class Aequitas(LocalRoot):
"""manage the aequitas project"""
@Aequitas.register
class Container(Local):
"""manage the aequitas docker image and container"""
image_name = 'aequitas'
container_name = 'aequitas-dev'
@localmethod
def build(self):
"""build image"""
return self.local['docker'][
'build',
'-t',
self.image_name,
ROOT_PATH,
]
@localmethod
def create(self):
"""create local container"""
return self.local['docker'][
'create',
'-p', '5000:5000',
'-e', 'HOST=0.0.0.0',
'--name', self.container_name,
self.image_name,
]
@localmethod
def start(self):
"""start local container"""
return self.local['docker'][
'start',
self.container_name,
]
@localmethod
def stop(self):
"""stop local container"""
return self.local['docker'][
'stop',
self.container_name,
]
@Aequitas.register
class Web(Local):
"""manage the aequitas webapp"""
class Dev(Local):
"""run the web development server"""
def prepare(self):
return (
self.local.FG,
project_local['python']['-m', 'serve']
)
class Env(Local):
"""manage the webapp environment"""
ENV = 'aequitas-pro'
def __init__(self, parser):
parser.add_argument(
'-n', '--name',
default=self.ENV,
help=f"environment name (default: {self.ENV})",
)
@localmethod
def console(self, args):
"""open the environment web console"""
return (self.local.FG, self.local['eb']['console', args.name])
@localmethod
def logs(self, args):
"""read environment logs"""
return (self.local.FG, self.local['eb']['logs', args.name])
@localmethod
def ssh(self, args):
"""ssh into the EC2 instance"""
return (self.local.FG, self.local['eb']['ssh', args.name])
class Create(Local):
"""create an environment"""
def __init__(self, parser):
parser.add_argument(
'-v', '--version',
help='previous version label to deploy to new environment',
)
def prepare(self, args):
# AWS_EB_PROFILE=dsapp-ddj
command = self.local['eb'][
'create',
'-nh', # return immediately
'-s', # stand-alone for now (no ELB)
args.name,
]
if args.version:
command = command['--version', args.version]
yield command
class Deploy(Local):
"""deploy to an environment"""
def __init__(self, parser):
parser.add_argument(
'version',
help='version label to apply',
)
def prepare(self, args):
return (
self.local.FG,
self.local['eb'][
'deploy',
'-nh',
'-l', args.version,
args.name,
]
)
@Aequitas.register
class Release(Local):
"""manage the aequitas releases and upload to pypi"""
package_name = 'aequitas'
bump_default_message = "Bump version: {current_version} → {new_version}"
@localmethod('part', choices=('major', 'minor', 'patch'),
help="part of the version to be bumped")
@localmethod('-m', '--message',
help=f"Tag message (in addition to default: "
f"'{bump_default_message}')")
def bump(self, args):
"""increment package version"""
if args.message:
message = f"{self.bump_default_message}\n\n{args.message}"
else:
message = self.bump_default_message
return self.local['bumpversion'][
'--message', message,
args.part,
]
@localmethod
def build(self):
"""build the python distribution"""
return (self.local.FG, self.local['python'][
'setup.py',
'sdist',
'bdist_wheel',
])
@localmethod('versions', metavar='version', nargs='*',
help="specific version(s) to upload (default: all)")
def upload(self, args):
"""upload distribution(s) to pypi"""
if args.versions:
targets = [f'dist/{self.package_name}-{version}*'
for version in args.versions]
else:
targets = [f'dist/{self.package_name}-*']
return (self.local.FG, self.local['twine']['upload'][targets])