Beispiel #1
0
def session_rescan(timeout=None):
    args = [constants.EXT_ISCSIADM, "-m", "session", "-R"]

    p = commands.start(args,
                       sudo=True,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)

    try:
        out, err = p.communicate(timeout=timeout)
    except subprocess.TimeoutExpired:
        # TODO: Raising a timeout allows a new scan to start before this scan
        # terminates. The new scan is likely to be blocked until this scan
        # terminates.
        commands.wait_async(p)
        raise IscsiSessionRescanTimeout(p.pid, timeout)

    # This is an expected condition before connecting to iSCSI storage
    # server during startup, or after disconnecting.
    if p.returncode == ISCSI_ERR_OBJECT_NOT_FOUND:
        log.debug("No iSCSI sessions found")
        return

    # Any other error is reported to the caller.
    if p.returncode != 0:
        raise IscsiSessionError(p.returncode, out, err)
Beispiel #2
0
    def test_normal_termination(self):
        event = threading.Event()
        p = commands.start(["sleep", "0.1"])

        # Start async waiter waiting for normal terminatin.
        commands.wait_async(p, event=event)

        if not event.wait(1):
            raise RuntimeError("Error waiting for termination")

        assert p.returncode == 0
Beispiel #3
0
    def test_out_err(self):
        event = threading.Event()
        p = commands.start(["sh", "-c", "echo out>&1; echo err>&2; sleep 0.1"],
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)

        commands.wait_async(p, event=event)

        if not event.wait(1):
            raise RuntimeError("Error waiting for termination")

        assert p.returncode == 0
Beispiel #4
0
    def test_terminate_async(self):
        event = threading.Event()
        p = commands.start(["sleep", "10"])

        # Terminate the command without waiting for it, and start async waiter.
        p.terminate()
        commands.wait_async(p, event=event)

        if not event.wait(1):
            raise RuntimeError("Error waiting for termination")

        assert p.returncode == -15
Beispiel #5
0
    def _runAs(self, user, groups, func, args=(), kwargs={}):
        def child(writer):
            try:
                uid = resolveUid(user)

                if groups:
                    gids = [resolveGid(g) for g in groups]
                    os.setgid(gids[0])
                    os.setgroups(gids)

                os.setuid(uid)

                res = func(*args, **kwargs)

                writer.send((res, None))
            except BaseException as e:
                writer.send((None, e))

            writer.close()

        reader, writer = Pipe(duplex=False)
        with closing(reader), closing(writer):
            proc = Process(target=child, args=(writer,))
            proc.start()
            try:
                if not reader.poll(RUN_AS_TIMEOUT):
                    raise Timeout()

                res, err = reader.recv()
                if err is not None:
                    raise err

                return res
            finally:
                proc.terminate()
                proc.join(1)

                if proc.exitcode is None:
                    try:
                        os.kill(proc.pid, signal.SIGKILL)
                    except ProcessLookupError:
                        pass
                    else:
                        commands.wait_async(PopenAdapter(proc))
Beispiel #6
0
def _rescan():
    """
    Called from supervdsm to perform rescan as root.
    """
    timeout = config.getint('irs', 'scsi_rescan_maximal_timeout')

    p = commands.start(
        [constants.EXT_FC_SCAN],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
    try:
        out, err = p.communicate(timeout=timeout)
    except subprocess.TimeoutExpired:
        # TODO: Raising a timeout allows a new scan to start before this scan
        # terminates. The new scan is likely to be blocked until this scan
        # terminates.
        commands.wait_async(p)
        raise Error("Timeout scanning (pid=%s)" % p.pid)

    if p.returncode != 0:
        raise Error("Scan failed with rc=%s out=%r err=%r"
                    % (p.returncode, out, err))