Beispiel #1
0
 def init(self):
     self.in_dir = os.environ['MFDATA_DATA_IN_DIR']
     conf_file = os.path.join(os.environ['MODULE_RUNTIME_HOME'], "tmp",
                              "config_auto", "switch.ini")
     if not os.path.exists(conf_file):
         self.error_and_die("no switch configuration file")
     self.condition_tuples = []
     parser = ExtendedConfigParser(config=CONFIG,
                                   strict=False,
                                   inheritance='im',
                                   interpolation=None)
     parser.read(conf_file)
     sections = parser.sections()
     for section in sections:
         directory = parser.get(section, "directory").strip()
         plugin_name = parser.get(section, "plugin_name").strip()
         condition = parser.get(section, "condition").strip()
         magic_file = parser.get(section, "magic_file").strip()
         use_hardlink = parser.getboolean(section, "use_hardlink")
         if magic_file.lower() in ('null', 'none'):
             magic_file = None
         self.condition_tuples.append(
             (plugin_name, condition, directory, magic_file, use_hardlink))
     self.no_match_policy = self.args.no_match_policy
     if self.no_match_policy not in ('keep', 'delete', 'move'):
         self.error_and_die("unknown no-match policy: %s",
                            self.no_match_policy)
     if self.no_match_policy == "move":
         nmpmdd = self.args.no_match_policy_move_dest_dir
         if nmpmdd is None:
             self.error_and_die('you have to set a '
                                'no-match-policy-move-dest-dir'
                                ' in case of move no-match policy')
         mkdir_p_or_die(nmpmdd)
def main():
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument("path",
                            help="Chemin du fichier de config à visualiser")
    arg_parser.add_argument(
        "module",
        help="Précise le module dans lequel se trouve le fichier de config")
    args = arg_parser.parse_args()

    parser = ExtendedConfigParser(config=environ.get("MFCONFIG", "GENERIC"),
                                  strict=False,
                                  inheritance='im')
    data = codecs.open(args.path, "r", "utf-8")
    module = args.module

    home_env = str(module) + "_HOME"
    home = environ.get(home_env)
    parser.read_file(data)
    res_dict = read_config_file(parser, parser.config_name, home)
    for s in res_dict:
        for o in res_dict[s]:
            print("export " + module.upper() + "_" + s.upper() + "_" +
                  o.upper() + "=\"" + str(res_dict[s][o]) + "\"")

    print("export %s_CONF_GENERATION_TIME=%i" % (module.upper(), time.time()))
Beispiel #3
0
def main():
    arg_parser = argparse.ArgumentParser(
        description="Lit le contenu d'un fichier .ini")
    arg_parser.add_argument("path",
                            help="Chemin du fichier de config a visualiser")
    arg_parser.add_argument("section",
                            help="Precise la section a afficher",
                            nargs='?',
                            default=None)
    arg_parser.add_argument("option",
                            help="Precise l'option a afficher",
                            nargs='?',
                            default=None)
    arg_parser.add_argument(
        "config",
        help="Precise la config a utiliser pour l'affichage",
        nargs='?',
        default=None)
    args = arg_parser.parse_args()

    if (args.config):
        parser = ExtendedConfigParser(config="NULL",
                                      strict=False,
                                      inheritance='im')
    else:
        parser = ExtendedConfigParser(config=environ.get(
            "SYNCONFIG", "GENERIC"),
                                      strict=False,
                                      inheritance='im')
    data = codecs.open(str(args.path), "r", "utf-8")
    parser.read_file(data)

    if (args.section):
        # Affichage de la valeur d'une option
        if (args.option):
            print_option(parser, args.section, args.option)

        # Affichage du contenu d'une section
        else:
            print_section(parser, args.section)

    # Affichage de l'ensemble des sections du fichier
    else:
        print_file(parser)
