def process_params(executor_name):
    end = False
    section = Sections.EXECUTOR_PARAMS.format(executor_name)

    while not end:
        print(
            f"The actual {Bcolors.BOLD}{Bcolors.OKBLUE}{executor_name}"
            f" executor's arguments{Bcolors.ENDC} are: "
            f"{Bcolors.OKGREEN}{config.instance.options(section)}"
            f"{Bcolors.ENDC}"
        )
        value = choose_adm("argument")
        if value == "A":
            param = click.prompt("Argument name").lower()
            if param in config.instance.options(section):
                print(f"{Bcolors.WARNING}The argument {param} already exists" f"{Bcolors.ENDC}")
            else:
                value = confirm_prompt("Is mandatory?")
                config.instance.set(section, param, f"{value}")
        elif value == "M":
            param = click.prompt("Argument name").lower()
            if param not in config.instance.options(section):
                print(f"{Bcolors.WARNING}There is no {param} argument" f"{Bcolors.ENDC}")
            else:
                def_value, param = get_new_name(param, section, "argument")
                value = confirm_prompt("Is mandatory?", default=def_value)
                config.instance.set(section, param, f"{value}")
        elif value == "D":
            param = click.prompt("Argument name").lower()
            if param not in config.instance.options(section):
                print(f"{Bcolors.WARNING}There is no {param} argument" f"{Bcolors.ENDC}")
            else:
                config.instance.remove_option(section, param)
        else:
            end = True
def process_params(executor_name):
    end = False
    if "params" not in config.instance[Sections.AGENT][
            Sections.EXECUTORS][executor_name]:
        config.instance[Sections.AGENT][
            Sections.EXECUTORS][executor_name]["params"] = {}
    section = config.instance[Sections.AGENT][
        Sections.EXECUTORS][executor_name].get("params")

    while not end:
        print(f"The current {Bcolors.BOLD}{Bcolors.OKBLUE}{executor_name}"
              f" executor's arguments{Bcolors.ENDC} are: "
              f"{Bcolors.OKGREEN}{list(section.keys())}"
              f"{Bcolors.ENDC}")
        value = choose_adm("argument")
        if value == "A":
            param = click.prompt("Argument name").lower()
            if param in section:
                print(f"{Bcolors.WARNING}The argument {param} already exists"
                      f"{Bcolors.ENDC}")
            else:
                mandatory = confirm_prompt("Is mandatory?")
                input_type = click.prompt("Type?",
                                          type=click.Choice(DATA_TYPE.keys()))
                input_base_type = DATA_TYPE[input_type].type().base
                section[param] = {
                    "mandatory": mandatory,
                    "type": input_type,
                    "base": input_base_type
                }
        elif value == "M":
            param = click.prompt("Argument name").lower()
            if param not in section:
                print(f"{Bcolors.WARNING}There is no {param} argument"
                      f"{Bcolors.ENDC}")
            else:
                def_value, param = get_new_name(param, section, "argument")
                mandatory = confirm_prompt("Is mandatory?",
                                           default=def_value["mandatory"])
                input_type = click.prompt("Type?",
                                          type=click.Choice(DATA_TYPE.keys()),
                                          default=def_value["type"])
                input_base_type = DATA_TYPE[input_type].type().base
                section[param] = {
                    "mandatory": mandatory,
                    "type": input_type,
                    "base": input_base_type
                }
        elif value == "D":
            param = click.prompt("Argument name").lower()
            if param not in section:
                print(f"{Bcolors.WARNING}There is no {param} argument"
                      f"{Bcolors.ENDC}")
            else:
                section.pop(param)
        else:
            end = True
