Minor style cleanup (qubes.py only)

Break long lines, remove unused imports, remove dead code
This commit is contained in:
Marek Marczykowski 2013-03-16 14:19:03 +01:00
parent ecd8837113
commit 0f7308ceeb

View File

@ -21,20 +21,13 @@
# #
import sys import sys
import stat
import os import os
import os.path import os.path
import subprocess
import lxml.etree import lxml.etree
import xml.parsers.expat import xml.parsers.expat
import fcntl import fcntl
import re
import shutil
import uuid
import time import time
import warnings import warnings
from datetime import datetime
from qmemman_client import QMemmanClient
# Do not use XenAPI or create/read any VM files # Do not use XenAPI or create/read any VM files
# This is for testing only! # This is for testing only!
@ -110,7 +103,8 @@ defaults = {
qubes_max_qid = 254 qubes_max_qid = 254
qubes_max_netid = 254 qubes_max_netid = 254
class QubesException (Exception) : pass class QubesException (Exception):
pass
if not dry_run: if not dry_run:
xc = xen.lowlevel.xc.xc() xc = xen.lowlevel.xc.xc()
@ -140,15 +134,17 @@ class QubesHost(object):
ret = self.physinfo['free_memory'] ret = self.physinfo['free_memory']
return long(ret) return long(ret)
# measure cpu usage for all domains at once def measure_cpu_usage(self, previous=None, previous_time = None,
def measure_cpu_usage(self, previous=None, previous_time = None, wait_time=1): wait_time=1):
"""measure cpu usage for all domains at once"""
if previous is None: if previous is None:
previous_time = time.time() previous_time = time.time()
previous = {} previous = {}
info = xc.domain_getinfo(0, qubes_max_qid) info = xc.domain_getinfo(0, qubes_max_qid)
for vm in info: for vm in info:
previous[vm['domid']] = {} previous[vm['domid']] = {}
previous[vm['domid']]['cpu_time'] = vm['cpu_time']/vm['online_vcpus'] previous[vm['domid']]['cpu_time'] = (
vm['cpu_time'] / vm['online_vcpus'])
previous[vm['domid']]['cpu_usage'] = 0 previous[vm['domid']]['cpu_usage'] = 0
time.sleep(wait_time) time.sleep(wait_time)
@ -157,11 +153,13 @@ class QubesHost(object):
info = xc.domain_getinfo(0, qubes_max_qid) info = xc.domain_getinfo(0, qubes_max_qid)
for vm in info: for vm in info:
current[vm['domid']] = {} current[vm['domid']] = {}
current[vm['domid']]['cpu_time'] = vm['cpu_time']/max(vm['online_vcpus'],1) current[vm['domid']]['cpu_time'] = (
vm['cpu_time'] / max(vm['online_vcpus'], 1))
if vm['domid'] in previous.keys(): if vm['domid'] in previous.keys():
current[vm['domid']]['cpu_usage'] = \ current[vm['domid']]['cpu_usage'] = (
float(current[vm['domid']]['cpu_time'] - previous[vm['domid']]['cpu_time']) \ float(current[vm['domid']]['cpu_time'] -
/ long(1000**3) / (current_time-previous_time) * 100 previous[vm['domid']]['cpu_time']) /
long(1000**3) / (current_time-previous_time) * 100)
if current[vm['domid']]['cpu_usage'] < 0: if current[vm['domid']]['cpu_usage'] < 0:
# VM has been rebooted # VM has been rebooted
current[vm['domid']]['cpu_usage'] = 0 current[vm['domid']]['cpu_usage'] = 0
@ -176,7 +174,8 @@ class QubesVmLabel(object):
self.index = index self.index = index
self.color = color if color is not None else name self.color = color if color is not None else name
self.icon = icon if icon is not None else name self.icon = icon if icon is not None else name
self.icon_path = os.path.join(system_path['qubes_icon_dir'], self.icon) + ".png" self.icon_path = os.path.join(
system_path['qubes_icon_dir'], self.icon) + ".png"
# Globally defined lables # Globally defined lables
QubesVmLabels = { QubesVmLabels = {
@ -207,7 +206,6 @@ defaults["servicevm_label"] = QubesVmLabels["red"]
QubesVmClasses = {} QubesVmClasses = {}
def register_qubes_vm_class(vm_class): def register_qubes_vm_class(vm_class):
global QubesVmClasses
QubesVmClasses[vm_class.__name__] = vm_class QubesVmClasses[vm_class.__name__] = vm_class
# register class as local for this module - to make it easy to import from # register class as local for this module - to make it easy to import from
# other modules # other modules
@ -227,6 +225,7 @@ class QubesVmCollection(dict):
self.updatevm_qid = None self.updatevm_qid = None
self.qubes_store_filename = store_filename self.qubes_store_filename = store_filename
self.clockvm_qid = None self.clockvm_qid = None
self.qubes_store_file = None
def values(self): def values(self):
for qid in self.keys(): for qid in self.keys():
@ -256,7 +255,7 @@ class QubesVmCollection(dict):
vm = QubesVmClasses[vm_type](qid=qid, collection=self, **kwargs) vm = QubesVmClasses[vm_type](qid=qid, collection=self, **kwargs)
if not self.verify_new_vm(vm): if not self.verify_new_vm(vm):
raise QubesException("Wrong VM description!") raise QubesException("Wrong VM description!")
self[vm.qid]=vm self[vm.qid] = vm
# make first created NetVM the default one # make first created NetVM the default one
if self.default_fw_netvm_qid is None and vm.is_netvm(): if self.default_fw_netvm_qid is None and vm.is_netvm():
@ -372,7 +371,9 @@ class QubesVmCollection(dict):
return self[self.default_netvm_qid] return self[self.default_netvm_qid]
def set_default_kernel(self, kernel): def set_default_kernel(self, kernel):
assert os.path.exists(os.path.join(system_path["qubes_kernels_base_dir"], kernel)), "Kerel {0} not installed!".format(kernel) assert os.path.exists(
os.path.join(system_path["qubes_kernels_base_dir"], kernel)), \
"Kerel {0} not installed!".format(kernel)
self.default_kernel = kernel self.default_kernel = kernel
def get_default_kernel(self): def get_default_kernel(self):
@ -452,7 +453,8 @@ class QubesVmCollection(dict):
# Verify that name is unique # Verify that name is unique
for vm in self.values(): for vm in self.values():
if vm.name == new_vm.name: if vm.name == new_vm.name:
print >> sys.stderr, "ERROR: The name={0} is already used by other VM with qid='{1}'!".\ print >> sys.stderr, \
"ERROR: The name={0} is already used by other VM with qid='{1}'!".\
format(vm.name, vm.qid) format(vm.name, vm.qid)
return False return False
@ -552,7 +554,8 @@ class QubesVmCollection(dict):
if "uses_default_netvm" not in kwargs: if "uses_default_netvm" not in kwargs:
vm.uses_default_netvm = True vm.uses_default_netvm = True
else: else:
vm.uses_default_netvm = True if kwargs["uses_default_netvm"] == "True" else False vm.uses_default_netvm = (
True if kwargs["uses_default_netvm"] == "True" else False)
if vm.uses_default_netvm is True: if vm.uses_default_netvm is True:
if vm.is_proxyvm(): if vm.is_proxyvm():
netvm = self.get_default_fw_netvm() netvm = self.get_default_fw_netvm()
@ -614,9 +617,6 @@ class QubesVmCollection(dict):
self[dom0vm.qid] = dom0vm self[dom0vm.qid] = dom0vm
self.default_netvm_qid = 0 self.default_netvm_qid = 0
global dom0_vm
dom0_vm = dom0vm
try: try:
tree = lxml.etree.parse(self.qubes_store_file) tree = lxml.etree.parse(self.qubes_store_file)
except (EnvironmentError, except (EnvironmentError,
@ -661,7 +661,8 @@ class QubesVmCollection(dict):
self.clockvm_qid = clockvm.qid self.clockvm_qid = clockvm.qid
# Disable ntpd in ClockVM - to not conflict with ntpdate (both are using 123/udp port) # Disable ntpd in ClockVM - to not conflict with ntpdate (both are
# using 123/udp port)
if self.clockvm_qid is not None: if self.clockvm_qid is not None:
self[self.clockvm_qid].services['ntpd'] = False self[self.clockvm_qid].services['ntpd'] = False
return True return True
@ -706,18 +707,11 @@ class QubesDaemonPidfile(object):
# check if the pid file is valid... # check if the pid file is valid...
proc_path = "/proc/" + str(self.read_pid()) + "/cmdline" proc_path = "/proc/" + str(self.read_pid()) + "/cmdline"
if not os.path.exists (proc_path): if not os.path.exists (proc_path):
print >> sys.stderr, "Path {0} doesn't exist, assuming stale pidfile.".format(proc_path) print >> sys.stderr, \
"Path {0} doesn't exist, assuming stale pidfile.".\
format(proc_path)
return True return True
f = open (proc_path)
cmdline = f.read ()
f.close()
# The following doesn't work with python -- one would have to get argv[1] and compare it with self.name...
# if not cmdline.strip().endswith(self.name):
# print >> sys.stderr, "{0} = {1} doesn't seem to point to our process ({2}), assuming stale pidile.".format(proc_path, cmdline, self.name)
# return True
return False # It's a good pidfile return False # It's a good pidfile
def remove_pidfile(self): def remove_pidfile(self):