Add rollback option to deploy.py
This commit is contained in:
parent
aeaed5bf45
commit
61dddea655
@ -8,6 +8,7 @@ from os import environ
|
|||||||
|
|
||||||
|
|
||||||
DRY_RUN = False
|
DRY_RUN = False
|
||||||
|
DRY_RUN = True
|
||||||
|
|
||||||
|
|
||||||
# Map of Docker service names to Azure DevOps CI pipeline definition IDs
|
# Map of Docker service names to Azure DevOps CI pipeline definition IDs
|
||||||
@ -44,7 +45,7 @@ class DevOpsAPI: # https://learn.microsoft.com/en-us/rest/api/azure/devops
|
|||||||
json = self.ses.get('{}/{}/_apis/build/builds?definitions={}&$top={}'.format(
|
json = self.ses.get('{}/{}/_apis/build/builds?definitions={}&$top={}'.format(
|
||||||
self.base, self.proj, definition, top
|
self.base, self.proj, definition, top
|
||||||
)).json()
|
)).json()
|
||||||
return json['value'][0] if top == 1 else json
|
return json['value'][-1]
|
||||||
|
|
||||||
|
|
||||||
class Deployer: # https://docker-py.readthedocs.io/en/stable/index.html
|
class Deployer: # https://docker-py.readthedocs.io/en/stable/index.html
|
||||||
@ -68,43 +69,51 @@ class Deployer: # https://docker-py.readthedocs.io/en/stable/index.html
|
|||||||
'' if self.is_prod else 'demo',
|
'' if self.is_prod else 'demo',
|
||||||
'front' if is_front else 'back')
|
'front' if is_front else 'back')
|
||||||
|
|
||||||
self.img_name = lambda svc_name: 'enevodocker/enevoleads{}e:{}_{}'.format(
|
def img_name(self, svc_name, top):
|
||||||
|
return 'enevodocker/enevoleads{}e:{}_{}'.format(
|
||||||
'f' if 'front' in svc_name else 'b',
|
'f' if 'front' in svc_name else 'b',
|
||||||
'prod' if self.is_prod else 'stage',
|
'prod' if self.is_prod else 'stage',
|
||||||
self.last_build_id(svc_name))
|
self.get_build_id(svc_name, top))
|
||||||
|
|
||||||
def last_build_id(self, service):
|
def get_build_id(self, service, top):
|
||||||
return self.az.builds(SERVICE_MAPPER[service])['id']
|
return self.az.builds(SERVICE_MAPPER[service], top)['id']
|
||||||
|
|
||||||
def get_svc(self, name):
|
def get_svc(self, name):
|
||||||
for svc in self.svcs:
|
for svc in self.svcs:
|
||||||
if svc.name == name:
|
if svc.name == name:
|
||||||
return svc
|
return svc
|
||||||
|
|
||||||
def deploy(self, name):
|
def deploy(self, name, top=1):
|
||||||
img = self.img_name(name)
|
img = self.img_name(name, top)
|
||||||
print('Deploying image {} on service {}'.format(img, name))
|
print('Deploying image {} on service {}'.format(img, name))
|
||||||
return self.get_svc(name).update(image=img, name=name)
|
return self.get_svc(name).update(image=img, name=name)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from getopt import getopt
|
from getopt import GetoptError, getopt
|
||||||
from sys import argv
|
from sys import argv
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
print(
|
print(
|
||||||
'Usage: {} [option(s)]'.format(argv[0]) +
|
'Usage: {} [option(s)]\n'.format(argv[0]) +
|
||||||
'\n\t-p\tUse production environment' +
|
'\n\t-p\tUse production environment' +
|
||||||
'\n\t-f\tDeploy frontend service' +
|
'\n\t-f\tDeploy frontend service' +
|
||||||
'\n\t-b\tDeploy backend service' +
|
'\n\t-b\tDeploy backend service' +
|
||||||
'\n\t-n\tDeploy nextcom service' +
|
'\n\t-n\tDeploy nextcom service' +
|
||||||
|
'\n\t-r{x}\tDeploy x number of builds back' +
|
||||||
'\n\t-h\tShow this help')
|
'\n\t-h\tShow this help')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
is_prod = False
|
is_prod = False
|
||||||
svcs = []
|
svcs = []
|
||||||
|
top = 1
|
||||||
|
|
||||||
|
try:
|
||||||
|
opts, args = getopt(argv[1:], 'pfbnhr:')
|
||||||
|
except GetoptError as exc:
|
||||||
|
print('\nError: {}\n'.format(exc))
|
||||||
|
usage()
|
||||||
|
|
||||||
opts, args = getopt(argv[1:], 'pfbnh')
|
|
||||||
if '-p' in [opt for opt, _arg in opts]:
|
if '-p' in [opt for opt, _arg in opts]:
|
||||||
is_prod = True
|
is_prod = True
|
||||||
|
|
||||||
@ -117,6 +126,8 @@ if __name__ == '__main__':
|
|||||||
svcs.append(dep.svc_name(False))
|
svcs.append(dep.svc_name(False))
|
||||||
elif opt == '-n':
|
elif opt == '-n':
|
||||||
svcs.append('byndle_nextcom')
|
svcs.append('byndle_nextcom')
|
||||||
|
elif opt == '-r':
|
||||||
|
top += int(arg)
|
||||||
elif opt == '-h':
|
elif opt == '-h':
|
||||||
usage()
|
usage()
|
||||||
|
|
||||||
@ -126,11 +137,11 @@ if __name__ == '__main__':
|
|||||||
if DRY_RUN:
|
if DRY_RUN:
|
||||||
for svc in svcs:
|
for svc in svcs:
|
||||||
print('Would deploy image {} on service {}'.format(
|
print('Would deploy image {} on service {}'.format(
|
||||||
dep.img_name(svc), svc))
|
dep.img_name(svc, top), svc))
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
for svc in svcs:
|
for svc in svcs:
|
||||||
res = dep.deploy(svc)
|
res = dep.deploy(svc, top)
|
||||||
|
|
||||||
if res['Warnings']:
|
if res['Warnings']:
|
||||||
from json import dumps
|
from json import dumps
|
||||||
|
Loading…
x
Reference in New Issue
Block a user