Exemplo n.º 1
0
        def convert_passwords(path, val):
            log.info("Converting %s", path)
            password = str(path)[len("passwords."):]
            configuration.converters.started(path)
            environment = configuration['bespin'].environment

            val_as_dict = configuration["passwords"][password].as_dict()
            if not environment:
                raise BespinError("No environment was provided",
                                  available=list(
                                      configuration["environments"].keys()))

            password_environment_as_dict = {}
            if ["passwords", password, environment] in configuration:
                password_environment_as_dict = configuration[[
                    "passwords", password, environment
                ]].as_dict()

            base = MergedOptions(dont_prefix=path.configuration.dont_prefix,
                                 converters=path.configuration.converters)
            everything = path.configuration.root().wrapped()

            base.update(val_as_dict)
            everything[path] = val_as_dict

            base.update(password_environment_as_dict)
            everything[path].update(password_environment_as_dict)

            for thing in (base, everything):
                thing["__password__"] = val
                thing["__environment__"] = configuration["environments"][
                    environment]

            meta = Meta(everything, [("passwords", ""), (password, "")])
            return bespin_spec.password_spec.normalise(meta, base)
Exemplo n.º 2
0
    def extra_prepare(self, configuration, args_dict):
        """Called before the configuration.converters are activated"""
        harpoon = MergedOptions.using(
            configuration.get('harpoon', MergedOptions()).as_dict(),
            dict(args_dict.get("harpoon", MergedOptions()).items())).as_dict()

        # Args_dict may itself be a MergedOptions
        while "harpoon" in args_dict:
            del args_dict["harpoon"]

        # Create the addon getter and register the crosshair namespace
        self.addon_getter = AddonGetter()
        self.addon_getter.add_namespace("harpoon.crosshairs",
                                        Result.FieldSpec(), Addon.FieldSpec())

        # Initiate the addons from our configuration
        self.register = Register(self.addon_getter, self)
        if ("addons" in harpoon) and (
                type(harpoon["addons"]) in (MergedOptions, dict)
                or getattr(harpoon["addons"], "is_dict", False)):
            for namespace, adns in sb.dictof(
                    sb.string_spec(), sb.listof(sb.string_spec())).normalise(
                        Meta(harpoon, []).at("addons"),
                        harpoon["addons"]).items():
                self.register.add_pairs(*[(namespace, adn) for adn in adns])

        # Import our addons
        self.register.recursive_import_known()

        # Resolve our addons
        self.register.recursive_resolve_imported()

        # Make sure images is started
        if "images" not in self.configuration:
            self.configuration["images"] = {}

        # Add our special stuff to the configuration
        self.configuration.update(
            {
                "$@": harpoon.get("extra", ""),
                "bash": args_dict["bash"] or NotSpecified,
                "harpoon": harpoon,
                "command": args_dict['command'] or NotSpecified,
                "assume_role": args_dict["assume_role"] or NotSpecified
            },
            source="<args_dict>")
Exemplo n.º 3
0
    def extra_prepare(self, configuration, args_dict):
        """Called before the configuration.converters are activated"""
        bespin = dict(args_dict.get("bespin", MergedOptions()).items())

        while "bespin" in args_dict:
            del args_dict["bespin"]

        environment = bespin.get("environment")

        bespin["configuration"] = configuration
        self.configuration.update(
            {
                "$@": bespin["extra"],
                "bespin": bespin,
                "command": args_dict['command'],
                "environment": environment
            },
            source="<args_dict>")
Exemplo n.º 4
0
 def start_configuration(self):
     """Create the base of the configuration"""
     return MergedOptions(dont_prefix=[dictobj])
Exemplo n.º 5
0
    def collect_configuration(self, configuration_file):
        """Return us a MergedOptions with this configuration and any collected configurations"""
        errors = []

        result = self.read_yaml(configuration_file)
        configuration_dir = os.path.dirname(
            os.path.abspath(configuration_file))

        images_from = []
        images_from_path = None
        if "images" in result and "__images_from__" in result["images"]:
            images_from_path = result["images"]["__images_from__"]

            if not images_from_path.startswith("/"):
                images_from_path = os.path.join(configuration_dir,
                                                images_from_path)

            if not os.path.exists(images_from_path) or not os.path.isdir(
                    images_from_path):
                raise BadConfiguration(
                    "Specified folder for other configuration files points to a folder that doesn't exist",
                    path="images.__images_from__",
                    value=images_from_path)

            images_from = sorted(
                chain.from_iterable([[
                    os.path.join(root, fle) for fle in files
                    if fle.endswith(".yml") or fle.endswith(".yaml")
                ] for root, dirs, files in os.walk(images_from_path)]))

        harpoon_spec = HarpoonSpec()
        configuration = MergedOptions(dont_prefix=[dictobj])

        home_dir_configuration = self.home_dir_configuration_location()
        sources = [home_dir_configuration, configuration_file] + images_from

        def make_mtime_func(source):
            """Lazily calculate the mtime to avoid wasted computation"""
            return lambda: self.get_committime_or_mtime(source)

        for source in sources:
            if source is None or not os.path.exists(source):
                continue

            try:
                result = self.read_yaml(source)
            except BadYaml as error:
                errors.append(error)
                continue

            if "images" in result and "__images_from__" in result["images"]:
                del result["images"]["__images_from__"]

            if source in images_from:
                result = {
                    "images": {
                        os.path.splitext(os.path.basename(source))[0]: result
                    }
                }

            result["mtime"] = make_mtime_func(source)

            if "images" in result:
                images = result.pop("images")
                images = dict(
                    (image,
                     MergedOptions.using(configuration.root(),
                                         val,
                                         converters=configuration.converters,
                                         source=source))
                    for image, val in images.items())
                result["images"] = images

            configuration.update(result, dont_prefix=[dictobj], source=source)

            for image in result.get('images', {}).keys():
                self.make_image_converters(image, configuration, harpoon_spec)

        def convert_harpoon(path, val):
            log.info("Converting %s", path)
            meta = Meta(path.configuration, [("harpoon", "")])
            configuration.converters.started(path)
            return harpoon_spec.harpoon_spec.normalise(meta, val)

        harpoon_converter = Converter(convert=convert_harpoon,
                                      convert_path=["harpoon"])
        configuration.add_converter(harpoon_converter)

        if errors:
            raise BadConfiguration("Some of the configuration was broken",
                                   _errors=errors)

        return configuration
Exemplo n.º 6
0
            with mock.patch.multiple(collector
                , find_photons_app_options = find_photons_app_options
                , determine_mainline_module = determine_mainline_module
                , setup_addon_register = setup_addon_register
                ):
                yield __main__

            find_photons_app_options.assert_called_once_with(configuration, args_dict)
            determine_mainline_module.assert_called_once_with()
            setup_addon_register.assert_called_once_with(photons_app, __main__)

        it "puts things into the configuration and sets up the addon register":
            extra = str(uuid.uuid1())
            photons_app = {"extra": extra}
            configuration = MergedOptions()
            collector = Collector()
            register = mock.Mock(name="register")
            args_dict = mock.Mock(name="args_dict")

            with self.mocks(collector, configuration, args_dict, photons_app, register):
                collector.extra_prepare(configuration, args_dict)

            class AFuture:
                def __eq__(self, other):
                    return isinstance(other, asyncio.Future)

            self.assertIs(collector.register, register)
            self.assertEqual(configuration.as_dict()
                , { "$@": extra
                  , "collector": collector