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()
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))
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))
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)
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)
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)
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)
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)
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)
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)
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)
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)
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()
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)