Beispiel #1
0
def test_set_variable_empty(tmpdir):
    """When we set a variable with a no value, we expect the variable to have no value in our yml file"""
    set_config_file = Path(tmpdir, "test_app_set_variable_empty.yml")
    set_config_file.touch()
    var_manager = VariablesManager(set_config_file)
    variable_path = "empty"
    var_manager.set_variable(variable_path, None)
    compare_file = Path(
        Path(__file__).parent,
        "variables_manager_resources/test_app_empty_value.yml",
    )
    assert filecmp.cmp(set_config_file, compare_file)
Beispiel #2
0
def test_set_int_variable(tmpdir):
    """When we set a variable of type int, we expect the varible to be of type int in our yml file"""
    set_config_file = Path(tmpdir, "test_app_set_int_variable.yml")
    set_config_file.touch()
    var_manager = VariablesManager(set_config_file)
    variable_path = "int"
    variable_value = 12345
    var_manager.set_variable(variable_path, variable_value)
    compare_file = Path(
        Path(__file__).parent,
        "variables_manager_resources/test_app_int_value.yml",
    )
    assert filecmp.cmp(set_config_file, compare_file)
Beispiel #3
0
def create_var_manager_from_resource(configFile) -> VariablesManager:
    """Creates a variables manager object from a file in our variables_manager_resources directory"""
    # directory containing this script
    return VariablesManager(
        Path(
            Path(__file__).parent,
            f"variables_manager_resources/{configFile}.yml"))
Beispiel #4
0
        def info(ctx):
            cli_context: CliContext = ctx.obj
            app_config_file = cli_context.get_app_configuration_file()
            variables_manager = VariablesManager(app_config_file)

            print()
            print("=== CLI CONTEXT ===")
            pprint(cli_context)

            print()
            print("=== CONFIGURATION ===")
            pprint(self.cli_configuration)

            print()
            print("=== ORCHESTRATOR CONFIGURATION ===")
            pprint(vars(self.cli_configuration.orchestrator))

            print()
            print("=== VARIABLES ===")
            pprint(variables_manager.get_all_variables())
Beispiel #5
0
        def info(ctx):
            cli_context: CliContext = ctx.obj
            cli_context.get_configuration_dir_state().verify_command_allowed(
                AppcliCommand.DEBUG_INFO)
            app_config_file = cli_context.get_app_configuration_file()
            variables_manager = VariablesManager(app_config_file)

            print()
            print("=== CLI CONTEXT ===")
            pprint(cli_context)

            print()
            print("=== CONFIGURATION ===")
            pprint(self.cli_configuration)

            print()
            print("=== ORCHESTRATOR CONFIGURATION ===")
            pprint(vars(self.cli_configuration.orchestrator))

            print()
            print("=== VARIABLES ===")
            pprint(variables_manager.get_all_variables())
Beispiel #6
0
def test_set_all_variables(tmpdir):
    """The dictionary used to set set_all_variables() should be equal to the dictionary returned from it"""
    set_config_file = Path(tmpdir, "test_app_set_all_variables.yml")
    set_config_file.touch()
    var_manager = VariablesManager(set_config_file)
    dictionary = {
        "object": {
            "booleans": {
                "false_boolean": False,
                "true_boolean": True,
            },
            "float": 12345.6789,
            "int": 12345,
            "string": "Value",
        },
    }
    var_manager.set_all_variables(dictionary)
    compare_file = Path(
        Path(__file__).parent,
        "variables_manager_resources/test_app_get_all_variables.yml",
    )
    assert filecmp.cmp(set_config_file, compare_file)
