Esempio n. 1
0
    def test_exec_ipmitool(self):
        pw_file = "/tmp/password_file"

        self.mox.StubOutWithMock(ipmi, "_make_password_file")
        self.mox.StubOutWithMock(utils, "execute")
        self.mox.StubOutWithMock(utils, "delete_if_exists")
        ipmi._make_password_file(self.ipmi.password).AndReturn(pw_file)
        args = [
            "ipmitool",
            "-I",
            "lanplus",
            "-H",
            self.ipmi.address,
            "-U",
            self.ipmi.user,
            "-f",
            pw_file,
            "A",
            "B",
            "C",
        ]
        utils.execute(*args, attempts=3).AndReturn(("", ""))
        utils.delete_if_exists(pw_file).AndReturn(None)
        self.mox.ReplayAll()

        self.ipmi._exec_ipmitool("A B C")
        self.mox.VerifyAll()
Esempio n. 2
0
 def stop_console(self):
     console_pid = _get_console_pid(self.node_id)
     if console_pid:
         # Allow exitcode 99 (RC_UNAUTHORIZED)
         utils.execute('kill', '-TERM', str(console_pid),
                       run_as_root=True,
                       check_exit_code=[0, 99])
     utils.delete_if_exists(_get_console_pid_path(self.node_id))
Esempio n. 3
0
def make_persistent_password_file(path, password):
    """Writes a file containing a password until deleted."""

    try:
        utils.delete_if_exists(path)
        os.mknod(path, 0o600)
        with open(path, 'wb') as file:
            file.write(password)
        return path
    except Exception as e:
        utils.delete_if_exists(path)
        raise exception.PasswordFileFailedToCreate(error=str(e))
Esempio n. 4
0
def _make_password_file(password):
    try:
        fd, path = tempfile.mkstemp()
        os.fchmod(fd, stat.S_IRUSR | stat.S_IWUSR)
        with os.fdopen(fd, "w") as f:
            f.write(password)

        yield path
        utils.delete_if_exists(path)
    except Exception:
        with excutils.save_and_reraise_exception():
            utils.delete_if_exists(path)
Esempio n. 5
0
def make_persistent_password_file(path, password):
    """Writes a file containing a password until deleted."""

    try:
        utils.delete_if_exists(path)
        with open(path, 'wb') as file:
            os.chmod(path, 0o600)
            file.write(password.encode())
        return path
    except Exception as e:
        utils.delete_if_exists(path)
        raise exception.PasswordFileFailedToCreate(error=e)
Esempio n. 6
0
def make_persistent_password_file(path, password):
    """Writes a file containing a password until deleted."""

    try:
        utils.delete_if_exists(path)
        with open(path, "wb") as file:
            os.chmod(path, 0o600)
            file.write(password.encode())
        return path
    except Exception as e:
        utils.delete_if_exists(path)
        raise exception.PasswordFileFailedToCreate(error=e)
Esempio n. 7
0
def _make_password_file(password):
    try:
        fd, path = tempfile.mkstemp()
        os.fchmod(fd, stat.S_IRUSR | stat.S_IWUSR)
        with os.fdopen(fd, "w") as f:
            f.write(password)

        yield path
        utils.delete_if_exists(path)
    except Exception:
        with excutils.save_and_reraise_exception():
            utils.delete_if_exists(path)
Esempio n. 8
0
    def test_console_pid_nan(self):
        fd, path = tempfile.mkstemp()
        with os.fdopen(fd, 'w') as f:
            f.write("hello world\n")

        self.mox.StubOutWithMock(ipmi, '_get_console_pid_path')
        ipmi._get_console_pid_path(self.ipmi.node_id).AndReturn(path)
        self.mox.ReplayAll()

        pid = ipmi._get_console_pid(self.ipmi.node_id)
        utils.delete_if_exists(path)
        self.mox.VerifyAll()
        self.assertTrue(pid is None)
