|
1
|
|
|
import copy, os |
|
2
|
|
|
|
|
3
|
|
|
from fabric.api import local, prompt |
|
4
|
|
|
from fabric.contrib.console import confirm |
|
5
|
|
|
import yaml |
|
6
|
|
|
from git import Repo as _Repo |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
def _image_version(config): |
|
10
|
|
|
""" |
|
11
|
|
|
Returns the image and version from a kubernets config |
|
12
|
|
|
""" |
|
13
|
|
|
image_version = config['spec']['template']['spec']['containers'][0]['image'] |
|
14
|
|
|
return image_version.split(':') # image, version |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def _build(image, version, directory): |
|
18
|
|
|
""" |
|
19
|
|
|
Builds docker image from directory after confirmation |
|
20
|
|
|
""" |
|
21
|
|
|
tag = image + ':' + str(version) |
|
22
|
|
|
if confirm('Build new version {v} of {i}?'.format(i=image, v=version)): |
|
23
|
|
|
print('') |
|
24
|
|
|
local('docker build -t {t} {d}'.format(t=tag, d=directory)) |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
def _push(image, version): |
|
28
|
|
|
""" |
|
29
|
|
|
Pushes image to gcloud |
|
30
|
|
|
""" |
|
31
|
|
|
tag = image + ':' + str(version) |
|
32
|
|
|
if confirm('Push image {t} to gcloud?'.format(t=tag)): |
|
33
|
|
|
print('') |
|
34
|
|
|
local('gcloud docker -- push {t}'.format(t=tag)) |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
def _git_tag(version): |
|
38
|
|
|
""" |
|
39
|
|
|
Tags the latest commit with the new version |
|
40
|
|
|
""" |
|
41
|
|
|
if confirm('Tag latest commit to repo?'): |
|
42
|
|
|
repo = _Repo(os.path.dirname(os.path.abspath(__file__))) |
|
43
|
|
|
commit = repo.head.commit |
|
44
|
|
|
repo.create_tag(version, ref=commit) |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
def _update_config_image(config, tag): |
|
48
|
|
|
""" |
|
49
|
|
|
Returns a new config object with updated image tag |
|
50
|
|
|
""" |
|
51
|
|
|
config['spec']['template']['spec']['containers'][0]['image'] = tag |
|
52
|
|
|
return config |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
def _update_config(filename, tag): |
|
56
|
|
|
""" |
|
57
|
|
|
Updates the config file for latest tag |
|
58
|
|
|
""" |
|
59
|
|
|
with open(filename) as f: |
|
60
|
|
|
config = yaml.safe_load(f) |
|
61
|
|
|
|
|
62
|
|
|
new_config = _update_config_image(copy.deepcopy(config), tag) |
|
63
|
|
|
yaml_config = yaml.dump(new_config, default_flow_style=False) |
|
64
|
|
|
|
|
65
|
|
|
print('New config:') |
|
66
|
|
|
print('') |
|
67
|
|
|
print(yaml_config) |
|
68
|
|
|
print('') |
|
69
|
|
|
|
|
70
|
|
|
if confirm('Write config to file?'): |
|
71
|
|
|
with open(filename, 'w') as f: |
|
72
|
|
|
f.write(yaml_config) |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
def _k8s_replace(filename): |
|
76
|
|
|
""" |
|
77
|
|
|
Kubectl replace given filename |
|
78
|
|
|
""" |
|
79
|
|
|
if confirm('Replace ' + filename + ' in k8s?'): |
|
80
|
|
|
local('kubectl replace -f ' + filename) |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
def deploy(): |
|
84
|
|
|
""" |
|
85
|
|
|
Deploy updated web, celery, and celery-beat containers to kubernetes |
|
86
|
|
|
""" |
|
87
|
|
|
|
|
88
|
|
|
paths = ('k8s/web.yaml', 'k8s/celery.yaml', 'k8s/celery-beat.yaml') |
|
89
|
|
|
|
|
90
|
|
|
with open(paths[0]) as f: |
|
91
|
|
|
config = yaml.safe_load(f) |
|
92
|
|
|
image, version = _image_version(config) |
|
93
|
|
|
|
|
94
|
|
|
print('Current version {v} for image {i}'.format(v=version, i=image)) |
|
95
|
|
|
|
|
96
|
|
|
new_version = prompt('New version:') |
|
97
|
|
|
print('') |
|
98
|
|
|
|
|
99
|
|
|
_build(image, new_version, 'web/') |
|
100
|
|
|
print('') |
|
101
|
|
|
|
|
102
|
|
|
_push(image, new_version) |
|
103
|
|
|
print('') |
|
104
|
|
|
|
|
105
|
|
|
tag = image + ':' + str(new_version) |
|
106
|
|
|
|
|
107
|
|
|
for path in paths: |
|
108
|
|
|
_update_config(path, tag) |
|
109
|
|
|
_k8s_replace(path) |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
# git tag, push |
|
113
|
|
|
# sentry release tracking? |
|
114
|
|
|
# for path in paths: |
|
115
|
|
|
# write new config |
|
116
|
|
|
# replace |
|
117
|
|
|
|
|
118
|
|
|
# copy static files to nginx pod and push |