Пример #1
0
def _init():
    cfg = config.initUp2dateConfig()
    cfg_dict = dict(cfg.items())
    server_url = config.getServerlURL()
    cfg_dict['proto'], cfg_dict['server_name'] = utils.parse_url(server_url[0], scheme="https")[:2]
    if len(server_url) > 1:
        cfg_dict['server_list'] = server_url
    local_config.init('rhncfg-client', defaults=cfg_dict)
    set_debug_level(int(local_config.get('debug_level') or 0))
    set_logfile("/var/log/rhncfg-actions")
Пример #2
0
def _init():
    cfg = config.initUp2dateConfig()
    cfg_dict = dict(cfg.items())
    server_url = config.getServerlURL()
    cfg_dict['proto'], cfg_dict['server_name'] = utils.parse_url(server_url[0], scheme="https")[:2]
    if len(server_url) > 1:
        cfg_dict['server_list'] = server_url
    local_config.init('rhncfg-client', defaults=cfg_dict)
    set_debug_level(int(local_config.get('debug_level') or 0))
    set_logfile("/var/log/rhncfg-actions")
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
#

import os
import fnmatch

from actions import configfiles
from config_common import local_config, rhn_log

local_config.init("rhncfg")
rhn_log.set_debug_level(local_config.get('debug'))


def test(msg, test):
    ok_str = 'NOT OK'
    if test:
        ok_str = 'ok'

    print("%s: %s" % (msg, ok_str))


def stray_files(path):
    (directory, filename) = os.path.split(path)
    for path in os.listdir(directory):
        if fnmatch.fnmatch(path, "%s*" % filename):
            return 1
Пример #4
0
def _init():
    up2date_config = utils.get_up2date_config()
    local_config.init('rhncfg-client', defaults=up2date_config)
    set_debug_level(int(local_config.get('debug_level') or 0))
    set_logfile("/var/log/rhncfg-actions")
Пример #5
0
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
#

import os
import fnmatch

from actions import configfiles
from config_common import local_config, rhn_log

local_config.init("rhncfg")
rhn_log.set_debug_level(local_config.get('debug'))

def test(msg, test):
    ok_str = 'NOT OK'
    if test:
        ok_str = 'ok'

    print "%s: %s" % (msg, ok_str)

def stray_files(path):
    (directory, filename) = os.path.split(path)
    for path in os.listdir(directory):
        if fnmatch.fnmatch(path, "%s*" % filename):
            return 1
    return 0