Beispiel #7
0
def test_set_bool_variable(tmpdir):
    """When we set a variable of type bool, we expect the varible to be of type bool in our yml file"""
    set_config_file = Path(tmpdir, "test_app_set_bool_variable.yml")
    set_config_file.touch()
    var_manager = VariablesManager(set_config_file)
    variable_path_false = "false_boolean"
    variable_path_true = "true_boolean"
    var_manager.set_variable(variable_path_false, False)
    var_manager.set_variable(variable_path_true, True)
    compare_file = Path(
        Path(__file__).parent,
        "variables_manager_resources/test_app_bool_value.yml",
    )
    assert filecmp.cmp(set_config_file, compare_file)
    def migrate_configuration(self):
        """Migrates the configuration version to the current application version"""

        if self.config_repo.is_repo_on_master_branch():
            error_and_exit("Cannot migrate, repo is on master branch.")

        config_version: str = self.config_repo.get_repository_version()
        app_version: str = self.cli_context.app_version

        # If the configuration version matches the application version, no migration is required.
        if config_version == app_version:
            logger.info(
                f"Migration not required. Config version [{config_version}] matches application version [{app_version}]"
            )
            return

        logger.info(
            f"Migrating configuration version [{config_version}] to match application version [{app_version}]"
        )

        app_version_branch: str = self.config_repo.generate_branch_name(app_version)
        if self.config_repo.does_branch_exist(app_version_branch):
            # If the branch already exists, then this version has previously been installed.

            logger.warning(
                f"Version [{app_version}] of this application was previously installed. Rolling back to previous configuration. Manual remediation may be required."
            )

            # Switch to that branch, no further migration steps will be taken. This is effectively a roll-back.
            self.config_repo.checkout_existing_branch(app_version_branch)
            return

        # Get the stack-settings file contents if it exists
        stack_config_file = self.cli_context.get_stack_configuration_file()
        stack_settings_exists_pre_migration = stack_config_file.is_file()
        current_stack_settings_variables = (
            stack_config_file.read_text()
            if stack_settings_exists_pre_migration
            else None
        )

        # Migrate the current configuration variables
        current_variables = self.__get_variables_manager().get_all_variables()

        # Compare migrated config to the 'clean config' of the new version, and make sure all variables have been set and are the same type.
        clean_new_version_variables = VariablesManager(
            self.cli_configuration.seed_app_configuration_file
        ).get_all_variables()

        migrated_variables = self.cli_configuration.hooks.migrate_variables(
            self.cli_context,
            current_variables,
            config_version,
            clean_new_version_variables,
        )

        if not self.cli_configuration.hooks.is_valid_variables(
            self.cli_context, migrated_variables, clean_new_version_variables
        ):
            error_and_exit(
                "Migrated variables did not pass application-specific variables validation function."
            )

        baseline_template_overrides_dir = (
            self.cli_context.get_baseline_template_overrides_dir()
        )
        override_backup_dir: Path = self.__backup_directory(
            baseline_template_overrides_dir
        )

        configurable_templates_dir = self.cli_context.get_configurable_templates_dir()
        configurable_templates_backup_dir = self.__backup_directory(
            configurable_templates_dir
        )

        # Backup and remove the existing generated config dir since it's now out of date
        self.__backup_and_create_new_generated_config_dir(config_version)

        # Initialise the new configuration branch and directory with all new files
        self.__create_new_configuration_branch_and_files()

        self.__overwrite_directory(override_backup_dir, baseline_template_overrides_dir)
        if self.__directory_is_not_empty(baseline_template_overrides_dir):
            logger.warning(
                f"Overrides directory [{baseline_template_overrides_dir}] is non-empty, please check for compatibility of overridden files"
            )

        self.__overwrite_directory(
            configurable_templates_backup_dir, configurable_templates_dir
        )
        if self.__directory_is_not_empty(configurable_templates_dir):
            logger.warning(
                f"Configurable templates directory [{configurable_templates_dir}] is non-empty, please check for compatibility"
            )

        # Write out 'migrated' variables file
        self.__get_variables_manager().set_all_variables(migrated_variables)

        # If stack settings existed pre-migration, then replace the default with the existing settings
        if stack_settings_exists_pre_migration:
            logger.warning(
                "Stack settings file was copied directly from previous version, please check for compatibility"
            )
            stack_config_file.write_text(current_stack_settings_variables)

        # Commit the new variables file
        self.config_repo.commit_changes(
            f"Migrated variables file from version [{config_version}] to version [{app_version}]"
        )
 def __get_stack_variables_manager(self):
     return VariablesManager(self.cli_context.get_stack_configuration_file())
 def __get_variables_manager(self):
     """Get the variables manager for the current configuration"""
     return VariablesManager(self.cli_context.get_app_configuration_file())