def console(self, timeout=CONSOLE_TIMEOUT): command = " ".join(_VIRSH + ["console", "--force", self.name]) self.logger.debug("opening console with: %s", command) console = shell.Remote(command, hostname=self.host_name, prefix=self.prefix) # Give the virsh process a chance set up its control-c # handler. Otherwise something like control-c as the first # character sent might kill it. If the machine is down, it # will get an EOF. if console.expect( [ pexpect.EOF, # earlier "Connected to domain %s\r\nEscape character is \\^]" % self.name, # libvirt >= 7.0 "Connected to domain '%s'\r\nEscape character is \\^] \(Ctrl \+ ]\)\r\n" % self.name ], timeout=timeout) == 0: self.logger.debug("got EOF from console") console.close() console = None return console
def console(self, timeout=CONSOLE_TIMEOUT): if self._console: self.logger.info("console already open") return self._console if self._console is False: self.logger.info("console already failed") return self._console # self._console is None command_args = _VIRSH + ["console", "--force", self.domain_name] self.logger.info("spawning: %s", " ".join(command_args)) self._console = shell.Remote(command_args, self.logger, hostname=self.host_name) # Give the virsh process a chance set up its control-c # handler. Otherwise something like control-c as the first # character sent might kill it. If the machine is down, it # will get an EOF. if self._console.expect( [ pexpect.EOF, # earlier "Connected to domain %s\r\nEscape character is \\^]" % self.domain_name, # libvirt >= 7.0 "Connected to domain '%s'\r\nEscape character is \\^] \(Ctrl \+ ]\)\r\n" % self.domain_name ], timeout=timeout) > 0: return self._console self.logger.debug("got EOF from console") self._console.close() self._console = False
def console(self, timeout=CONSOLE_TIMEOUT): command = "sudo virsh console --force %s" % (self.name) self.logger.debug("opening console with: %s", command) console = shell.Remote(command, hostname=self.hostname, logger=self.logger) # Give the virsh process a chance set up its control-c # handler. Otherwise something like control-c as the first # character sent might kill it. If the machine is down, it # will get an EOF. if console.expect(["Connected to domain %s\r\n" % self.name, pexpect.EOF], timeout=timeout): self.logger.debug("got EOF from console") console.close() console = None return console
def connect_to_kvm(args): vmlist = subprocess.getoutput("sudo virsh list") running = 0 for line in vmlist.split("\n")[2:]: try: num, host, state = line.split() if host == args.hostname and state == "running": running = 1 print("Found %s running already" % args.hostname) continue except: pass if args.reboot: waittime = 20 if not running: print("Booting %s - pausing %s seconds" % (args.hostname, waittime)) subprocess.getoutput("sudo virsh start %s" % args.hostname) time.sleep(waittime) else: subprocess.getoutput("sudo virsh reboot %s" % args.hostname) print("Rebooting %s - pausing %s seconds" % (args.hostname, waittime)) time.sleep(waittime) print("Taking %s console by force" % args.hostname) cmd = "sudo virsh console --force %s" % args.hostname timer = 120 # Need to use spawnu with python3. child = shell.Remote(cmd, hostname=args.hostname, username="******") child.output(sys.stdout) prompt = child.prompt print("Shell prompt '%s'" % prompt.pattern) done = 0 tries = 60 print("Waiting on %s login: or shell prompt" % (args.hostname)) while not done and tries != 0: try: print("sending ctrl-c return") #child = pexpect.spawn (cmd) #child.sendcontrol('c') child.sendline('') print("found, waiting on login: or shell prompt") res = child.expect(['login: '******'root') print("found, expecting password prompt") child.expect('Password:'******'swan') print("waiting on root shell prompt") child.expect(prompt, timeout=1) print("done") done = 1 elif res == 1: print('----------------------------------------------------') print('Already logged in as root on %s' % args.hostname) print('----------------------------------------------------') done = 1 except: print("(%s [%s] waiting)" % (args.hostname, tries)) tries -= 1 time.sleep(1) if not done: print('console is not answering on host %s, aborting' % args.hostname) return None # Make certain that both ends are in sync and set up as expected. child.sync() return child