From: Frank Brehm Date: Fri, 13 Apr 2018 14:18:57 +0000 (+0200) Subject: Changing MAC address of VM after installation X-Git-Tag: 0.1.1~30 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=95edc1494290bdc17b5a6edf08ff50c814dd7522;p=pixelpark%2Fcreate-vmware-tpl.git Changing MAC address of VM after installation --- diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index fed0751..6ead378 100644 --- a/lib/cr_vmware_tpl/config.py +++ b/lib/cr_vmware_tpl/config.py @@ -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 diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index 9fff9ef..61c5a43 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -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):