def ask_value(agent_dict, opt, section, ssl, control_opt=None):
    info_url = {}

    if agent_dict[section][opt]["type"] == click.BOOL:
        def_value = config.instance[section].get(opt, None)
        if def_value is None:
            def_value = agent_dict[section][opt]["default_value"](ssl)
    else:
        def_value = config.instance[section].get(
            opt, None) or agent_dict[section][opt]["default_value"](ssl)

    value = None
    while value is None:
        if agent_dict[section][opt]["type"] == click.BOOL:
            value = confirm_prompt(f"{opt}", default=def_value)
        else:
            value = click.prompt(f"{opt}",
                                 default=def_value,
                                 type=agent_dict[section][opt]["type"])
        if opt == "host":
            info_url = url_setting(value)
            value = info_url["url_name"]

        if value == "":
            click.secho("Trying to save with empty value", fg="yellow")
        try:
            if control_opt is None:
                config.__control_dict[section][opt](opt, value)
            else:
                config.__control_dict[section][control_opt](opt, value)
        except ValueError as e:
            click.secho(f"{e}", fg="red")
            value = None
    return value, info_url
Пример #4
0
 async def new_executor(self):
     name = self.check_executors_name("Name")
     if name:
         self.executors_list.append(name)
         custom_executor = confirm_prompt("Is a custom executor?", default=False)
         if custom_executor:
             self.new_custom_executor(name)
         else:
             await self.new_repo_executor(name)
Пример #5
0
    async def process_executors(self):
        end = False

        while not end:
            print(f"The actual configured {Bcolors.OKBLUE}{Bcolors.BOLD}"
                  f"executors{Bcolors.ENDC} are: {Bcolors.OKGREEN}"
                  f"{list(self.executors_dict.keys())}{Bcolors.ENDC}")
            value = choose_adm("executor")
            if value.upper() == "A":
                await self.new_executor()
            elif value.upper() == "M":
                self.edit_executor()
            elif value.upper() == "D":
                self.delete_executor()
            else:
                quit_executor_msg = f"{Bcolors.WARNING}There are no executors loaded. Are you sure?{Bcolors.ENDC}"
                return confirm_prompt(
                    quit_executor_msg,
                    default=False) if not self.executors_dict else True
Пример #6
0
    async def run(self):
        end = False
        ignore_changes = False
        def_value, choices = get_default_value_and_choices(
            "Q", ["A", "E", "Q"])

        while not end:
            value = click.prompt(
                "Do you want to edit the [A]gent or the [E]xecutors? Do you "
                "want to [Q]uit?",
                type=click.Choice(choices=choices, case_sensitive=False),
                default=def_value,
            )
            if value.upper() == "A":
                process_agent()
            elif value.upper() == "E":
                await self.process_executors()
            else:
                process_choice_errors(value)
                try:
                    if Sections.AGENT in config.instance:
                        click.echo(
                            self.status_report(sections=config.instance))
                        config.control_config()
                        end = True
                    else:
                        if confirm_prompt(
                                click.style(
                                    "File configuration not saved. Are you sure?",
                                    fg="yellow")):
                            click.echo(
                                self.status_report(
                                    sections=config.instance.sections()))
                            end = True
                            ignore_changes = True
                        else:
                            end = False

                except ValueError as e:
                    click.secho(f"{e}", fg="red")
        if not ignore_changes:
            config.save_config(self.config_filepath)