Esempio n. 9
0
    def test_console_pid_nan(self):
        fd, path = tempfile.mkstemp()
        with os.fdopen(fd, 'w') as f:
            f.write("hello world\n")

        self.mox.StubOutWithMock(ipmi, '_get_console_pid_path')
        ipmi._get_console_pid_path(self.ipmi.node_id).AndReturn(path)
        self.mox.ReplayAll()

        pid = ipmi._get_console_pid(self.ipmi.node_id)
        utils.delete_if_exists(path)
        self.mox.VerifyAll()
        self.assertTrue(pid is None)
Esempio n. 10
0
    def test_console_pid(self):
        fd, path = tempfile.mkstemp()
        with os.fdopen(fd, "w") as f:
            f.write("12345\n")

        self.mox.StubOutWithMock(ipmi, "_get_console_pid_path")
        ipmi._get_console_pid_path(self.ipmi.node_id).AndReturn(path)
        self.mox.ReplayAll()

        pid = ipmi._get_console_pid(self.ipmi.node_id)
        utils.delete_if_exists(path)
        self.mox.VerifyAll()
        self.assertEqual(pid, 12345)
Esempio n. 11
0
 def _exec_ipmitool(self, command):
     args = [
         'ipmitool', '-I', 'lanplus', '-H', self.address, '-U', self.user,
         '-f'
     ]
     pwfile = _make_password_file(self.password)
     try:
         args.append(pwfile)
         args.extend(command.split(" "))
         out, err = utils.execute(*args, attempts=3)
         LOG.debug(_("ipmitool stdout: '%(out)s', stderr: '%(err)s'"),
                   locals())
         return out, err
     finally:
         utils.delete_if_exists(pwfile)
Esempio n. 12
0
def _make_password_file(password):
    """Makes a temporary file that contains the password.

    :param password: the password
    :returns: the absolute pathname of the temporary file
    :raises: Exception from creating or writing to the temporary file
    """
    try:
        fd, path = tempfile.mkstemp()
        os.fchmod(fd, stat.S_IRUSR | stat.S_IWUSR)
        with os.fdopen(fd, "w") as f:
            f.write(password)

        yield path
        utils.delete_if_exists(path)
    except Exception:
        with excutils.save_and_reraise_exception():
            utils.delete_if_exists(path)
Esempio n. 13
0
def _make_password_file(password):
    """Makes a temporary file that contains the password.

    :param password: the password
    :returns: the absolute pathname of the temporary file
    :raises: Exception from creating or writing to the temporary file
    """
    try:
        fd, path = tempfile.mkstemp()
        os.fchmod(fd, stat.S_IRUSR | stat.S_IWUSR)
        with os.fdopen(fd, "w") as f:
            f.write(password)

        yield path
        utils.delete_if_exists(path)
    except Exception:
        with excutils.save_and_reraise_exception():
            utils.delete_if_exists(path)
Esempio n. 14
0
 def _exec_ipmitool(self, command):
     args = ['ipmitool',
             '-I',
             'lanplus',
             '-H',
             self.address,
             '-U',
             self.user,
             '-f']
     pwfile = _make_password_file(self.password)
     try:
         args.append(pwfile)
         args.extend(command.split(" "))
         out, err = utils.execute(*args, attempts=3)
         LOG.debug(_("ipmitool stdout: '%(out)s', stderr: '%(err)s'"),
                   locals())
         return out, err
     finally:
         utils.delete_if_exists(pwfile)
Esempio n. 15
0
def _make_password_file(password):
    """Makes a temporary file that contains the password.

    :param password: the password
    :returns: the absolute pathname of the temporary file
    :raises: PasswordFileFailedToCreate from creating or writing to the
             temporary file
    """
    try:
        fd, path = tempfile.mkstemp()
        os.fchmod(fd, stat.S_IRUSR | stat.S_IWUSR)
        with os.fdopen(fd, "w") as f:
            f.write(password)

        yield path
        utils.delete_if_exists(path)
    except Exception as exc:
        utils.delete_if_exists(path)
        raise exception.PasswordFileFailedToCreate(error=exc)
