in gcpdiag/runbook/gce/serial_log_analyzer.py [0:0]
def execute(self):
"""Fetching VM details"""
project = crm.get_project(op.get(flags.PROJECT_ID))
try:
vm = gce.get_instance(project_id=op.get(flags.PROJECT_ID),
zone=op.get(flags.ZONE),
instance_name=op.get(flags.NAME))
except googleapiclient.errors.HttpError:
op.add_skipped(
project,
reason=('Instance {} does not exist in zone {} or project {}').format(
op.get(flags.NAME), op.get(flags.ZONE), op.get(flags.PROJECT_ID)))
else:
if vm and vm.is_running:
# Check for instance id and instance name
if not op.get(flags.ID):
op.put(flags.ID, vm.id)
elif not op.get(flags.NAME):
op.put(flags.NAME, vm.name)
else:
op.add_failed(vm,
reason=op.prep_msg(op.FAILURE_REASON,
full_resource_path=vm.full_path,
status=vm.status),
remediation=op.prep_msg(op.FAILURE_REMEDIATION,
full_resource_path=vm.full_path,
status=vm.status))
# file sanity checks
if op.get(flags.SERIAL_CONSOLE_FILE):
for file in op.get(flags.SERIAL_CONSOLE_FILE).split(','):
try:
with open(file, 'rb') as f:
results = mimetypes.guess_type(file)[0]
if results and not results.startswith('text/'):
# Peek at content for further clues
content_start = f.read(1024) # Read a small chunk
# Check for gzip and xz magic number (first two bytes)
if content_start.startswith(
b'\x1f\x8b') or content_start.startswith(b'\xfd'):
op.add_skipped(
vm,
reason=('File {} appears to be compressed, not plain text.'
).format(file))
else:
# If not gzip or tar, try simple text encoding detection (UTF-8, etc.)
try:
content_start.decode()
except UnicodeDecodeError:
op.add_skipped(
vm,
reason=('File {} does not appear to be plain text.'
).format(file))
except FileNotFoundError:
op.add_skipped(
vm,
reason=('The file {} does not exists. Please verify if '
'you have provided the correct absolute file path'
).format(file))