mirror of
https://github.com/virt-manager/virt-manager.git
synced 2025-02-25 18:55:27 -06:00
sysinfo: Validate in shared code, not at cli parse time
This commit is contained in:
parent
1757e394bb
commit
7a8f756a42
@ -21,7 +21,6 @@
|
||||
|
||||
import argparse
|
||||
import collections
|
||||
import datetime
|
||||
import logging
|
||||
import logging.handlers
|
||||
import os
|
||||
@ -1743,41 +1742,15 @@ class ParserSYSInfo(VirtCLIParser):
|
||||
else:
|
||||
fail(_("Unknown sysinfo flag '%s'") % val)
|
||||
|
||||
def validate_date_cb(self, inst, val, virtarg):
|
||||
# If supplied, date must be in either mm/dd/yy or mm/dd/yyyy format
|
||||
try:
|
||||
datetime.datetime.strptime(val, '%m/%d/%Y')
|
||||
except ValueError:
|
||||
try:
|
||||
datetime.datetime.strptime(val, '%m/%d/%y')
|
||||
except ValueError:
|
||||
raise RuntimeError(_("SMBios date string '%s' is invalid.")
|
||||
% val)
|
||||
inst.bios_date = val
|
||||
return val
|
||||
|
||||
def validate_uuid_cb(self, inst, val, virtarg):
|
||||
def set_uuid_cb(self, inst, val, virtarg):
|
||||
# If a uuid is supplied it must match the guest UUID. This would be
|
||||
# impossible to guess if the guest uuid is autogenerated so just
|
||||
# overwrite the guest uuid with what is passed in assuming it passes
|
||||
# the sanity checking below.
|
||||
try:
|
||||
util.validate_uuid(val)
|
||||
except ValueError:
|
||||
raise ValueError(_("Invalid uuid for SMBios: %s") % val)
|
||||
|
||||
if util.vm_uuid_collision(self.guest.conn, val):
|
||||
raise ValueError(_("UUID '%s' is in use by another guest.") %
|
||||
val)
|
||||
|
||||
# Override guest uuid with passed in SMBios value (they must match)
|
||||
self.guest.uuid = val
|
||||
inst.system_uuid = val
|
||||
self.guest.uuid = val
|
||||
|
||||
def _parse(self, inst):
|
||||
if self.optstr == "none":
|
||||
self.guest.skip_default_sysinfo = True
|
||||
return
|
||||
if self.optstr == "host" or self.optstr == "emulate":
|
||||
self.optdict['type'] = self.optstr
|
||||
|
||||
@ -1791,8 +1764,7 @@ ParserSYSInfo.add_arg("type", "type",
|
||||
# <bios> type 0 BIOS Information
|
||||
ParserSYSInfo.add_arg("bios_vendor", "bios_vendor")
|
||||
ParserSYSInfo.add_arg("bios_version", "bios_version")
|
||||
ParserSYSInfo.add_arg("bios_date", "bios_date",
|
||||
cb=ParserSYSInfo.validate_date_cb)
|
||||
ParserSYSInfo.add_arg("bios_date", "bios_date")
|
||||
ParserSYSInfo.add_arg("bios_release", "bios_release")
|
||||
|
||||
# <system> type 1 System Information
|
||||
@ -1801,7 +1773,7 @@ ParserSYSInfo.add_arg("system_product", "system_product")
|
||||
ParserSYSInfo.add_arg("system_version", "system_version")
|
||||
ParserSYSInfo.add_arg("system_serial", "system_serial")
|
||||
ParserSYSInfo.add_arg("system_uuid", "system_uuid",
|
||||
cb=ParserSYSInfo.validate_uuid_cb)
|
||||
cb=ParserSYSInfo.set_uuid_cb)
|
||||
ParserSYSInfo.add_arg("system_sku", "system_sku")
|
||||
ParserSYSInfo.add_arg("system_family", "system_family")
|
||||
|
||||
|
@ -20,8 +20,10 @@
|
||||
"""
|
||||
Classes for building and installing with libvirt <sysinfo> XML
|
||||
"""
|
||||
import datetime
|
||||
|
||||
from .xmlbuilder import XMLBuilder, XMLProperty
|
||||
from . import util
|
||||
|
||||
|
||||
class SYSInfo(XMLBuilder):
|
||||
@ -39,16 +41,42 @@ class SYSInfo(XMLBuilder):
|
||||
|
||||
type = XMLProperty("./@type")
|
||||
|
||||
def _validate_date(self, val):
|
||||
# If supplied, date must be in either mm/dd/yy or mm/dd/yyyy format
|
||||
try:
|
||||
datetime.datetime.strptime(val, '%m/%d/%Y')
|
||||
except ValueError:
|
||||
try:
|
||||
datetime.datetime.strptime(val, '%m/%d/%y')
|
||||
except ValueError:
|
||||
raise RuntimeError(_("SMBios date string '%s' is invalid.")
|
||||
% val)
|
||||
return val
|
||||
|
||||
bios_date = XMLProperty("./bios/entry[@name='date']",
|
||||
validate_cb=_validate_date)
|
||||
bios_vendor = XMLProperty("./bios/entry[@name='vendor']")
|
||||
bios_version = XMLProperty("./bios/entry[@name='version']")
|
||||
bios_date = XMLProperty("./bios/entry[@name='date']")
|
||||
bios_release = XMLProperty("./bios/entry[@name='release']")
|
||||
|
||||
|
||||
def _validate_uuid(self, val):
|
||||
try:
|
||||
util.validate_uuid(val)
|
||||
except ValueError:
|
||||
raise ValueError(_("Invalid uuid for SMBios: %s") % val)
|
||||
|
||||
if util.vm_uuid_collision(self.conn, val):
|
||||
raise ValueError(_("UUID '%s' is in use by another guest.") %
|
||||
val)
|
||||
return val
|
||||
system_uuid = XMLProperty("./system/entry[@name='uuid']",
|
||||
validate_cb=_validate_uuid)
|
||||
|
||||
system_manufacturer = XMLProperty("./system/entry[@name='manufacturer']")
|
||||
system_product = XMLProperty("./system/entry[@name='product']")
|
||||
system_version = XMLProperty("./system/entry[@name='version']")
|
||||
system_serial = XMLProperty("./system/entry[@name='serial']")
|
||||
system_uuid = XMLProperty("./system/entry[@name='uuid']")
|
||||
system_sku = XMLProperty("./system/entry[@name='sku']")
|
||||
system_family = XMLProperty("./system/entry[@name='family']")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user