Пример #6
0
    def main(self):
        args = []

        show_help = None
        debug_level = 3
        mode = None

        dict_name_opt={'--server-name': None,'--password': None,'--username': None,}
        for index in range(1,len(sys.argv)):
            arg=sys.argv[index]
            param = [x for x in dict_name_opt.items() if x[1] == 0]
            if param:
                if arg.startswith('-') or arg in self.modes:
                  # not perfect, but at least a little bit better
                  print("Option %s requires an argument" % dict_name_opt[param[0][0]])
                  return 1
                dict_name_opt[param[0][0]] = arg
                continue

            if arg in ('--help', '-h'):
                show_help = 1
                continue

            param = [s for s in dict_name_opt.keys() if arg.startswith(s)]
            if param:
                rarg = arg[len(param[0]):]
                if not rarg:
                    dict_name_opt[param[0]] = 0
                    if index == len(sys.argv) - 1:
                       print("Option %s requires an argument" % param[0])
                       return 1
                    continue
                if rarg[0] == '=':
                   if len(rarg) == 1:
                      print("Option %s requires an argument" % param[0])
                      return 1

                   dict_name_opt[param[0]] = rarg[1:]
                   continue
                print("Unknown option %s" % arg)
                return 1

            if mode is None:
                # This should be the mode
                mode = arg
                if mode == '':
                    # Bad
                    self.usage(1)

                if mode[0] == '-':
                    # Mode can't be an option
                    self.usage(1)

                if mode not in self.modes:
                    print("Unknown mode %s" % mode)
                    self.usage(1)

                continue

            args.append(arg)

        server_name = dict_name_opt['--server-name']
        password = dict_name_opt['--password']
        username = dict_name_opt['--username']

        rhn_log.set_debug_level(debug_level)

        if mode is None:
            # No args were specified
            self.usage(0)

        execname = os.path.basename(sys.argv[0])
        # Class names cannot have dot in them, so strip the extension
        execname = execname.split('.', 1)[0]

        mode_module = mode.replace('-', '_')
        module_name = "%s_%s" % (self.mode_prefix, mode_module)
        full_module_name = "%s.%s" % (self.plugins_dir, module_name)

        try:
            module = __import__(full_module_name)
        except ImportError:
            e = sys.exc_info()[1]
            rhn_log.die(1, "Unable to load plugin for mode '%s': %s" % (mode, e))

        module = getattr(module, module_name)

        if show_help:
            # Display the per-module help
            handler = module.Handler(args, None, mode=mode, exec_name=execname)
            handler.usage()
            return 0

        cfg = config.initUp2dateConfig()
        up2date_cfg = dict(cfg.items())

        if server_name:
            local_config.init(self.config_section, defaults=up2date_cfg, server_url="https://" + server_name)
        else:
            local_config.init(self.config_section, defaults=up2date_cfg)

            try:
                server_name = local_config.get('server_url')
            except InterpolationError:
                e = sys.exc_info()[1]
                if e.option == 'server_url':
                    server_name = config.getServerlURL()
                    up2date_cfg['proto'] = urlsplit(server_name[0])[0]
                    if up2date_cfg['proto'] == '':
                        up2date_cfg['proto'] = 'https'
                        up2date_cfg['server_list'] = [urlsplit(x)[2] for x in server_name]
                    else:
                        up2date_cfg['server_list'] = [urlsplit(x)[1] for x in server_name]
                    server_name = (up2date_cfg['server_list'])[0]
                    local_config.init(self.config_section, defaults=up2date_cfg, server_name=server_name)

        print("Using server name %s" % server_name)

        # set the debug level through the config
        rhn_log.set_debug_level(int(local_config.get('debug_level') or 0))
        rhn_log.set_logfile(local_config.get('logfile') or "/var/log/rhncfg")

        # Multi-level import - __import__("foo.bar") does not return the bar
        # module, but the foo module with bar loaded
        # XXX Error checking
        repo_class = local_config.get('repository_type')
        if repo_class is None:
            rhn_log.die(1, "repository_type not set (missing configuration file?)")

        repo_module_name = "%s.%s" % (self.plugins_dir, repo_class)
        try:
            repo_module = __import__(repo_module_name)
        except ImportError:
            e = sys.exc_info()[1]
            rhn_log.die(1, "Unable to load repository module:  %s" % e)

        try:
            repo_module = getattr(repo_module, repo_class)
        except AttributeError:
            rhn_log.die(1, "Malformed repository module")

        try:
            repo = getattr(repo_module, self.repository_class_name)()
        except AttributeError:
            rhn_log.die(1, "Missing repository class")
        except InterpolationError:
            e = sys.exc_info()[1]
            if e.option == 'server_url':
                #pkilambi: bug#179367# backtic is replaced with single quote
                rhn_log.die(1, "Missing 'server_url' configuration variable; please refer to the config file")
            raise
        except cfg_exceptions.ConfigurationError:
            e = sys.exc_info()[1]
            rhn_log.die(e)
        except gaierror:
            e = sys.exc_info()[1]
            print("Socket Error: %s" % (e.args[1],))
            sys.exit(1)

        handler = module.Handler(args, repo, mode=mode, exec_name=execname)
        try:
            try:
                handler.authenticate(username, password)
                handler.run()
            except cfg_exceptions.AuthenticationError:
                e = sys.exc_info()[1]
                rhn_log.die(1, "Authentication failed: %s" % e)
            except Exception:
                raise
        finally:
            repo.cleanup()
        return 0
