Exemplo n.º 1
0
def action(xml, machineid, ipcidr=None, vlan=None):
    import libvirt
    from CloudscalerLibcloud.utils.libvirtutil import LibvirtUtil
    from CloudscalerLibcloud.utils.network import Network, NetworkTool
    connection = LibvirtUtil()
    netinfo = []
    if vlan:
        netinfo.append({'id': vlan, 'type': vlan})
    try:
        with NetworkTool(netinfo, connection):
            domain = connection.get_domain_obj(machineid)
            if domain is None:
                return
            flags = 0
            if domain.state()[0] in (libvirt.VIR_DOMAIN_RUNNING,
                                     libvirt.VIR_DOMAIN_PAUSED):
                flags |= libvirt.VIR_DOMAIN_DEVICE_MODIFY_LIVE
            if domain.isPersistent():
                flags |= libvirt.VIR_DOMAIN_DEVICE_MODIFY_CONFIG
            if flags != 0:
                try:
                    domain.attachDeviceFlags(xml, flags)
                except libvirt.libvirtError as e:
                    if e.get_error_code(
                    ) == libvirt.VIR_ERR_CONFIG_UNSUPPORTED:
                        return False
                    raise

            if ipcidr:
                network = Network(connection)
                network.protect_external(domain, ipcidr)
            return domain.XMLDesc()
    finally:
        connection.close()
Exemplo n.º 2
0
def action(machineid, disks, iotune):
    from CloudscalerLibcloud.utils.libvirtutil import LibvirtUtil
    import libvirt

    connection = LibvirtUtil()
    domain = connection.get_domain_obj(machineid)
    if domain is None:
        return
    domaindisks = list(connection.get_domain_disks(domain.XMLDesc()))
    flags = []
    if domain.isPersistent():
        flags.append("--config")
    if domain.state()[0] == libvirt.VIR_DOMAIN_RUNNING:
        flags.append("--live")

    for diskurl in disks:
        dev = connection.get_domain_disk(diskurl, domaindisks)
        if dev:
            cmd = ["virsh", "blkdeviotune", str(machineid), str(dev)]
            for key, value in iotune.items():
                if value is not None:
                    cmd.extend(["--%s" % key, str(value)])
            cmd.extend(flags)
            j.system.process.execute(" ".join(cmd))

    return True
Exemplo n.º 3
0
def action(machineid, force=False):
    from CloudscalerLibcloud.utils.libvirtutil import LibvirtUtil
    from CloudscalerLibcloud.utils.network import Network
    connection = LibvirtUtil()
    network = Network(connection)
    domain = connection.get_domain_obj(machineid)
    if domain:
        network.cleanup_external(domain)
    return connection.shutdown(machineid, force)
Exemplo n.º 4
0
def action(ovs_connection, size, diskguid, disk_info=None):
    import json

    # Creates a disk from a disk template
    #
    # ovs_connection: dict holding connection info for ovs restapi
    #   eg: { ips: ['ip1', 'ip2', 'ip3'], client_id: 'dsfgfs', client_secret: 'sadfafsdf'}
    # size: size of the disk in GB
    # diskguid: guid of the disk that needs to be used to update
    #   the volume.

    # returns true if disk was resized. False if need to restart machine in order to reflect the changes

    ovs = j.clients.openvstorage.get(
        ips=ovs_connection["ips"],
        credentials=(ovs_connection["client_id"],
                     ovs_connection["client_secret"]),
    )

    # Then resize to the correct size
    path = "/vdisks/{}/extend".format(diskguid)
    res = True
    new_size = size * 1024**3
    params = dict(new_size=new_size)
    taskguid = ovs.post(path, data=json.dumps(params))
    success, result = ovs.wait_for_task(taskguid)

    if not success:
        raise Exception("Could not update disk:\n{}".format(result))

    if disk_info is not None and disk_info["machineRefId"] is not None:
        # leave import here as this is only relevant when execute on cpu node
        from CloudscalerLibcloud.utils.libvirtutil import LibvirtUtil
        import libvirt

        connection = LibvirtUtil()
        domain = connection.get_domain_obj(disk_info["machineRefId"])
        if domain:
            domaindisks = list(connection.get_domain_disks(domain.XMLDesc()))
            dev = connection.get_domain_disk(disk_info["referenceId"],
                                             domaindisks)
            try:
                domain.blockResize(dev, new_size,
                                   libvirt.VIR_DOMAIN_BLOCK_RESIZE_BYTES)
            except:
                res = False

    return res
Exemplo n.º 5
0
def action(machineid, vcpus):
    import libvirt
    from CloudscalerLibcloud.utils.libvirtutil import LibvirtUtil
    connection = LibvirtUtil()
    try:
        domain = connection.get_domain_obj(machineid)
        if domain is None:
            return
        try:
            domain.setVcpus(vcpus)
        except libvirt.libvirtError as e:
            if e.get_error_code() == 8:
                return False
            raise
        return True
    finally:
        connection.close()