From 3b396e83210703991f15f9b41cb1bb10834db17c Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Thu, 13 Jun 2019 16:02:58 -0400 Subject: virt-install: Split out --wait handling into a helper class And add much more clitest coverage --- virt-install | 130 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 75 insertions(+), 55 deletions(-) (limited to 'virt-install') diff --git a/virt-install b/virt-install index 240c9f28..0646ac85 100755 --- a/virt-install +++ b/virt-install @@ -562,38 +562,74 @@ def build_guest_instance(conn, options): # Install process helpers # ########################### -def start_install(guest, installer, options): - if options.wait is not None: - wait_on_install = True - wait_time = options.wait * 60 - else: - wait_on_install = False - wait_time = -1 +def _sleep(secs): + if not cli.in_testsuite(): + time.sleep(secs) # pragma: no cover - # If --wait specified, we don't want the default behavior of waiting - # for virt-viewer to exit, since then we can't exit the app when time - # expires - wait_on_console = not wait_on_install - if wait_time == 0: - # --wait 0 implies --noautoconsole - autoconsole = False - else: - autoconsole = options.autoconsole +class WaitHandler: + """ + Helper class for handling the --wait option sleeping and time tracking + """ + def __init__(self, wait): + self.wait_is_requested = False + self._wait_mins = 0 + self._start_time = 0 + + if wait is not None: + self.wait_is_requested = True + self._wait_mins = wait + + @property + def wait_for_console_to_exit(self): + # If --wait specified, we don't want the default behavior of waiting + # for virt-viewer to exit, we want to launch it, then manually count + # down time for ourselves + return not self.wait_is_requested + @property + def _wait_forever(self): + return self._wait_mins < 0 + @property + def _wait_secs(self): + return self._wait_mins * 60 + + def start(self): + self._start_time = time.time() + + def get_time_string(self): + timestr = _(" %d minutes") % self._wait_secs + if self._wait_forever: + timestr = "" + ret = _("Waiting%(time_string)s for installation to complete.") % { + "time_string": timestr} + return ret + + def wait(self): + """ + sleep 1 second, then teturn True if wait time has expired + """ + _sleep(1) + if self._wait_forever: + if cli.in_testsuite(): + return True + return False # pragma: no cover + time_elapsed = (time.time() - self._start_time) + return (time_elapsed >= self._wait_secs) or cli.in_testsuite() + + +def start_install(guest, installer, options): conscb = None - if autoconsole: + if options.autoconsole: conscb = cli.get_console_cb(guest) - if not conscb: + if not conscb and options.wait is None: # If there isn't any console to actually connect up, # default to --wait -1 to get similarish behavior - autoconsole = False - if options.wait is None: - logging.warning(_("No console to launch for the guest, " - "defaulting to --wait -1")) - wait_on_install = True - wait_time = -1 + logging.warning(_("No console to launch for the guest, " + "defaulting to --wait -1")) + options.wait = -1 + waithandler = WaitHandler(options.wait) meter = cli.get_meter() logging.debug("Guest.has_install_phase: %s", installer.has_install_phase()) @@ -603,7 +639,7 @@ def start_install(guest, installer, options): domain = None try: - start_time = time.time() + waithandler.start() domain = installer.start_install(guest, meter=meter, doboot=not options.noreboot, @@ -612,10 +648,10 @@ def start_install(guest, installer, options): if options.destroy_on_exit: atexit.register(_destroy_on_exit, domain) - cli.connect_console(guest, domain, conscb, wait_on_console, + cli.connect_console(guest, domain, conscb, + waithandler.wait_for_console_to_exit, options.destroy_on_exit) - check_domain(installer, domain, conscb, options.transient, - wait_on_install, wait_time, start_time) + check_domain(installer, domain, conscb, options.transient, waithandler) print_stdout(_("Domain creation completed.")) if not options.transient and not domain.isActive(): @@ -644,8 +680,7 @@ def start_install(guest, installer, options): _destroy_on_exit(domain) -def check_domain(installer, domain, conscb, transient, - wait_for_install, wait_time, start_time): +def check_domain(installer, domain, conscb, transient, waithandler): """ Make sure domain ends up in expected state, and wait if for install to complete if requested @@ -674,16 +709,14 @@ def check_domain(installer, domain, conscb, transient, # just closed the console and the VM is still running. In the # the former case, libvirt may not have caught up yet with the # VM having exited, so wait a bit and check again - if not cli.in_testsuite(): - time.sleep(2) # pragma: no cover + _sleep(2) if check_domain_inactive(): return # pragma: no cover # If we reach here, the VM still appears to be running. - if not wait_for_install or wait_time == 0: + if not waithandler.wait_is_requested: # User either: # used --noautoconsole - # used --wait 0 # killed console and guest is still running if not installer.has_install_phase(): return @@ -693,13 +726,8 @@ def check_domain(installer, domain, conscb, transient, " to \nthe console to complete the installation process.")) sys.exit(0) - wait_forever = (wait_time < 0) - timestr = (not wait_forever and - _(" %d minutes") % (int(wait_time) / 60) or "") - print_stdout( - _("Domain installation still in progress. Waiting" - "%(time_string)s for installation to complete.") % - {"time_string": timestr}) + print_stdout(_("Domain installation still in progress.")) + print_stdout(waithandler.get_time_string()) # Wait loop while True: @@ -707,20 +735,12 @@ def check_domain(installer, domain, conscb, transient, print_stdout(_("Domain has shutdown. Continuing.")) break - if not cli.in_testsuite(): # pragma: no cover - time.sleep(1) - - time_elapsed = (time.time() - start_time) - if not cli.in_testsuite(): # pragma: no cover - if wait_forever: - continue - if time_elapsed < wait_time: - continue - - print_stdout( - _("Installation has exceeded specified time limit. " - "Exiting application.")) - sys.exit(1) + done = waithandler.wait() + if done: + print_stdout( + _("Installation has exceeded specified time limit. " + "Exiting application.")) + sys.exit(1) ######################## -- cgit v1.2.1