Ejemplo n.º 1
0
def run_prog(env, cmd, user='******', group='srvadmins'):
    """ Run the cmd as a subprocess as the given user/group """
    run_env = {
        'PATH' : '%(opt)s/bin:/usr/bin:/bin' % env,
        'EBPY_SRV_ROOT' : '%(base)s' % env,
    }
    method = []
    if isinstance(cmd, basestring):
        method.append(cmd)
    else:
        method.extend(cmd)
    cureuid = os.geteuid()
    curegid = os.getegid()
    # Set new ids
    success = False
    try:
        os.setegid(get_gid(group))
        os.seteuid(get_uid(user))
        p = Popen(method, stdout=PIPE, stderr=STDOUT, env=run_env)
        output = p.communicate()[0]
        rc = p.returncode
        success = rc == 0
        log.debug("run_prog: %s returned %s" % (str(method), str(rc)))
        if not success:
            log.debug("run_prog: %s:\n%s" % (str(method), str(output)))
    except NonExistentUser:
        log.error("Can not run program as non-existent user: %s" % str(user))
    except NonExistentGroup:
        log.error("Can not run program as non-existent group: %s" % str(group))
    except Exception, e:
        log.error("Unable to run program: %s" % str(cmd), exc_info=True)
Ejemplo n.º 2
0
def check_dir(path="", uid=None, gid=None, mode=None, path_env={}):
    """ Verifies the path exists and is set to the given uid:gid and mode

    If the path does not exist, it is created and the given permissions are
    set.  Additionally, it will adjust any permissions that are off.

    The `uid` and `gid` can be the numeric value or the name.  The `mode`
    must be provided in octal
    """
    log.debug(
        "check_dir(path=%s, uid=%s, gid=%s, mode=%s, path_env=%s)"
        % (str(path), str(uid), str(gid), str(mode), str(path_env))
    )
    success = False
    if not path:
        log.warning("Trying to create an empty path is a NOOP")
        return True
    path = path % path_env
    try:
        if not os.path.isdir(path):
            os.makedirs(path)
        if uid or gid:
            chown(path, get_uid(uid), get_gid(gid))
        if not (mode is None):
            os.chmod(path, mode)
        success = True
    except NonExistentUser, e:
        log.error("Cannot check_path(%s, %s, %s, %o):  The user %s does not " "exist." % (path, uid, gid, mode, e.uid))
Ejemplo n.º 3
0
def copy(source=None, dest=None, uid=None, gid=None, mode=None, transform=lambda x, y: y.read(), path_env={}):
    """ Copy source to destination """
    success = True
    if not source:
        log.warning("Trying to copy an empty source is a NOOP")
        return True
    sources = glob.glob(source % path_env)
    num_sources = len(sources)
    dst = dest % path_env
    dst_isdir = os.path.isdir(dst) or dst.endswith(os.path.sep) or (num_sources > 1)
    if sources:
        if dst_isdir:
            try:
                os.makedirs(dst)
            except os.error, e:
                # if the directory already exists, ignore the error.
                # Otherwise, propagate the error up.
                if e.errno != 17:
                    raise e
            pass
        for src in sources:
            if os.path.isfile(src):
                try:
                    if dst_isdir:
                        final_dest = os.path.join(dst, os.path.basename(src))
                    else:
                        final_dest = dst
                    open(final_dest, "wb").write(transform(path_env, open(src, "rb")))
                    if uid or gid:
                        chown(final_dest, get_uid(uid), get_gid(gid))
                    if not (mode is None):
                        os.chmod(final_dest, mode)
                except os.error, e:
                    success = False
                    log.error(
                        "Cannot copy(%s, %s, %s, %o):  Err#: %d - Message: %s "
                        "- Filename: %s" % (path, uid, gid, mode, e.errno, e.strerror, getattr(e, "filename", ""))
                    )
            else:
                success = False
                log.error("The source does not exist or is not a file: %s" % str(src))
            continue