Example #1
0
    def format(self, msg, force_pretty=False, tag=None):

        if not self.pretty and not force_pretty:
            return msg
        elif isinstance(msg, (list, tuple)):
            return assert_json(msg, indent=4)
        elif hasattr(msg, 'pretty'):
            msg = getattr(msg, 'pretty')()

        if isinstance(msg, dict):
            clean_dyct = self.cleaner(msg)
            kaptan = Kaptan().import_config(clean_dyct)
            yaml = kaptan.export(handler=LoggerConst.PRETTY_FMT,
                                 explicit_start=True)
            return order_yaml(yaml)

        msg = assert_str(msg, allow_none=True)

        if tag is None:
            return msg
        else:
            return '{ts} - {tag} - {msg}'.format(ts=datetime.now().strftime(
                DateFmt.READABLE),
                                                 tag=tag,
                                                 msg=msg)
Example #2
0
def config():
    logger.debug('Loading configuration')
    from kaptan import Kaptan
    cfg = Kaptan()
    cfg.import_config({
        'authn': {
            'cookie': {
                'secure': False,
                'session_name': 'sid',
                'id_name': 'uid',
                'max_age': 3600
            },
            'oidc': {
                'provider_protocol': 'http',
            }
        },
        'http_server': {
            'endpoints': {
                'login': {
                    'path': '/auth/login'
                },
                'registration': {
                    'path': '/auth/registration'
                }
            }
        },
        'oidc': {
            'iss': 'https://localhost:8889',
        },
    })
    return cfg
Example #3
0
def _load_config(cfg_path):
    """Load config from a given config file.

    Raises `ValidationError` if the config does not define a session name"""
    config = Kaptan()
    config.import_config(str(cfg_path))
    config = tmuxp.config.expand(config.configuration_data)

    if "session_name" not in config:
        raise ValidationError("No session name configured")

    return config
Example #4
0
class Config:
    def __init__(self):
        import os
        script_path = os.path.dirname(os.path.abspath(__file__))
        self.path_to_config = script_path + "/config.json"
        self.config = Kaptan(handler="json")

    # def write_config(self):
    #     f = open(self.path_to_config,'w')
    #     f.write(self.config.export("json"))
    #     f.close()

    def read_config(self):
        f = open(self.path_to_config, 'r')
        self.config.import_config(f.read())
        f.close()

    def __getitem__(self, item):
        self.read_config()
        return self.config.get(item)

    # overrides the . operator.
    def __getattr__(self, item):
        return self.__getitem__(item)
Example #5
0
    def load(self):

        default_conf, user_conf, local_conf = [
            None if not os.path.exists(src) else Kaptan(
                handler=Config.FMT).import_config(src) for src in self.sources
        ]

        assert default_conf is not None,\
            ConfigurationError("Default configuration not found at {}".format(self.sources[0]))

        conf_opts = [local_conf, user_conf,
                     {}]  # if local conf was found, user conf is ignored
        child_conf = filter(
            lambda x: x is not None,
            conf_opts).__next__()  # returns first non empty option

        super().__init__(**join_dicts(default_conf.get(self.namespace, {}),
                                      child_conf.get(self.namespace, {}),
                                      allow_overwrite=True))

        return self
Example #6
0
 def __init__(self, handler=None):
     self.__dict__ = self.__shared_state
     self._cpt = Kaptan(handler)
