2016-05-14 03:50:02 +02:00
#!/usr/bin/env python3
2016-05-17 06:36:27 +02:00
import subprocess
2016-05-14 03:50:02 +02:00
import argparse
import time
import sys
2016-05-17 06:36:27 +02:00
import os
2016-05-14 03:50:02 +02:00
from os.path import expanduser
#the term qube refers to a qubes vm
2016-05-17 06:36:27 +02:00
def is_program_installed_in_qube( program, qube_name ):
2016-05-17 20:15:26 +02:00
is_installed = True
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
try:
command = 'command -v ' + program
2020-01-15 15:18:43 +01:00
subprocess.check_call([ 'qvm-run', '--no-color-output', qube_name, command ], stdout = open( os.devnull, 'w' ) )
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
except subprocess.CalledProcessError:
is_installed = False
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
return is_installed
2016-05-14 03:50:02 +02:00
2016-05-17 20:31:24 +02:00
#this function requires virsh
2016-05-14 03:50:02 +02:00
#domstate only works for Xen domU (guests)
def is_qube_running( qube_name ):
2016-05-17 20:15:26 +02:00
runs = False
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
out = subprocess.check_output([ "virsh", "-c", "xen:///", "domstate", qube_name ])
out = out.decode('utf-8').replace('\n', '')
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
if 'running' == out:
runs = True
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
return runs
2016-05-14 03:50:02 +02:00
def get_qube_packages( qube_name ):
2016-05-17 20:15:26 +02:00
content = "## Qubes Packages\n\n"
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
#a qube can have more than one package manager installed (only one is functional)
pkg_cmd = { 'dpkg' : 'dpkg -l qubes-*', 'pacman' : 'pacman -Qs qubes', 'rpm' : 'rpm -qa qubes-*' }
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
if is_qube_running( qube_name ):
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
for package_manager in pkg_cmd.keys():
if is_program_installed_in_qube( package_manager, qube_name ):
pkg_list_cmd = pkg_cmd[package_manager]
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
try:
out = subprocess.check_output([ 'qvm-run', qube_name, '--pass-io', '--no-color-output', pkg_list_cmd ], stderr = open( os.devnull, 'w' ) )
out = out.decode('utf-8')
content += create_heading( ( "Package Manager: " + package_manager ), 3 )
content += wrap_code( out )
except subprocess.CalledProcessError:
pass #do nothing
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
else:
2016-05-17 20:31:24 +02:00
content += "**No packages listed, because Qube " + qube_name + " was not running**\n\n"
return content
2016-05-14 03:50:02 +02:00
def get_dom0_packages():
2016-05-17 20:15:26 +02:00
content = create_heading( "Dom0 Packages", 2 )
out = subprocess.check_output([ "rpm", "-qa", "qubes-*" ])
out = out.decode('utf-8')
content += wrap_code( out )
2016-05-17 20:31:24 +02:00
2016-05-17 20:15:26 +02:00
return content
2016-05-14 03:50:02 +02:00
def wrap_code( text ):
2016-05-17 20:15:26 +02:00
code = "~~~\n" + text + "~~~\n\n"
2016-05-17 20:31:24 +02:00
2016-05-17 20:15:26 +02:00
return code
2016-05-14 03:50:02 +02:00
2016-05-17 06:36:27 +02:00
def create_heading( heading, level ):
2016-05-17 20:15:26 +02:00
heading = heading + "\n\n"
2016-05-17 20:31:24 +02:00
2016-05-17 20:15:26 +02:00
if 1 == level:
heading = "# " + heading
2016-05-17 20:31:24 +02:00
elif 2 == level:
2016-05-17 20:15:26 +02:00
heading = "## " + heading
else:
heading = "### " + heading
2016-05-17 06:36:27 +02:00
2016-05-17 20:15:26 +02:00
return heading
2016-05-17 20:31:24 +02:00
2016-05-17 06:36:27 +02:00
2016-05-14 03:50:02 +02:00
def get_log_file_content( qube_name ):
2016-05-17 20:15:26 +02:00
content = "## Log Files\n\n"
qubes_os_log = "/var/log/qubes/"
ext = ".log"
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
log_prefix = [ "guid", "pacat", "qubesdb", "qrexec" ]
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
#constructs for each log file prefix the full path and reads the log file
for prefix in log_prefix:
log_file = prefix + "." + qube_name + ext
content += create_heading( ( "Log File: " + log_file ), 3 )
content += wrap_code( get_log_file( qubes_os_log + log_file ) )
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
return content
2016-05-14 03:50:02 +02:00
def get_qube_prefs( qube_name ):
2016-05-17 20:15:26 +02:00
qube_prefs = subprocess.check_output([ "qvm-prefs", qube_name ])
qube_prefs = qube_prefs.decode('utf-8')
2016-05-17 20:31:24 +02:00
2016-05-17 20:15:26 +02:00
content = create_heading( "Qube Prefs", 2 )
content += wrap_code( qube_prefs )
2016-05-17 20:31:24 +02:00
2016-05-17 20:15:26 +02:00
return content
2016-05-14 03:50:02 +02:00
def report( qube_name ):
2016-05-17 20:15:26 +02:00
template = '''{title}
2016-05-14 03:50:02 +02:00
{content}
'''
2016-05-17 20:15:26 +02:00
title_text = create_heading( "Bug report: " + qube_name, 1 )
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
content_text = get_qube_prefs( qube_name )
content_text += get_dom0_packages()
content_text += get_log_file_content( qube_name )
content_text += get_qube_packages( qube_name )
2016-05-17 20:31:24 +02:00
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
report = template.format( title=title_text, content=content_text )
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
return report
2016-05-14 03:50:02 +02:00
def write_report( report_content, file_path ):
2016-05-17 20:15:26 +02:00
with open( file_path, 'w' ) as report_file:
report_file.write( report_content )
2016-05-14 03:50:02 +02:00
def send_report( dest_qube, file_path):
2016-05-17 20:15:26 +02:00
#if dest_qube is not running -> start dest_qube
if not is_qube_running( dest_qube ):
try:
subprocess.check_call([ "qvm-start", dest_qube ])
except subprocess.CalledProcessError:
print( "Error while starting: " + dest_qube, file = sys.stderr )
2016-05-14 03:50:02 +02:00
2016-05-17 20:31:24 +02:00
try:
2016-05-17 20:15:26 +02:00
subprocess.check_call([ "qvm-move-to-vm", dest_qube, file_path ])
except subprocess.calledProcessError:
2016-05-17 20:31:24 +02:00
print( "Moving file bug-report failed", file = sys.stderr )
2016-05-14 03:50:02 +02:00
def get_log_file( log_file ):
2016-05-17 20:15:26 +02:00
data = ""
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
#open and close the file
with open( log_file ) as log:
data = log.read()
2016-05-14 03:50:02 +02:00
2016-05-17 20:31:24 +02:00
return data
2016-05-14 03:50:02 +02:00
def qube_exist( qube_name ):
2016-05-17 20:15:26 +02:00
exists = True
2016-05-17 20:31:24 +02:00
2016-05-17 20:15:26 +02:00
try:
#calls: qvm-check --quiet vmanme
subprocess.check_call([ "qvm-check", "--quiet", qube_name ])
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
except subprocess.CalledProcessError:
exists = False
2016-05-14 03:50:02 +02:00
2016-05-17 20:31:24 +02:00
return exists
2016-05-14 03:50:02 +02:00
def get_report_file_path( qube_name ):
2016-05-17 20:15:26 +02:00
#exapanduser -> works corss platform
home_dir = expanduser("~")
date = time.strftime("%H%M%S")
file_path = home_dir + "/" + qube_name + "_bug-report_" + date + ".md"
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
return file_path
2016-05-14 03:50:02 +02:00
2016-05-17 06:36:27 +02:00
2016-05-14 03:50:02 +02:00
def main():
2016-05-17 20:15:26 +02:00
parser = argparse.ArgumentParser( description = 'Generates a bug report for a specific qube (Qubes VM)' )
parser.add_argument( 'vmname', metavar = '<vmanme>', type = str )
parser.add_argument( '-d', '--dest-vm', metavar = '<dest-vm>', dest = "destvm", type = str, default = 'dom0', help = "send the report to the destination VM" )
parser.add_argument( '-p', '--print-report', action = 'store_const', const = "print_report", required = False, help = "prints the report without writing it or sending it to a destination VM" )
args = parser.parse_args()
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
if qube_exist( args.vmname ):
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
if qube_exist( args.destvm ):
#get the report
report_content = report( args.vmname )
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
#if -p or --print-report is an argument print the report
if args.print_report:
print( report_content )
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
#write and send the report
else:
file_path = get_report_file_path( args.vmname )
write_report( report_content, file_path )
print( "Report written to: " + file_path )
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
if 'dom0' != args.destvm:
send_report( args.destvm, file_path )
print( "Report send to VM: " + args.destvm )
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
exit(0)
2016-05-14 03:50:02 +02:00
2016-05-17 20:15:26 +02:00
else:
print ( "Destination VM does not exist" )
exit(1)
2016-05-17 20:31:24 +02:00
2016-05-17 20:15:26 +02:00
else:
print( "VM does not exist" )
exit(1)
2016-05-17 20:31:24 +02:00
2016-05-14 03:50:02 +02:00
2016-05-17 06:36:27 +02:00
#calls the main function -> program start point
2016-05-14 03:50:02 +02:00
main()