Skip to main content

Creating a virtual machine from a template in content library

Creating a virtual machine from a template in content library

You can create a virtual machine from a template in content library and configure its settings using the template.

  • template_name: Select the name of the VM template to use.
  • cluster_name: Select the name of the cluster where the virtual machine will reside.
  • vm_name: Enter the name of the virtual machine.

The return value is the created virtual machine.

from cloudtower.api import VmApi, ContentLibraryVmTemplateApi, ClusterApi
from cloudtower.utils import login, wait_tasks
from cloudtower.configuration import Configuration
from cloudtower import ApiClient
import os
configuration = Configuration(host=os.getenv("CLOUDTOWER_ENDPOINT"))
client = ApiClient(configuration)
login(client, os.getenv("CLOUDTOWER_USERNAME"), os.getenv("CLOUDTOWER_PASSWORD"))
def create_vm_from_template(template_name, cluster_name, vm_name):
vm_api = VmApi(client)
cluster_api = ClusterApi(client)
template_api = ContentLibraryVmTemplateApi(client)
cluster = cluster_api.get_clusters({
"where": {
"name": cluster_name
}
})
if len(cluster) == 0:
raise Exception("cluster not found")
template = template_api.get_content_library_vm_templates({
"where": {
"name": template_name
}
})
if len(template) == 0:
raise Exception("template not found")
with_task_vms = vm_api.create_vm_from_content_library_template([
{
"template_id": template[0].id,
"cluster_id": cluster[0].id,
"name": vm_name,
"is_full_copy": False
}
])
tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
vm_ids = [
with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, client)
return vm_api.get_vms({
"where": {
"id_in": vm_ids
}
})[0]

Creating a virtual machine from a template in content library and editing its disks

You can create a virtual machine from a template in content library and configure its disks.

  • template_name: The name of the VM template in content library.
  • cluster_name: The name of the cluster.
  • vm_name: The name of the virtual machine.
  • disk_operate: The disk operations.

For details, see the method for create_vm_from_template_modify_disk_example.

The return value is the created virtual machine.

When creating a virtual machine from a template in content library, you can modify the source disks by configuring the disk_operate parameter. The disk_operate parameter is of the VmDiskOperate type, which is a dictionary containing the following fields:

  • remove_disks: Delete disks by specifying their index.
  • modify_disks: Modify existing disk configurations. Currently, only changing the bus type is supported. For other modifications, delete the original disk first.
  • new_disks: Add new disks. The parameter type is VmDiskParams, which is a dictionary containing the following fields:
    • mount_cd_roms: Mount CD-ROMs.
    • mount_disks: Mount existing disks.
    • mount_new_create_disks: Mount newly created disks.
from cloudtower.api import VmApi, ContentLibraryVmTemplateApi, ClusterApi
from cloudtower.utils import login, wait_tasks
from cloudtower.configuration import Configuration
from cloudtower.models import Bus, VmVolumeElfStoragePolicyType
from cloudtower import ApiClient
import os
configuration = Configuration(host=os.getenv("CLOUDTOWER_ENDPOINT"))
client = ApiClient(configuration)
login(client, os.getenv("CLOUDTOWER_USERNAME"), os.getenv("CLOUDTOWER_PASSWORD"))
def create_vm_from_template_modify_disk(template_name, cluster_name, vm_name, disk_operate):
vm_api = VmApi(client)
cluster_api = ClusterApi(client)
template_api = ContentLibraryVmTemplateApi(client)
cluster = cluster_api.get_clusters({
"where": {
"name": cluster_name
}
})
if len(cluster) == 0:
raise Exception("cluster not found")
template = template_api.get_content_library_vm_templates({
"where": {
"name": template_name
}
})
if len(template) == 0:
raise Exception("template not found")
with_task_vms = vm_api.create_vm_from_content_library_template([
{
"template_id": template[0].id,
"cluster_id": cluster[0].id,
"name": vm_name,
"is_full_copy": False,
"disk_operate": disk_operate
}
])
tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
vm_ids = [
with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, client)
return vm_api.get_vms({
"where": {
"id_in": vm_ids
}
})[0]
def create_vm_from_template_modify_disk_example():
disk_operate = {
"remove_disks": {
"disk_index": [0] # 用于删除指定 index 的磁盘,index 从 0 开始计算,这里既是删除第一块磁盘
},
"new_disks": {
"mount_cd_roms": [
{
"boot": 2, # 启动顺序
"content_library_image_id": "" # 指定挂载内容库镜像的 id
}
],
"mount_disks": [
{
"boot": 3, # 启动顺序
"bus": Bus.VIRTIO, # 总线类型
"vm_volume_id": "cljm6x2g1405g0958tp3zkhvh" # 被挂载虚拟卷的 id
}
],
"mount_new_create_disks": [
{
"boot": 4,
"bus": Bus.VIRTIO,
"vm_volume": {
"name": "test", # 新建虚拟卷的名称
"size": 10 * 1024 * 1024 * 1024, # 新建虚拟卷的大小,单位是字节
"elf_storage_policy": VmVolumeElfStoragePolicyType._2_THIN_PROVISION # 存储策略
}
}
]
}
}
create_vm_from_template_modify_disk("template-name", "cluster-name", "vm-name", disk_operate)

Creating and editing virtual NICs via a VM template in content library

You can create a virtual machine from a template in content library and configure its NICs.

  • template_name: The name of the template in content library.
  • cluster_name: The name of the cluster.
  • vm_name: The name of the virtual machine.
  • nic_params: The NIC parameters.

For details, see the method for create_vm_from_template_modified_nic_example.

The return value is the created virtual machine.

