diff options
| author | bozo.kopic <bozo.kopic@gmail.com> | 2017-07-03 23:37:44 +0200 |
|---|---|---|
| committer | bozo.kopic <bozo.kopic@gmail.com> | 2017-07-03 23:37:44 +0200 |
| commit | 12287171a8a9b9408b82dffa039804e0d8f62a95 (patch) | |
| tree | e1b824d6e7265718f2289b58726321cb68bca44b /src_py/hatter/vm.py | |
| parent | fe888f22a7183740dd2381b8d5d08c0fb1fcf3b7 (diff) | |
executor
Diffstat (limited to 'src_py/hatter/vm.py')
| -rw-r--r-- | src_py/hatter/vm.py | 105 |
1 files changed, 0 insertions, 105 deletions
diff --git a/src_py/hatter/vm.py b/src_py/hatter/vm.py deleted file mode 100644 index 87e71b9..0000000 --- a/src_py/hatter/vm.py +++ /dev/null @@ -1,105 +0,0 @@ -import time -import libvirt - - -class VM: - - def __init__(self, domain_name, uri='qemu:///system', snapshot_name=None, - temp_snapshot_name='temp_hatter', address_retry_count=10, - address_delay=5): - self._domain_name = domain_name - self._uri = uri - self._snapshot_name = snapshot_name - self._temp_snapshot_name = temp_snapshot_name - self._address_retry_count = address_retry_count - self._address_delay = address_delay - self._conn = None - self._domain = None - self._temp_snapshot = None - self._address = None - - @property - def address(self): - return self._address - - def start(self): - try: - self._conn = _libvirt_connect(self._uri) - self._domain = _libvirt_get_domain(self._conn, self._domain_name) - if self._domain.isActive(): - self._domain.destroy() - self._temp_snapshot = _libvirt_create_temp_snapshot( - self._domain, self._temp_snapshot_name) - if self._snapshot_name: - _libvirt_revert_snapshot(self._domain, self._snapshot_name) - _libvirt_start_domain(self._domain) - for _ in range(self._address_retry_count): - self._address = _libvirt_get_address(self._domain) - if self._address: - return - time.sleep(self._address_delay) - raise Exception('ip addess not detected') - except Exception: - self.stop() - raise - - def stop(self): - if self._domain: - self._domain.destroy() - if self._domain and self._temp_snapshot: - self._domain.revertToSnapshot(self._temp_snapshot) - if self._temp_snapshot: - self._temp_snapshot.delete() - if self._conn: - self._conn.close() - self._temp_snapshot = None - self._domain = None - self._conn = None - self._address = None - - -def _libvirt_connect(uri): - conn = libvirt.open(uri) - if not conn: - raise Exception('could not open connection to {}'.format(uri)) - return conn - - -def _libvirt_get_domain(conn, domain_name): - domain = conn.lookupByName(domain_name) - if not domain: - raise Exception('domain {} not available'.format(domain_name)) - return domain - - -def _libvirt_start_domain(domain): - if domain.create(): - raise Exception('could not run vm') - - -def _libvirt_create_temp_snapshot(domain, temp_snapshot_name): - temp_snapshot = domain.snapshotLookupByName(temp_snapshot_name) - if temp_snapshot: - temp_snapshot.delete() - temp_snapshot = domain.snapshotCreateXML( - "<domainsnapshot><name>{}</name></domainsnapshot>".format( - temp_snapshot_name)) - if not temp_snapshot: - raise Exception('could not create snapshot {}'.format( - temp_snapshot_name)) - return temp_snapshot - - -def _libvirt_revert_snapshot(domain, snapshot_name): - snapshot = domain.snapshotLookupByName(snapshot_name) - if not snapshot: - raise Exception('snapshot {} not available'.format(snapshot_name)) - if domain.revertToSnapshot(snapshot): - raise Exception('could not revert snapshot {}'.format(snapshot_name)) - - -def _libvirt_get_address(domain): - addresses = domain.interfaceAddresses(0) - for i in addresses.values(): - for j in i.get('addrs', []): - return j.get('addr') |
