Example #1
0
File: Syncdb.py Project: ab/bcfg2
    def __call__(self, args):
        import Bcfg2.Server.Admin

        Bcfg2.Server.Admin.Mode.__call__(self, args)

        # Parse options
        self.opts = Bcfg2.Options.OptionParser(self.options)
        self.opts.parse(args)

        setup_environ(Bcfg2.settings)
        import Bcfg2.Server.models

        Bcfg2.Server.models.load_models(cfile=self.opts["configfile"])

        try:
            update_database()
        except UpdaterError:
            print("Update failed")
            raise SystemExit(-1)
Example #2
0
    def __call__(self, args):
        import Bcfg2.Server.Admin
        Bcfg2.Server.Admin.Mode.__call__(self, args)

        # Parse options
        self.opts = Bcfg2.Options.OptionParser(self.options)
        self.opts.parse(args)

        # we have to set up the django environment before we import
        # the syncdb command, but we have to wait to set up the
        # environment until we've read the config, which has to wait
        # until we've parsed options.  it's a windy, twisting road.
        Bcfg2.settings.read_config(cfile=self.opts['web_configfile'],
                                   repo=self.opts['repo'])
        setup_environ(Bcfg2.settings)
        import Bcfg2.Server.models
        Bcfg2.Server.models.load_models(cfile=self.opts['configfile'])

        try:
            update_database()
        except UpdaterError:
            print("Update failed")
            raise SystemExit(-1)
Example #3
0
    def __init__(self, setup, start_fam_thread=False):
        self.datastore = setup['repo']

        if setup['debug']:
            level = logging.DEBUG
        elif setup['verbose']:
            level = logging.INFO
        else:
            level = logging.WARNING
        # we set a higher log level for the console by default.  we
        # assume that if someone is running bcfg2-server in such a way
        # that it _can_ log to console, they want more output.  if
        # level is set to DEBUG, that will get handled by
        # setup_logging and the console will get DEBUG output.
        Bcfg2.Logger.setup_logging('bcfg2-server',
                                   to_console=logging.INFO,
                                   to_syslog=setup['syslog'],
                                   to_file=setup['logging'],
                                   level=level)
        self.logger = logging.getLogger('bcfg2-server')

        try:
            fm = Bcfg2.Server.FileMonitor.available[setup['filemonitor']]
        except KeyError:
            self.logger.error("File monitor driver %s not available; "
                              "forcing to default" % filemonitor)
            fm = Bcfg2.Server.FileMonitor.available['default']
        famargs = dict(ignore=[], debug=False)
        if 'ignore' in setup:
            famargs['ignore'] = setup['ignore']
        if 'debug' in setup:
            famargs['debug'] = setup['debug']
        try:
            self.fam = fm(**famargs)
        except IOError:
            msg = "Failed to instantiate fam driver %s" % setup['filemonitor']
            self.logger.error(msg, exc_info=1)
            raise CoreInitError(msg)
        self.pubspace = {}
        self.cfile = setup['configfile']
        self.cron = {}
        self.plugins = {}
        self.plugin_blacklist = {}
        self.revision = '-1'
        self.password = setup['password']
        self.encoding = setup['encoding']
        self.setup = setup
        atexit.register(self.shutdown)
        # Create an event to signal worker threads to shutdown
        self.terminate = threading.Event()

        # generate Django ORM settings.  this must be done _before_ we
        # load plugins
        Bcfg2.settings.read_config(cfile=self.setup['web_configfile'],
                                   repo=self.datastore)

        self._database_available = False
        # verify our database schema
        try:
            from Bcfg2.Server.SchemaUpdater import update_database, UpdaterError
            try:
                update_database()
                self._database_available = True
            except UpdaterError:
                err = sys.exc_info()[1]
                self.logger.error("Failed to update database schema: %s" % err)
        except ImportError:
            # assume django is not installed
            pass
        except Exception:
            inst = sys.exc_info()[1]
            self.logger.error("Failed to update database schema")
            self.logger.error(str(inst))
            self.logger.error(str(type(inst)))
            raise CoreInitError

        if '' in setup['plugins']:
            setup['plugins'].remove('')

        for plugin in setup['plugins']:
            if not plugin in self.plugins:
                self.init_plugins(plugin)
        # Remove blacklisted plugins
        for p, bl in list(self.plugin_blacklist.items()):
            if len(bl) > 0:
                self.logger.error("The following plugins conflict with %s;"
                                  "Unloading %s" % (p, bl))
            for plug in bl:
                del self.plugins[plug]
        # This section logs the experimental plugins
        expl = [plug for (name, plug) in list(self.plugins.items())
                if plug.experimental]
        if expl:
            self.logger.info("Loading experimental plugin(s): %s" %
                             (" ".join([x.name for x in expl])))
            self.logger.info("NOTE: Interfaces subject to change")
        # This section logs the deprecated plugins
        depr = [plug for (name, plug) in list(self.plugins.items())
                if plug.deprecated]
        if depr:
            self.logger.info("Loading deprecated plugin(s): %s" %
                             (" ".join([x.name for x in depr])))

        mlist = self.plugins_by_type(Bcfg2.Server.Plugin.Metadata)
        if len(mlist) == 1:
            self.metadata = mlist[0]
        else:
            self.logger.error("No Metadata Plugin loaded; "
                              "failed to instantiate Core")
            raise CoreInitError("No Metadata Plugin")
        self.statistics = self.plugins_by_type(Bcfg2.Server.Plugin.Statistics)
        self.pull_sources = self.plugins_by_type(Bcfg2.Server.Plugin.PullSource)
        self.generators = self.plugins_by_type(Bcfg2.Server.Plugin.Generator)
        self.structures = self.plugins_by_type(Bcfg2.Server.Plugin.Structure)
        self.connectors = self.plugins_by_type(Bcfg2.Server.Plugin.Connector)
        self.ca = setup['ca']
        self.fam_thread = \
            threading.Thread(name="%sFAMThread" % setup['filemonitor'],
                             target=self._file_monitor_thread)
        self.lock = threading.Lock()

        if start_fam_thread:
            self.fam_thread.start()
            self.fam.AddMonitor(self.cfile, self.setup)