def process_agent():
    agent_dict = {
        Sections.SERVER: {
            "host": {
                "default_value": lambda _: "127.0.0.1",
                "type": click.STRING,
            },
            "ssl": {
                "default_value": lambda _: "True",
                "type": click.BOOL,
            },
            "ssl_port": {
                "default_value": lambda _: "443",
                "type": click.IntRange(min=1, max=65535),
            },
            "ssl_cert": {
                "default_value": lambda _: "",
                "type": click.Path(allow_dash=False, dir_okay=False),
            },
            "api_port": {
                "default_value": lambda _ssl: "443" if _ssl else "5985",
                "type": click.IntRange(min=1, max=65535),
            },
            "websocket_port": {
                "default_value": lambda _ssl: "443" if _ssl else "9000",
                "type": click.IntRange(min=1, max=65535),
            },
            "workspaces": {
                "default_value": lambda _: "workspace",
                "type": click.STRING,
            },
        },
        Sections.TOKENS: {
            "registration": {
                "default_value": lambda _: "ACorrectTokenIs25CharLen",
                "type": click.STRING,
            },
            "agent": {},
        },
        Sections.AGENT: {
            "agent_name": {
                "default_value": lambda _: "agent",
                "type": click.STRING,
            }
        },
    }

    ssl = True

    for section in agent_dict:
        print(f"{Bcolors.OKBLUE}Section: {section}{Bcolors.ENDC}")
        for opt in agent_dict[section]:
            if section not in config.instance:
                config.instance.add_section(section)
            if section == Sections.TOKENS and opt == "agent":
                if "agent" in config.instance.options(section) and confirm_prompt("Delete agent token?"):
                    config.instance.remove_option(section, opt)
            elif section == Sections.SERVER and opt.__contains__("port"):
                if opt == "ssl_port":
                    if ssl:
                        value = ask_value(agent_dict, opt, section, ssl, "api_port")
                        config.instance.set(section, "api_port", str(value))
                        config.instance.set(section, "websocket_port", str(value))
                    else:
                        continue
                else:
                    if not ssl:
                        value = ask_value(agent_dict, opt, section, ssl)
                        config.instance.set(section, opt, str(value))
                    else:
                        continue
            elif opt == "ssl_cert":
                if ssl:

                    if confirm_prompt("Default SSL behavior?"):
                        path = ""
                    else:
                        path = None
                        while path is None:
                            value = ask_value(agent_dict, opt, section, ssl)
                            if value != "" and Path(value).exists():
                                path = value
                    config.instance.set(section, opt, str(path))
            elif opt == "workspaces":
                process_workspaces()
            else:
                value = ask_value(agent_dict, opt, section, ssl)
                if opt == "ssl":
                    ssl = str(value).lower() == "true"
                config.instance.set(section, opt, str(value))
Пример #8
0
def process_agent():
    agent_dict = {
        Sections.SERVER: {
            "host": {
                "default_value": lambda _: "127.0.0.1",
                "type": click.STRING,
            },
            "ssl": {
                "default_value": lambda _: "True",
                "type": click.BOOL,
            },
            "ssl_cert": {
                "default_value": lambda _: "",
                "type": click.Path(allow_dash=False, dir_okay=False),
            },
            "workspaces": {
                "default_value": lambda _: "workspace",
                "type": click.STRING,
            },
        },
        Sections.TOKENS: {
            "agent": {},
        },
        Sections.AGENT: {
            "agent_name": {
                "default_value": lambda _: "agent",
                "type": click.STRING,
            }
        },
    }

    ssl = True

    for section in agent_dict:
        if Sections.TOKENS == section and (
                section not in config.instance
                or "agent" not in config.instance.options(section)):
            continue
        print(f"{Bcolors.OKBLUE}Section: {section}{Bcolors.ENDC}")
        for opt in agent_dict[section]:
            if section not in config.instance:
                config.instance.add_section(section)
            if section == Sections.TOKENS and opt == "agent":
                if "agent" in config.instance.options(
                        section) and confirm_prompt("Delete agent token?"):
                    config.instance.remove_option(section, opt)
            elif opt == "ssl_cert":
                if ssl:

                    if confirm_prompt("Default SSL behavior?"):
                        path = ""
                    else:
                        path = None
                        while path is None:
                            value, _ = ask_value(agent_dict, opt, section, ssl)
                            if value != "" and Path(value).exists():
                                path = value
                    config.instance.set(section, opt, str(path))
            elif opt == "workspaces":
                process_workspaces()
            else:
                if opt == "host":
                    value, url_json = ask_value(agent_dict, opt, section, ssl)
                    if url_json["url_path"]:
                        config.instance.set(section, "base_route",
                                            str(url_json["url_path"]))
                elif opt == "ssl":
                    if url_json["check_ssl"] is None:
                        value, _ = ask_value(agent_dict, opt, section, ssl)
                        ssl = str(value).lower() == "true"
                    else:
                        ssl = str(url_json["check_ssl"]).lower() == "true"
                        value = ssl

                    if url_json["api_port"] is None:
                        agent_dict = append_keys(agent_dict, Sections.SERVER)
                        for type_ports in ["api_port", "websocket_port"]:
                            value_port, _ = ask_value(agent_dict, type_ports,
                                                      section, ssl, type_ports)
                            config.instance.set(section, type_ports,
                                                str(value_port))
                            agent_dict[Sections.SERVER].pop(type_ports, None)

                    else:
                        config.instance.set(section, "api_port",
                                            str(url_json["api_port"]))
                        config.instance.set(section, "websocket_port",
                                            str(url_json["websocket_port"]))

                else:
                    value, _ = ask_value(agent_dict, opt, section, ssl)
                config.instance.set(section, opt, str(value))