Esempio n. 16
0
def _make_password_file(password):
    """Makes a temporary file that contains the password.

    :param password: the password
    :returns: the absolute pathname of the temporary file
    :raises: PasswordFileFailedToCreate from creating or writing to the
             temporary file
    """
    try:
        fd, path = tempfile.mkstemp()
        os.fchmod(fd, stat.S_IRUSR | stat.S_IWUSR)
        with os.fdopen(fd, "w") as f:
            f.write(password)

        yield path
        utils.delete_if_exists(path)
    except Exception as exc:
        utils.delete_if_exists(path)
        raise exception.PasswordFileFailedToCreate(error=exc)
Esempio n. 17
0
    def test_exec_ipmitool(self):
        pw_file = '/tmp/password_file'

        self.mox.StubOutWithMock(ipmi, '_make_password_file')
        self.mox.StubOutWithMock(utils, 'execute')
        self.mox.StubOutWithMock(utils, 'delete_if_exists')
        ipmi._make_password_file(self.ipmi.password).AndReturn(pw_file)
        args = [
                'ipmitool',
                '-I', 'lanplus',
                '-H', self.ipmi.address,
                '-U', self.ipmi.user,
                '-f', pw_file,
                'A', 'B', 'C',
                ]
        utils.execute(*args, attempts=3).AndReturn(('', ''))
        utils.delete_if_exists(pw_file).AndReturn(None)
        self.mox.ReplayAll()

        self.ipmi._exec_ipmitool('A B C')
        self.mox.VerifyAll()
Esempio n. 18
0
    def start_console(self):
        if not self.port:
            return
        args = []
        args.append(CONF.terminal)
        if CONF.terminal_cert_dir:
            args.append("-c")
            args.append(CONF.terminal_cert_dir)
        else:
            args.append("-t")
        args.append("-p")
        args.append(str(self.port))
        args.append("--background=%s" % _get_console_pid_path(self.node_id))
        args.append("-s")

        try:
            pwfile = _make_password_file(self.password)
            ipmi_args = "/:%(uid)s:%(gid)s:HOME:ipmitool -H %(address)s" \
                    " -I lanplus -U %(user)s -f %(pwfile)s sol activate" \
                    % {'uid': os.getuid(),
                       'gid': os.getgid(),
                       'address': self.address,
                       'user': self.user,
                       'pwfile': pwfile,
                       }

            args.append(ipmi_args)
            # Run shellinaboxd without pipes. Otherwise utils.execute() waits
            # infinitely since shellinaboxd does not close passed fds.
            x = ["'" + arg.replace("'", "'\\''") + "'" for arg in args]
            x.append('</dev/null')
            x.append('>/dev/null')
            x.append('2>&1')
            utils.execute(' '.join(x), shell=True)
        finally:
            utils.delete_if_exists(pwfile)
Esempio n. 19
0
    def start_console(self):
        if not self.port:
            return
        args = []
        args.append(CONF.terminal)
        if CONF.terminal_cert_dir:
            args.append("-c")
            args.append(CONF.terminal_cert_dir)
        else:
            args.append("-t")
        args.append("-p")
        args.append(str(self.port))
        args.append("--background=%s" % _get_console_pid_path(self.node_id))
        args.append("-s")

        try:
            pwfile = _make_password_file(self.password)
            ipmi_args = "/:%(uid)s:%(gid)s:HOME:ipmitool -H %(address)s" \
                    " -I lanplus -U %(user)s -f %(pwfile)s sol activate" \
                    % {'uid': os.getuid(),
                       'gid': os.getgid(),
                       'address': self.address,
                       'user': self.user,
                       'pwfile': pwfile,
                       }

            args.append(ipmi_args)
            # Run shellinaboxd without pipes. Otherwise utils.execute() waits
            # infinitely since shellinaboxd does not close passed fds.
            x = ["'" + arg.replace("'", "'\\''") + "'" for arg in args]
            x.append('</dev/null')
            x.append('>/dev/null')
            x.append('2>&1')
            utils.execute(' '.join(x), shell=True)
        finally:
            utils.delete_if_exists(pwfile)