大蟒蛇

python-ovmclient的Python项目详细描述


用法

虚拟机生命周期

如何创建、修改、启动、停止和删除虚拟机:

import ovmclient
from ovmclient import constants

client = ovmclient.Client(
    'https://host:7002/ovm/core/wsapi/rest', 'admin', 'yadayada')

# Make sure the manager is running
client.managers.wait_for_manager_state()

repo_id = client.repositories.get_id_by_name('repo1')
pool_id = client.server_pools.get_id_by_name('pool1')
network_id = client.networks.get_id_by_name('management')

# Create a virtual disk
disk_data = {
    'diskType': constants.DISK_TYPE_VIRTUAL_DISK,
    'size': 1024 * 1024 * 1024,
    'shareable': False,
    'name': 'dummy.img',
}

job = client.jobs.wait_for_job(
    client.repository_virtual_disks(repo_id).create(disk_data, sparse='true'))
disk_id = job['resultId']

# Create a VM
vm_data = {
    'name': 'vm1',
    'description': 'Blah',
    'vmDomainType': constants.VM_DOMAIN_TYPE_XEN_HVM_PV_DRIVERS,
    'repositoryId': repo_id,
    'serverPoolId': pool_id,
    'cpuCount': 4,
    'cpuCountLimit': 4,
    'hugePagesEnabled': False,
    'memory': 1024,
    'memoryLimit': 1024,
    'osType': 'Oracle Linux 7',
}

job = client.jobs.wait_for_job(client.vms.create(vm_data))
vm_id = job['resultId']

# Map the virtual disk
vm_disk_mapping_data = {
    'virtualDiskId': disk_id,
    'diskWriteMode': constants.DISK_WRITE_MODE_READ_WRITE,
    'emulatedBlockDevice': False,
    'storageElementId': None,
    'diskTarget': 0,
}

job = client.jobs.wait_for_job(
    client.vm_disk_mappings(vm_id).create(vm_disk_mapping_data))

# Add a vnic
vnic_data = {
    'networkId': network_id,
}

client.jobs.wait_for_job(client.vm_virtual_nics(vm_id).create(vnic_data))

# Retrieve the VM
vm = client.vms.get_by_id(vm_id)

# Update the VM, e.g. setting a new name
vm['name'] = 'vm2'
client.jobs.wait_for_job(client.vms.update(vm_id, vm))

# Start the VM
client.jobs.wait_for_job(client.vms.start(vm_id))

# Kill the VM
client.jobs.wait_for_job(client.vms.kill(vm_id))

# Delete the VM
client.jobs.wait_for_job(client.vms.delete(vm_id))

# Delete the virtual disk
client.jobs.wait_for_job(
    client.repository_virtual_disks(repo_id).delete(disk_id))

克隆虚拟机

如何克隆虚拟机或虚拟机模板:

import ovmclient

client = ovmclient.Client(
    'https://host:7002/ovm/core/wsapi/rest', 'admin', 'yadayada')

# Make sure the manager is running
client.managers.wait_for_manager_state()

pool_id = client.server_pools.get_id_by_name('pool1')

# Get an existing VM or a VM template
vm_id = client.vms.get_id_by_name('vm1')

# Set to True to create a VM template, False for a regular VM
create_template = True

# Clone the VM
job = client.jobs.wait_for_job(
    client.vms.clone(vm_id, pool_id, create_template=create_template))
new_vm_id = job['resultId']

# Rename the VM template
data = client.vms.get_by_id(new_vm_id)
data["name"] = 'new_name'
client.jobs.wait_for_job(client.vms.update(new_vm_id, data))

# Delete the VM template
client.jobs.wait_for_job(client.vms.delete(new_vm_id))

发现服务器

如何发现和拥有未拥有的Oracle虚拟机主机:

import ovmclient

client = ovmclient.Client(
    'https://host:7002/ovm/core/wsapi/rest', 'admin', 'yadayada')

# Make sure the manager is running
client.managers.wait_for_manager_state()

# Discover a new host and take ownership
client.servers.discover('newovmhost')

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
如何使用java创建灰度图像   java Android VideoView在长时间运行(超过24小时)后冻结   javaxml到XSL格式   servlets如何在Java中获取POST变量值   java给加入indtantly jda的用户jda一个角色的最佳方式是什么   java在读取带有列表的文件时获取所有空值   java在无管理员权限的NetBeans中使用JavaFX场景生成器2.0   java如何通过JSON API实现复杂的条件批量部分更新?   java使用ApacheLucene索引大文件时,如何避免内存不足错误?   使用协议“mapi://”从java对outlook中打开的邮件进行编码   java trywithresources详细信息   java在对JTable进行排序后无法从中获取正确的行(Swing)   单元测试如何在测试类(java)中的main中存根函数   oauth 0auth_签名在java中使用SHA1RSA   java重写equals和hashCode只是为了调用super。等于/哈希代码还是抛出断言错误?