def _wait_until_pod_is_ready(component_label, deployment_target): logger.info("waiting for \"%s\" to complete initialization" % component_label) while get_pod_status(component_label, deployment_target, status_type=POD_READY_STATUS) != "true": time.sleep(5)
def _wait_until_pod_is_running(component_label, deployment_target, pod_number=0): logger.info( "waiting for \"%(component_label)s\" pod #%(pod_number)s to enter Running state" % locals()) while get_pod_status(component_label, deployment_target, status_type=POD_RUNNING_STATUS, pod_number=pod_number) != "Running": time.sleep(5)
def port_forward(component_port_pairs=[], deployment_target=None, wait=True, open_browser=False, use_kubectl_proxy=False): """Executes kubectl command to forward traffic between localhost and the given pod. While this is running, connecting to localhost:<port> will be the same as connecting to that port from the pod's internal network. Args: component_port_pairs (list): 2-tuple(s) containing keyword to use for looking up a kubernetes pod, along with the port to forward to that pod (eg. ('mongo', 27017), or ('phenotips', 8080)) deployment_target (string): "local", "gcloud-dev", etc. See constants.DEPLOYMENT_TARGETS. wait (bool): Whether to block indefinitely as long as the forwarding process is running. open_browser (bool): If component_port_pairs includes components that have an http server (eg. "seqr" or "phenotips"), then open a web browser window to the forwarded port. use_kubectl_proxy (bool): Whether to use kubectl proxy instead of kubectl port-forward (see https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#manually-constructing-apiserver-proxy-urls) Returns: (list): Popen process objects for the kubectl port-forward processes. """ procs = [] for component_label, port in component_port_pairs: while get_pod_status(component_label, deployment_target) != "Running": time.sleep(5) logger.info("Forwarding port %s for %s" % (port, component_label)) pod_name = get_pod_name(component_label, deployment_target=deployment_target) if use_kubectl_proxy: command = "kubectl proxy --port 8001" else: command = "kubectl port-forward %(pod_name)s %(port)s" % locals() p = run_in_background(command) if open_browser and component_label in COMPONENTS_TO_OPEN_IN_BROWSER: if use_kubectl_proxy: url = "http://localhost:8001/api/v1/namespaces/default/services/%(component_label)s:%(port)s/proxy/" % locals( ) else: url = "http://localhost:%s" % port os.system("open " + url) procs.append(p) if wait: wait_for(procs) return procs
def print_log(components, deployment_target, enable_stream_log, wait=True): """Executes kubernetes command to print logs for the given pod. Args: components (list): one or more kubernetes pod labels (eg. 'phenotips' or 'nginx'). If more than one is specified, logs will be printed from all components in parallel. deployment_target (string): "local", "gcloud-dev", etc. See constants.DEPLOYMENT_TARGETS. enable_stream_log (bool): whether to continuously stream the log instead of just printing the log up to now. wait (bool): If False, this method will return without waiting for the log streaming process to finish printing all logs. Returns: (list): Popen process objects for the kubectl port-forward processes. """ stream_arg = "-f" if enable_stream_log else "" procs = [] for component_label in components: while get_pod_status(component_label, deployment_target) != "Running": time.sleep(5) pod_name = get_pod_name(component_label, deployment_target=deployment_target) p = run_in_background("kubectl logs %(stream_arg)s %(pod_name)s" % locals()) def print_command_log(): for line in iter(p.stdout.readline, ''): logger.info(line.strip('\n')) t = Thread(target=print_command_log) t.start() procs.append(p) if wait: wait_for(procs) return procs
if "nginx" in components: deploy_nginx(settings) if "pipeline-runner" in components: deploy_pipeline_runner(settings) def delete_pod(component_label, settings, async=False, custom_yaml_filename=None): yaml_filename = custom_yaml_filename or (component_label + ".%(DEPLOY_TO_PREFIX)s.yaml") deployment_target = settings["DEPLOY_TO"] if get_pod_status(component_label, deployment_target) == "Running": run(" ".join([ "kubectl delete", "-f %(DEPLOYMENT_TEMP_DIR)s/deploy/kubernetes/" + component_label + "/" + yaml_filename, ]) % settings, errors_to_ignore=["not found"]) logger.info("waiting for \"%s\" to exit Running status" % component_label) while get_pod_status(component_label, deployment_target) in [ "Running", "Terminating" ] and not async: time.sleep(5) def _wait_until_pod_is_ready(component_label, deployment_target):
if "kibana" in components: deploy_kibana(settings) if "nginx" in components: deploy_nginx(settings) if "pipeline-runner" in components: deploy_pipeline_runner(settings) def delete_pod(component_label, settings, async=False, custom_yaml_filename=None): yaml_filename = custom_yaml_filename or (component_label+".%(DEPLOY_TO_PREFIX)s.yaml") deployment_target = settings["DEPLOY_TO"] if get_pod_status(component_label, deployment_target) == "Running": run(" ".join([ "kubectl delete", "-f %(DEPLOYMENT_TEMP_DIR)s/deploy/kubernetes/"+component_label+"/"+yaml_filename, ]) % settings, errors_to_ignore=["not found"]) logger.info("waiting for \"%s\" to exit Running status" % component_label) while get_pod_status(component_label, deployment_target) in ["Running", "Terminating"] and not async: time.sleep(5) def _wait_until_pod_is_ready(component_label, deployment_target): logger.info("waiting for \"%s\" to complete initialization" % component_label) while get_pod_status(component_label, deployment_target, status_type=POD_READY_STATUS) != "true": time.sleep(5)
# deploy_elasticsearch(settings) # else: # deploy_elasticsearch_sharded(settings) #if "kibana" in components: # deploy_kibana(settings) if "nginx" in components: deploy_nginx(settings) def delete_pod(component_label, settings, async=False, custom_yaml_filename=None): yaml_filename = custom_yaml_filename or (component_label+".%(DEPLOY_TO_PREFIX)s.yaml") deployment_target = settings["DEPLOY_TO"] if get_pod_status(component_label, deployment_target) == "Running": run(" ".join([ "kubectl delete", "-f %(DEPLOYMENT_TEMP_DIR)s/deploy/kubernetes/"+component_label+"/"+yaml_filename, ]) % settings, errors_to_ignore=["not found"]) logger.info("waiting for \"%s\" to exit Running status" % component_label) while get_pod_status(component_label, deployment_target) == "Running" and not async: time.sleep(5) def _wait_until_pod_is_ready(component_label, deployment_target): logger.info("waiting for \"%s\" to complete initialization" % component_label) while get_pod_status(component_label, deployment_target, status_type=POD_READY_STATUS) != "true": time.sleep(5)