跳到主要内容

创建空白虚拟机

简单创建

from cloudtower import (
ApiClient,
Configuration,
VmApi,
VmStatus,
VmFirmware,
Bus
)
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
{
"cluster_id": "cluster_id",
"name": "vm_name",
"ha": True,
"cpu_cores": 4,
"cpu_sockets": 4,
"memory": 4*1024*1024*1024,
"vcpu": 16,
"status": VmStatus.STOPPED,
"firmware": VmFirmware.BIOS,
"vm_nics": [
{
"connect_vlan_id": "vlan_id",
}
],
"vm_disks": {
"mount_cd_roms": [{
"boot": 0,
"index": 0
}],
}
}
])[0]

wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})

创建时配置虚拟盘

CD-ROM 加载 ISO

from cloudtower import (
ApiClient,
Configuration,
VmApi,
VmStatus,
VmFirmware,
Bus
)
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
{
"cluster_id": "cluster_id",
"name": "vm_name",
"ha": True,
"cpu_cores": 4,
"cpu_sockets": 4,
"memory": 4*1024*1024*1024,
"vcpu": 16,
"status": VmStatus.STOPPED,
"firmware": VmFirmware.BIOS,
"vm_nics": [
{
"connect_vlan_id": "vlan_id",
}
],
"vm_disks": {
"mount_cd_roms": [{
"index": 0,
"boot": 0,
"elf_image_id": "elf_image_id"
}],
}
}
])[0]

wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})

挂载虚拟卷为虚拟盘

from cloudtower import (
ApiClient,
Configuration,
VmApi,
VmStatus,
VmFirmware,
Bus
)
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
{
"cluster_id": "cluster_id",
"name": "vm_name",
"ha": True,
"cpu_cores": 4,
"cpu_sockets": 4,
"memory": 4*1024*1024*1024,
"vcpu": 16,
"status": VmStatus.STOPPED,
"firmware": VmFirmware.BIOS,
"vm_nics": [
{
"connect_vlan_id": "vlan_id",
}
],
"vm_disks": {
"mount_disks": [{
"index": 0,
"boot": 0,
"bus": Bus.VIRTIO,
"vm_volume_id": "vm_volume_id",
"index": 0,
}],
}
}
])[0]

wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})

挂载新增虚拟盘

from cloudtower import (
ApiClient,
Configuration,
VmApi,
VmStatus,
VmFirmware,
Bus,
VmVolumeElfStoragePolicyType
)
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
{
"cluster_id": "cluster_id",
"name": "vm_name",
"ha": True,
"cpu_cores": 4,
"cpu_sockets": 4,
"memory": 4 * 1024*1024*1024,
"vcpu": 16,
"status": VmStatus.STOPPED,
"firmware": VmFirmware.BIOS,
"vm_nics": [
{
"connect_vlan_id": "vlan_id",
}
],
"vm_disks": {
"mount_new_create_disks": [{
"boot": 0,
"bus": Bus.VIRTIO,
"vm_volume": {
"elf_storage_policy": VmVolumeElfStoragePolicyType._2_THIN_PROVISION,
"size": 10 * 1024 * 1024 * 1024,
"name": "new_volume_name"
}
}],
}
}
])[0]

wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})

创建时配置虚拟网卡

from cloudtower import (
ApiClient,
Configuration,
VmApi,
VmStatus,
VmFirmware,
Bus,
VmVolumeElfStoragePolicyType
)
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
{
"cluster_id": "cluster_id",
"name": "vm_name",
"ha": True,
"cpu_cores": 4,
"cpu_sockets": 4,
"memory": 4 * 1024*1024*1024,
"vcpu": 16,
"status": VmStatus.STOPPED,
"firmware": VmFirmware.BIOS,
"vm_nics": [
{
"connect_vlan_id": "vlan_id",
}
],
"vm_disks": {
"mount_new_create_disks": [{
"boot": 0,
"bus": Bus.VIRTIO,
"vm_volume": {
"elf_storage_policy": VmVolumeElfStoragePolicyType._2_THIN_PROVISION,
"size": 10 * 1024 * 1024 * 1024,
"name": "new_volume_name"
}
}],
}
}
])[0]

wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
"where": {
"id": with_task_vm.data.id
}
})