Exemplo n.º 1
0
    def prepare(self, npm_deps, compiled_static_folder):
        ctxt = docker_context()
        harpoon_options = {
              "docker_context": ctxt
            , "no_intervention": True
            , "docker_context_maker": docker_context
            }
        self.harpoon = HarpoonSpec().harpoon_spec.normalise(Meta({}, []), harpoon_options)

        config_root = pkg_resources.resource_filename(__package__, "")

        deps = self.default_npm_deps()
        deps.update(npm_deps)

        image_name = "dashmat-jsx-builder"
        everything = MergedOptions(dont_prefix=[dictobj])
        everything.update(
            { "harpoon": self.harpoon
            , "config_root": config_root
            , "images":
              { image_name:
                { "harpoon": self.harpoon
                , "configuration": everything
                , "volumes":
                  { "mount":
                    [ [ compiled_static_folder, "/compiled" ]
                    ]
                  }
                , "context":
                  { "enabled" : False
                  }
                , "persistence":
                  { "action": "npm install && npm dedup"
                  , "folders": ["/project/node_modules", "/usr/lib/node_modules"]
                  }
                , "commands":
                  [ "FROM gliderlabs/alpine:3.2"

                  , "RUN apk-install bash nodejs"
                  , "RUN npm install -g npm"

                  , "RUN mkdir /project"
                  , "WORKDIR /project"
                  , [ "ADD"
                    , { "dest": "/project/package.json"
                      , "content": json.dumps(
                          { "name": "dashmat"
                          , "version": "0.1.0"
                          , "dependencies": collections.OrderedDict(sorted(deps.items()))
                          }
                        , sort_keys=True
                        )
                      , "mtime": mtime
                      }
                    ]
                  ]
                }
              }
            }
          )

        def convert_image(path, val):
            meta = Meta(everything, [(part, "") for part in path.path])
            return HarpoonSpec().image_spec.normalise(meta, val)
        everything.add_converter(Converter(convert=convert_image, convert_path=["images", image_name]))
        everything.converters.activate()

        self.image = everything[["images", image_name]]
        Builder().make_image(self.image, {self.image.name: self.image})
Exemplo n.º 2
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.º 3
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