Skip to content

Commit

Permalink
Connect to libvirtd lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
erosennin committed Dec 26, 2018
1 parent 3ac4ca3 commit a7436f0
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions nixops/backends/libvirtd.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,27 @@ def get_type(cls):

def __init__(self, depl, name, id):
MachineState.__init__(self, depl, name, id)
self._conn = None
self._dom = None
self._pool = None
self._vol = None

def connect(self):
self.logger.log('Connecting to {}...'.format(self.uri))
try:
self.conn = libvirt.open(self.uri)
except libvirt.libvirtError as error:
self.logger.error(error.get_error_message())
if error.get_error_code() == libvirt.VIR_ERR_NO_CONNECT:
# this error code usually means "no connection driver available for qemu:///..."
self.logger.error('make sure qemu-system-x86_64 is installed on the target host')
raise Exception('Failed to connect to the hypervisor at {}'.format(self.uri))
@property
def conn(self):
if self._conn is None:
self.logger.log('Connecting to {}...'.format(self.uri))
try:
self._conn = libvirt.open(self.uri)
except libvirt.libvirtError as error:
self.logger.error(error.get_error_message())
if error.get_error_code() == libvirt.VIR_ERR_NO_CONNECT:
# this error code usually means "no connection driver available for qemu:///..."
self.logger.error('make sure qemu-system-x86_64 is installed on the target host')
raise Exception('Failed to connect to the hypervisor at {}'.format(self.uri))
return self._conn

@property
def dom(self):
self.connect()
if self._dom is None:
try:
self._dom = self.conn.lookupByName(self._vm_id())
Expand Down Expand Up @@ -145,8 +148,6 @@ def create(self, defn, check, allow_reboot, allow_recreate):
self.storage_pool_name = defn.storage_pool_name
self.uri = defn.uri

self.connect()

# required for virConnectGetDomainCapabilities()
# https://libvirt.org/formatdomaincaps.html
if self.conn.getLibVersion() < 1002007:
Expand Down

0 comments on commit a7436f0

Please sign in to comment.