When creating a virtual machine from a template in content library, if you do not provide the vm_nics parameter, the virtual machine will use the NIC configuration from the template by default. To modify the NIC configuration, you can pass the vm_nics parameter, which is a list with each element being a dictionary:

  • connect_vlan_id: The ID of the VM network to which the NIC corresponds. It is not the vlan_id of the VM network.
  • enabled: Specifies whether to enable the NIC.
  • model: The NIC type. You can use the attributes of the VmNicModel class, such as VmNicModel.VIRTIO. When creating a virtual machine, you cannot modify the NIC's IP address, MAC address, gateway, or subnet mask. To configure IP, subnet, and gateway settings, use cloud-init. The template must support cloud-init.
from cloudtower.api import VmApi, ContentLibraryVmTemplateApi, ClusterApi
from cloudtower.utils import login, wait_tasks
from cloudtower.configuration import Configuration
from cloudtower.models import Bus, VmNicModel
from cloudtower import ApiClient
import os
configuration = Configuration(host=os.getenv("CLOUDTOWER_ENDPOINT"))
client = ApiClient(configuration)
login(client, os.getenv("CLOUDTOWER_USERNAME"), os.getenv("CLOUDTOWER_PASSWORD"))
def create_vm_from_template_modified_nic(template_name, cluster_name, vm_name, nic_params):
vm_api = VmApi(client)
cluster_api = ClusterApi(client)
template_api = ContentLibraryVmTemplateApi(client)
cluster = cluster_api.get_clusters({
"where": {
"name": cluster_name
}
})
if len(cluster) == 0:
raise Exception("cluster not found")
template = template_api.get_content_library_vm_templates({
"where": {
"name": template_name
}
})
if len(template) == 0:
raise Exception("template not found")
with_task_vms = vm_api.create_vm_from_content_library_template([
{
"template_id": template[0].id,
"cluster_id": cluster[0].id,
"name": vm_name,
"is_full_copy": False,
"vm_nics": nic_params
}
])
tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
vm_ids = [
with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, client)
return vm_api.get_vms({
"where": {
"id_in": vm_ids
}
})[0]
def create_vm_from_template_modified_nic_example():
nic_params = [
{
"connect_vlan_id": "vlan_id",
"enabled": True,
"model": VmNicModel.VIRTIO
}
]
create_vm_from_template_modified_nic("template_name", "cluster_name", "vm_name", nic_params)

Creating and editing cloud-init using a template in content library

You can create a virtual machine from a template in content library and configure its cloud-init settings. The template must have cloud-init enabled.

  • template_name: The name of the template in content library.
  • cluster_name: The name of the cluster.
  • vm_name: Enter the name of the virtual machine.
  • cloud_init: The cloud-init configuration. For details, see the method for create_vm_from_template_with_cloudinit_example.

The return value is the created virtual machine.

You can use cloud-init to initialize a virtual machine, such as configuring the network and the default user password. To work properly, the template must have the cloud-init or cloudbase-init service installed during its creation.

The cloud_init parameter is of the TemplateCloudInit type and is a dictionary containing the following fields:

  • default_user_password: Configure the default user password.
  • nameservers: DNS server addresses. This is a list of strings, supporting up to three entries.
  • networks: The network configuration, which is a list of dictionaries containing:
    • ip_address: The IP address, required after configuring a static address.
    • netmask: The subnet mask, required after configuring a static address.
    • nic_index: The NIC index, starting from 0.
    • routes: The static route configuration. A list of dictionaries containing:
      • gateway: The gateway address.
      • network: The target network.
      • netmask: The target subnet.
  • hostname: The host name.
  • public_keys: Public keys for login.
  • user_data: The user data configuration.
from cloudtower.api import VmApi, ContentLibraryVmTemplateApi, ClusterApi
from cloudtower.utils import login, wait_tasks
from cloudtower.configuration import Configuration
from cloudtower import ApiClient
import os
configuration = Configuration(host=os.getenv("CLOUDTOWER_ENDPOINT"))
client = ApiClient(configuration)
login(client, os.getenv("CLOUDTOWER_USERNAME"), os.getenv("CLOUDTOWER_PASSWORD"))
def create_vm_from_template_with_cloudinit(template_name, cluster_name, vm_name, cloud_init):
vm_api = VmApi(client)
cluster_api = ClusterApi(client)
template_api = ContentLibraryVmTemplateApi(client)
cluster = cluster_api.get_clusters({
"where": {
"name": cluster_name
}
})
if len(cluster) == 0:
raise Exception("cluster not found")
template = template_api.get_content_library_vm_templates({
"where": {
"name": template_name
}
})
if len(template) == 0:
raise Exception("template not found")
with_task_vms = vm_api.create_vm_from_content_library_template([
{
"template_id": template[0].id,
"cluster_id": cluster[0].id,
"name": vm_name,
"is_full_copy": False,
"cloud_init": cloud_init
}
])
tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
vm_ids = [
with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, client)
return vm_api.get_vms({
"where": {
"id_in": vm_ids
}
})[0]
def create_vm_from_template_with_cloudinit_example():
cloud_init = {
"default_user_password": "password",
"nameservers": [
"114.114.114.114"
],
"networks": [
{
"ip_address": "192.168.20.1",
"netmask": "255.255.240.0",
"nic_index": 0,
"routes": [
{
"gateway": "192.168.16.1", # 默认网关配置
"network": "0.0.0.0",
"netmask": "0.0.0.0",
},
]
}
],
"hostname": "test",
"public_keys": [
"key_content"
],
"user_data": "user_data"
}
create_vm_from_template_with_cloudinit("template_name", "cluster_name", "vm_name", cloud_init)