Page MenuHomePhabricator
Paste P10075

/usr/bin/migrate-webservice-k8s-cluster
ActivePublic

Authored by Legoktm on Tue, Jan 7, 9:53 PM.
#!/usr/bin/env python3
# (C) 2020 Kunal Mehta <legoktm@member.fsf.org> under Apache-2.0
import argparse
import os
import subprocess
import sys
import yaml
MANIFEST = os.path.expanduser('~/service.manifest')
def current_webservice():
if not os.path.exists(MANIFEST):
print('This tool does not currently have a webservice running')
sys.exit(1)
with open(MANIFEST) as f:
data = yaml.safe_load(f)
if data['backend'] != 'kubenetes':
print('This tool is not running its webservice on kubernetes')
sys.exit(1)
if 'web' not in data:
print('This tool does not currently have a webservice running')
sys.exit(1)
return data['web']
def main():
parser = argparse.ArgumentParser(description='Migrate your webservice to the new k8s cluster')
parser.add_argument('--revert', action='store_bool', help='Revert back to the old k8s cluster')
args = parser.parse_args()
# TODO: check that it already hasn't migrated over to the new cluster...how?
current = current_webservice()
subprocess.check_call(['webservice', 'stop'])
context = 'default' if args.revert else 'toolforge'
subprocess.check_call(['/usr/bin/kubectl', 'config', 'use-context', context])
subprocess.check_call(['webservice', current, '--backend=kubernetes', 'start'])
subprocess.check_call(['dologmsg', 'Switched over to use new k8s cluster'])
if __name__ == '__main__':
main()

Event Timeline