Beispiel #4
0
def build_plugin(plugin_path, plugins_base_dir=None):
    plugin_path = os.path.abspath(plugin_path)
    plugins_base_dir = _get_plugins_base_dir(plugins_base_dir)
    base = os.path.join(plugins_base_dir, "base")
    pwd = os.getcwd()
    parser = ExtendedConfigParser(config=os.environ.get('MFCONFIG', 'GENERIC'),
                                  strict=False,
                                  inheritance="im")
    with open(os.path.join(plugin_path, "config.ini"), "r") as f:
        config_content = f.read()
    if six.PY2:
        parser.read_string(config_content.decode('utf-8'))
    else:
        parser.read_string(config_content)
    name = parser['general']['name']
    version = parser['general']['version']
    summary = parser['general']['summary']
    license = parser['general']['license']
    try:
        packager = parser['general']['packager']
    except Exception:
        packager = parser['general']['maintainer']
    vendor = parser['general']['vendor']
    url = parser['general']['url']
    tmpdir = os.path.join(RUNTIME_HOME, "tmp",
                          "plugin_%s" % get_unique_hexa_identifier())
    mkdir_p_or_die(os.path.join(tmpdir, "BUILD"))
    mkdir_p_or_die(os.path.join(tmpdir, "RPMS"))
    mkdir_p_or_die(os.path.join(tmpdir, "SRPMS"))
    _make_plugin_spec(os.path.join(tmpdir, "specfile.spec"), name, version,
                      summary, license, packager, vendor, url)
    cmd = "source %s/lib/bash_utils.sh ; " % MFEXT_HOME
    cmd = cmd + "layer_load rpm@mfext ; "
    cmd = cmd + 'rpmbuild --define "_topdir %s" --define "pwd %s" ' \
        '--define "prefix %s" --dbpath %s ' \
        '-bb %s/specfile.spec' % (tmpdir, plugin_path, tmpdir,
                                  base, tmpdir)
    x = BashWrapperOrRaise(cmd, MFUtilPluginCantBuild,
                           "can't build plugin %s" % plugin_path)
    tmp = glob.glob(os.path.join(tmpdir, "RPMS", "x86_64", "*.rpm"))
    if len(tmp) == 0:
        raise MFUtilPluginCantBuild("can't find generated plugin" %
                                    plugin_path,
                                    bash_wrapper=x)
    plugin_path = tmp[0]
    new_basename = \
        os.path.basename(plugin_path).replace("x86_64.rpm",
                                              "metwork.%s.plugin" %
                                              MODULE_LOWERCASE)
    new_plugin_path = os.path.join(pwd, new_basename)
    shutil.move(plugin_path, new_plugin_path)
    shutil.rmtree(tmpdir, True)
    os.chdir(pwd)
    return new_plugin_path
Beispiel #5
0
def read_config_file(parser, config, home):
    res = OrderedDict()
    for s in parser._sections:
        # On inclut les sections des fichiers de config à inclure
        if (s.startswith("INCLUDE_")):
            parser_included = ExtendedConfigParser(config=config,
                                                   inheritance='im')
            included_file = s[8:]
            data = codecs.open(home + "/config/" + included_file, "r", "utf-8")
            parser_included.read_file(data)
            res.update(read_config_file(parser_included, config, home))
        # On ajoute la section si elle n'y est pas ou on la met à jour
        # si elle a été incluse auparavant
        else:
            try:
                res[s].update(read_section(parser, s, home))
            except KeyError:
                res[s] = read_section(parser, s, home)
    return res
    def parse(self, stream):
        """Parses the keys and values from a config file.

        NOTE: For keys that were specified to configargparse as
        action="store_true" or "store_false", the config file value must be
        one of: "yes", "no", "true", "false".
        Otherwise an error will be raised.

        Args:
            stream: A config file input stream (such as an open file object).

        Returns:
            OrderedDict of items where the keys have type string and the
            values have type either string or list (eg. to support config file
            formats like YAML which allow lists).
        """
        f = self.custom_environment_callable
        f(self.plugin_name, self.step_name)
        config = os.environ.get('MFCONFIG', 'GENERIC')
        config_parser = ExtendedConfigParser(config=config,
                                             inheritance='im',
                                             interpolation=None)
        content = stream.read()
        if (sys.version_info < (3, 0)):
            content = content.decode("utf-8")
        config_parser.read_string(content)
        section = "step_%s" % self.step_name
        result = collections.OrderedDict()
        for key in config_parser.options(section):
            if not key.startswith('arg_'):
                continue
            if (sys.version_info < (3, 0)):
                result[key.replace('arg_', '', 1)] = envtpl.render_string(
                    config_parser.get(section, key),
                    keep_multi_blank_lines=False).encode('utf-8')
            else:
                result[key.replace('arg_', '', 1)] = envtpl.render_string(
                    config_parser.get(section, key),
                    keep_multi_blank_lines=False)
        return result
