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 |
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(filename, tag): |
48
|
|
|
""" |
49
|
|
|
Updates the config file for latest tag |
50
|
|
|
""" |
51
|
|
|
with open(filename) as f: |
52
|
|
|
config = yaml.safe_load(f) |
53
|
|
|
|
54
|
|
|
new_config = _update_config_image(copy.deepcopy(config), tag) |
55
|
|
|
yaml_config = yaml.dump(new_config, default_flow_style=False) |
56
|
|
|
|
57
|
|
|
print('New config:') |
58
|
|
|
print('') |
59
|
|
|
print(yaml_config) |
60
|
|
|
print('') |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def deploy(): |
64
|
|
|
""" |
65
|
|
|
Deploy updated web, celery, and celery-beat containers to kubernetes |
66
|
|
|
""" |
67
|
|
|
|
68
|
|
|
paths = ('k8s/web.yaml', 'k8s/celery.yaml', 'k8s/celery-beat.yaml') |
69
|
|
|
|
70
|
|
|
with open(paths[0]) as f: |
71
|
|
|
config = yaml.safe_load(f) |
72
|
|
|
image, version = _image_version(config) |
73
|
|
|
|
74
|
|
|
print('Current version {v} for image {i}'.format(v=version, i=image)) |
75
|
|
|
|
76
|
|
|
new_version = prompt('New version:') |
77
|
|
|
print('') |
78
|
|
|
|
79
|
|
|
_build(image, new_version, 'web/') |
80
|
|
|
print('') |
81
|
|
|
|
82
|
|
|
_push(image, new_version) |
83
|
|
|
|
84
|
|
|
# git tag, push |
85
|
|
|
# sentry release tracking? |
86
|
|
|
# for path in paths: |
87
|
|
|
# write new config |
88
|
|
|
# replace |
89
|
|
|
|