Example #1
0
def menu_level2():
    # intialize help
    help = Help()

    user_input = ""
    while not user_input in ['q', 'Q']:
        display_menu2()
        user_input = user_io.read_kbd("Enter Selection ('h' for help)", [], '', True, True, help.menu_interview("menu2"))
        if user_input == '1':
            action_header("MANAGE AUTHORIZATION PROFILES")
            selected_profile = interview.add_edit_auth_profile()
            if selected_profile != None:
                if selected_profile == "define-new-auth-profile":
                    target_profile = None
                else:
                    target_profile = selected_profile
                interview.add_auth_profile(target_profile)
        elif user_input == '2':
            action_header("MANAGE BOND PROFILES")
            selected_profile = interview.add_edit_bond_profile()
            if selected_profile != None:
                if selected_profile == "define-new-bond-profile":
                    target_profile = None
                else:
                    target_profile = selected_profile
                interview.add_bond_profile(target_profile)
        elif user_input == '3':
            action_header("MANAGE ROLE PROFILES")
            selected_profile = interview.add_edit_role_profile()
            if selected_profile != None:
                if selected_profile == "define-new-role-profile":
                    target_profile = None
                else:
                    target_profile = selected_profile
                interview.add_role_profile(target_profile)
        elif user_input == '4':
            action_header("MANAGE HOST TEMPLATES")
            selected_profile = interview.add_edit_host_profile()
            if selected_profile != None:
                if selected_profile == "define-new-host-profile":
                    target_profile = None
                else:
                    target_profile = selected_profile
                interview.add_host_profile(target_profile)
        elif user_input == '5':
            auth_entries = datamodel.get_auth_profiles()
            reports.report_auth_profiles(auth_entries)
        elif user_input == '6':
            bond_entries = datamodel.get_bond_profiles()
            reports.report_bond_profiles(bond_entries)
        elif user_input == '7':
            role_entries = datamodel.get_role_profiles()
            reports.report_role_profiles(role_entries)
        elif user_input == '8':
            host_profile_entries = datamodel.get_host_profiles()
            reports.report_host_profiles(host_profile_entries)
        elif user_input in ['q', 'Q']:
            None
        else:
            sys.stdout.write("ERROR: Invalid Selection (enter 'q' to quit)\n")
Example #2
0
def view_log(log_files):
    # intialize help
    help = Help()

    cnt = 1
    allowed_values = ['q']
    for log_file in log_files:
        sys.stdout.write("{}. {}\n".format(cnt, log_file))
        allowed_values.append(str(cnt))
        cnt += 1
    user_input = user_io.read_kbd("Select Log", allowed_values, '', True, True, help.menu_interview("select-log"))
    if user_input != "q":
        idx = int(user_input) - 1
        target_log = log_files[idx]
        target_log_path = "{}/{}".format(globals.EXPRESS_LOG_DIR, target_log)
        dump_text_file(target_log_path)
Example #3
0
def menu_level1():
    # intialize help
    help = Help()

    user_input = ""
    while not user_input in ['q', 'Q']:
        display_menu1()
        user_input = user_io.read_kbd("Enter Selection ('h' for help)", [], '', True, True, help.menu_interview("menu1"))
        if user_input == '1':
            selected_du = interview.select_du()
            if selected_du:
                if selected_du != "q":
                    datamodel.delete_du(selected_du)
        elif user_input == '2':
            sys.stdout.write("\nNot Implemented\n")
        elif user_input == '3':
            dump_database(globals.CONFIG_FILE)
        elif user_input == '4':
            dump_database(globals.HOST_FILE)
        elif user_input == '5':
            dump_database(globals.CLUSTER_FILE)
        elif user_input == '6':
            selected_du = interview.select_du()
            if selected_du:
                if selected_du != "q":
                    view_config(selected_du)
        elif user_input == '7':
            selected_du = interview.select_du()
            if selected_du:
                if selected_du != "q":
                    host_entries = datamodel.get_hosts(selected_du['url'])
                    view_inventory(selected_du, host_entries)
        elif user_input == '8':
            log_files = get_logs()
            if len(log_files) == 0:
                sys.stdout.write("\nNo Logs Found")
            else:
                view_log(log_files)
        elif user_input in ['q', 'Q']:
            None
        else:
            sys.stdout.write("ERROR: Invalid Selection (enter 'q' to quit)\n")
