大蟒蛇

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 JPA。Eclipselink没有为mySQL提供密码,但它应该提供   我的Servlet和@FormDataParam存在java问题   java将什么作为上下文参数传递到文件I/O方法中?   如果两个值相同,java无法找到其中一个单选按钮   java在变量和方法名中使用下划线   JavaSpringMVC单线程安全?   klazz类的java Arraylist(反射Api)   java如何在数字字符串中查找最频繁的数字?   JavaAPI设计:使数据更易于阅读与强制更多API调用   JavaHadoopMapReduceforGoogleWebGraph   java无法启动gauge API:Runner意外退出   java如何在bluemix上使用ibm工作负载调度器?   拉取一年中某一周特定日期的所有日期   java为什么是我的角节点。js应用程序将图像上传到S3� 邮递员正确上传时的符号?   在不使用任何第三方jar的情况下将文件从本地传输到linux系统(java代码)   java将现有文件夹复制到Eclipse工作区中新创建的项目中   Java中的regex RegExp帮助   当使用“系统”外观时,Java组合框setSelectedItem会出现故障   JavaASM:在类的方法中获取局部变量名和值