def _create_service(self, folder, service_name, networks): """ Retrieves and clones a service into a master folder. :param folder: Folder to create service in :type folder: vim.Folder :param str service_name: Name of the service to clone :param list networks: Networks to configure the service with :return: The service VM instance :rtype: :class:`VM` """ if not self._is_vsphere(service_name): self._log.debug("Skipping non-vsphere service '%s'", service_name) return None config = self.services[service_name] vm_name = self.master_prefix + service_name test = folder.traverse_path(vm_name) # Check service already exists if test is None: # Find the template that matches the service definition template = self.template_folder.traverse_path(config["template"]) if not template: self._log.error( "Could not find template '%s' for service '%s'", config["template"], service_name) return None self._log.info("Creating service '%s'", service_name) vm = VM(name=vm_name, folder=folder, resource_pool=self.server.get_pool(), datastore=self.server.datastore, host=self.host) if not vm.create(template=template): return None else: self._log.warning("Service %s already exists", service_name) vm = VM(vm=test) if vm.is_template(): # Check if it's been converted already self._log.warning( "Service %s is a Template, " "skipping configuration", service_name) return vm # Resource configurations (minus storage currently) if "resource-config" in config: vm.edit_resources(**config["resource-config"]) if "note" in config: # Set VM note if specified vm.set_note(config["note"]) # NOTE: management interfaces matter here! # (If implemented with Monitoring extensions) self._configure_nics(vm, networks=networks) # Configure VM NICs # Post-creation snapshot vm.create_snapshot("Start of Mastering", "Beginning of Mastering phase for exercise %s", self.metadata["name"]) return vm
def _convert_and_verify(self, folder): """ Converts Masters to Templates before deployment. This also ensures they are powered off before being cloned. :param folder: Folder containing Master instances to convert and verify :type folder: vim.Folder """ self._log.debug("Converting Masters in folder '%s' to templates", folder.name) for item in folder.childEntity: if is_vm(item): vm = VM(vm=item) self.masters[vm.name] = vm if vm.is_template(): # Skip if they already exist from a previous run self._log.debug("Master '%s' is already a template", vm.name) continue # Cleanly power off VM before converting to template if vm.powered_on(): vm.change_state("off", attempt_guest=True) # Take a snapshot to allow reverts to the start of the exercise vm.create_snapshot( "Start of exercise", "Beginning of deployment phase, " "post-master configuration") # Convert Master instance to Template vm.convert_template() if not vm.is_template(): self._log.error("Master '%s' did not convert to Template", vm.name) else: self._log.debug("Converted Master '%s' to Template", vm.name) elif is_folder(item): # Recurse into sub-folders self._convert_and_verify(item) else: self._log.debug( "Unknown item found while " "templatizing Masters: %s", str(item))