def print_live_versions(self, item_id): # Get and print live versions of the VM template item versions_info = self.client.versions_service.list(item_id) print("Live versions of VM template item:") for version_info in versions_info: vm_template = get_obj_by_moId(self.servicemanager.content, [vim.VirtualMachine], version_info.vm_template) print("Version: {}, VM template name: {}".format( version_info.version, vm_template.name))
def _cleanup(self): for lib_id in self.pub_libs_to_clean: print("deleting published library: {0}".format(lib_id)) self.client.local_library_service.delete(lib_id) for lib_id in self.sub_libs_to_clean: print("deleting subscribed library: {0}".format(lib_id)) self.client.subscribed_library_service.delete(lib_id) for vm_id in self.vms_to_clean: vm_obj = get_obj_by_moId(self.servicemanager.content, [vim.VirtualMachine], vm_id) delete_object(self.servicemanager.content, vm_obj)
def _execute(self): # Get the identifiers of the resources used for deployment item_id = self.helper.get_item_id_by_name(self.item_name) assert item_id folder_id = get_folder(self.vsphere_client, self.datacenter_name, self.folder_name) assert folder_id resource_pool_id = get_resource_pool(self.vsphere_client, self.datacenter_name, self.resource_pool_name) assert resource_pool_id datastore_id = get_datastore_id(self.servicemanager, self.datastore_name) assert datastore_id # Build the deployment specification placement_spec = VmtxLibraryItem.DeployPlacementSpec( folder=folder_id, resource_pool=resource_pool_id) vm_home_storage_spec = VmtxLibraryItem.DeploySpecVmHomeStorage( datastore=datastore_id) disk_storage_spec = VmtxLibraryItem.DeploySpecDiskStorage( datastore=datastore_id) deploy_spec = VmtxLibraryItem.DeploySpec( name=self.vm_name, placement=placement_spec, vm_home_storage=vm_home_storage_spec, disk_storage=disk_storage_spec) # Deploy a virtual machine from the VM template item self.vm_id = self.client.vmtx_service.deploy(item_id, deploy_spec) self.vm = get_obj_by_moId(self.servicemanager.content, [vim.VirtualMachine], self.vm_id) print("Deployed VM '{0}' with ID: {1}".format(self.vm.name, self.vm_id)) # Print a summary of the deployed VM vm_summary = self.vm.summary.config print('Guest OS: {0}'.format(vm_summary.guestId)) print('{0} CPU(s)'.format(vm_summary.numCpu)) print('{0} MB memory'.format(vm_summary.memorySizeMB)) print('{0} disk(s)'.format(vm_summary.numVirtualDisks)) print('{0} network adapter(s)'.format(vm_summary.numEthernetCards))
def deploy_ovf_template(self, lib_item_id, ovf_summary, deployment_target): # Build the deployment spec deployment_spec = LibraryItem.ResourcePoolDeploymentSpec( name=self.vm_name, annotation=ovf_summary.annotation, accept_all_eula=True, network_mappings=None, storage_mappings=None, storage_provisioning=None, storage_profile_id=None, locale=None, flags=None, additional_parameters=None, default_datastore_id=None) # Deploy the ovf template result = self.client.ovf_lib_item_service.deploy( lib_item_id, deployment_target, deployment_spec, client_token=generate_random_uuid()) # The type and ID of the target deployment is available in the deployment result. if result.succeeded: print( 'Deployment successful. Result resource: {0}, ID: {1}'.format( result.resource_id.type, result.resource_id.id)) self.vm_id = result.resource_id.id error = result.error if error is not None: for warning in error.warnings: print('OVF warning: {}'.format(warning.message)) # Power on the VM and wait for the power on operation to be completed self.vm_obj = get_obj_by_moId(self.servicemanager.content, [vim.VirtualMachine], self.vm_id) assert self.vm_obj is not None poweron_vm(self.servicemanager.content, self.vm_obj) else: print('Deployment failed.') for error in result.error.errors: print('OVF error: {}'.format(error.message))
def deploy_kmip_node(self, spec, debug=False): # Check if we know about the deployment target yet if self.vcs_cluster_obj is None: # Find the cluster's resource pool moid print() print("Obtaining the cluster's resource pool moid...") self.vcs_cluster_obj = get_obj(self.service_manager.content, [vim.ClusterComputeResource], self.deploy_to_cluster) assert self.vcs_cluster_obj is not None if debug: print(" --> Cluster: {0}".format(self.vcs_cluster_obj)) # Get a deployment target resource pool from the Cluster Object deployment_target = LibraryItem.DeploymentTarget( resource_pool_id=self.vcs_cluster_obj.resourcePool._GetMoId()) if debug: print(" --> Resource Pool Moref: {0}".format( self.vcs_cluster_obj.resourcePool._GetMoId())) # Find lib item id from given item name find_spec = Item.FindSpec() find_spec.name = self.selected_item["name"] item_ids = self.sm_client.library_item_service.find(find_spec) assert (item_ids is not None and len(item_ids) > 0), ( 'No items found with name: {0}'.format(self.selected_item["name"])) lib_item_id = item_ids[0] ovf_summary = self.sm_client.ovf_lib_item_service.filter( ovf_library_item_id=lib_item_id, target=deployment_target) print() print('Deploying OVF template: {0} to cluster: {1}...'.format( ovf_summary.name, self.deploy_to_cluster)) # Build the deployment spec deployment_spec = LibraryItem.ResourcePoolDeploymentSpec( name=spec.name, annotation=ovf_summary.annotation, accept_all_eula=True, network_mappings=None, storage_mappings=None, storage_provisioning=None, storage_profile_id=None, locale=None, flags=None, additional_parameters=None, default_datastore_id=None) # Deploy the ovf template print("Deploying HyTrust OVF Template -- please wait...") print() result = self.sm_client.ovf_lib_item_service.deploy( lib_item_id, deployment_target, deployment_spec, client_token=generate_random_uuid()) # The type and ID of the target deployment is available in the deployment result. if result.succeeded: print('>>> Deployment successful.') if debug: print('Result resource: {0}, ID: {1}'.format( result.resource_id.type, result.resource_id.id)) error = result.error # if error is not None: # for warning in error.warnings: # print('OVF warning: {}'.format(warning.message)) # Get the vm object if spec.is_primary(): self.primary_node_id = result.resource_id.id self.primary_node_obj = get_obj_by_moId( self.service_manager.content, [vim.VirtualMachine], self.primary_node_id) assert self.primary_node_obj is not None vm_obj = self.primary_node_obj vm_id = self.primary_node_id else: self.secondary_node_id = result.resource_id.id self.secondary_node_obj = get_obj_by_moId( self.service_manager.content, [vim.VirtualMachine], self.secondary_node_id) assert self.secondary_node_obj is not None vm_obj = self.secondary_node_obj vm_id = self.secondary_node_id # Invoke the SmartConnect() method by supplying the host name, user name, and password. self.service_instance_stub = SmartConnect(host=self.web_svcs_host, user=self.sso_user, pwd=self.sso_pass) atexit.register(connect.Disconnect, self.service_instance_stub) content = self.service_instance_stub.RetrieveContent() if debug: print("VM UUID: {0}".format(vm_obj.summary.config.uuid)) if spec.is_primary(): self.primary_node_vm = content.searchIndex.FindByUuid( None, vm_obj.summary.config.uuid, True) vm = self.primary_node_vm else: self.secondary_node_vm = content.searchIndex.FindByUuid( None, vm_obj.summary.config.uuid, True) vm = self.secondary_node_vm print() print("Setting vApp Options on the VM...") vapp_spec_list = list() # Domain Name vapp_domainName = vim.vApp.PropertyInfo() vapp_domainName.key = 0 vapp_domainName.value = spec.domain vapp_spec1 = vim.vApp.PropertySpec() vapp_spec1.info = vapp_domainName vapp_spec1.operation = 'edit' vapp_spec_list.append(vapp_spec1) # Netmask vapp_netmask = vim.vApp.PropertyInfo() vapp_netmask.key = 1 vapp_netmask.value = spec.subnet vapp_spec2 = vim.vApp.PropertySpec() vapp_spec2.info = vapp_netmask vapp_spec2.operation = 'edit' vapp_spec_list.append(vapp_spec2) # KCMaster vapp_kcmaster = vim.vApp.PropertyInfo() vapp_kcmaster.key = 2 if spec.is_primary(): vapp_kcmaster.value = '' else: vapp_kcmaster.userConfigurable = True vapp_kcmaster.value = self.primary_node_spec.ip vapp_spec3 = vim.vApp.PropertySpec() vapp_spec3.info = vapp_kcmaster vapp_spec3.operation = 'edit' vapp_spec_list.append(vapp_spec3) # Console Password vapp_consolepw = vim.vApp.PropertyInfo() vapp_consolepw.key = 3 vapp_consolepw.userConfigurable = True vapp_consolepw.value = spec.consolepw vapp_spec4 = vim.vApp.PropertySpec() vapp_spec4.info = vapp_consolepw vapp_spec4.operation = 'edit' vapp_spec_list.append(vapp_spec4) # HTKC Hostname vapp_htkcHostname = vim.vApp.PropertyInfo() vapp_htkcHostname.key = 4 vapp_htkcHostname.value = spec.name vapp_spec5 = vim.vApp.PropertySpec() vapp_spec5.info = vapp_htkcHostname vapp_spec5.operation = 'edit' vapp_spec_list.append(vapp_spec5) # DNS Servers vapp_dns = vim.vApp.PropertyInfo() vapp_dns.key = 5 vapp_dns.value = spec.dns vapp_spec6 = vim.vApp.PropertySpec() vapp_spec6.info = vapp_dns vapp_spec6.operation = 'edit' vapp_spec_list.append(vapp_spec6) # HTKC IP Address vapp_htkcIP = vim.vApp.PropertyInfo() vapp_htkcIP.key = 6 vapp_htkcIP.value = spec.ip vapp_spec7 = vim.vApp.PropertySpec() vapp_spec7.info = vapp_htkcIP vapp_spec7.operation = 'edit' vapp_spec_list.append(vapp_spec7) # NTP Servers vapp_ntp = vim.vApp.PropertyInfo() vapp_ntp.key = 7 vapp_ntp.value = '0.us.pool.ntp.org, 1.us.pool.ntp.org' vapp_spec8 = vim.vApp.PropertySpec() vapp_spec8.info = vapp_ntp vapp_spec8.operation = 'edit' vapp_spec_list.append(vapp_spec8) # Gateway vapp_gateway = vim.vApp.PropertyInfo() vapp_gateway.key = 8 vapp_gateway.value = spec.gateway vapp_spec9 = vim.vApp.PropertySpec() vapp_spec9.info = vapp_gateway vapp_spec9.operation = 'edit' vapp_spec_list.append(vapp_spec9) # KC Cluster Password vapp_kcpw = vim.vApp.PropertyInfo() vapp_kcpw.key = 9 vapp_kcpw.userConfigurable = True vapp_kcpw.value = spec.clusterpw vapp_spec10 = vim.vApp.PropertySpec() vapp_spec10.info = vapp_kcpw vapp_spec10.operation = 'edit' vapp_spec_list.append(vapp_spec10) # Make the modifications config_spec = vim.vm.ConfigSpec() config_spec.vAppConfig = vim.vApp.VmConfigSpec() config_spec.vAppConfig.property = vapp_spec_list reconfig_task = vm.ReconfigVM_Task(config_spec) task.wait_for_task(reconfig_task) print("Successfully modified VM:[{0}] properties".format( spec.name)) # Add the KMS tag to the VM print() print('Tagging the KMS VM :: {0}...'.format(spec.name)) kms_tag_attached = self.tag_vm(vm_id, self.kms_tag_id) assert kms_tag_attached if debug: print('Tagged KMS vm: {0}'.format(vm_id)) # Power on the VM and wait for the power on operation to be completed print() print("Powering on the KMS Server: {0} ...".format(spec.name)) poweron_vm(self.service_manager.content, vm_obj) while vm.runtime.powerState != vim.VirtualMachinePowerState.poweredOn: print("Waiting for VM [{0}] to power on...".format(spec.name)) time.sleep(3) # Get a WebKMS ticket so we can manipulate the console if we need to if spec.is_primary(): self.primary_node_ticket = vm.AcquireTicket("webmks") ticket = self.primary_node_ticket else: self.secondary_node_ticket = vm.AcquireTicket("webmks") ticket = self.secondary_node_ticket print("WebMKS Ticket: {0}".format(ticket)) print("WMKS URL: wss://{0}:{1}/ticket/{2}".format( ticket.host, ticket.port, ticket.ticket)) else: print('Deployment failed.') for error in result.error.errors: print('OVF error: {}'.format(error.message))