Exemplo n.º 1
0
def shutdown_via_ssh(node_list, ssh_file):

    if ssh_file != "":
        ssh_param = " -i " + ssh_file
    else:
        ssh_param = ""
    for node in node_list:
        logging.info("==== Shutdown " + str(node) + " ====")
        run_cmd("ssh " + ssh_param + "core@" + node + " sudo shutdown - h")
Exemplo n.º 2
0
def backup_etcd(master_node):

    run_cmd(
        "oc debug node/" + master_node +
        " -- chroot /host /usr/local/bin/cluster-backup.sh /home/core/assets/backup"
    )
Exemplo n.º 3
0
def wait_for_ready_status(node, timeout):
    run_cmd("kubectl wait --for=condition=Ready "
            "node/" + node + " --timeout=" + str(timeout) + "s")
Exemplo n.º 4
0
def approve_crs():
    run_cmd("oc get csr | grep Pending | cut -f1 -d"
            " | while read i; do oc adm certificate approve $i; done")
Exemplo n.º 5
0
def main(cfg):
    # Parse and read the config
    if os.path.isfile(cfg):
        with open(cfg, 'r') as f:
            config = yaml.full_load(f)
        config = config["shutdown"][0]
        cloud_type = config['cloud_type']
        kubeconfig_path = config.get("kubeconfig_path", "~/.kube/config")

        shutdown_master_num = config.get("shutdown_master_num", "all")
        shutdown_worker_num = config.get("shutdown_worker_num", "all")
        shutdown_infra_num = config.get("shutdown_infra_num", "all")

        ssh_file = config.get("ssh_file", "")

        initialize_clients(kubeconfig_path)
        downtime = calc_time(config.get("downtime", "300 s"))

        masters = list_nodes("node-role.kubernetes.io/master")
        backup_etcd(masters[1])

        if shutdown_master_num != "all":
            new_master_list = []
            for i in range(int(shutdown_master_num)):
                new_master_list.append(masters[i])
            masters = new_master_list

        workers = list_nodes(
            "node-role.kubernetes.io/worker=,node-role.kubernetes.io/infra!=")
        if shutdown_worker_num != "all":
            new_worker_list = []
            for i in range(int(shutdown_worker_num)):
                new_worker_list.append(workers[i])
            workers = new_worker_list

        infras = list_nodes("node-role.kubernetes.io/infra")

        if shutdown_infra_num != "all":
            new_infra_list = []
            for i in range(int(shutdown_infra_num)):
                new_infra_list.append(infras[i])
            infras = new_infra_list

        node_list = workers + infras + masters
        logging.info('node list ' + str(node_list))

        if cloud_type == "aws":
            aws = aws_node_scenarios()
            for node in node_list:
                logging.info('stop node ' + str(node))
                aws_node_scenarios.node_stop_scenario(aws, node)

        elif cloud_type == "azure" or cloud_type == "az":
            logging.info("azure")
            az_account = run_cmd("az account list -o yaml")
            az = azure_node_scenarios(az_account)
            for node in node_list:
                logging.info('stop node ' + str(node))
                azure_node_scenarios.node_stop_scenario(az, node)
        elif cloud_type == "gcp":
            logging.info('gcp')
            project = run_cmd('gcloud config get-value project').split(
                '/n')[0].strip()
            gcp = gcp_node_scenarios(project)

            for node in node_list:
                logging.info('stop node ' + str(node))
                gcp_node_scenarios.node_stop_scenario(gcp, node)
        else:
            logging.info("Shutting down using ssh")
            shutdown_via_ssh(node_list, ssh_file)

        # wait period
        time.sleep(downtime)

        # restart cluster
        # start nodes based on cloud provider
        if cloud_type == "aws":
            for node in node_list:
                logging.info('start node ' + str(node))
                aws_node_scenarios.node_start_scenario(aws, node)

        elif cloud_type == "azure" or cloud_type == "az":

            for node in node_list:
                logging.info('start node ' + str(node))
                azure_node_scenarios.node_start_scenario(az, node)

        elif cloud_type == "gcp":
            for node in node_list:
                logging.info('start node ' + str(node))
                gcp_node_scenarios.node_start_scenario(gcp, node)
        else:
            logging.info("Cloud type " + str(cloud_type) +
                         " is not supported ")
            sys.exit(1)

        wait_for_all_nodes_ready(masters)

        wait_for_all_nodes_ready(workers)

        wait_for_all_nodes_ready(infras)

        cluster_operators = run_cmd("oc get co")

        run_cmd("oc get nodes")