def invoke_express_cli(nodes, cluster_name, node_type):
    # intialize help
    help = Help()

    sys.stdout.write("\nRunning PF9-Express CLI\n")
    user_input = user_io.read_kbd("--> Do you want to tail the log",
                                  ['q', 'y', 'n'], 'y', True, True,
                                  help.onboard_interview("run-express-cli"),
                                  True)
    sys.stdout.flush()
    sys.stdout.write(
        "\n\n>>>> DBG: just flushed stdout after reading from keyboard with timeout\n\n"
    )
    if user_input == 'q':
        return ()

    # build command args
    command_args = [globals.EXPRESS_CLI, 'cluster', 'attach-node']
    for node in nodes:
        if node_type == "master":
            command_args.append("-m")
        else:
            command_args.append("-w")
        command_args.append(node['ip'])
    command_args.append(cluster_name)
    cmd = ""
    for c in command_args:
        cmd = "{} {}".format(cmd, c)
    sys.stdout.write("Running: {}\n".format(cmd))
    c = subprocess.Popen(command_args,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    if user_input == 'y':
        sys.stdout.write(
            "----------------------------------- Start Log -----------------------------------\n"
        )
        tail_log(c)
    else:
        wait_for_job(c)
Example #5
0
def menu_level0():
    # intialize help
    help = Help()

    user_input = ""
    while not user_input in ['q', 'Q']:
        display_menu0()
        user_input = user_io.read_kbd("Enter Selection ('h' for help)", [], '', True, True, help.menu_interview("menu0"))
        if user_input == '1':
            action_header("MANAGE REGIONS")
            selected_du = interview.add_edit_du()
            if selected_du != None:
                if selected_du == "define-new-du":
                    target_du = None
                else:
                    target_du = selected_du
                new_du_list = interview.add_region(target_du)
                if new_du_list:
                    reports.report_du_info(new_du_list)
        elif user_input == '2':
            menu_level2()
        elif user_input == '3':
            action_header("MANAGE CLUSTERS")
            sys.stdout.write("\nSelect Region to add Cluster to:")
            selected_du = interview.select_du(['Kubernetes', 'KVM/Kubernetes'])
            if selected_du:
                if selected_du != "q":
                    new_cluster = interview.add_cluster(selected_du)
        elif user_input == '4':
            action_header("MANAGE HOSTS")
            sys.stdout.write("\nSelect Region to Add Host To:")
            selected_du = interview.select_du()
            if selected_du:
                if selected_du != "q":
                    flag_more_hosts = True
                    while flag_more_hosts:
                        new_host = interview.add_host(selected_du)
                        user_input = user_io.read_kbd("\nAdd Another Host?", ['y','n'], 'y', True, True, help.menu_interview("add-another-host"))
                        if user_input in ['n']:
                            flag_more_hosts = False
        elif user_input == '5':
            action_header("ONBOARD HOSTS")
            selected_du = interview.select_du()
            if selected_du:
                if selected_du != "q":
                    if selected_du['du_type'] == "Kubernetes":
                        sys.stdout.write("\nKubernetes Region: onboarding K8s nodes\n")
                        express_utils.run_express_cli(selected_du)
                        user_input = user_io.read_kbd("\nWould you like to run Region Discovery?", ['y','n'], 'y', True, True, help.menu_interview("run-region-discovery"))
                        if user_input == 'y':
                            sys.stdout.write("\nPerforming Discovery\n")
                            sys.stdout.write("--> Discovering hosts:\n")
                            num_hosts = datamodel.discover_region_hosts(selected_du)
                            sys.stdout.write("    # of hosts discovered: {}\n".format(num_hosts))
                            if selected_du['du_type'] == "Kubernetes":
                                sys.stdout.write("--> Discovering clusters:\n")
                                num_clusters_created, num_clusters_discovered  = datamodel.discover_region_clusters(selected_du)
                                sys.stdout.write("    # of clusters discovered/created: {}/{}\n".format(num_clusters_discovered, num_clusters_created))
                    elif selected_du['du_type'] == "KVM":
                        sys.stdout.write("\nKVM Region: onboarding KVM hypervisors\n")
                        host_entries = datamodel.get_hosts(selected_du['url'])
                        express_utils.run_express(selected_du, host_entries)
                        user_input = user_io.read_kbd("\nWould you like to run Region Discovery?", ['y','n'], 'y', True, True, help.menu_interview("run-region-discovery"))
                        if user_input == 'y':
                            sys.stdout.write("\nPerforming Discovery\n")
                            sys.stdout.write("--> Discovering hosts:\n")
                            num_hosts = datamodel.discover_region_hosts(selected_du)
                            sys.stdout.write("    # of hosts discovered: {}\n".format(num_hosts))
                            if selected_du['du_type'] == "Kubernetes":
                                sys.stdout.write("--> Discovering clusters:\n")
                                num_clusters_created, num_clusters_discovered  = datamodel.discover_region_clusters(selected_du)
                                sys.stdout.write("    # of clusters discovered/created: {}/{}\n".format(num_clusters_discovered, num_clusters_created))
        elif user_input == '6':
            action_header("SHOW REGION")
            selected_du = interview.select_du()
            if selected_du:
                if selected_du != "q":
                    du_entries = datamodel.get_configs(selected_du['url'])
                    reports.report_du_info(du_entries)
                    host_entries = datamodel.get_hosts(selected_du['url'])
                    reports.report_host_info(host_entries)
                    if selected_du['du_type'] in ['Kubernetes', 'KVM/Kubernetes']:
                        cluster_entries = datamodel.get_clusters(selected_du['url'])
                        reports.report_cluster_info(cluster_entries)
        elif user_input == '7':
            menu_level1()
        elif user_input in ['q', 'Q']:
            None
        else:
            sys.stdout.write("ERROR: Invalid Selection (enter 'q' to quit)\n")
def run_express(du, host_entries):
    # intialize help
    help = Help()

    sys.stdout.write("\nPF9-Express Inventory (region type = {})\n".format(
        du['du_type']))
    if du['du_type'] == "Kubernetes":
        express_inventories = ['k8s_master', 'k8s_worker']
    elif du['du_type'] == "KVM":
        express_inventories = [
            'all', 'hypervisors', 'glance', 'cinder', 'designate'
        ]
    else:
        express_inventories = [
            'all', 'hypervisors', 'glance', 'cinder', 'designate',
            'k8s_master', 'k8s_worker'
        ]

    cnt = 1
    allowed_values = ['q']
    for inventory in express_inventories:
        sys.stdout.write("    {}. {}\n".format(cnt, inventory))
        allowed_values.append(str(cnt))
        cnt += 1
    custom_idx = cnt
    sys.stdout.write("    {}. custom inventory\n".format(cnt))
    allowed_values.append(str(cnt))
    user_input = user_io.read_kbd(
        "\nSelect Inventory (to run PF9-Express against)", allowed_values, '1',
        True, True, help.onboard_interview("select-inventory"))
    if user_input == "q":
        return ()
    if int(user_input) != custom_idx:
        idx = int(user_input) - 1
        target_inventory = express_inventories[idx]
    else:
        user_input = user_io.read_kbd(
            "\nInventory Targets (comma/space-delimitted list of hostnames)",
            [], '', True, True, help.onboard_interview("custom-inventory"))
        target_inventory = user_input

    sys.stdout.write("\nPF9-Express Role Assignment\n")
    sys.stdout.write("    1. Install Hostagent\n")
    sys.stdout.write("    2. Install Hostagent and Assign Roles\n")
    assign_roles = user_io.read_kbd("\nRole Assignment", ['q', '1', '2'], '2',
                                    True, True,
                                    help.onboard_interview("role-assignment"))
    if assign_roles == "q":
        return ()
    else:
        if int(assign_roles) == 2:
            role_flag = 1
        else:
            role_flag = 0

    flag_installed = install_express(du)
    if flag_installed == True:
        express_config = build_express_config(du)
        if express_config:
            express_inventory = build_express_inventory(du, host_entries)
            if express_inventory:
                invoke_express(express_config, express_inventory,
                               target_inventory, role_flag)
def run_express_cli(du):
    # intialize help
    help = Help()

    selected_cluster = interview.select_target_cluster(du['url'])
    if selected_cluster:
        user_input = user_io.read_kbd("\nAttach Master Nodes:",
                                      ['y', 'n', 'q'], 'y', True, True,
                                      help.onboard_interview("attach-masters"))
        if user_input == "y":
            master_entries = datamodel.get_unattached_masters(selected_cluster)
            if not master_entries:
                sys.stdout.write(
                    "INFO: no master nodes are mapped to this cluster\n")
            else:
                reports.report_host_info(master_entries)
                allowed_values = ['q', 'all']
                for node in master_entries:
                    allowed_values.append(node['hostname'])
                user_input = user_io.read_kbd(
                    "\nSelect Master Node to Attach ('all' to attach all master nodes):",
                    allowed_values, 'all', True, True,
                    help.onboard_interview("select-masters"))
                if user_input == "all":
                    targets = master_entries
                else:
                    idx = int(user_input) - 1
                    targets = master_entries[idx]

                flag_installed = install_express(du)
                if flag_installed == True:
                    express_config = build_express_config(du)
                    if express_config:
                        express_inventory = build_express_inventory(
                            du, master_entries)
                        if express_inventory:
                            try:
                                shutil.copyfile(express_config,
                                                globals.EXPRESS_CLI_CONFIG_DIR)
                            except:
                                sys.stdout.write(
                                    "ERROR: failed to update {}\n".format(
                                        globals.EXPRESS_CLI_CONFIG_DIR))
                                return ()
                            sys.stdout.write(
                                "\n***INFO: invoking pf9-express for node prep (system/pip packages)\n"
                            )
                            invoke_express(express_config, express_inventory,
                                           "k8s_master", 1)
                            sys.stdout.write(
                                "\n***INFO: invoking express-cli for node attach (cluster attach-node <cluster>))\n"
                            )
                            invoke_express_cli(targets,
                                               selected_cluster['name'],
                                               "master")

        user_input = user_io.read_kbd("\nAttach Worker Nodes:",
                                      ['y', 'n', 'q'], 'y', True, True,
                                      help.onboard_interview("attach-workers"))
        if user_input == "y":
            worker_entries = datamodel.get_unattached_workers(selected_cluster)
            if not worker_entries:
                sys.stdout.write(
                    "INFO: no worker nodes are mapped to this cluster\n")
            else:
                reports.report_host_info(worker_entries)
                allowed_values = ['q', 'all']
                for node in worker_entries:
                    allowed_values.append(node['hostname'])
                user_input = user_io.read_kbd(
                    "\nSelect Worker Node to Attach ('all' to attach all worker nodes):",
                    allowed_values, 'all', True, True,
                    help.onboard_interview("select-workers"))
                if user_input == "all":
                    targets = worker_entries
                else:
                    idx = int(user_input) - 1
                    targets = worker_entries[idx]

                flag_installed = install_express(du)
                if flag_installed == True:
                    express_config = build_express_config(du)
                    if express_config:
                        express_inventory = build_express_inventory(
                            du, worker_entries)
                        if express_inventory:
                            try:
                                shutil.copyfile(express_config,
                                                globals.EXPRESS_CLI_CONFIG_DIR)
                            except:
                                sys.stdout.write(
                                    "ERROR: failed to update {}\n".format(
                                        globals.EXPRESS_CLI_CONFIG_DIR))
                                return ()
                            sys.stdout.write(
                                "\n***INFO: invoking pf9-express for node prep (system/pip packages)\n"
                            )
                            invoke_express(express_config, express_inventory,
                                           "k8s_worker", 1)
                            sys.stdout.write(
                                "\n***INFO: invoking express-cli for node attach (cluster attach-node <cluster>))\n"
                            )
                            invoke_express_cli(targets,
                                               selected_cluster['name'],
                                               "worker")
def invoke_express(express_config, express_inventory, target_inventory,
                   role_flag):
    # intialize help
    help = Help()

    sys.stdout.write("\nRunning PF9-Express\n")
    user_input = user_io.read_kbd(
        "--> Installing PF9-Express Prerequisites, do you want to tail the log (enter 's' to skip)",
        ['q', 'y', 'n', 's'], 's', True, True,
        help.onboard_interview("express-prereqs"))
    if user_input == 'q':
        return ()
    if user_input in ['y', 'n']:
        p = subprocess.Popen([globals.PF9_EXPRESS, '-i', '-c', express_config],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        if user_input == 'y':
            sys.stdout.write(
                "----------------------------------- Start Log -----------------------------------\n"
            )
            tail_log(p)
        else:
            wait_for_job(p)

    user_input = user_io.read_kbd(
        "--> Running PF9-Express, do you want to tail the log",
        ['q', 'y', 'n'], 'n', True, True,
        help.onboard_interview("run-express"))
    if user_input == 'q':
        return ()
    if role_flag == 1:
        # install pf9-hostagent / assign roles
        if target_inventory in ['k8s_master', 'ks8_worker']:
            # pmk
            sys.stdout.write("Running: {} -a -b -c {} -v {} {}\n".format(
                globals.PF9_EXPRESS, express_config, express_inventory,
                target_inventory))
            cmd_args = [
                globals.PF9_EXPRESS, '-a', '-b', '-c', express_config, '-v',
                express_inventory, target_inventory
            ]
            p = subprocess.Popen(cmd_args,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
        else:
            # pmo
            sys.stdout.write("Running: {} -a -b -c {} -v {} {}\n".format(
                globals.PF9_EXPRESS, express_config, express_inventory,
                target_inventory))
            cmd_args = [
                globals.PF9_EXPRESS, '-a', '-b', '-c', express_config, '-v',
                express_inventory, target_inventory
            ]
            p = subprocess.Popen(cmd_args,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
    else:
        # install pf9-hostagent / skip role assignment
        if target_inventory in ['k8s_master', 'ks8_worker']:
            # pmk
            sys.stdout.write("Running: {} -b --pmk -c {} -v {} {}\n".format(
                globals.PF9_EXPRESS, express_config, express_inventory,
                target_inventory))
            cmd_args = [
                globals.PF9_EXPRESS, '-b', '--pmk', '-c', express_config, '-v',
                express_inventory, target_inventory
            ]
            p = subprocess.Popen(cmd_args,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
        else:
            # pmo
            sys.stdout.write("Running: . {} && {} -b -c {} -v {} {}\n".format(
                globals.WIZARD_VENV, globals.PF9_EXPRESS, express_config,
                express_inventory, target_inventory))
            cmd_args = [
                '.', globals.WIZARD_VENV, '&&', globals.PF9_EXPRESS, '-b',
                '-c', express_config, '-v', express_inventory, target_inventory
            ]
            p = subprocess.Popen(cmd_args,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)

    if user_input == 'y':
        sys.stdout.write(
            "----------------------------------- Start Log -----------------------------------\n"
        )
        tail_log(p)
    else:
        wait_for_job(p)