Exemple #1
0
def setup_logging():
    """
    Setup logging configuration.
    """
    configuration_path = "sysconf/logging.yaml"
    logging_configuration = file_handler.load_yaml_config(configuration_path)
    logging.config.dictConfig(logging_configuration)
Exemple #2
0
    def process_args(self, args):
        cluster_object_model_k8s = cluster_object_model_generate_k8s(args.config_path)
        node_list = file_handler.load_yaml_config(args.node_list)

        if not kubectl_env_checking(cluster_object_model_k8s):
            raise RuntimeError("failed to do kubectl checking")

        for host in node_list["machine-list"]:
            if "nodename" not in host:
                host["nodename"] = host["hostip"]

        return cluster_object_model_k8s, node_list
    def process_args(self, args):
        cluster_object_model_instance = cluster_object_model(args.config_path)
        com = cluster_object_model_instance.kubernetes_config()
        node_list = file_handler.load_yaml_config(args.node_list)

        if not kubectl_env_checking(com):
            raise RuntimeError("failed to do kubectl checking")

        for host in node_list["machine-list"]:
            if "nodename" not in host:
                host["nodename"] = host["hostip"]

        return com, node_list
Exemple #4
0
def generate_configuration(quick_start_config_file, configuration_directory, force):
    """Automatically generate the following configuration files from a quick-start file:
        * Machine-level configurations: laylout.yaml
        * Kubernetes-level configurations I: kubernetes-configuration.yaml
        * Kubernetes-level configurations II: k8s-role-definition.yaml
        * Service-level configurations: service-configuration.yaml
    """
    quick_start_config_raw = file_handler.load_yaml_config(quick_start_config_file)

    #
    # Prepare machine list
    machine_list = []
    for m in quick_start_config_raw["machines"]:
        machine = {"hostip": m, "machine-type": "GENERIC"}
        # TODO on premise, using ip as "nodename"
        machine["nodename"] = m
        machine["docker-data"] = "/var/lib/docker"
        machine["username"] = quick_start_config_raw["ssh-username"]
        if "ssh-password" in quick_start_config_raw:
            machine["password"] = quick_start_config_raw["ssh-password"]
        else:
            machine["ssh-keyfile-path"] = quick_start_config_raw["ssh-keyfile-path"]
            machine["ssh-secret-name"] = quick_start_config_raw["ssh-secret-name"]
        machine["ssh-port"] = 22 if "ssh-port" not in quick_start_config_raw else quick_start_config_raw["ssh-port"]

        machine_list.append(machine)

    # workers
    worker_noders = machine_list[1:] if len(machine_list) > 1 else machine_list
    for machine in worker_noders:
        # k8s attributes
        machine["k8s-role"] = "worker"
        # PAI attributes
        machine["pai-worker"] = "true"

    # master
    master_node = machine_list[0]
    # k8s attributes
    master_node["k8s-role"] = "master"
    master_node["etcdid"] = "etcdid1"
    master_node["dashboard"] = "true"
    # PAI attributes
    master_node["pai-master"] = "true"
    master_node["zkid"] = "1"

    #
    # Prepare config of cluster IP range.
    service_cluster_ip_range = \
        "10.254.0.0/16" if "service-cluster-ip-range" not in quick_start_config_raw \
        else quick_start_config_raw["service-cluster-ip-range"]
    #
    # Auto-complete missing configuration items: Part 1 -- DNS.
    if "dns" in quick_start_config_raw:
        dns = quick_start_config_raw["dns"]
    else:
        result_stdout, result_stderr = pai_common.ssh_shell_paramiko_with_result(
            master_node,
            "cat /etc/resolv.conf | grep nameserver | cut -d ' ' -f 2 | head -n 1")
        dns = result_stdout.strip()
    #
    # Auto-complete missing configuration items: Part 2 -- hostnames.
    for host_config in machine_list:
        result_stdout, result_stderr = pai_common.ssh_shell_paramiko_with_result(
            host_config,
            "hostname")
        host_config["hostname"] = result_stdout.strip()

    #
    # kubernetes info
    api_servers_url = "http://{0}:{1}".format(master_node["hostip"], 8080)
    # TODO we some k8s template still using the 'dashboard_host'
    dashboard_host = master_node["hostip"]
    dashboard_url = "http://{0}:{1}".format(master_node["hostip"], 9090)

    #
    # Generate configuration files.
    target_file_names = [
        "layout.yaml",
        "kubernetes-configuration.yaml",
        "k8s-role-definition.yaml",
        "services-configuration.yaml"
    ]
    for x in target_file_names:
        target_file_path = os.path.join(configuration_directory, x)
        if file_handler.file_exist_or_not(target_file_path) and force is False:
            logger.warning("File %s exists. Skip." % (target_file_path))
            pass
        else:
            file_handler.create_folder_if_not_exist(configuration_directory)
            file_handler.write_generated_file(
                target_file_path,
                template_handler.generate_from_template_dict(
                    file_handler.read_template("./deployment/quick-start/%s.template" % (x)),
                    {"env":
                        {
                            "machines": machine_list,
                            "dns": dns,
                            "load-balance-ip": master_node["hostip"],
                            "service-cluster-ip-range": service_cluster_ip_range,
                            "api-servers-url": api_servers_url,
                            "dashboard-host": dashboard_host,
                            "dashboard-url": dashboard_url,
                            "pai-version": pai_version.paictl_version()
                        }
                     }))
