Ejemplo n.º 1
0
def tartarus_import(*args):
    """This function is used as import hook in Tartarus."""

    name = args[0]
    if len(args) > 3:
        fromlist = args[3]
    else:
        fromlist = None

    logging.trace(
        __name__,
        "Import hook invoked for name '%s', fromlist = %s" % (name, fromlist),
        trace >= 16)
    if name.startswith(_MOD_PREFIX):
        try:
            mname = name[_MOD_PREFIX_LEN:]
            load(mname)
        except Exception:
            pass

    elif name == _MOD_PREFIX[:-1] and fromlist is not None:
        # import in form "from $_MOD_PREFIX import ModuleName1, ModuleName2"
        for mname in fromlist:
            try:
                load(mname)
            except Exception:
                pass

    return _orig_import(*args)
Ejemplo n.º 2
0
 def execute(self, con, query, *params):
     cur = con.cursor()
     q = self.trans.query(query, len(params))
     p = self.trans.params(params)
     if self.trace > 16:
         logging.trace(
             __name__, "Query on database %s:\n%s" %
             (self.options['database'], _query2string(query, params)))
     cur.execute(q, p)
     return cur
Ejemplo n.º 3
0
def init(adapter):
    c = adapter.getCommunicator()
    logging.trace("Tartarus.Time",
                  "Initializing...",
                  log_to=c,
                  cond="Tartarus.Time.trace")
    #d = c.getProperties().getPropertyAsInt('Tartarus.Time.deploy')

    adapter.add(service.service(), c.stringToIdentity("Service/Time"))
    loc = auth.SrvLocator()
    loc.add_object(TimeI.TimeI(), c.stringToIdentity("Time/Server"))
    adapter.addServantLocator(loc, "Time")
Ejemplo n.º 4
0
def init(adapter):
    c = adapter.getCommunicator()
    logging.trace("Tartarus.Kadmin5",
                  "Initializing...",
                  log_to=c,
                  cond="Tartarus.Kadmin5.trace")
    d = c.getProperties().getPropertyAsInt('Tartarus.Kadmin5.deploy')

    kdb_common = kdb.Kdb(d)

    adapter.add(kdb.KadminService(kdb_common),
                c.stringToIdentity("Service/Kerberos"))
    loc = auth.SrvLocator()
    loc.add_object(KadminI.KadminI(kdb_common),
                   c.stringToIdentity("Kerberos/Kadmin"))
    adapter.addServantLocator(loc, "Kerberos")
Ejemplo n.º 5
0
    def create_db(self):
        dbh = self._dbh
        logging.trace(__name__, 'Initializing empty database', dbh.trace)
        try:
            create = _creator_map[dbh.modname]
        except KeyError:
            raise dbh.ConfigError('Database engine not supported', dbh.engine)

        try:
            con = dbh.get_connection()
            for q in create:
                dbh.execute(con, q)
            con.commit()
        except dbh.Error, e:
            raise dbh.DBError(
                    'Database failure while initializing new database',
                    e.message)
Ejemplo n.º 6
0
 def execute_many(self, con, query, mparams):
     if self.trace > 16:
         mparams = list(mparams)
         if len(mparams) == 0:
             logging.trace(
                 __name__,
                 "Multiple query with empty paramter list, %s" % query)
         else:
             logging.trace(
                 __name__, "Multiple query on database %s, first is:\n%s" %
                 (self.options['database'], _query2string(
                     query, mparams[0])))
     cur = con.cursor()
     l = query.count('%s')
     q = self.trans.query(query, l)
     p = [self.trans.params(p) for p in mparams]
     cur.executemany(q, p)
     return cur
Ejemplo n.º 7
0
def load_config(props, cfg_path):
    if len(cfg_path) == 0:
        logging.warning("Tartarus configuration path not specified")
        return
    logging.trace(__name__, "Loading configuration from %s" % cfg_path, trace)
    if not os.path.isdir(cfg_path):
        logging.error("Invalid cfg_path to configuration files: %s" % cfg_path)
        return

    for fi in os.listdir(cfg_path):
        f = os.path.join(cfg_path, fi)
        if not os.path.isfile(f):
            continue
        if not (f.endswith('.conf') or f.endswith('.config')):
            continue
        try:
            props.load(f)
        except Exception:
            logging.error("Failed to load configuration file: %s" % f)
Ejemplo n.º 8
0
def load_module(modname, adapter):

    logging.trace(__name__, "Loading module %s." % modname, trace)

    modname = "Tartarus.%s" % modname
    try:
        __import__(modname)
        module = sys.modules[modname]
        module.init(adapter)
        modules.append(module)
        return True
    except Exception:
        et, ev, tb = sys.exc_info()
        if et is ImportError and ev.args[0] == modname:
            logging.error("Failed to load module %s, skipping." % modname)
            return False
        if trace <= 16:
            tb = None
        msg = str().join(traceback.format_exception(et, ev, tb))
        logging.error("Failed to load module %s: %s" %
                (modname, msg.strip()))
        return False
Ejemplo n.º 9
0
def load(name):
    """Load Tartarus interface.

    Name is in form ModName.SubModName.SubModName ...

    This function finds directory ModName in n Tartarus.slices.path and loads
    all slice files from it in hope that it makes module Tartarus.iface.<name>
    avaliable.
    """
    global path

    # append default path if needed.
    if len(path) == 0:
        path = [os.path.join(sys.prefix, 'share', 'Tartarus', 'slice')]

    period = name.find(".")
    if period > 0:
        modname = name[:period]
    else:
        modname = name

    if modname in _loaded_modules:
        logging.trace(__name__, "Found %s interface in cache" % modname,
                      trace >= 4)
        return

    logging.trace(__name__, "Loading %s interface" % modname, trace)

    mpath = None
    for p in path:
        test = os.path.join(p, modname)
        if os.path.isdir(test):
            mpath = test
            break

    logging.trace(__name__, "In path %s found dir %s" % (path, mpath),
                  trace >= 16)

    if mpath is None:
        raise RuntimeError("Could not find module '%s' in path %s" %
                           (modname, path))

    files = glob.glob(os.path.join(mpath, "*.ice"))
    logging.trace(__name__, "Loading slices: %s" % files, trace >= 16)
    Ice.loadSlice("--all -I%s" % mpath, ["-I%s" % d for d in path] + files)
    _loaded_modules[modname] = sys.modules[_MOD_PREFIX + modname]
Ejemplo n.º 10
0
    files = [('/etc/krb5.conf', _kt('krb5.conf')),
             (_kc('kdc.conf'), _kt('kdc.conf')),
             (_kc('kadm5.acl'), _kt('kadm5.acl'))]

    try:
        for n, t in files:
            gen_config_from_file(n, t, opts, backup)
    except KeyError, e:
        raise C.ConfigError("Mandatory option not specified.", e.args[0])
    except IOError, e:
        msg = "Configuration reset failed: %s" % e.args[1]
        if e.filename and len(e.filename) > 0:
            msg += ": " + e.filename
        raise C.RuntimeError("Configuration reset failed: %s" % msg)
    except Exception, e:
        logging.trace(__name__, "Unknown error: %s" % e)
        raise C.RuntimeError("Configuration reset failed.")


# {{{2 Miscellaneous


def _run_command(args, msg):
    try:
        p = subprocess.Popen(args,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        res = p.wait()
        s = p.communicate()[1]
        if res != 0:
            raise C.RuntimeError(s)