예제 #1
0
파일: cfgm.py 프로젝트: tokyodevs/dcos
    def _process_pkgconf_srcfile(self, src_fpath, tmp_dpath, context=None):
        """Process DC/OS package configuration source file.

        :param src_fpath: pathlib.Path, path to a source configuration file
        :param tmp_dpath: pathlib.Path, path to a temporary directory to save
                          intermediate rendered content
        :param context:   ResourceContext, rendering context data object
        """
        assert isinstance(src_fpath, Path) and src_fpath.is_absolute(), (
            f'Argument: src_fpath: Absolute pathlib.Path is required:'
            f' {src_fpath}')
        assert isinstance(tmp_dpath, Path) and tmp_dpath.is_absolute(), (
            f'Argument: tmp_dpath: Absolute pathlib.Path is required:'
            f' {tmp_dpath}')

        if '.j2' in src_fpath.suffixes[-1:]:
            dst_fname = src_fpath.stem
            json_ready = '.json' in src_fpath.suffixes[-2:-1]
        else:
            dst_fname = src_fpath.name
            json_ready = '.json' in src_fpath.suffixes[-1:]

        if context is None:
            context_items = {}
        else:
            assert isinstance(context, ResourceContext), (
                f'Argument: context:'
                f' Got {type(context).__name__} instead of ResourceContext')

            context_items = context.get_items(json_ready=json_ready)

        try:
            j2_env = j2.Environment(
                loader=j2.FileSystemLoader(str(src_fpath.parent)))
            j2_tmpl = j2_env.get_template(str(src_fpath.name))
            rendered_str = j2_tmpl.render(**context_items)
            LOG.debug(f'{self.msg_src}: Process configuration file:'
                      f' {src_fpath}: Rendered content: {rendered_str}')

            dst_fpath = tmp_dpath.joinpath(dst_fname)
            dst_fpath.write_text(rendered_str, encoding='utf-8')
            LOG.debug(f'{self.msg_src}: Process configuration file:'
                      f' {src_fpath}: Save: {dst_fpath}')
        except (FileNotFoundError, j2.TemplateNotFound) as e:
            err_msg = f'Load: {src_fpath}'
            raise cfgm_exc.PkgConfFileNotFoundError(err_msg) from e
        except (OSError, RuntimeError) as e:
            err_msg = f'Load: {src_fpath}: {type(e).__name__}: {e}'
            raise cfgm_exc.PkgConfError(err_msg) from e
        except j2.TemplateError as e:
            err_msg = f'Load: {src_fpath}: {type(e).__name__}: {e}'
            raise cfgm_exc.PkgConfFileInvalidError(err_msg) from e
예제 #2
0
    def _process_pkgconf_srcfile(self,
                                 src_fpath: Path,
                                 tmp_dpath: Path,
                                 context: ResourceContext = None):
        """Process DC/OS package configuration source file.

        :param src_fpath: Path, path to a source configuration file
        :param tmp_dpath: Path, path to a temporary directory to save
                          intermediate rendered content
        :param context:   ResourceContext, rendering context data object
        """
        if '.j2' in src_fpath.suffixes[-1:]:
            dst_fname = src_fpath.stem
            json_ready = '.json' in src_fpath.suffixes[-2:-1]
        else:
            dst_fname = src_fpath.name
            json_ready = '.json' in src_fpath.suffixes[-1:]

        try:
            j2_env = j2.Environment(
                loader=j2.FileSystemLoader(str(src_fpath.parent)))
            j2_tmpl = j2_env.get_template(str(src_fpath.name))
            context_items = {} if context is None else context.get_items(
                json_ready=json_ready)
            rendered_str = j2_tmpl.render(**context_items)
            dst_fpath = tmp_dpath.joinpath(dst_fname)
            LOG.debug('Render file %s -> %s', src_fpath, dst_fpath)
            dst_fpath.write_text(rendered_str, encoding='utf-8')
        except (FileNotFoundError, j2.TemplateNotFound) as e:
            err_msg = f'Load: {src_fpath}'
            raise cfgm_exc.PkgConfFileNotFoundError(err_msg) from e
        except (OSError, RuntimeError) as e:
            err_msg = f'Load: {src_fpath}: {type(e).__name__}: {e}'
            raise cfgm_exc.PkgConfError(err_msg) from e
        except j2.TemplateError as e:
            err_msg = f'Load: {src_fpath}: {type(e).__name__}: {e}'
            raise cfgm_exc.PkgConfFileInvalidError(err_msg) from e
예제 #3
0
파일: command.py 프로젝트: tokyodevs/dcos
    def _deploy_dcos_conf(self):
        """Deploy aggregated DC/OS configuration object."""
        LOG.debug(f'{self.msg_src}: Execute: Deploy aggregated config: ...')

        context = ResourceContext(
            istor_nodes=self.config.inst_storage.istor_nodes,
            cluster_conf=self.config.cluster_conf,
            extra_values=self.config.dcos_conf.get('values'))
        context_items = context.get_items()
        context_items_jr = context.get_items(json_ready=True)

        t_elements = self.config.dcos_conf.get('template').get('package', [])
        for t_element in t_elements:
            path = t_element.get('path')
            content = t_element.get('content')

            try:
                j2t = j2.Environment().from_string(path)
                rendered_path = j2t.render(**context_items)
                dst_fpath = Path(rendered_path)
                j2t = j2.Environment().from_string(content)
                if '.json' in dst_fpath.suffixes[-1:]:
                    rendered_content = j2t.render(**context_items_jr)
                else:
                    rendered_content = j2t.render(**context_items)
            except j2.TemplateError as e:
                err_msg = (f'Execute: Deploy aggregated config: Render:'
                           f' {path}: {type(e).__name__}: {e}')
                raise cfgm_exc.PkgConfFileInvalidError(err_msg) from e

            if not dst_fpath.parent.exists():
                try:
                    dst_fpath.parent.mkdir(parents=True, exist_ok=True)
                    LOG.debug(f'{self.msg_src}: Execute: Deploy aggregated'
                              f' config: Create directory:'
                              f' {dst_fpath.parent}: OK')
                except (OSError, RuntimeError) as e:
                    err_msg = (f'Execute: Deploy aggregated config: Create'
                               f' directory: {dst_fpath.parent}:'
                               f' {type(e).__name__}: {e}')
                    raise cr_exc.SetupCommandError(err_msg) from e
            elif not dst_fpath.parent.is_dir():
                err_msg = (f'Execute: Deploy aggregated config: Save content:'
                           f' {dst_fpath}: Existing parent is not a directory:'
                           f' {dst_fpath.parent}')
                raise cr_exc.SetupCommandError(err_msg)
            elif dst_fpath.exists():
                err_msg = (f'Execute: Deploy aggregated config: Save content:'
                           f' {dst_fpath}: Same-named file already exists!')
                raise cr_exc.SetupCommandError(err_msg)

            try:
                dst_fpath.write_text(rendered_content)
                LOG.debug(f'{self.msg_src}: Execute: Deploy aggregated config:'
                          f' Save content: {dst_fpath}: OK')
            except (OSError, RuntimeError) as e:
                err_msg = (f'Execute: Deploy aggregated config: Save content:'
                           f' {dst_fpath}: {type(e).__name__}: {e}')
                raise cr_exc.SetupCommandError(err_msg) from e

        LOG.debug(f'{self.msg_src}: Execute: Deploy aggregated config: OK')