forked from broadinstitute/seqr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servctl
executable file
·90 lines (67 loc) · 4.56 KB
/
servctl
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
#!/usr/bin/env python3
import argparse
import logging
import os
import sys
from deploy.servctl_utils.deploy_command_utils import deploy, DEPLOYMENT_TARGETS, DEPLOYMENT_ENVS, SECRETS
from deploy.servctl_utils.other_command_utils import delete_component, delete_all
logging.basicConfig(stream=sys.stdout, format='%(asctime)s %(levelname)-8s %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
DEPLOY_COMMAND = 'deploy'
DEPLOY_ALL_COMMAND = 'deploy-all'
DELETE_COMMAND = 'delete'
DELETE_ALL_COMMAND = 'delete-all'
p = argparse.ArgumentParser()
subparsers = p.add_subparsers(dest='command')
COMPONENT_ARG_CHOICES = DEPLOYMENT_TARGETS
DEPLOYMENT_TARGETS_SET = set(DEPLOYMENT_ENVS)
## DELETE_COMMAND
sp = subparsers.add_parser(DELETE_COMMAND, description="Terminate any live deployments, services, and pods for a given component")
sp.add_argument("components", nargs="+", help="terminate all deployments, services, and pods for the given component(s)", choices=COMPONENT_ARG_CHOICES)
sp.add_argument("deployment_target", choices=DEPLOYMENT_TARGETS_SET, help="kubernetes cluster")
## DELETE_ALL_COMMAND
sp = subparsers.add_parser(DELETE_ALL_COMMAND, description="Delete all components + the cluster")
sp.add_argument("deployment_target", choices=DEPLOYMENT_TARGETS_SET, help="kubernetes cluster")
for command_name in [DEPLOY_COMMAND, DEPLOY_ALL_COMMAND]:
sp = subparsers.add_parser(command_name, description="Deploy one or more components")
sp.add_argument("-d", "--delete-before-deploy", action="store_true", help="run 'kubectl delete' on component(s) before deploying them")
sp.add_argument("-b", "--build-docker-images", action="store_true", help="build the docker image for each component before deploying it. ")
sp.add_argument("-f", "--force", action="store_true", help="same as -b and -d together. Also, forces docker image to be rebuilt from the beginning with --no-cache.")
g = sp.add_mutually_exclusive_group()
g.add_argument("-p", "--push-to-registry", action="store_true", help="push local docker image to a docker registry. The destination registry is determined by the "
"DOCKER_IMAGE_PREFIX value in shared-settings.yaml. "
"When deploying to gcloud-dev or gcloud-prod, -p is necessary to make an image available because google container engine clusters "
"can only use images from the google container registry and/or public registries like dockerhub.")
g.add_argument("--only-push-to-registry", action="store_true", help="push local docker image to a docker registry and then stop, without deploying the component(s) to kubernetes.")
sp.add_argument("-t", "--docker-image-tag", help="deploy docker image(s) with this tag. If -b also used, the built docker images will be tagged with this.")
if command_name == DEPLOY_COMMAND:
choices = set(SECRETS.keys())
choices.update(COMPONENT_ARG_CHOICES)
sp.add_argument("components", nargs="+", help="one or more components to deploy", choices=choices)
sp.add_argument("deployment_target", choices=DEPLOYMENT_TARGETS_SET, help="which kubernetes cluster to deploy to")
args = p.parse_args()
# process command-line args
if args.command in [DEPLOY_COMMAND, DEPLOY_ALL_COMMAND]:
# transfer args to runtime_settings
runtime_settings = {}
runtime_settings["BASE_DIR"] = os.path.abspath(os.path.dirname(__file__))
runtime_settings['FORCE_BUILD_DOCKER_IMAGES'] = bool(args.force)
runtime_settings["DELETE_BEFORE_DEPLOY"] = True if args.delete_before_deploy or args.force else None
runtime_settings["BUILD_DOCKER_IMAGES"] = True if args.build_docker_images or args.force else None
runtime_settings["PUSH_TO_REGISTRY"] = bool(args.push_to_registry) or bool(args.only_push_to_registry)
runtime_settings["ONLY_PUSH_TO_REGISTRY"] = bool(args.only_push_to_registry)
runtime_settings["DOCKER_IMAGE_TAG"] = (":" + args.docker_image_tag) if args.docker_image_tag else (":" + args.deployment_target)
if args.command == DEPLOY_COMMAND:
components_to_deploy = args.components
elif args.command == DEPLOY_ALL_COMMAND:
components_to_deploy = DEPLOYMENT_TARGETS
deploy(args.deployment_target, components=components_to_deploy, runtime_settings=runtime_settings)
logger.info("==> Deployed " + ", ".join(components_to_deploy))
elif args.command == DELETE_COMMAND:
for component in args.components:
delete_component(component, deployment_target=args.deployment_target)
elif args.command == DELETE_ALL_COMMAND:
delete_all(args.deployment_target)
else:
p.error("Unexpected command: " + str(args.command))