Example #7
0
class Config(object):
    """ This class has a shared state like a Borg.

    This is a Kaptan class that infers the file handler
    from the file extension using the `from_file` function.

    Parameters
    ----------
    handler: str or kaptan.BaseHandler
        This parameter is to keep compatibility with Kaptan's instantiation protocol.
        See its documentation on how to use it: http://emre.github.io/kaptan/
        I personally prefer to use the `from_file` function.
    """
    __shared_state = {}

    def __init__(self, handler=None):
        self.__dict__ = self.__shared_state
        self._cpt = Kaptan(handler)

    @classmethod
    def from_file(cls, file_path):
        """ Returns a Config instance with the data from
        `file_path`.

        Parameters
        ----------
        file_path: str
            Path to a configuration file.
            Its extension can be any from config.HANDLER_EXT, i.e.,
            {'conf': 'ini',
             'ini': 'ini',
             'json': 'json',
             'py': 'file',
             'yaml': 'yaml',
             'yml': 'yaml'}

        Returns
        -------
        cfg: Config
        """
        _check_file(file_path)
        cfg = Config()
        cfg._cpt = _load_config(file_path)
        return cfg

    def update_from_file(self, file_path):
        """ Updates the config parameters with the data from
        `file_path`.

        Parameters
        ----------
        file_path: str
            Path to a configuration file.
            Its extension can be any from config.HANDLER_EXT, i.e.,
            {'conf': 'ini',
             'ini': 'ini',
             'json': 'json',
             'py': 'file',
             'yaml': 'yaml',
             'yml': 'yaml'}

        Returns
        -------
        cfg: Config
        """
        _check_file(file_path)
        cpt = _load_config(file_path)

        params = cpt.configuration_data
        _update_kaptan(self._cpt, params)

    def check_file(self, item):
        """ This is a __getitem__ operator for file path values, if the file does not exist an IOError is raised."""
        try:
            fpath = self.__getitem__(item)
        except KeyError:
            raise
        else:
            if not op.exists(fpath):
                raise IOError(
                    'Could not find file for key {} in the {}.'.format(
                        item, fpath))
            return fpath

    def update(self, adict):
        for k, v in adict.items():
            self._cpt.upsert(k, v)

    def keys(self):
        return self._cpt.configuration_data.keys()

    def items(self):
        return self._cpt.configuration_data.items()

    def get(self, item, default=None):
        return self._cpt.configuration_data.get(item, default)

    def __getitem__(self, item):
        if item not in self._cpt.configuration_data:
            raise KeyError(
                'Could not find key {} in configuration content.'.format(item))
        return self._cpt.configuration_data[item]

    def __setitem__(self, key, value):
        return self._cpt.upsert(key, value)

    def __repr__(self):
        return '<config.Config> ({})'.format('\n'.join(
            [str(i) for i in self.items()]))
Example #8
0
def _load_config(file_path):
    cpt = Kaptan()
    return cpt.import_config(file_path)
Example #9
0
 def __init__(self):
     import os
     script_path = os.path.dirname(os.path.abspath(__file__))
     self.path_to_config = script_path + "/config.json"
     self.config = Kaptan(handler="json")
Example #10
0
    def deploy(self,
               img: ImageSpec,
               env_vars,
               mounts,
               cargos,
               ports,
               cmd: list,
               name: str,
               tasks: int = 1,
               allow_probe=False,
               delay_readiness: int = 0):

        [self.load_vol(v, name) for v in cargos]
        vol_refs, vol_defs = self.kube_vols(cargos)
        mount_refs, mount_defs = self.kube_mounts(mounts)
        port_refs, port_defs = self.kube_svc_ports(name, ports)

        container = dict(name=name,
                         image=img.target,
                         imagePullPolicy='Always',
                         command=cmd,
                         resources=self.kube_resources(),
                         volumeMounts=vol_refs + mount_refs,
                         env=self.kube_env_vars(env_vars),
                         ports=port_refs,
                         livenessProbe=self.kube_healthcheck(allow_probe),
                         readinessProbe=self.kube_readiness(delay_readiness))

        template = self.cleaner(
            dict(apiVersion="apps/v1",
                 kind="Deployment",
                 metadata={'name': name},
                 spec=dict(replicas=tasks,
                           selector={'matchLabels': {
                               'app': name
                           }},
                           template=dict(metadata={
                               'labels': {
                                   'app': name
                               },
                               'annotations': {
                                   'updated':
                                   datetime.now().strftime(DateFmt.READABLE)
                               }
                           },
                                         spec={
                                             'containers': [container],
                                             'volumes':
                                             vol_defs + mount_defs,
                                             'imagePullSecrets': [{
                                                 'name':
                                                 self.secret
                                             }]
                                         }))))

        if self.find_depl(name) is None:
            self.LOG.info("Creating deployment '{}'".format(name))
            self.LOG.debug(template)
            yaml = Kaptan().import_config(template).export(handler='yaml')
            depl = Deployment(namespace=self.namespace,
                              create_in_cluster=True,
                              from_template=yaml)
        else:
            self.LOG.info("Updating deployment '{}'".format(name))
            self.LOG.debug(template)
            with K8sBackend(logging_level=logging.ERROR) as k8s_backend:
                k8s_backend.apps_api.replace_namespaced_deployment(
                    name, self.namespace, template)
            depl = self.find_depl(name)

        self.handle_svc(name, port_defs)
        self.handle_autoscaler(name)

        return depl
