-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtasks.py
More file actions
200 lines (152 loc) · 5.58 KB
/
tasks.py
File metadata and controls
200 lines (152 loc) · 5.58 KB
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
from invoke import task
import json
import yaml
import semver
import os
def format_yaml(template, config):
# Replace in ${ENV_VAR} in template with value:
formatted = template
for k, v in config.items():
formatted = formatted.replace('${%s}' % k, v)
return formatted
@task
def templater(ctx, config, template='k8s/templates/all-in-one.yaml'):
""" Creates deployment setup for given config file"""
if config[-5:] != '.yaml':
config += '.yaml'
# Get path of tasks.py file to allow independence from CWD
dir_path = os.path.dirname(os.path.realpath(__file__))
if not os.path.isabs(config):
config = os.path.join(dir_path, config)
if not os.path.isabs(template):
template = os.path.join(dir_path, template)
with open(config, 'r') as stream:
config_dict = yaml.load(stream)
with open(template, 'r') as myfile:
template_str = myfile.read()
formatted = format_yaml(template_str, config_dict)
output_dir = os.path.join(dir_path, 'k8s', config_dict['NAMESPACE'])
output_path = os.path.join(output_dir, 'all-in-one.yaml')
if os.path.isfile(output_path):
print('Deployment config already exists. Aborting.')
else:
os.mkdir(output_dir)
with open(output_path, 'w') as myfile:
myfile.write(formatted)
@task
def build(ctx, config, version_tag):
"""
Build project's docker image
"""
config_dict = get_config(config)
image_name = config_dict['IMAGE'].split(':')[0]
image = '{}:{}'.format(image_name, version_tag)
cmd = 'docker build -t %s .' % image
ctx.run(cmd, echo=True)
return image
@task
def run(ctx, config, version_tag, port=8080):
"""
Run specified docker image on specified port
"""
config_dict = get_config(config)
image_name = config_dict['IMAGE'].split(':')[0]
image = '{}:{}'.format(image_name, version_tag)
cmd = 'docker run -p {}:{} --rm {}'.format(port, 80, image)
ctx.run(cmd, echo=True)
return image
def get_config(config):
if config[-5:] != '.yaml':
config += '.yaml'
# Use /server as base path
dir_path = os.path.dirname(os.path.realpath(__file__))
server_dir_path = dir_path
if not os.path.isabs(config):
config = os.path.join(server_dir_path, config)
with open(config, 'r') as stream:
config_dict = yaml.load(stream)
return config_dict
@task
def push(ctx, config, version_tag):
"""
Build, tag and push docker image
"""
config_dict = get_config(config)
image_name = config_dict['IMAGE'].split(':')[0]
image = '{}:{}'.format(image_name, version_tag)
build(ctx, config, version_tag)
ctx.run('gcloud docker -- push %s' % image, echo=True)
@task
def version(ctx, bump='prerelease'):
"""
Returns incremented version number by looking at git tags
"""
# Get latest git tag:
result = ctx.run('git tag --sort=-v:refname', hide='both')
latest_tag = result.stdout.split('\n')[0][1:]
increment = {'prerelease': semver.bump_prerelease,
'patch': semver.bump_patch,
'minor': semver.bump_minor,
'major': semver.bump_major}
incremented = increment[bump](latest_tag)
print(incremented)
return incremented
@task
def release(ctx, config, version_bump='prerelease'):
"""
Bump version, push git tag, push docker image
N.B. Commit changes first
"""
config_dict = get_config(config)
bumped_version = version(ctx, bump=version_bump)
tag = 'v' + bumped_version
comment = 'Version ' + bumped_version
# Create, tag and push docker image:
image_name = config_dict['IMAGE'].split(':')[0]
push(ctx, config, tag)
# Create an push git tag:
ctx.run("git tag '%s' -m '%s'" % (tag, comment), echo=True)
ctx.run("git push origin %s" % tag, echo=True)
print('Release Info:\n'
'Tag: {}\n'
'Image: {}\n'.format(tag, image_name))
@task
def deploy(ctx, config, version_tag):
"""
Updates kubernetes deployment to use specified version
"""
config_dict = get_config(config)
image_name = config_dict['IMAGE'].split(':')[0]
image = '{}:{}'.format(image_name, version_tag)
ctx.run('kubectl set image deployment/{} '
'{}={} --namespace={}'.format(config_dict['PROJECT_NAME'],
config_dict['PROJECT_NAME'],
image,
config_dict['NAMESPACE']), echo=True)
@task
def live(ctx, config):
"""Checks which version_tag is live"""
config_dict = get_config(config)
result = ctx.run('kubectl get deployment/{} --output=json --namespace={}'.format(config_dict['PROJECT_NAME'],
config_dict['NAMESPACE']),
echo=True,
hide='stdout')
server_config = json.loads(result.stdout)
image = server_config['spec']['template']['spec']['containers'][0]['image']
print(image)
return image
@task
def setup(ctx, config):
"""
Updates kubernetes deployment to use specified version
"""
config_dict = get_config(config)
ctx.run('kubectl apply -f k8s/{}/all-in-one.yaml'.format(config_dict['NAMESPACE']))
@task
def ip(ctx, config):
"""
Updates kubernetes deployment to use specified version
"""
config_dict = get_config(config)
ctx.run('kubectl get ingress --namespace {} {}'.format(config_dict['NAMESPACE'],
config_dict['PROJECT_NAME']), echo=True)