]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Changing MAC address of VM after installation
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 13 Apr 2018 14:18:57 +0000 (16:18 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 13 Apr 2018 14:18:57 +0000 (16:18 +0200)
lib/cr_vmware_tpl/config.py
lib/cr_vmware_tpl/handler.py

index fed07517c3f7a5940e830f5314b9c292049e6efc..6ead378974bb12d2e6755f57f5d0c6f4ec183943 100644 (file)
@@ -27,7 +27,7 @@ from .errors import FunctionNotImplementedError, PpError
 
 from .obj import PpBaseObject
 
-__version__ = '0.5.3'
+__version__ = '0.5.4'
 LOG = logging.getLogger(__name__)
 
 
@@ -63,6 +63,8 @@ class CrTplConfiguration(PpBaseObject):
     min_max_wait_for_finish_install = 3 * 60
     max_max_wait_for_finish_install = 24 * 60 * 60
 
+    mac_address_template = "00:16:3e:53:{:02x}:{:02x}"
+
     # -------------------------------------------------------------------------
     def __init__(
         self, appname=None, verbose=0, version=__version__, base_dir=None,
@@ -159,6 +161,7 @@ class CrTplConfiguration(PpBaseObject):
         res['default_max_nr_templates_stay'] = self.default_max_nr_templates_stay
         res['min_max_wait_for_finish_install'] = self.min_max_wait_for_finish_install
         res['max_max_wait_for_finish_install'] = self.max_max_wait_for_finish_install
+        res['mac_address_template'] = self.mac_address_template
         res['data_size_mb'] = self.data_size_mb
         res['data_size_kb'] = self.data_size_kb
         res['data_size'] = self.data_size
index 9fff9ef84d251b1f8876a26174397f0a779e3480..61c5a43ba859c32a9bb97e01531ea5e7d4ba0b57 100644 (file)
@@ -38,7 +38,7 @@ from .obj import PpBaseObject
 
 from .config import CrTplConfiguration
 
-__version__ = '0.7.4'
+__version__ = '0.7.5'
 LOG = logging.getLogger(__name__)
 TZ = pytz.timezone('Europe/Berlin')
 
@@ -224,6 +224,7 @@ class CrTplHandler(PpBaseObject):
                 self.wait_for_finish_install()
                 self.post_install_tasks_ssh()
                 self.poweroff_vm()
+                self.change_mac_address()
             self.rotate_templates()
             if not self.rotate_only:
                 self.rename_and_change_vm()
@@ -650,11 +651,7 @@ class CrTplHandler(PpBaseObject):
             guestId='oracleLinux64Guest',
             version='vmx-11',
             bootOptions=boot_opts,
-            #guestFullName='Oracle Enterprise Linux 7 (64 Bit)',
-            #alternateGuestName='OEL 7/64',
         )
-            #files=vm_file_info, guestId='OracleLinux7_Guest')
-            #files=vm_file_info, guestId='OracleLinux7_Guest', version='oel7-4')
 
         if self.verbose > 2:
             LOG.debug("Generated VM config:\n{}".format(pp(config)))
@@ -927,6 +924,49 @@ class CrTplHandler(PpBaseObject):
             "VM {h!r} was not shut down after {t:0.1f} seconds, current state is {s!r}.".format(
                 h=self.config.template_vm, t=cur_diff, s=guest_state))
 
+    # -------------------------------------------------------------------------
+    def change_mac_address(self):
+
+        LOG.info("Setting a new, randomized MAC address for template VM ...")
+
+        current_macs = []
+
+        last_tuple1 = random.randint(1, 254)
+        last_tuple2 = random.randint(1, 254)
+        new_mac = self.config.mac_address_template.format(last_tuple1, last_tuple2)
+        LOG.debug("New MAC address: {!r}.".format(new_mac))
+
+        vm = self.get_temp_tpl_vm()
+
+        virtual_nic_device = None
+        for dev in vm.config.hardware.device:
+            if isinstance(dev, vim.vm.device.VirtualEthernetCard):
+                virtual_nic_device = dev
+                break
+
+        if not virtual_nic_device:
+            raise HandlerError('Could not found virtual EthernetCard.')
+
+        virtual_nic_spec = vim.vm.device.VirtualDeviceSpec()
+        virtual_nic_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
+        virtual_nic_spec.device = virtual_nic_device
+        virtual_nic_spec.device.macAddress = new_mac
+        virtual_nic_spec.device.backing = virtual_nic_device.backing
+        virtual_nic_spec.device.wakeOnLanEnabled = virtual_nic_device.wakeOnLanEnabled
+        virtual_nic_spec.device.connectable = virtual_nic_device.connectable
+
+        dev_changes = []
+        dev_changes.append(virtual_nic_spec)
+        spec = vim.vm.ConfigSpec()
+        spec.deviceChange = dev_changes
+
+        if self.verbose > 2:
+            LOG.debug("Changes of MAC address:\n{}".format(pp(spec)))
+
+        task = vm.ReconfigVM_Task(spec=spec)
+        self.wait_for_tasks([task])
+        LOG.debug("Successful changed MAC address of VM.")
+
     # -------------------------------------------------------------------------
     def rotate_templates(self):