Example #11
0
    def dump(self):

        return Kaptan().import_config(
            self.as_dict()).export(handler=Config.FMT)
Example #12
0
 def __init__(self, handler=None):
     self.__dict__ = self.__shared_state
     self._cpt = Kaptan(handler)
Example #13
0
class Config(object):
    """ This class has a shared state like a Borg.

    This is a Kaptan class that infers the file handler
    from the file extension using the `from_file` function.

    Parameters
    ----------
    handler: str or kaptan.BaseHandler
        This parameter is to keep compatibility with Kaptan's instantiation protocol.
        See its documentation on how to use it: http://emre.github.io/kaptan/
        I personally prefer to use the `from_file` function.
    """
    __shared_state = {}

    def __init__(self, handler=None):
        self.__dict__ = self.__shared_state
        self._cpt = Kaptan(handler)

    @classmethod
    def from_file(cls, file_path):
        """ Returns a Config instance with the data from
        `file_path`.

        Parameters
        ----------
        file_path: str
            Path to a configuration file.
            Its extension can be any from config.HANDLER_EXT, i.e.,
            {'conf': 'ini',
             'ini': 'ini',
             'json': 'json',
             'py': 'file',
             'yaml': 'yaml',
             'yml': 'yaml'}

        Returns
        -------
        cfg: Config
        """
        _check_file(file_path)
        cfg = Config()
        cfg._cpt = _load_config(file_path)
        return cfg

    def update_from_file(self, file_path):
        """ Updates the config parameters with the data from
        `file_path`.

        Parameters
        ----------
        file_path: str
            Path to a configuration file.
            Its extension can be any from config.HANDLER_EXT, i.e.,
            {'conf': 'ini',
             'ini': 'ini',
             'json': 'json',
             'py': 'file',
             'yaml': 'yaml',
             'yml': 'yaml'}

        Returns
        -------
        cfg: Config
        """
        _check_file(file_path)
        cpt = _load_config(file_path)

        params = cpt.configuration_data
        _update_kaptan(self._cpt, params)

    def check_file(self, item):
        """ This is a __getitem__ operator for file path values, if the file does not exist an IOError is raised."""
        try:
            fpath = self.__getitem__(item)
        except KeyError:
            raise
        else:
            if not op.exists(fpath):
                raise IOError('Could not find file for key {} in the {}.'.format(item, fpath))
            return fpath

    def update(self, adict):
        for k, v in adict.items():
            self._cpt.upsert(k, v)

    def keys(self):
        return self._cpt.configuration_data.keys()

    def items(self):
        return self._cpt.configuration_data.items()

    def get(self, item, default=None):
        return self._cpt.configuration_data.get(item, default)

    def __getitem__(self, item):
        if item not in self._cpt.configuration_data:
            raise KeyError('Could not find key {} in configuration content.'.format(item))
        return self._cpt.configuration_data[item]

    def __setitem__(self, key, value):
        return self._cpt.upsert(key, value)

    def __repr__(self):
        return '<config.Config> ({})'.format('\n'.join([str(i) for i in self.items()]))
Example #14
0
def _load_config(file_path):
    cpt = Kaptan()
    return cpt.import_config(file_path)