def tail_serial_console(instance_name, zone, start=None, until=None): next_start = start if start else '0' while True: try: result = gcloud.get_serial_port_output(instance_name, zone=zone, start=next_start) except subprocess.CalledProcessError as e: if 'Could not fetch serial port output: TIMEOUT' in e.stderr: gcloud.debug('tail_serial_console: Retrying after TIMEOUT') continue gcloud.debug( 'tail_serial_console: Done, because got exception: {}'.format( e)) if e.stdout: gcloud.debug('stdout: ' + e.stdout) if e.stderr: gcloud.debug('stderr: ' + e.stderr) break print_pretty_logs(instance_name, result.stdout) next_start = re.search(r'--start=(\d*)', result.stderr).group(1) if until and until in result.stdout: gcloud.debug( 'tail_serial_console: Done, because found string "{}"'.format( until)) break time.sleep(5) return next_start
def wait_for_instance(instance_name, zone, status): while True: result = gcloud.describe_instance(instance_name, zone=zone, format='json') current_status = json.loads(result.stdout)['status'] if current_status == status: gcloud.debug( 'wait_for_instance: {}/{} arrived at status {}'.format( zone, instance_name, status)) break else: gcloud.debug( 'wait_for_instance: Waiting for {}/{} to go from status {} to status {}' .format(zone, instance_name, current_status, status)) time.sleep(5)