Exemplo n.º 1
0
    def get_composition_path(self, nix, cloud_type, raw_config, custom):
        # Use the default local repo (not versioned).
        path = os.path.join(
            self.kompos_config.terraform_local_path(),
            self.kompos_config.terraform_root_path(),
            cloud_type,
        )

        # Overwrite with the nix output, if the nix integration is enabled.
        if nix:
            pname = self.kompos_config.terraform_repo_name()

            nix_install(
                pname,
                self.kompos_config.terraform_repo_url(),
                get_value_or(raw_config, 'infrastructure/terraform/version',
                             'master'),
                get_value_or(raw_config, 'infrastructure/terraform/sha256'),
            )

            # Nix store is read-only, and terraform doesn't work properly outside
            # of the module directory, so as a workaround we're using a temporary directory
            # with the contents of the derivation so terraform can create new files.
            # See: https://github.com/hashicorp/terraform/issues/18030
            path = os.path.join(
                writeable_nix_out_path(pname),
                self.kompos_config.terraform_root_path(),
                cloud_type,
            )

        if custom:
            path = os.path.join(path, "custom")

        return path
Exemplo n.º 2
0
    def run(self, args, extra_args):
        if not os.path.isdir(self.cluster_config_path):
            raise Exception("Provide a valid composition directory path.")

        composition_order = self.kompos_config.helmfile_composition_order()
        compositions = get_compositions(self.cluster_config_path,
                                        composition_order,
                                        path_type="composition",
                                        composition_type="helmfile",
                                        reverse=False)

        # We're assuming local path by default.
        helmfile_path = os.path.join(
            self.kompos_config.helmfile_local_path(),
            self.kompos_config.helmfile_root_path(),
        )

        # Overwrite if CLI flag is set.
        if args.helmfile_path:
            helmfile_path = args.helmfile_path

        # Overwrite if nix is enabled.
        if is_nix_enabled(args, self.kompos_config.nix()):
            pname = self.kompos_config.helmfile_repo_name()

            raw_config = self.generate_config(
                config_path=get_config_path(self.cluster_config_path,
                                            HELMFILE_COMPOSITION_NAME),
                filters=self.kompos_config.filtered_output_keys(
                    HELMFILE_COMPOSITION_NAME),
                exclude_keys=self.kompos_config.excluded_config_keys(
                    HELMFILE_COMPOSITION_NAME))

            nix_install(
                pname,
                self.kompos_config.helmfile_repo_url(),
                get_value_or(raw_config, 'infrastructure/helmfile/version',
                             'master'),
                get_value_or(raw_config, 'infrastructure/helmfile/sha256'),
            )

            # FIXME: Nix store is read-only, and helmfile configuration has a hardcoded path for
            # the generated config, so as a workaround we're using a temporary directory
            # with the contents of the derivation so helmfile can create the config file.

            helmfile_path = os.path.join(
                writeable_nix_out_path(pname),
                self.kompos_config.helmfile_root_path())

        self.setup_kube_config(
            self.generate_helmfile_config(
                get_config_path(self.cluster_config_path, compositions[0]),
                helmfile_path))

        return_code = self.execute(self.run_helmfile(helmfile_path,
                                                     extra_args))

        return return_code
Exemplo n.º 3
0
    def run(self, args, extra_args):
        compositions = CompositionSorter(
            self.kompos_config.helmfile_composition_order(
            )).get_sorted_compositions(self.cluster_config_path)

        if not compositions or compositions[0] != HELMFILE_COMPOSITION_NAME:
            raise Exception(
                "Please provide the full path to composition=%s".format(
                    HELMFILE_COMPOSITION_NAME))

        # We're assuming local path by default.
        helmfile_path = os.path.join(
            self.kompos_config.helmfile_local_path(),
            self.kompos_config.helmfile_root_path(),
        )

        # Overwrite if CLI flag is set.
        if args.helmfile_path:
            helmfile_path = args.helmfile_path

        # Overwrite if nix is enabled.
        if args.nix:
            pname = self.kompos_config.helmfile_repo_name()

            raw_config = self.generate_config(
                config_path=get_config_path(self.cluster_config_path,
                                            'helmfile'),
                filters=self.kompos_config.filtered_output_keys('helmfile'),
                exclude_keys=self.kompos_config.excluded_config_keys(
                    'helmfile'))

            nix_install(
                pname,
                self.kompos_config.helmfile_repo_url(),
                get_value_or(raw_config, 'infrastructure/helmfile/version',
                             'master'),
                get_value_or(raw_config, 'infrastructure/helmfile/sha256'),
            )

            # FIXME: Nix store is read-only, and helmfile configuration has a hardcoded path for
            # the generated config, so as a workaround we're using a temporary directory
            # with the contents of the derivation so helmfile can create the config file.

            helmfile_path = os.path.join(
                writeable_nix_out_path(pname),
                self.kompos_config.helmfile_root_path())

        self.setup_kube_config(
            self.generate_helmfile_config(
                get_config_path(self.cluster_config_path, compositions[0]),
                helmfile_path))

        return dict(
            command=self.get_helmfile_command(helmfile_path, extra_args))
Exemplo n.º 4
0
    def run_compositions(self, args, extra_args, config_path, compositions):
        return_code = 0

        for composition in compositions:
            logger.info("Running composition: %s", composition)

            filtered_output_keys = self.kompos_config.filtered_output_keys(
                composition)
            excluded_config_keys = self.kompos_config.excluded_config_keys(
                composition)
            tf_config_generator = TerraformConfigGenerator(
                excluded_config_keys, filtered_output_keys)
            pre_config_generator = PreConfigGenerator(excluded_config_keys,
                                                      filtered_output_keys)

            raw_config = pre_config_generator.pre_generate_config(
                config_path, composition)
            cloud_type = raw_config["cloud"]["type"]

            # Use the default local repo (not versioned).
            terraform_composition_path = os.path.join(
                self.kompos_config.terraform_local_path(),
                self.kompos_config.terraform_root_path(),
                cloud_type,
            )

            # Overwrite with the nix output, if the nix integration is enabled.
            if is_nix_enabled(args, self.kompos_config.nix()):
                pname = self.kompos_config.terraform_repo_name()

                nix_install(
                    pname,
                    self.kompos_config.terraform_repo_url(),
                    get_value_or(raw_config,
                                 'infrastructure/terraform/version', 'master'),
                    get_value_or(raw_config,
                                 'infrastructure/terraform/sha256'),
                )

                # Nix store is read-only, and terraform doesn't work properly outside
                # of the module directory, so as a workaround we're using a temporary directory
                # with the contents of the derivation so terraform can create new files.
                # See: https://github.com/hashicorp/terraform/issues/18030
                terraform_composition_path = os.path.join(
                    writeable_nix_out_path(pname),
                    self.kompos_config.terraform_root_path(),
                    cloud_type,
                )

            parser = ConfigRunner.get_parser(argparse.ArgumentParser())

            if args.himl_args:
                himl_args = parser.parse_args(args.himl_args.split())
                logger.info("Extra himl arguments: %s", args.himl_args.split())
            else:
                himl_args = parser.parse_args([])

            tf_config_generator.generate_files(himl_args, config_path,
                                               terraform_composition_path,
                                               composition, raw_config)

            return_code = self.execute(
                self.run_terraform(args, extra_args,
                                   terraform_composition_path, composition,
                                   raw_config))

            if return_code != 0:
                logger.error(
                    "Command finished with nonzero exit code for composition '%s'."
                    "Will skip remaining compositions.", composition)
                return return_code

        return return_code