z-hypervisor/zhypervisor/machine.py

56 lines
1.5 KiB
Python
Raw Permalink Normal View History

2016-12-26 16:42:48 -08:00
import logging
from zhypervisor.clients.qmachine import QMachine
2017-02-24 18:11:36 -08:00
from zhypervisor.clients.dockermachine import DockerMachine
MACHINETYPES = {"q": QMachine, "docker": DockerMachine}
2016-12-26 16:42:48 -08:00
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
self.properties = spec
2016-12-26 16:42:48 -08:00
2017-02-24 18:11:36 -08:00
try:
machine_type = MACHINETYPES[self.properties.get("type", None)]
except KeyError:
raise Exception("Unknown or missing machine type: {}".format(self.properties.get("type", None)))
self.machine = machine_type(self)
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, self.properties, write=True)
2016-12-28 23:32:57 -08:00
2016-12-26 17:14:28 -08:00
def serialize(self):
"""
Return a serializable form of this machine's specs
"""
return self.properties