Exemple #5
0
def pai_machine():

    if len(sys.argv) < 2:
        pai_machine_info()
        return

    option = sys.argv[1]
    del sys.argv[1]

    if option not in ["add", "remove", "etcd-fix"]:
        pai_machine_info()
        return

    parser = argparse.ArgumentParser()
    parser.add_argument('-p',
                        '--config-path',
                        dest="config_path",
                        required=True,
                        help="The path of your configuration directory.")
    parser.add_argument('-l',
                        '--node-list',
                        dest="node_list",
                        required=True,
                        help="The node-list to be operator")
    args = parser.parse_args(sys.argv[1:])

    config_path = args.config_path
    node_lists_path = args.node_list

    cluster_object_model = cluster_object_model_generate_service(config_path)
    cluster_object_model_k8s = cluster_object_model_generate_k8s(config_path)
    node_list = file_handler.load_yaml_config(node_lists_path)

    if kubectl_env_checking(cluster_object_model_k8s) == False:
        return

    for host in node_list['machine-list']:

        if 'nodename' not in host:
            host['nodename'] = host['hostip']

    if option == "add":

        for host in node_list['machine-list']:
            add_worker = k8s_add.add(cluster_object_model_k8s, host, True)
            add_worker.run()

            if host['k8s-role'] == 'master':
                logger.info(
                    "Master Node is added, sleep 60s to wait it ready.")
                time.sleep(60)

    if option == "remove":

        for host in node_list['machine-list']:
            add_worker = k8s_remove.remove(cluster_object_model_k8s, host,
                                           True)
            add_worker.run()

            if host['k8s-role'] == 'master':
                logger.info(
                    "master node is removed, sleep 60s for etcd cluster's updating"
                )
                time.sleep(60)

    if option == "etcd-fix":

        if len(node_list['machine-list']) > 1:
            logger.error(
                "etcd-fix can't fix more than one machine everytime. Please fix them one by one!"
            )
            sys.exit(1)

        for host in node_list['machine-list']:
            etcd_fix_worker = k8s_etcd_fix.etcdfix(cluster_object_model_k8s,
                                                   host, True)
            etcd_fix_worker.run()

        logger.info("Etcd has been fixed.")
Exemple #6
0
def pai_machine():

    if len(sys.argv) < 2:
        pai_machine_info()
        return

    option = sys.argv[1]
    del sys.argv[1]

    if option not in ["add", "remove"]:
        pai_machine_info()
        return

    parser = argparse.ArgumentParser()
    parser.add_argument('-p',
                        '--config-path',
                        dest="config_path",
                        required=True,
                        help="The path of your configuration directory.")
    parser.add_argument('-l',
                        '--node-list',
                        dest="node_list",
                        required=True,
                        help="The node-list to be operator")
    args = parser.parse_args(sys.argv[1:])

    config_path = args.config_path
    node_lists_path = args.node_list

    cluster_object_model = cluster_object_model_generate_service(config_path)
    cluster_object_model_k8s = cluster_object_model_generate_k8s(config_path)
    node_list = file_handler.load_yaml_config(node_lists_path)

    for host in node_list['machine-list']:

        if 'nodename' not in host:
            host['nodename'] = host['hostip']

    if option == "add":

        for host in node_list['machine-list']:
            add_worker = k8s_add.add(cluster_object_model_k8s, host, True)
            add_worker.run()

            if host['k8s-role'] == 'master':
                logger.info(
                    "Master Node is added, sleep 60s to wait it ready.")
                time.sleep(60)

    if option == "remove":

        for host in node_list['machine-list']:
            add_worker = k8s_remove.remove(cluster_object_model_k8s, host,
                                           True)
            add_worker.run()

            if host['k8s-role'] == 'master':
                logger.info(
                    "master node is removed, sleep 60s for etcd cluster's updating"
                )
                time.sleep(60)