Пример #7
0
    def main(self):
        args = []

        show_help = None
        debug_level = 3
        mode = None

        dict_name_opt = {
            '--server-name': None,
            '--password': None,
            '--username': None,
            '--config': None,
        }
        for index in range(1, len(sys.argv)):
            arg = sys.argv[index]
            param = [x for x in dict_name_opt.items() if x[1] == 0]
            if param:
                if arg.startswith('-') or arg in self.modes:
                    # not perfect, but at least a little bit better
                    print("Option %s requires an argument" %
                          dict_name_opt[param[0][0]])
                    return 1
                dict_name_opt[param[0][0]] = arg
                continue

            if arg in ('--help', '-h'):
                show_help = 1
                continue

            param = [s for s in dict_name_opt.keys() if arg.startswith(s)]
            if param:
                rarg = arg[len(param[0]):]
                if not rarg:
                    dict_name_opt[param[0]] = 0
                    if index == len(sys.argv) - 1:
                        print("Option %s requires an argument" % param[0])
                        return 1
                    continue
                if rarg[0] == '=':
                    if len(rarg) == 1:
                        print("Option %s requires an argument" % param[0])
                        return 1

                    dict_name_opt[param[0]] = rarg[1:]
                    continue
                print("Unknown option %s" % arg)
                return 1

            if mode is None:
                # This should be the mode
                mode = arg
                if mode == '':
                    # Bad
                    self.usage(1)

                if mode[0] == '-':
                    # Mode can't be an option
                    self.usage(1)

                if mode not in self.modes:
                    print("Unknown mode %s" % mode)
                    self.usage(1)

                continue

            args.append(arg)

        server_name = dict_name_opt['--server-name']
        password = dict_name_opt['--password']
        username = dict_name_opt['--username']
        config_file_override = dict_name_opt['--config']

        if config_file_override and not os.path.isfile(config_file_override):
            rhn_log.die(1, "Config file %s does not exist.",
                        config_file_override)

        rhn_log.set_debug_level(debug_level)

        if mode is None:
            # No args were specified
            self.usage(0)

        execname = os.path.basename(sys.argv[0])
        # Class names cannot have dot in them, so strip the extension
        execname = execname.split('.', 1)[0]

        mode_module = mode.replace('-', '_')
        module_name = "%s_%s" % (self.mode_prefix, mode_module)
        full_module_name = "%s.%s" % (self.plugins_dir, module_name)

        try:
            module = __import__(full_module_name)
        except ImportError:
            e = sys.exc_info()[1]
            rhn_log.die(1,
                        "Unable to load plugin for mode '%s': %s" % (mode, e))

        module = getattr(module, module_name)

        if show_help:
            # Display the per-module help
            handler = module.Handler(args, None, mode=mode, exec_name=execname)
            handler.usage()
            return 0

        cfg = config.initUp2dateConfig()
        up2date_cfg = dict(cfg.items())

        if server_name:
            local_config.init(self.config_section,
                              defaults=up2date_cfg,
                              config_file_override=config_file_override,
                              server_url="https://" + server_name)
        else:
            local_config.init(self.config_section,
                              defaults=up2date_cfg,
                              config_file_override=config_file_override)

            try:
                server_name = local_config.get('server_url')
            except InterpolationError:
                e = sys.exc_info()[1]
                if e.option == 'server_url':
                    server_name = config.getServerlURL()
                    up2date_cfg['proto'] = urlsplit(server_name[0])[0]
                    if up2date_cfg['proto'] == '':
                        up2date_cfg['proto'] = 'https'
                        up2date_cfg['server_list'] = [
                            urlsplit(x)[2] for x in server_name
                        ]
                    else:
                        up2date_cfg['server_list'] = [
                            urlsplit(x)[1] for x in server_name
                        ]
                    server_name = (up2date_cfg['server_list'])[0]
                    local_config.init(
                        self.config_section,
                        defaults=up2date_cfg,
                        config_file_override=config_file_override,
                        server_name=server_name)

        print("Using server name %s" % server_name)

        # set the debug level through the config
        rhn_log.set_debug_level(int(local_config.get('debug_level') or 0))
        rhn_log.set_logfile(local_config.get('logfile') or "/var/log/rhncfg")

        # Multi-level import - __import__("foo.bar") does not return the bar
        # module, but the foo module with bar loaded
        # XXX Error checking
        repo_class = local_config.get('repository_type')
        if repo_class is None:
            rhn_log.die(
                1, "repository_type not set (missing configuration file?)")

        repo_module_name = "%s.%s" % (self.plugins_dir, repo_class)
        try:
            repo_module = __import__(repo_module_name)
        except ImportError:
            e = sys.exc_info()[1]
            rhn_log.die(1, "Unable to load repository module:  %s" % e)

        try:
            repo_module = getattr(repo_module, repo_class)
        except AttributeError:
            rhn_log.die(1, "Malformed repository module")

        try:
            repo = getattr(repo_module, self.repository_class_name)()
        except AttributeError:
            rhn_log.die(1, "Missing repository class")
        except InterpolationError:
            e = sys.exc_info()[1]
            if e.option == 'server_url':
                #pkilambi: bug#179367# backtic is replaced with single quote
                rhn_log.die(
                    1,
                    "Missing 'server_url' configuration variable; please refer to the config file"
                )
            raise
        except cfg_exceptions.ConfigurationError:
            e = sys.exc_info()[1]
            rhn_log.die(e)
        except gaierror:
            e = sys.exc_info()[1]
            print("Socket Error: %s" % (e.args[1], ))
            sys.exit(1)

        handler = module.Handler(args, repo, mode=mode, exec_name=execname)
        try:
            try:
                handler.authenticate(username, password)
                handler.run()
            except cfg_exceptions.AuthenticationError:
                e = sys.exc_info()[1]
                rhn_log.die(1, "Authentication failed: %s" % e)
            except Exception:
                raise
        finally:
            repo.cleanup()
        return 0