def process_agent():
    agent_dict = {
        Sections.SERVER: {
            "host": {
                "default_value": lambda _: "127.0.0.1",
                "type": click.STRING,
            },
            "ssl": {
                "default_value": lambda _: True,
                "type": click.BOOL,
            },
            "ssl_ignore": {
                "default_value": lambda _: False,
                "type": click.BOOL,
            },
            "workspaces": {
                "default_value": lambda _: "workspace",
                "type": click.STRING,
            },
        },
        Sections.TOKENS: {
            "agent": {},
        },
        Sections.AGENT: {
            "agent_name": {
                "default_value": lambda _: "agent",
                "type": click.STRING,
            }
        },
    }

    ssl = True

    for section in agent_dict:
        if Sections.TOKENS == section and (section not in config.instance
                                           or "agent"
                                           not in config.instance[section]):
            continue
        print(f"{Bcolors.OKBLUE}Section: {section}{Bcolors.ENDC}")
        for opt in agent_dict[section]:
            if section not in config.instance:
                config.instance[section] = dict()
            if section == Sections.TOKENS and opt == "agent":
                if "agent" in config.instance[section] and confirm_prompt(
                        "Delete agent token?", default=None):
                    config.instance[section].pop(opt)
            elif opt == "workspaces":
                process_workspaces()
            else:
                if opt == "host":
                    value, url_json = ask_value(agent_dict, opt, section, ssl)
                    if url_json["url_path"]:
                        config.instance[section]["base_route"] = str(
                            url_json["url_path"])
                elif opt == "ssl":
                    old_ssl_value = config.instance[section].get("ssl", None)
                    if url_json["check_ssl"] is None:
                        value, _ = ask_value(agent_dict, opt, section, ssl)
                        ssl = value
                    else:
                        ssl = str(url_json["check_ssl"]).lower() == "true"
                        value = ssl

                    if old_ssl_value != value:
                        config.instance[section].pop("api_port", None)
                        config.instance[section].pop("websocket_port", None)

                    if url_json["api_port"] is None:
                        agent_dict = append_keys(agent_dict, Sections.SERVER)
                        for type_ports in ["api_port", "websocket_port"]:
                            value_port, _ = ask_value(agent_dict, type_ports,
                                                      section, ssl, type_ports)
                            config.instance[section][type_ports] = value_port
                            agent_dict[Sections.SERVER].pop(type_ports, None)

                    else:
                        config.instance[section]["api_port"] = url_json[
                            "api_port"]
                        config.instance[section]["websocket_port"] = url_json[
                            "websocket_port"]
                elif opt == "ssl_ignore" and not ssl:
                    continue
                else:
                    value, _ = ask_value(agent_dict, opt, section, ssl)
                config.instance[section][opt] = value