Example #4
0
    cf.read([cpath])

    if not statpath:
        try:
            statpath = "%s/etc/statistics.xml" % cf.get("server", "repository")
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            print("Could not read bcfg2.conf; exiting")
            raise SystemExit(1)
    try:
        statsdata = XML(open(statpath).read())
    except (IOError, XMLSyntaxError):
        print("StatReports: Failed to parse %s" % (statpath))
        raise SystemExit(1)

    try:
        encoding = cf.get("components", "encoding")
    except:
        encoding = "UTF-8"

    q = "-O3" in sys.argv

    # don't load this at the top.  causes a circular import error
    from Bcfg2.Server.SchemaUpdater import update_database, UpdaterError

    # Be sure the database is ready for new schema
    try:
        update_database()
    except UpdaterError:
        raise SystemExit(1)
    load_stats(statsdata, encoding, verb, logger, quick=q, location=platform.node())
Example #5
0
File: Reports.py Project: ab/bcfg2
    def __call__(self, args):
        Bcfg2.Server.Admin.Mode.__call__(self, args)
        if len(args) == 0 or args[0] == '-h':
            print(self.__usage__)
            raise SystemExit(0)

        # FIXME - dry run

        if args[0] in self.django_commands:
            self.django_command_proxy(args[0])
        elif args[0] == 'scrub':
            self.scrub()
        elif args[0] in ['init', 'update']:
            try:
                update_database()
            except UpdaterError:
                print("Update failed")
                raise SystemExit(-1)
        elif args[0] == 'load_stats':
            quick = '-O3' in args
            stats_file = None
            i = 1
            while i < len(args):
                if args[i] == '-s' or args[i] == '--stats':
                    stats_file = args[i + 1]
                    if stats_file[0] == '-':
                        self.errExit("Invalid statistics file: %s" % stats_file)
                elif args[i] == '-c' or args[i] == '--clients-file':
                    print("DeprecationWarning: %s is no longer used" % args[i])
                i = i + 1
            self.load_stats(stats_file, self.log.getEffectiveLevel() > logging.WARNING, quick)
        elif args[0] == 'purge':
            expired = False
            client = None
            maxdate = None
            state = None
            i = 1
            while i < len(args):
                if args[i] == '-c' or args[i] == '--client':
                    if client:
                        self.errExit("Only one client per run")
                    client = args[i + 1]
                    print(client)
                    i = i + 1
                elif args[i] == '--days':
                    if maxdate:
                        self.errExit("Max date specified multiple times")
                    try:
                        maxdate = datetime.datetime.now() - datetime.timedelta(days=int(args[i + 1]))
                    except:
                        self.log.error("Invalid number of days: %s" % args[i + 1])
                        raise SystemExit(-1)
                    i = i + 1
                elif args[i] == '--expired':
                    expired = True
                i = i + 1
            if expired:
                if state:
                    self.log.error("--state is not valid with --expired")
                    raise SystemExit(-1)
                self.purge_expired(maxdate)
            else:
                self.purge(client, maxdate, state)
        else:
            print("Unknown command: %s" % args[0])