def _get_libvirt_connection(driver_info): """Get the libvirt connection. :param driver_info: driver info :returns: the active libvirt connection :raises: LibvirtError if failed to connect to the Libvirt uri. """ uri = driver_info['libvirt_uri'] sasl_username = driver_info.get('sasl_username') sasl_password = driver_info.get('sasl_password') ssh_key_filename = driver_info.get('ssh_key_filename') try: if sasl_username and sasl_password: def request_cred(credentials, user_data): for credential in credentials: if credential[0] == libvirt.VIR_CRED_AUTHNAME: credential[4] = sasl_username elif credential[0] == libvirt.VIR_CRED_PASSPHRASE: credential[4] = sasl_password return 0 auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_cred, None] conn = libvirt.openAuth(uri, auth, 0) elif ssh_key_filename: uri += "?keyfile=%s&no_verify=1" % ssh_key_filename conn = libvirt.open(uri) else: conn = libvirt.open(uri) except libvirt.libvirtError as e: raise f_exc.LibvirtError(err=e) if conn is None: raise f_exc.LibvirtError(err=_("Failed to open connection to %s") % uri) return conn
def _get_power_state(domain): """Get the current power state of domain. :param domain: libvirt domain object. :returns: power state. One of :class:`ironic.common.states`. :raises: LibvirtErr if failed to get doamin status. """ try: if domain.isActive(): return states.POWER_ON except libvirt.libvirtError as e: raise f_exc.LibvirtError(err=e) return states.POWER_OFF
def _power_cycle(domain): """Power cycles a node. :param domain: libvirt domain object. :raises: PowerStateFailure if it failed to set power state to POWER_ON. :raises: LibvirtError if failed to power cycle domain. """ try: _power_off(domain) state = _power_on(domain) except libvirt.libvirtError as e: raise f_exc.LibvirtError(err=e) if state != states.POWER_ON: raise exception.PowerStateFailure(pstate=states.POWER_ON)
def _power_off(domain): """Power OFF this domain. :param domain: libvirt domain object. :returns: one of ironic.common.states POWER_OFF or ERROR. :raises: LibvirtError if failed to destroy domain. """ current_pstate = _get_power_state(domain) if current_pstate == states.POWER_OFF: return current_pstate try: domain.destroy() except libvirt.libvirtError as e: raise f_exc.LibvirtError(err=e) current_pstate = _get_power_state(domain) if current_pstate == states.POWER_OFF: return current_pstate else: return states.ERROR
def _power_on(domain): """Power ON this domain. :param domain: libvirt domain object. :returns: one of ironic.common.states POWER_ON or ERROR. :raises: LibvirtError if failed to connect to start domain. """ current_pstate = _get_power_state(domain) if current_pstate == states.POWER_ON: return current_pstate try: domain.create() except libvirt.libvirtError as e: raise f_exc.LibvirtError(err=e) current_pstate = _get_power_state(domain) if current_pstate == states.POWER_ON: return current_pstate else: return states.ERROR
def _set_boot_device(conn, domain, device): """Set the boot device. :param conn: active libvirt connection. :param domain: libvirt domain object. :raises: LibvirtError if failed update domain xml. """ parsed = ET.fromstring(domain.XMLDesc()) os = parsed.find('os') boot_list = os.findall('boot') # Clear boot list for boot_el in boot_list: os.remove(boot_el) boot_el = ET.SubElement(os, 'boot') boot_el.set('dev', device) try: conn.defineXML(ET.tostring(parsed)) except libvirt.libvirtError as e: raise f_exc.LibvirtError(err=e)