def run_command(self, argv, set_env=True, stdin_text=None, log_stdout=True, raiseonerr=True, cwd=None): # This will give us a Bash shell command = self.transport.start_shell(argv, log_stdout=log_stdout) # Set working directory if cwd is None: cwd = self.config.test_dir command.stdin.write('cd %s\n' % ipautil.shell_quote(cwd)) # Set the environment if set_env: command.stdin.write('. %s\n' % ipautil.shell_quote(self.env_sh_path)) command.stdin.write('set -e\n') if isinstance(argv, basestring): # Run a shell command given as a string command.stdin.write('(') command.stdin.write(argv) command.stdin.write(')') else: # Run a command given as a popen-style list (no shell expansion) for arg in argv: command.stdin.write(ipautil.shell_quote(arg)) command.stdin.write(' ') command.stdin.write(';exit\n') if stdin_text: command.stdin.write(stdin_text) command.stdin.flush() command.wait(raiseonerr=raiseonerr) return command
def backup_file(host, filename): if host.transport.file_exists(filename): backupname = os.path.join(host.config.test_dir, "file_backup", filename.lstrip("/")) host.transport.mkdir_recursive(os.path.dirname(backupname)) host.run_command(["cp", "-af", filename, backupname]) return True else: rmname = os.path.join(host.config.test_dir, "file_remove") host.run_command("echo %s >> %s" % (ipautil.shell_quote(filename), ipautil.shell_quote(rmname))) contents = host.get_file_contents(rmname) host.transport.mkdir_recursive(os.path.dirname(rmname)) return False
def backup_file(host, filename): if host.transport.file_exists(filename): backupname = os.path.join(host.config.test_dir, 'file_backup', filename.lstrip('/')) host.transport.mkdir_recursive(os.path.dirname(backupname)) host.run_command(['cp', '-af', filename, backupname]) return True else: rmname = os.path.join(host.config.test_dir, 'file_remove') host.run_command('echo %s >> %s' % ( ipautil.shell_quote(filename), ipautil.shell_quote(rmname))) host.transport.mkdir_recursive(os.path.dirname(rmname)) return False
def restore_files(host): backupname = os.path.join(host.config.test_dir, 'file_backup') rmname = os.path.join(host.config.test_dir, 'file_remove') # Prepare command for restoring context of the backed-up files sed_remove_backupdir = 's/%s//g' % backupname.replace('/', '\/') restorecon_command = ( "find %s | " "sed '%s' | " "sed '/^$/d' | " "xargs -d '\n' " "/sbin/restorecon -v" % (backupname, sed_remove_backupdir)) # Prepare command for actual restoring of the backed up files copyfiles_command = 'if [ -d %(dir)s/ ]; then cp -arvf %(dir)s/* /; fi' % { 'dir': ipautil.shell_quote(backupname)} # Run both commands in one session. For more information, see: # https://fedorahosted.org/freeipa/ticket/4133 host.run_command('%s ; (%s ||:)' % (copyfiles_command, restorecon_command)) # Remove all the files that did not exist and were 'backed up' host.run_command(['xargs', '-d', r'\n', '-a', rmname, 'rm', '-vf'], raiseonerr=False) host.run_command(['rm', '-rvf', backupname, rmname], raiseonerr=False)
def fix_hostname(host): backup_file(host, paths.ETC_HOSTNAME) host.put_file_contents(paths.ETC_HOSTNAME, host.hostname + '\n') host.run_command(['hostname', host.hostname]) backupname = os.path.join(host.config.test_dir, 'backup_hostname') host.run_command('hostname > %s' % ipautil.shell_quote(backupname))
def fix_hostname(host): backup_file(host, paths.ETC_HOSTNAME) host.put_file_contents(paths.ETC_HOSTNAME, host.hostname + "\n") host.run_command(["hostname", host.hostname]) backupname = os.path.join(host.config.test_dir, "backup_hostname") host.run_command("hostname > %s" % ipautil.shell_quote(backupname))
def run_beakerlib_command(self, cmd): """Given a command as a Popen-style list, run it in the Bash process""" if not self.bash: return for word in cmd: self.bash.stdin.write(ipautil.shell_quote(word)) self.bash.stdin.write(' ') self.bash.stdin.write('\n') self.bash.stdin.flush() assert self.bash.returncode is None, "BeakerLib Bash process exited"
def restore_files(host): backupname = os.path.join(host.config.test_dir, 'file_backup') rmname = os.path.join(host.config.test_dir, 'file_remove') # Restore the backed up files host.run_command('cp -arvf %s/* /' % ipautil.shell_quote(backupname), raiseonerr=False) # Restore context of the backed-up files sed_remove_backupdir = 's/%s//g' % backupname.replace('/', '\/') host.run_command("find %s | " "sed '%s' | " "sed '/^$/d' | " "xargs -d '\n' " "/sbin/restorecon -v" % (backupname, sed_remove_backupdir)) # Remove all the files that did not exist and were 'backed up' host.run_command(['xargs', '-d', r'\n', '-a', rmname, 'rm', '-vf'], raiseonerr=False) host.run_command(['rm', '-rvf', backupname, rmname], raiseonerr=False)
def env_to_script(env): return ''.join([ 'export %s=%s\n' % (key, ipautil.shell_quote(value)) for key, value in env.items() ])
def shell_quote(string): if re.match('^[-._~a-zA-Z0-9]+$', string): return string else: return ipautil.shell_quote(string)
def restore_files(host): backupname = os.path.join(host.config.test_dir, "file_backup") rmname = os.path.join(host.config.test_dir, "file_remove") # Prepare command for restoring context of the backed-up files sed_remove_backupdir = "s/%s//g" % backupname.replace("/", "\/") restorecon_command = ( "find %s | " "sed '%s' | " "sed '/^$/d' | " "xargs -d '\n' " "/sbin/restorecon -v" % (backupname, sed_remove_backupdir) ) # Prepare command for actual restoring of the backed up files copyfiles_command = "if [ -d %(dir)s/ ]; then cp -arvf %(dir)s/* /; fi" % {"dir": ipautil.shell_quote(backupname)} # Run both commands in one session. For more information, see: # https://fedorahosted.org/freeipa/ticket/4133 host.run_command("%s ; (%s ||:)" % (copyfiles_command, restorecon_command)) # Remove all the files that did not exist and were 'backed up' host.run_command(["xargs", "-d", r"\n", "-a", rmname, "rm", "-vf"], raiseonerr=False) host.run_command(["rm", "-rvf", backupname, rmname], raiseonerr=False)
def env_to_script(env): return ''.join(['export %s=%s\n' % (key, ipautil.shell_quote(value)) for key, value in env.items()])