]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Using another create_vm()
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 25 Oct 2018 14:47:02 +0000 (16:47 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 25 Oct 2018 14:47:02 +0000 (16:47 +0200)
lib/cr_vmware_tpl/handler.py
python_fb_tools

index ed43a79b02ba4c28106f09c43ab99f440f44b9cb..e352149000b995931dca4035341c4f9de9c5fa9d 100644 (file)
@@ -39,10 +39,11 @@ from fb_tools.handler import BaseHandler
 
 from fb_tools.vsphere import BaseVsphereHandler
 from fb_tools.vsphere.server import VsphereServer
+from fb_tools.vsphere.iface import VsphereVmInterface
 
 from .config import CrTplConfiguration
 
-__version__ = '0.10.6'
+__version__ = '0.10.7'
 
 LOG = logging.getLogger(__name__)
 TZ = pytz.timezone('Europe/Berlin')
@@ -109,7 +110,7 @@ class CrTplHandler(BaseHandler):
             host=self.config.vsphere_host, port=self.config.vsphere_port,
             user=self.config.vsphere_user, password=self.config.password,
             dc=self.config.dc, cluster=self.config.vsphere_cluster,
-            auto_close=True, simulate=self.simulate, force=self.force,
+            auto_close=True, simulate=self.simulate, force=self.force, tz=TZ,
             terminal_has_colors=self.terminal_has_colors, initialized=False)
         self.cluster = None
 
@@ -160,6 +161,8 @@ class CrTplHandler(BaseHandler):
                 n=self.config.network, c=self.cluster.name))
             return 6
 
+        self.vsphere.get_networks()
+
         self.vsphere.ensure_vm_folder(self.config.folder)
         self.check_for_temp_tpl_vm(no_error=True)
         self.select_data_store()
@@ -464,126 +467,28 @@ class CrTplHandler(BaseHandler):
     # -------------------------------------------------------------------------
     def create_vm(self):
 
-        LOG.info("Creating VM {!r} ...".format(self.config.template_vm))
-
-        tstamp = datetime.datetime.now(tz=TZ).strftime('%Y-%m-%d_%H-%M')
-
-        datastore_path = (
-            '[' + self.tpl_data_store.name + '] template-' + tstamp + '/')
-        LOG.debug("Datastore path: {!r}".format(datastore_path))
-        vm_path_name = datastore_path + self.config.template_vm + '.vmx'
-        LOG.debug("VM path name: {!r}".format(vm_path_name))
-
-        vm_file_info = vim.vm.FileInfo(
-            logDirectory=datastore_path, snapshotDirectory=datastore_path,
-            suspendDirectory=datastore_path, vmPathName=vm_path_name)
+        disk_size = self.config.data_size_gb
 
