~pjdc/mojo/clarify-container-class-message

« back to all changes in this revision

Viewing changes to mojo/juju/status.py

  • Committer: mergebot at canonical
  • Author(s): "Aurelien Lourot"
  • Date: 2020-05-29 12:06:38 UTC
  • mfrom: (547.1.4 mojo)
  • Revision ID: mergebot@juju-139df4-prod-is-toolbox-0.canonical.com-20200529120638-vq49d73o1sc5u5t9
Fix Juju2Status.check_and_wait()

Reviewed-on: https://code.launchpad.net/~aurelien-lourot/mojo/mojo/+merge/384531
Reviewed-by: Tom Haddon <tom.haddon@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 
9
9
from collections import Counter
10
10
 
11
 
from .wait import wait
 
11
from .wait import wait, JujuWaitException
12
12
from ..shutil_which import get_command
13
13
 
14
14
# We can't use the implementation in .utils due to a circular import
470
470
        return True
471
471
 
472
472
    def check_and_wait(self, wait_for_steady=False, max_wait=None, timeout=1800):
473
 
        """ Check the current status of Juju raise JujuStatusError on error state. Optionally wait for busy states.
474
 
            Note: max_wait and timeout are redundant and both exist only for compatibility with Juju1.
 
473
        """ Wait for the current status of Juju to become ready (e.g. not blocked or in maintenance) and raise
 
474
            JujuStatusError on error state. Optionally at the end wait for the current status to become steady
 
475
            (i.e. not running any hook).
475
476
        Args:
476
 
            wait_for_steady: Wait for busy state to clear.
477
 
            max_wait: If defined used instead of timeout.
478
 
            timeout: Max amount of time to wait for a busy state to clear. Raise JujuWaitException if this passes.
 
477
            wait_for_steady: Whether or not to wait for steadiness at the end.
 
478
            max_wait: If defined used instead of timeout when waiting for steadiness.
 
479
            timeout: Max amount of time to wait for readiness. Raise JujuWaitException after timeout. Also Max
 
480
                amount of time to wait for steadiness if max_wait is None.
479
481
        Returns: None
480
482
        """
481
 
        if max_wait is not None:
482
 
            timeout = max_wait
483
 
 
484
 
        self.status(force_update=True)
485
 
 
486
 
        # self.ready() will raise exceptions on error states
487
 
        if self.ready() and not wait_for_steady:
 
483
        logging.info("Waiting up to {} seconds for environment to become ready".format(timeout)
 
484
                     + " (not blocked or in maintenance)")
 
485
        check_and_wait_start = datetime.datetime.now()
 
486
        while True:
 
487
            check_and_wait_running = datetime.datetime.now() - check_and_wait_start
 
488
            if check_and_wait_running.total_seconds() > int(timeout):
 
489
                raise JujuWaitException("Timed out checking Juju status for stable state")
 
490
            self.status(force_update=True)
 
491
            if self.ready():  # self.ready() will raise exceptions on error states
 
492
                break
 
493
            time.sleep(5)
 
494
        logging.info("Environment is ready (not blocked or in maintenance)")
 
495
 
 
496
        if not wait_for_steady:
 
497
            logging.info("Not waiting for environment to reach steady state (not running any hook)")
488
498
            return
489
499
 
490
 
        logging.info("Waiting for environment to reach steady state")
491
 
        wait(max_wait=timeout)
492
 
        logging.info("Environment has reached steady state")
 
500
        if max_wait is None:
 
501
            max_wait = timeout
 
502
 
 
503
        logging.info("Waiting up to {} seconds for environment to reach steady state".format(max_wait)
 
504
                     + " (not running any hook)")
 
505
        wait(max_wait=max_wait)
 
506
        logging.info("Environment has reached steady state (not running any hook)")
493
507
 
494
508
    def controller_version(self):
495
509
        """ Parses the output of 'juju controllers' returning the controller version for the current controller.