z-hypervisor/zhypervisor/machine.py

55 lines
1.6 KiB
Python
Raw Normal View History

2016-12-26 16:42:48 -08:00
import logging
from zhypervisor.clients.qmachine import QMachine
class MachineSpec(object):
2016-12-26 17:14:28 -08:00
"""
Represents a machine we may control
"""
2016-12-28 18:43:07 -08:00
def __init__(self, master, machine_id, spec):
2016-12-26 17:14:28 -08:00
"""
Initialize options and properties of the machine. More importantly, initialize the self.machine object which
should be a subclass of zhypervisor.util.Machine.
"""
2016-12-26 16:42:48 -08:00
logging.info("Initting machine %s", machine_id)
self.master = master
self.machine_id = machine_id
2016-12-26 17:14:28 -08:00
self.options = spec["options"]
self.properties = spec["properties"]
2016-12-26 16:42:48 -08:00
# TODO replace if/else with better system
2016-12-28 18:43:07 -08:00
if self.options["type"] == "q":
2016-12-26 16:42:48 -08:00
self.machine = QMachine(self)
else:
2016-12-28 18:43:07 -08:00
raise Exception("Unknown machine type: {}".format(self.options["type"]))
2016-12-26 16:42:48 -08:00
def start(self):
2016-12-26 17:14:28 -08:00
"""
Start this machine (pass-through)
"""
2016-12-28 16:53:26 -08:00
self.machine.block_respawns = False
2016-12-26 16:42:48 -08:00
self.machine.start_machine()
def stop(self):
2016-12-26 17:14:28 -08:00
"""
Stop this machine
"""
2016-12-26 16:42:48 -08:00
self.machine.block_respawns = True
self.machine.stop_machine()
2016-12-26 17:14:28 -08:00
2016-12-28 23:32:57 -08:00
def save(self):
"""
Write the machine's config to disk
"""
self.master.add_machine(self.machine_id, {"options": self.options, "properties": self.properties},
write=True)
2016-12-26 17:14:28 -08:00
def serialize(self):
"""
Return a serializable form of this machine's specs
"""
return {"options": self.options,
"properties": self.properties}