-        dev_changes = []
-
-        # Creating SCSI Controller and disk
-        scsi_ctr_spec = vim.vm.device.VirtualDeviceSpec()
-        scsi_ctr_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
-        scsi_ctr_spec.device = vim.vm.device.VirtualLsiLogicController()
-        scsi_ctr_spec.device.key = 0
-        scsi_ctr_spec.device.unitNumber = 1
-        scsi_ctr_spec.device.sharedBus = 'noSharing'
-        controller = scsi_ctr_spec.device
-
-        dev_changes.append(scsi_ctr_spec)
-
-        disk_spec = vim.vm.device.VirtualDeviceSpec()
-        disk_spec.fileOperation = "create"
-        disk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
-        disk_spec.device = vim.vm.device.VirtualDisk()
-        disk_spec.device.backing = vim.vm.device.VirtualDisk.FlatVer2BackingInfo()
-        disk_spec.device.backing.diskMode = 'persistent'
-        disk_spec.device.backing.fileName = '{}template-sda.vmdk'.format(datastore_path)
-        disk_spec.device.unitNumber = 0
-        # disk_spec.device.key = 1
-        disk_spec.device.capacityInKB = self.config.data_size_kb
-        disk_spec.device.controllerKey = controller.key
-
-        dev_changes.append(disk_spec)
-
-        # Creating network adapter
-        nic_spec = vim.vm.device.VirtualDeviceSpec()
-        nic_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
-        nic_spec.device = vim.vm.device.VirtualVmxnet3()
-        nic_spec.device.deviceInfo = vim.Description()
-        nic_spec.device.deviceInfo.label = 'eth0'
-        nic_spec.device.deviceInfo.summary = 'Primary network device'
-
-        nic_spec.device.backing = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
-        nic_spec.device.backing.useAutoDetect = False
-        nic_spec.device.backing.network = self.tpl_network
-        nic_spec.device.backing.deviceName = self.config.network
-
-        nic_spec.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
-        nic_spec.device.connectable.startConnected = True
-        nic_spec.device.connectable.allowGuestControl = True
-        nic_spec.device.wakeOnLanEnabled = True
-        nic_spec.device.addressType = 'assigned'
-        nic_spec.device.macAddress = self.config.mac_address
-
-        dev_changes.append(nic_spec)
-
-        # Graphic Card
-        video_spec = vim.vm.device.VirtualDeviceSpec()
-        video_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
-        video_spec.device = vim.vm.device.VirtualVideoCard()
-        video_spec.device.enable3DSupport = False
-        video_spec.device.graphicsMemorySizeInKB = 256 * 1024
-        video_spec.device.numDisplays = 1
-        video_spec.device.use3dRenderer = 'automatic'
-        video_spec.device.videoRamSizeInKB = 32 * 1024
-
-        dev_changes.append(video_spec)
-
-        # Some other flags
-        vm_flags = vim.vm.FlagInfo()
-        vm_flags.diskUuidEnabled = True
-
-        # Some extra options and properties
-        extra_opts = []
-        created_opt = vim.option.OptionValue()
-        created_opt.key = 'created'
-        created_opt.value = int(time.time())
-        extra_opts.append(created_opt)
-
-        # Set waiting for 3 second in BIOS before booting
-        boot_opts = vim.vm.BootOptions()
-        boot_opts.bootDelay = 3000
-        boot_opts.bootRetryEnabled = False
-        boot_opts.enterBIOSSetup = False
-
-        config = vim.vm.ConfigSpec(
-            name=self.config.template_vm, deviceChange=dev_changes,
-            flags=vm_flags, extraConfig=extra_opts,
-            memoryMB=self.config.ram_mb, memoryHotAddEnabled=True,
-            numCPUs=self.config.num_cpus, cpuHotAddEnabled=True, cpuHotRemoveEnabled=True,
-            files=vm_file_info,
-            guestId=self.config.os_version,
-            version=self.config.vmware_cfg_version,
-            bootOptions=boot_opts,
-        )
+        iface = VsphereVmInterface(
+            appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
+            name='eth', network=self.tpl_network, network_name=self.config.network,
+            mac_address=self.config.mac_address, summary='Primary network device')
 
-        if self.verbose > 1:
-            LOG.debug("Generated VM config:\n{}".format(pp(config)))
+        vm_spec = self.vsphere.generate_vm_create_spec(
+            name=self.config.template_vm, datastore=self.tpl_data_store.name,
+            disks=[disk_size], nw_interfaces=[iface], graphic_ram_mb=256,
+            videao_ram_mb=32, boot_delay_secs=5, ram_mb=self.config.ram_mb,
+            num_cpus=self.config.num_cpus, ds_with_timestamp=True,
+            os_version=self.config.os_version, cfg_version=self.config.vmware_cfg_version)
 
         tpl_vm_folder = self.vsphere.get_vm_folder(self.config.folder)
         if self.verbose > 1:
             LOG.debug("VM-Folder object for template VM: {c} - {n!r}\n{t}".format(
                 c=tpl_vm_folder, n=tpl_vm_folder.name, t=pp(tpl_vm_folder.childType)))
 
-        if self.simulate:
-            LOG.info("Simulation mode - VM {!r} will not be created.".format(
-                self.config.template_vm))
-            return
-
-        LOG.debug("Start Creating VM ...")
-        task = tpl_vm_folder.CreateVM_Task(config=config, pool=self.cluster.resource_pool)
-
-        self.vsphere.wait_for_tasks([task], poll_time=0.2)
+        self.vsphere.create_vm(
+            name=self.config.template_vm, vm_folder=tpl_vm_folder, vm_config_spec=vm_spec,
+            pool=self.cluster.resource_pool, max_wait=15)
 
     # -------------------------------------------------------------------------
     def poweron_vm(self):
index 5522764e8ca03bacdf3ccd75c2df8bc4101942db..054826f57a21a2a29fb19609d4192383844528a6 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 5522764e8ca03bacdf3ccd75c2df8bc4101942db
+Subproject commit 054826f57a21a2a29fb19609d4192383844528a6