Beispiel #7
0
def init():
    global monitors, inactivityTimeout, logger

    if monitors:
        # Init already performed
        return True

    arg_parser = ArgumentParser()
    arg_parser.add_argument('-c',
                            '--config',
                            help='Configuration filename '
                            '( absolute or relative path to a directory under '
                            'module home directory )')
    args = arg_parser.parse_args()
    homedir = None
    try:
        if args.config is None:
            logger.critical("you must provide a configuration file")
            sys.exit(1)
        if args.config.startswith('~'):
            args.config = os.path.abspath(args.config)

        if args.config.startswith('/'):
            ficconfig = args.config
            if not os.path.exists(ficconfig):
                logger.critical("File %s does not exist or is not accessible" %
                                ficconfig)
                ficconfig = None
        else:
            ficconfig = None
            homedir = os.environ[os.environ['MFMODULE'] + '_HOME']
            for directory in DEFAULT_HOME_CONFIG_DIRS:
                ficconfig = os.path.abspath(homedir)
                ficconfig = os.path.join(ficconfig, directory)
                ficconfig = os.path.join(ficconfig, args.config)
                if os.path.exists(ficconfig):
                    break
                ficconfig = None
            if not ficconfig:
                logger.info("File %s not found under standard configuration"
                            " directories")
                pass
    except KeyError:
        logger.critical("Can't obtain Module home directory")
        ficconfig = None

    if ficconfig and os.path.isdir(ficconfig):
        logger.critical("%s is a directory !" % ficconfig)
        ficconfig = None

    if not ficconfig:
        return False

    parser = ExtendedConfigParser(config=environ.get("MFCONFIG", "GENERIC"),
                                  strict=False,
                                  inheritance='im')
    data = codecs.open(ficconfig, "r", "utf-8")
    try:
        parser.read_file(data)
    except Exception:
        logger.critical('Error Loading config: %s' % ficconfig)
        return False

    Monitor.server = None
    Monitor.port = None
    Monitor.redis = None
    Monitor.unix_socket = None
    Monitor.wm = None

    monitor_inactivitytimeout = None

    monitors = []

    for s in parser._sections:
        mon = Monitor()
        active = False
        key = 'active'
        if parser.has_option(s, key):
            active = parser.getboolean(s, key)
        if not active:
            continue

        key = 'redis_server'
        if not parser.has_option(s, key):
            continue
        server = parser[s][key]
        if Monitor.server is None:
            Monitor.server = server
        elif Monitor.server != server:
            logger.critical('Only 1 redis server Please')
            return False

        key = 'redis_unix_socket'
        if not parser.has_option(s, key):
            continue

        unix_socket = parser[s][key]
        if len(unix_socket) == 0 or unix_socket == "null":
            unix_socket = None
        Monitor.unix_socket = unix_socket

        key = 'redis_port'
        if not parser.has_option(s, key):
            continue

        port = parser.getint(s, key)
        if Monitor.port is None:
            Monitor.port = port
        elif Monitor.port != port:
            logger.critical('Only 1 redis port Please')
            return False

        key = 'monitor_inactivitytimeout'
        if parser.has_option(s, key):
            timeout = parser.getint(s, key)
            if timeout <= 0:  # Standardization
                timeout = -1
            if monitor_inactivitytimeout is None:
                monitor_inactivitytimeout = timeout
                inactivityTimeout = timeout
            elif monitor_inactivitytimeout != timeout:
                logger.critical('Only 1 monitor_inactivitytimeout Please')
                return False

        key = 'directory'
        if not parser.has_option(s, key):
            continue

        mon.dir = parser[s][key]

        key = 'queue'
        if not parser.has_option(s, key):
            continue
        mon.redisqueue = parser[s][key]
        if not mon.redisqueue:
            logger.critical('Invalid redis queue name')
            continue

        q = mon.redisqueue.replace('queue:', '').replace('out_', 'out.')
        q = re.sub(r'stage(\d+)_', r'stage\1.', q)

        key = 'timeout'
        if parser.has_option(s, key):
            mon.redistimeout = parser.getint(s, key)

        key = 'forced_poll'
        if parser.has_option(s, key):
            mon.tpolling = parser.getint(s, key)

        key = 'poll_queue_limit'
        if parser.has_option(s, key):
            mon.pollinglimit = parser.getint(s, key)

        key = 'only'
        if parser.has_option(s, key):
            mon.only = getMask(parser[s][key])

        key = 'ignore'
        cnfgignorestr = None
        if parser.has_option(s, key):
            cnfgignorestr = parser[s][key]
        mon.ignore = getMask(cnfgignorestr, (r'^.*\.working$', ))

        monitors.append(mon)

    if not monitors or len(monitors) < 1:
        logger.critical('No directory to scan')
        return False

    return True