Esempio n. 1
0
    def func_with_context(name, config, environment, system_folders, *args,
                          **kwargs):

        # select configuration if none supplied
        if config:
            ctx = ServerContext.from_external_config_file(
                config, environment, system_folders)
        else:
            if name:
                name, environment = (name, environment)
            else:
                try:
                    name, environment = select_configuration_questionaire(
                        "server", system_folders)
                except Exception:
                    error("No configurations could be found!")
                    exit(1)

            # raise error if config could not be found
            if not ServerContext.config_exists(name, environment,
                                               system_folders):
                scope = "system" if system_folders else "user"
                error(
                    f"Configuration {Fore.RED}{name}{Style.RESET_ALL} with "
                    f"{Fore.RED}{environment}{Style.RESET_ALL} does not exist "
                    f"in the {Fore.RED}{scope}{Style.RESET_ALL} folders!")
                exit(1)

            # create server context, and initialize db
            ServerContext.LOGGING_ENABLED = False
            ctx = ServerContext(name,
                                environment=environment,
                                system_folders=system_folders)

        return func(ctx, *args, **kwargs)
Esempio n. 2
0
    def func_with_context(name, config, environment, system_folders,
                          *args, **kwargs):

        # select configuration if none supplied
        if config:
            ctx = ServerContext.from_external_config_file(
                config,
                environment,
                system_folders
            )
        else:
            if name:
                name, environment = (name, environment)
            else:
                try:
                    name, environment = select_configuration_questionaire(
                        system_folders
                    )
                except Exception:
                    error("No configurations could be found!")
                    exit()

            # raise error if config could not be found
            if not ServerContext.config_exists(
                name,
                environment,
                system_folders
            ):
                scope = "system" if system_folders else "user"
                error(
                    f"Configuration {Fore.RED}{name}{Style.RESET_ALL} with "
                    f"{Fore.RED}{environment}{Style.RESET_ALL} does not exist "
                    f"in the {Fore.RED}{scope}{Style.RESET_ALL} folders!"
                )
                exit(1)

            # create server context, and initialize db
            ctx = ServerContext(
                name,
                environment=environment,
                system_folders=system_folders
            )

        # initialize database (singleton)
        allow_drop_all = ctx.config["allow_drop_all"]
        Database().connect(URI=ctx.get_database_uri(), allow_drop_all=allow_drop_all)

        return func(ctx, *args, **kwargs)
Esempio n. 3
0
def cli_server_new(name, environment, system_folders):
    """Create new configuration."""

    if not name:
        name = q.text("Please enter a configuration-name:").ask()
        name_new = name.replace(" ", "-")
        if name != name_new:
            info(f"Replaced spaces from configuration name: {name}")
            name = name_new

    # Check that we can write in this folder
    if not check_config_write_permissions(system_folders):
        error("Your user does not have write access to all folders. Exiting")
        exit(1)

    # check that this config does not exist
    try:
        if ServerContext.config_exists(name, environment, system_folders):
            error(f"Configuration {Fore.RED}{name}{Style.RESET_ALL} with "
                  f"environment {Fore.RED}{environment}{Style.RESET_ALL} "
                  f"already exists!")
            exit(1)
    except Exception as e:
        print(e)
        exit(1)

    # create config in ctx location
    cfg_file = configuration_wizard(name, environment, system_folders)
    info(f"New configuration created: {Fore.GREEN}{cfg_file}{Style.RESET_ALL}")

    # info(f"root user created.")
    info(f"You can start the server by "
         f"{Fore.GREEN}vserver start{Style.RESET_ALL}.")
def configuration_wizard(instance_name, environment, system_folders):

    # for defaults and where to save the config
    dirs = ServerContext.instance_folders("server", instance_name,
                                          system_folders)

    # prompt questionaire
    config = server_configuration_questionaire(dirs, instance_name)

    # in the case of an environment we need to add it to the current
    # configuration. In the case of application we can simply overwrite this
    # key (although there might be environments present)
    config_file = Path(dirs.get("config")) / (instance_name + ".yaml")

    # check if configuration already exists
    if Path(config_file).exists():
        config_manager = ServerConfigurationManager.from_file(config_file)
    else:
        config_manager = ServerConfigurationManager(instance_name)

    # save the new comfiguration
    config_manager.put(environment, config)
    config_manager.save(config_file)

    return config_file
def select_configuration_questionaire(system_folders):
    """Asks which configuration the user want to use

    It shows only configurations that are in the default folder.
    """
    configs, f = ServerContext.available_configurations(system_folders)

    # each collection (file) can contain multiple configs. (e.g. test,
    # dev)
    choices = []
    for config_collection in configs:
        envs = config_collection.available_environments
        for env in envs:
            choices.append(q.Choice(
                title=f"{config_collection.name:25} {env}",
                value=(config_collection.name, env)))

    if not choices:
        raise Exception("No configurations could be found!")

    # pop the question
    name, env = q.select("Select the configuration you want to use:",
                         choices=choices).ask()

    return name, env
Esempio n. 6
0
def cli_server_configuration_list():
    """Print the available configurations."""

    client = docker.from_env()
    check_if_docker_deamon_is_running(client)

    running_server = client.containers.list(
        filters={"label": f"{APPNAME}-type=server"})
    running_node_names = []
    for node in running_server:
        running_node_names.append(node.name)

    header = \
        "\nName"+(21*" ") + \
        "Environments"+(20*" ") + \
        "Status"+(10*" ") + \
        "System/User"

    click.echo(header)
    click.echo("-" * len(header))

    running = Fore.GREEN + "Online" + Style.RESET_ALL
    stopped = Fore.RED + "Offline" + Style.RESET_ALL

    # system folders
    configs, f1 = ServerContext.available_configurations(system_folders=True)
    for config in configs:
        status = running if f"{APPNAME}-{config.name}-system-server" in \
            running_node_names else stopped
        click.echo(f"{config.name:25}"
                   f"{str(config.available_environments):32}"
                   f"{status:25} System ")

    # user folders
    configs, f2 = ServerContext.available_configurations(system_folders=False)
    for config in configs:
        status = running if f"{APPNAME}-{config.name}-user-server" in \
            running_node_names else stopped
        click.echo(f"{config.name:25}"
                   f"{str(config.available_environments):32}"
                   f"{status:25} User   ")

    click.echo("-" * 85)
    if len(f1) + len(f2):
        warning(
            f"{Fore.RED}Failed imports: {len(f1)+len(f2)}{Style.RESET_ALL}")
Esempio n. 7
0
def cli_server_configuration_list():
    """Print the available configurations."""

    click.echo("\nName"+(21*" ")+"Environments"+(21*" ")+"System/User")
    click.echo("-"*70)

    sys_configs, f1 = ServerContext.available_configurations(
        system_folders=True)
    for config in sys_configs:
        click.echo(f"{config.name:25}{str(config.available_environments):32} System ")

    usr_configs, f2 = ServerContext.available_configurations(system_folders=False)
    for config in usr_configs:
        click.echo(f"{config.name:25}{str(config.available_environments):32} User   ")
    click.echo("-"*70)

    if len(f1)+len(f2):
        warning(
            f"{Fore.YELLOW}Number of failed imports: "
            f"{len(f1)+len(f2)}{Style.RESET_ALL}"
        )