def get_vm_by_name(self, name, datacenter=None): """ Returns an instance of VIVirtualMachine. Where its name matches @name. The VM is searched throughout all the datacenters, unless the name or MOR of the datacenter the VM belongs to is provided. The first instance matching @name is returned. NOTE: As names might be duplicated is recommended to use get_vm_by_path instead. """ if not self.__logged: raise VIException( "Must call 'connect' before invoking this method", FaultTypes.NOT_CONNECTED) try: nodes = [None] if datacenter and VIMor.is_mor(datacenter): nodes = [datacenter] elif datacenter: dc = self.get_datacenters() nodes = [k for k, v in dc.items() if v == datacenter] for node in nodes: vms = self._get_managed_objects(MORTypes.VirtualMachine, from_mor=node) for k, v in vms.items(): if v == name: return VIVirtualMachine(self, k) except (VI.ZSI.FaultException) as e: raise VIApiException(e) raise VIException("Could not find a VM named '%s'" % name, FaultTypes.OBJECT_NOT_FOUND)
def __init__(self, server, entity=None, recursion=None, types=None, chain_id=None): """Creates a Event History Collector that gathers Event objects. based on the provides filters. * server: the connected VIServer instance * entity: Entity MOR, if provided filters events related to this entity * recursion: If 'entity' is provided then recursion is mandatory. specification of related managed entities in the inventory hierarchy should be either: 'all', 'children', or 'self' * types: if provided, limits the set of collected events by their types. * chain_id: if provided, retrieves events by chain ID """ super(VIEventHistoryCollector, self).__init__(server) if entity and not VIMor.is_mor(entity): raise VIException("Entity should be a MOR object", FaultTypes.PARAMETER_ERROR) if entity and not recursion in [Recursion.ALL, Recursion.CHILDREN, Recursion.SELF]: raise VIException("Recursion should be either: " "'all', 'children', or 'self'", FaultTypes.PARAMETER_ERROR) try: event_manager = server._do_service_content.EventManager request = VI.CreateCollectorForEventsRequestMsg() _this = request.new__this(event_manager) _this.set_attribute_type(event_manager.get_attribute_type()) request.set_element__this(_this) _filter = request.new_filter() if types and not isinstance(types, list): types = [types] if types: _filter.set_element_eventTypeId(types) if chain_id is not None: _filter.set_element_eventChainId(chain_id) if entity: entity_filter = _filter.new_entity() mor_entity = entity_filter.new_entity(entity) mor_entity.set_attribute_type(entity.get_attribute_type()) entity_filter.set_element_entity(mor_entity) entity_filter.set_element_recursion(recursion) _filter.set_element_entity(entity_filter) request.set_element_filter(_filter) resp = server._proxy.CreateCollectorForEvents(request)._returnval except (VI.ZSI.FaultException) as e: raise VIApiException(e) self._mor = resp self._props = VIProperty(self._server, self._mor)
def __init__(self, server, entity=None, recursion=None, states=None): """Creates a Task History Collector that gathers Task info objects. based on the provides filters. * server: the connected VIServer instance * entity: Entity MOR, if provided filters tasks related to this entity * recursion: If 'entity' is provided then recursion is mandatory. specification of related managed entities in the inventory hierarchy should be either: 'all', 'children', or 'self' * states: if provided, limits the set of collected tasks by their states. Should be a list or one of 'queued', 'running', 'error', or 'success' """ self._server = server self._mor = None if entity and not VIMor.is_mor(entity): raise VIException("Entity should be a MOR object", FaultTypes.PARAMETER_ERROR) if entity and not recursion in [ Recursion.ALL, Recursion.CHILDREN, Recursion.SELF ]: raise VIException( "Recursion should be either: " "'all', 'children', or 'self'", FaultTypes.PARAMETER_ERROR) try: task_manager = server._do_service_content.TaskManager request = VI.CreateCollectorForTasksRequestMsg() _this = request.new__this(task_manager) _this.set_attribute_type(task_manager.get_attribute_type()) request.set_element__this(_this) _filter = request.new_filter() if states and not isinstance(states, list): states = [states] if states: _filter.set_element_state(states) if entity: entity_filter = _filter.new_entity() mor_entity = entity_filter.new_entity(entity) mor_entity.set_attribute_type(entity.get_attribute_type()) entity_filter.set_element_entity(mor_entity) entity_filter.set_element_recursion(recursion) _filter.set_element_entity(entity_filter) request.set_element_filter(_filter) resp = server._proxy.CreateCollectorForTasks(request)._returnval except (VI.ZSI.FaultException) as e: raise VIApiException(e) self._mor = resp self._props = VIProperty(self._server, self._mor)
def __read_tasks(self, max_count, next_page): if not isinstance(max_count, int): raise VIException("max_count should be an integer", FaultTypes.PARAMETER_ERROR) if next_page: request = VI.ReadNextTasksRequestMsg() else: request = VI.ReadPreviousTasksRequestMsg() _this = request.new__this(self._mor) _this.set_attribute_type(self._mor.get_attribute_type()) request.set_element__this(_this) request.set_element_maxCount(max_count) try: if next_page: resp = self._server._proxy.ReadNextTasks(request)._returnval else: resp = self._server._proxy.ReadPreviousTasks( request)._returnval ret = [] for task in resp: ret.append(VITask(task.Task, self._server)) except (VI.ZSI.FaultException) as e: raise VIApiException(e) return ret
def get_datastore_by_name(self, name, datacenter=None): """ Returns an instance of VIDatastore, where its name matches @name. The datastore is searched throughout all the datacenters, unless the name or the MOR of the datacenter (the datastore belongs to) is given. The first instance matching @name is returned. """ if not self.__logged: raise VIException( "Must call 'connect' before invoking this method", FaultTypes.NOT_CONNECTED) try: if datacenter: if VIMor.is_mor(datacenter): nodes = [datacenter] else: dc = self.get_datacenters() nodes = [(k, v) for k, v in dc.iteritems() if v == datacenter] else: nodes = self.get_datacenters().iteritems() # todo: use self.get_datastores() instead, if possible for node in nodes: datacenter_mor, datacenter_name = node datastores = self._get_managed_objects(MORTypes.Datastore, from_mor=datacenter_mor) for k, v in datastores.iteritems(): if v == name: return VIDatastore(self, datacenter_name, v) except (VI.ZSI.FaultException), e: raise VIApiException(e)
def __read_events(self, max_count, next_page): """ Reads the 'scrollable view' from the current position. """ if not isinstance(max_count, int): raise VIException("max_count should be an integer", FaultTypes.PARAMETER_ERROR) if next_page: request = VI.ReadNextEventsRequestMsg() else: request = VI.ReadPreviousEventsRequestMsg() _this = request.new__this(self._mor) _this.set_attribute_type(self._mor.get_attribute_type()) request.set_element__this(_this) request.set_element_maxCount(max_count) try: if next_page: resp = self._server._proxy.ReadNextEvents(request)._returnval else: resp = self._server._proxy.ReadPreviousEvents(request)._returnval ret = [] for event in resp: ret.append(VIProperty(self._server,event)) except (VI.ZSI.FaultException), e: raise VIApiException(e)
def vms_from_folder(self, folder_name, status="poweredOn"): """Returns a list of VM Paths in a root folder. @folder_name: name to filter VMs registered in that folder @status: status to filter VMs, default poweredOn, pass None for not status filter """ folders = self._get_managed_objects(MORTypes.Folder) try: folder_mor = [ mor for mor, name in folders.iteritems() if name == folder_name ][0] except IndexError: raise VIException("Root folder not found: %s" % folder_name) properties = ['config.files.vmPathName', 'runtime.powerState'] vms = self._retrieve_properties_traversal( property_names=properties, obj_type=MORTypes.VirtualMachine, from_node=folder_mor) if status: status_filter = lambda vmstatus: vmstatus == status else: status_filter = lambda vmstatus: True return [ vm.PropSet[0].Val for vm in vms if status_filter(vm.PropSet[1].Val) ]
def _create_filter(self, property_names=[], from_node=None, obj_type='ManagedEntity', partial_updates=True): """Creates filter with given parameters and returns its MOR""" try: if not from_node: from_node = self._do_service_content.RootFolder elif isinstance(from_node, tuple) and len(from_node) == 2: from_node = VIMor(from_node[0], from_node[1]) elif not VIMor.is_mor(from_node): raise VIException("from_node must be a MOR object or a " "(<str> mor_id, <str> mor_type) tuple", FaultTypes.PARAMETER_ERROR) request = VI.CreateFilterRequestMsg() _this = request.new__this(self._do_service_content.PropertyCollector) _this.set_attribute_type(MORTypes.PropertyCollector) request.set_element__this(_this) request.set_element_partialUpdates(partial_updates) spec = request.new_spec() propSet = spec.new_propSet() propSet.set_element_type(obj_type) propSet.set_element_pathSet(property_names) spec.set_element_propSet([propSet]) objects_set = self._get_traversal_objects_set(spec, from_node) spec.set_element_objectSet(objects_set) request.set_element_spec(spec) mor = self._proxy.CreateFilter(request)._returnval return mor except (VI.ZSI.FaultException), e: raise VIApiException(e)
def _get_object_properties_bulk(self, mor_list, properties): """Similar to _get_object_properties but you can retrieve different sets of properties for many managed object of different types. @mor_list is a list of the managed object references you want to retrieve properties from, @properties is a dictionary where keys are managed object types and values are a list of the properties you want to retrieve for that type of object (or an empty list if you want to retrieve all). E.g. _get_object_properties_bulk( [mor_obj_1, mor_obj_2,...,mor_obj_3], {'VirtualMachine':['guest.toolStatus', 'name], 'ResourcePool':['summary'], 'VirtualMachineSnapthot':[]} Returns the corresponding objectContent data object array.""" if not self.__logged: raise VIException( "Must call 'connect' before invoking this method", FaultTypes.NOT_CONNECTED) try: request, request_call = self._retrieve_property_request() pc = request.new__this(self._do_service_content.PropertyCollector) pc.set_attribute_type(self._do_service_content.PropertyCollector. get_attribute_type()) request.set_element__this(pc) spec_sets = [] spec_set = request.new_specSet() prop_sets = [] for mo_type, path_set in properties.iteritems(): prop_set = spec_set.new_propSet() prop_set.set_element_type(mo_type) if path_set: prop_set.set_element_pathSet(path_set) prop_set.set_element_all(False) else: prop_set.set_element_all(True) prop_sets.append(prop_set) spec_set.set_element_propSet(prop_sets) object_sets = [] for mo in mor_list: object_set = spec_set.new_objectSet() obj = object_set.new_obj(mo) obj.set_attribute_type(mo.get_attribute_type()) object_set.set_element_obj(obj) object_set.set_element_skip(False) object_sets.append(object_set) spec_set.set_element_objectSet(object_sets) spec_sets.append(spec_set) request.set_element_specSet(spec_sets) return request_call(request) except (VI.ZSI.FaultException), e: raise VIApiException(e)
def delete_vm_by_name(self, name, remove_files=True): """ Unregisters a VM and remove it files from the datastore by name. @name is the VM name. @remove_files - if True (default) will delete VM files from datastore. """ statusLine = '' success = False if not self.__logged: raise VIException("Must call 'connect' before invoking this method", FaultTypes.NOT_CONNECTED) try: # Get VM vm = self.get_vm_by_name(name) if remove_files: # Invoke Destroy_Task request = VI.Destroy_TaskRequestMsg() _this = request.new__this(vm._mor) _this.set_attribute_type(vm._mor.get_attribute_type()) request.set_element__this(_this) ret = self._proxy.Destroy_Task(request)._returnval task = VITask(ret, self) # Wait for the task to finish status = task.wait_for_state([task.STATE_SUCCESS, task.STATE_ERROR]) if status == task.STATE_SUCCESS: statusLine = "VM successfully unregistered and deleted from datastore" success = True elif status == task.STATE_ERROR: statusLine = "Error removing vm: {}".format(task.get_error_message()) success = False else: # Invoke UnregisterVMRequestMsg request = VI.UnregisterVMRequestMsg() _this = request.new__this(vm._mor) _this.set_attribute_type(vm._mor.get_attribute_type()) request.set_element__this(_this) ret = self._proxy.UnregisterVM(request) task = VITask(ret, self) statusLine = "VM successfully unregistered (files still on datastore)" success = True except VI.ZSI.FaultException as e: raise VIApiException(e) finally: return success, statusLine
def get_vm_by_path(self, path, datacenter=None): """Returns an instance of VIVirtualMachine. Where its path matches @path. The VM is searched througout all the datacenters, unless the name or MOR of the datacenter the VM belongs to is provided.""" if not self.__logged: raise VIException( "Must call 'connect' before invoking this method", FaultTypes.NOT_CONNECTED) try: dc_list = [] if datacenter and VIMor.is_mor(datacenter): dc_list.append(datacenter) else: dc = self.get_datacenters() if datacenter: dc_list = [k for k, v in dc.items() if v == datacenter] else: dc_list = list(dc.keys()) for mor_dc in dc_list: request = VI.FindByDatastorePathRequestMsg() mor_search_index = request.new__this( self._do_service_content.SearchIndex) mor_search_index.set_attribute_type(MORTypes.SearchIndex) request.set_element__this(mor_search_index) mor_datacenter = request.new_datacenter(mor_dc) mor_datacenter.set_attribute_type(MORTypes.Datacenter) request.set_element_datacenter(mor_datacenter) request.set_element_path(path) try: vm = self._proxy.FindByDatastorePath(request)._returnval except VI.ZSI.FaultException: pass else: if vm: return VIVirtualMachine(self, vm) except (VI.ZSI.FaultException) as e: raise VIApiException(e) raise VIException("Could not find a VM with path '%s'" % path, FaultTypes.OBJECT_NOT_FOUND)
def reset(self): """ Moves the 'scrollable view' to the item immediately preceding the 'viewable latest page'. If you use 'read_previous_events' all items are retrieved from the newest item to the oldest item. """ if not self._mor: raise VIException("History collector is not properly initialized", FaultTypes.PARAMETER_ERROR) request = VI.ResetCollectorRequestMsg() _this = request.new__this(self._mor) _this.set_attribute_type(self._mor.get_attribute_type()) request.set_element__this(_this) self._server._proxy.ResetCollector(request)
def rewind(self): """ Moves the 'scrollable view' to the oldest item. If you use 'read_next_events', all items are retrieved from the oldest item to the newest item. This is the default setting when the collector is created. """ if not self._mor: raise VIException("History collector is not properly initialized", FaultTypes.PARAMETER_ERROR) request = VI.RewindCollectorRequestMsg() _this = request.new__this(self._mor) _this.set_attribute_type(self._mor.get_attribute_type()) request.set_element__this(_this) self._server._proxy.RewindCollector(request)
def keep_session_alive(self): """Asks sever time, usefull for keeping alive a session. Returns False if the session expired""" if not self.__logged: raise VIException("Must call 'connect' before invoking this method", FaultTypes.NOT_CONNECTED) request = VI.CurrentTimeRequestMsg() mor_service_instance = request.new__this("ServiceInstance") mor_service_instance.set_attribute_type(MORTypes.ServiceInstance) request.set_element__this(mor_service_instance) try: self._proxy.CurrentTime(request) return True except(VI.ZSI.FaultException): return False
def _get_object_properties(self, mor, property_names=[], get_all=False): """Returns the properties defined in property_names (or all if get_all is set to True) of the managed object reference given in @mor. Returns the corresponding objectContent data object.""" if not self.__logged: raise VIException( "Must call 'connect' before invoking this method", FaultTypes.NOT_CONNECTED) try: request, request_call = self._retrieve_property_request() _this = request.new__this( self._do_service_content.PropertyCollector) _this.set_attribute_type(MORTypes.PropertyCollector) request.set_element__this(_this) do_PropertyFilterSpec_specSet = request.new_specSet() props_set = [] do_PropertySpec_propSet = do_PropertyFilterSpec_specSet.new_propSet( ) do_PropertySpec_propSet.set_element_type(mor.get_attribute_type()) if not get_all: do_PropertySpec_propSet.set_element_pathSet(property_names) else: do_PropertySpec_propSet.set_element_all(True) props_set.append(do_PropertySpec_propSet) objects_set = [] do_ObjectSpec_objSet = do_PropertyFilterSpec_specSet.new_objectSet( ) obj = do_ObjectSpec_objSet.new_obj(mor) obj.set_attribute_type(mor.get_attribute_type()) do_ObjectSpec_objSet.set_element_obj(obj) do_ObjectSpec_objSet.set_element_skip(False) objects_set.append(do_ObjectSpec_objSet) do_PropertyFilterSpec_specSet.set_element_propSet(props_set) do_PropertyFilterSpec_specSet.set_element_objectSet(objects_set) request.set_element_specSet([do_PropertyFilterSpec_specSet]) ret = request_call(request) if ret and isinstance(ret, list): return ret[0] except (VI.ZSI.FaultException), e: raise VIApiException(e)
def _retrieve_properties_traversal(self, property_names=[], from_node=None, obj_type='ManagedEntity'): """Uses VI API's property collector to retrieve the properties defined in @property_names of Managed Objects of type @obj_type ('ManagedEntity' by default). Starts the search from the managed object reference @from_node (RootFolder by default). Returns the corresponding objectContent data object.""" try: if not from_node: from_node = self._do_service_content.RootFolder elif isinstance(from_node, tuple) and len(from_node) == 2: from_node = VIMor(from_node[0], from_node[1]) elif not VIMor.is_mor(from_node): raise VIException("from_node must be a MOR object or a " "(<str> mor_id, <str> mor_type) tuple", FaultTypes.PARAMETER_ERROR) request, request_call = self._retrieve_property_request() _this = request.new__this( self._do_service_content.PropertyCollector) _this.set_attribute_type(MORTypes.PropertyCollector) request.set_element__this(_this) do_PropertyFilterSpec_specSet = request.new_specSet() props_set = [] do_PropertySpec_propSet = do_PropertyFilterSpec_specSet.new_propSet() do_PropertySpec_propSet.set_element_type(obj_type) do_PropertySpec_propSet.set_element_pathSet(property_names) props_set.append(do_PropertySpec_propSet) objects_set = self._get_traversal_objects_set(do_PropertyFilterSpec_specSet, from_node) do_PropertyFilterSpec_specSet.set_element_propSet(props_set) do_PropertyFilterSpec_specSet.set_element_objectSet(objects_set) request.set_element_specSet([do_PropertyFilterSpec_specSet]) return request_call(request) except (VI.ZSI.FaultException), e: raise VIApiException(e)
def wait_for_state(self, states, check_interval=2, timeout=-1): """Waits for the task to be in any of the given states checking the status every @check_interval seconds. Raises an exception if @timeout is reached If @timeout is 0 or negative, waits indefinitely""" if not isinstance(states, list): states = [states] start_time = time.time() while True: cur_state = self.get_state() if cur_state in states: return cur_state if timeout > 0: if (time.time() - start_time) > timeout: raise VIException("Timed out waiting for task state.", FaultTypes.TIME_OUT) time.sleep(check_interval)
def acquire_clone_ticket(self): """Acquire a session-specific ticket string which can be used to clone the current session. The caller of this operation can pass the ticket value to another entity on the client. The recipient can then call CloneSession with the ticket string on an unauthenticated session and avoid having to re-enter credentials. The ticket may only be used once and becomes invalid after use. The ticket is also invalidated when the corresponding session is closed or expires. The ticket is only valid on the server which issued it.""" if not self.__logged: raise VIException("Must call 'connect' before invoking this method", FaultTypes.NOT_CONNECTED) try: request = VI.AcquireCloneTicketRequestMsg() _this = request.new__this(self._do_service_content.SessionManager) _this.set_attribute_type(MORTypes.SessionManager) request.set_element__this(_this) return self._proxy.AcquireCloneTicket(request)._returnval except (VI.ZSI.FaultException), e: raise VIApiException(e)
def connect(self, host, user, password, trace_file=None, sock_timeout=None): """Opens a session to a VC/ESX server with the given credentials: @host: is the server's hostname or address. If the web service uses another protocol or port than the default, you must use the full service URL (e.g. http://myhost:8888/sdk) @user: username to connect with @password: password to authenticate the session @trace_file: (optional) a file path to log SOAP requests and responses @sock_timeout: (optional) only for python >= 2.6, sets the connection timeout for sockets, in python 2.5 you'll have to use socket.setdefaulttimeout(secs) to change the global setting. """ self.__user = user self.__password = password #Generate server's URL if not isinstance(host, basestring): raise VIException("'host' should be a string with the ESX/VC url.", FaultTypes.PARAMETER_ERROR) elif (host.lower().startswith('http://') or host.lower().startswith('https://')): server_url = host.lower() else: server_url = 'https://%s/sdk' % host try: #get the server's proxy locator = VI.VimServiceLocator() args = {'url': server_url} if trace_file: trace = open(trace_file, 'w') args['tracefile'] = trace if sock_timeout and sys.version_info >= (2, 6): args['transdict'] = {'timeout': sock_timeout} self._proxy = locator.getVimPortType(**args) for header, value in self.__initial_headers.iteritems(): self._proxy.binding.AddHeader(header, value) #get service content from service instance request = VI.RetrieveServiceContentRequestMsg() mor_service_instance = request.new__this('ServiceInstance') mor_service_instance.set_attribute_type(MORTypes.ServiceInstance) request.set_element__this(mor_service_instance) self._do_service_content = self._proxy.RetrieveServiceContent( request)._returnval self.__server_type = self._do_service_content.About.Name self.__api_version = self._do_service_content.About.ApiVersion self.__api_type = self._do_service_content.About.ApiType #login request = VI.LoginRequestMsg() mor_session_manager = request.new__this( self._do_service_content.SessionManager) mor_session_manager.set_attribute_type(MORTypes.SessionManager) request.set_element__this(mor_session_manager) request.set_element_userName(user) request.set_element_password(password) self.__session = self._proxy.Login(request)._returnval self.__logged = True except (VI.ZSI.FaultException), e: raise VIApiException(e)
def get_registered_vms(self, datacenter=None, cluster=None, resource_pool=None, status=None, advanced_filters=None): """Returns a list of VM Paths. @datacenter: name or MORs to filter VMs registered in that datacenter @cluster: name or MORs to filter VMs registered in that cluster. If set datacenter is ignored. @resource_pool: name path or MORs to filter VMs registered in that resource pool. If set, both, @datacenter and @cluster are ignored. @status: either 'poweredOn', 'poweredOff', or 'suspended' to retrieve only VMs in that power state @advanced_filters: dictionary """ if not self.__logged: raise VIException( "Must call 'connect' before invoking this method", FaultTypes.NOT_CONNECTED) try: if not advanced_filters or not isinstance(advanced_filters, dict): advanced_filters = {} if status: advanced_filters['runtime.powerState'] = [status] property_filter = list(advanced_filters.iterkeys()) if not 'config.files.vmPathName' in property_filter: property_filter.insert(0, 'config.files.vmPathName') #Root MOR filters ret = [] nodes = [None] if resource_pool and VIMor.is_mor(resource_pool): nodes = [resource_pool] elif resource_pool: nodes = [ k for k, v in self.get_resource_pools().iteritems() if v == resource_pool ] elif cluster and VIMor.is_mor(cluster): nodes = [cluster] elif cluster: nodes = [ k for k, v in self.get_clusters().iteritems() if v == cluster ] elif datacenter and VIMor.is_mor(datacenter): nodes = [datacenter] elif datacenter: nodes = [ k for k, v in self.get_datacenters().iteritems() if v == datacenter ] for node in nodes: obj_content = self._retrieve_properties_traversal( property_names=property_filter, from_node=node, obj_type=MORTypes.VirtualMachine) if not obj_content: continue for obj in obj_content: try: prop_set = obj.PropSet except AttributeError: continue ppath = None filter_match = dict([(k, False) for k in advanced_filters.iterkeys()]) for item in prop_set: if item.Name == 'config.files.vmPathName': ppath = item.Val elif item.Name in filter_match: expected = advanced_filters.get(item.Name) if not isinstance(expected, list): expected = [expected] if item.Val in expected: filter_match[item.Name] = True if all(filter_match.values()): ret.append(ppath) return ret except (VI.ZSI.FaultException), e: raise VIApiException(e)
request.set_element__this(mor_search_index) mor_datacenter = request.new_datacenter(mor_dc) mor_datacenter.set_attribute_type(MORTypes.Datacenter) request.set_element_datacenter(mor_datacenter) request.set_element_path(path) try: vm = self._proxy.FindByDatastorePath(request)._returnval except VI.ZSI.FaultException: pass else: if vm: return VIVirtualMachine(self, vm) except (VI.ZSI.FaultException), e: raise VIApiException(e) raise VIException("Could not find a VM with path '%s'" % path, FaultTypes.OBJECT_NOT_FOUND) def get_vm_by_name(self, name, datacenter=None): """ Returns an instance of VIVirtualMachine. Where its name matches @name. The VM is searched throughout all the datacenters, unless the name or MOR of the datacenter the VM belongs to is provided. The first instance matching @name is returned. NOTE: As names might be duplicated is recommended to use get_vm_by_path instead. """ if not self.__logged: raise VIException( "Must call 'connect' before invoking this method", FaultTypes.NOT_CONNECTED) try:
def _retrieve_properties_traversal(self, property_names=[], from_node=None, obj_type='ManagedEntity'): """Uses VI API's property collector to retrieve the properties defined in @property_names of Managed Objects of type @obj_type ('ManagedEntity' by default). Starts the search from the managed object reference @from_node (RootFolder by default). Returns the corresponding objectContent data object.""" try: if not from_node: from_node = self._do_service_content.RootFolder elif isinstance(from_node, tuple) and len(from_node) == 2: from_node = VIMor(from_node[0], from_node[1]) elif not VIMor.is_mor(from_node): raise VIException( "from_node must be a MOR object or a " "(<str> mor_id, <str> mor_type) tuple", FaultTypes.PARAMETER_ERROR) request, request_call = self._retrieve_property_request() _this = request.new__this( self._do_service_content.PropertyCollector) _this.set_attribute_type(MORTypes.PropertyCollector) request.set_element__this(_this) do_PropertyFilterSpec_specSet = request.new_specSet() props_set = [] do_PropertySpec_propSet = do_PropertyFilterSpec_specSet.new_propSet( ) do_PropertySpec_propSet.set_element_type(obj_type) do_PropertySpec_propSet.set_element_pathSet(property_names) props_set.append(do_PropertySpec_propSet) objects_set = [] do_ObjectSpec_objSet = do_PropertyFilterSpec_specSet.new_objectSet( ) mor_obj = do_ObjectSpec_objSet.new_obj(from_node) mor_obj.set_attribute_type(from_node.get_attribute_type()) do_ObjectSpec_objSet.set_element_obj(mor_obj) do_ObjectSpec_objSet.set_element_skip(False) #Recurse through all ResourcePools rp_to_rp = VI.ns0.TraversalSpec_Def('rpToRp').pyclass() rp_to_rp.set_element_name('rpToRp') rp_to_rp.set_element_type(MORTypes.ResourcePool) rp_to_rp.set_element_path('resourcePool') rp_to_rp.set_element_skip(False) rp_to_vm = VI.ns0.TraversalSpec_Def('rpToVm').pyclass() rp_to_vm.set_element_name('rpToVm') rp_to_vm.set_element_type(MORTypes.ResourcePool) rp_to_vm.set_element_path('vm') rp_to_vm.set_element_skip(False) spec_array_resource_pool = [ do_ObjectSpec_objSet.new_selectSet(), do_ObjectSpec_objSet.new_selectSet() ] spec_array_resource_pool[0].set_element_name('rpToRp') spec_array_resource_pool[1].set_element_name('rpToVm') rp_to_rp.set_element_selectSet(spec_array_resource_pool) #Traversal through resource pool branch cr_to_rp = VI.ns0.TraversalSpec_Def('crToRp').pyclass() cr_to_rp.set_element_name('crToRp') cr_to_rp.set_element_type(MORTypes.ComputeResource) cr_to_rp.set_element_path('resourcePool') cr_to_rp.set_element_skip(False) spec_array_computer_resource = [ do_ObjectSpec_objSet.new_selectSet(), do_ObjectSpec_objSet.new_selectSet() ] spec_array_computer_resource[0].set_element_name('rpToRp') spec_array_computer_resource[1].set_element_name('rpToVm') cr_to_rp.set_element_selectSet(spec_array_computer_resource) #Traversal through host branch cr_to_h = VI.ns0.TraversalSpec_Def('crToH').pyclass() cr_to_h.set_element_name('crToH') cr_to_h.set_element_type(MORTypes.ComputeResource) cr_to_h.set_element_path('host') cr_to_h.set_element_skip(False) #Traversal through hostFolder branch dc_to_hf = VI.ns0.TraversalSpec_Def('dcToHf').pyclass() dc_to_hf.set_element_name('dcToHf') dc_to_hf.set_element_type(MORTypes.Datacenter) dc_to_hf.set_element_path('hostFolder') dc_to_hf.set_element_skip(False) spec_array_datacenter_host = [do_ObjectSpec_objSet.new_selectSet()] spec_array_datacenter_host[0].set_element_name('visitFolders') dc_to_hf.set_element_selectSet(spec_array_datacenter_host) #Traversal through vmFolder branch dc_to_vmf = VI.ns0.TraversalSpec_Def('dcToVmf').pyclass() dc_to_vmf.set_element_name('dcToVmf') dc_to_vmf.set_element_type(MORTypes.Datacenter) dc_to_vmf.set_element_path('vmFolder') dc_to_vmf.set_element_skip(False) spec_array_datacenter_vm = [do_ObjectSpec_objSet.new_selectSet()] spec_array_datacenter_vm[0].set_element_name('visitFolders') dc_to_vmf.set_element_selectSet(spec_array_datacenter_vm) #Traversal through datastore branch dc_to_ds = VI.ns0.TraversalSpec_Def('dcToDs').pyclass() dc_to_ds.set_element_name('dcToDs') dc_to_ds.set_element_type(MORTypes.Datacenter) dc_to_ds.set_element_path('datastore') dc_to_ds.set_element_skip(False) spec_array_datacenter_ds = [do_ObjectSpec_objSet.new_selectSet()] spec_array_datacenter_ds[0].set_element_name('visitFolders') dc_to_ds.set_element_selectSet(spec_array_datacenter_ds) #Recurse through all hosts h_to_vm = VI.ns0.TraversalSpec_Def('hToVm').pyclass() h_to_vm.set_element_name('hToVm') h_to_vm.set_element_type(MORTypes.HostSystem) h_to_vm.set_element_path('vm') h_to_vm.set_element_skip(False) spec_array_host_vm = [do_ObjectSpec_objSet.new_selectSet()] spec_array_host_vm[0].set_element_name('visitFolders') h_to_vm.set_element_selectSet(spec_array_host_vm) #Recurse through all datastores ds_to_vm = VI.ns0.TraversalSpec_Def('dsToVm').pyclass() ds_to_vm.set_element_name('dsToVm') ds_to_vm.set_element_type(MORTypes.Datastore) ds_to_vm.set_element_path('vm') ds_to_vm.set_element_skip(False) spec_array_datastore_vm = [do_ObjectSpec_objSet.new_selectSet()] spec_array_datastore_vm[0].set_element_name('visitFolders') ds_to_vm.set_element_selectSet(spec_array_datastore_vm) #Recurse through the folders visit_folders = VI.ns0.TraversalSpec_Def('visitFolders').pyclass() visit_folders.set_element_name('visitFolders') visit_folders.set_element_type(MORTypes.Folder) visit_folders.set_element_path('childEntity') visit_folders.set_element_skip(False) spec_array_visit_folders = [ do_ObjectSpec_objSet.new_selectSet(), do_ObjectSpec_objSet.new_selectSet(), do_ObjectSpec_objSet.new_selectSet(), do_ObjectSpec_objSet.new_selectSet(), do_ObjectSpec_objSet.new_selectSet(), do_ObjectSpec_objSet.new_selectSet(), do_ObjectSpec_objSet.new_selectSet(), do_ObjectSpec_objSet.new_selectSet(), do_ObjectSpec_objSet.new_selectSet() ] spec_array_visit_folders[0].set_element_name('visitFolders') spec_array_visit_folders[1].set_element_name('dcToHf') spec_array_visit_folders[2].set_element_name('dcToVmf') spec_array_visit_folders[3].set_element_name('crToH') spec_array_visit_folders[4].set_element_name('crToRp') spec_array_visit_folders[5].set_element_name('dcToDs') spec_array_visit_folders[6].set_element_name('hToVm') spec_array_visit_folders[7].set_element_name('dsToVm') spec_array_visit_folders[8].set_element_name('rpToVm') visit_folders.set_element_selectSet(spec_array_visit_folders) #Add all of them here spec_array = [ visit_folders, dc_to_vmf, dc_to_ds, dc_to_hf, cr_to_h, cr_to_rp, rp_to_rp, h_to_vm, ds_to_vm, rp_to_vm ] do_ObjectSpec_objSet.set_element_selectSet(spec_array) objects_set.append(do_ObjectSpec_objSet) do_PropertyFilterSpec_specSet.set_element_propSet(props_set) do_PropertyFilterSpec_specSet.set_element_objectSet(objects_set) request.set_element_specSet([do_PropertyFilterSpec_specSet]) return request_call(request) except (VI.ZSI.FaultException), e: raise VIApiException(e)
def clone(self, name, sync_run=True, folder=None, resourcepool=None, datastore=None, host=None, power_on=True, template=False, snapshot=None, linked=False, customize=None, data=None): """Clones this Virtual Machine @name: name of the new virtual machine @sync_run: if True (default) waits for the task to finish, and returns a VIVirtualMachine instance with the new VM (raises an exception if the task didn't succeed). If sync_run is set to False the task is started and a VITask instance is returned @folder: name of the folder that will contain the new VM, if not set the vm will be added to the folder the original VM belongs to @resourcepool: MOR of the resourcepool to be used for the new vm. If not set, it uses the same resourcepool than the original vm. @datastore: MOR of the datastore where the virtual machine should be located. If not specified, the current datastore is used. @host: MOR of the host where the virtual machine should be registered. IF not specified: * if resourcepool is not specified, current host is used. * if resourcepool is specified, and the target pool represents a stand-alone host, the host is used. * if resourcepool is specified, and the target pool represents a DRS-enabled cluster, a host selected by DRS is used. * if resource pool is specified and the target pool represents a cluster without DRS enabled, an InvalidArgument exception be thrown. @power_on: If the new VM will be powered on after being created. If template is set to True, this parameter is ignored. @template: Specifies whether or not the new virtual machine should be marked as a template. @snapshot: Snaphot MOR, or VISnaphost object, or snapshot name (if a name is given, then the first matching occurrence will be used). Is the snapshot reference from which to base the clone. If this parameter is set, the clone is based off of the snapshot point. This means that the newly created virtual machine will have the same configuration as the virtual machine at the time the snapshot was taken. If this parameter is not set then the clone is based off of the virtual machine's current configuration. @linked: If True (requires @snapshot to be set) creates a new child disk backing on the destination datastore. None of the virtual disk's existing files should be moved from their current locations. Note that in the case of a clone operation, this means that the original virtual machine's disks are now all being shared. This is only safe if the clone was taken from a snapshot point, because snapshot points are always read-only. Thus for a clone this option is only valid when cloning from a snapshot """ try: #get the folder to create the VM folders = self._server._retrieve_properties_traversal( property_names=['name', 'childEntity'], obj_type=MORTypes.Folder) folder_mor = None for f in folders: fname = "" children = [] for prop in f.PropSet: if prop.Name == "name": fname = prop.Val elif prop.Name == "childEntity": children = prop.Val.ManagedObjectReference if folder == fname or (not folder and self._mor in children): folder_mor = f.Obj break if not folder_mor and folder: raise VIException("Couldn't find folder %s" % folder, FaultTypes.OBJECT_NOT_FOUND) elif not folder_mor: raise VIException("Error locating current VM folder", FaultTypes.OBJECT_NOT_FOUND) request = VI.CloneVM_TaskRequestMsg() _this = request.new__this(self._mor) _this.set_attribute_type(self._mor.get_attribute_type()) request.set_element__this(_this) request.set_element_folder(folder_mor) request.set_element_name(name) spec = request.new_spec() if template: spec.set_element_powerOn(False) else: spec.set_element_powerOn(power_on) location = spec.new_location() if resourcepool: if not VIMor.is_mor(resourcepool): resourcepool = VIMor(resourcepool, MORTypes.ResourcePool) pool = location.new_pool(resourcepool) pool.set_attribute_type(resourcepool.get_attribute_type()) location.set_element_pool(pool) if datastore: if not VIMor.is_mor(datastore): datastore = VIMor(datastore, MORTypes.Datastore) ds = location.new_datastore(datastore) ds.set_attribute_type(datastore.get_attribute_type()) location.set_element_datastore(ds) if host: if not VIMor.is_mor(host): host = VIMor(host, MORTypes.HostSystem) hs = location.new_host(host) hs.set_attribute_type(host.get_attribute_type()) location.set_element_host(hs) if snapshot: sn_mor = None if VIMor.is_mor(snapshot): sn_mor = snapshot elif isinstance(snapshot, VISnapshot): sn_mor = snapshot._mor elif isinstance(snapshot, basestring): for sn in self.get_snapshots(): if sn.get_name() == snapshot: sn_mor = sn._mor break if not sn_mor: raise VIException( "Could not find snapshot '%s'" % snapshot, FaultTypes.OBJECT_NOT_FOUND) snapshot = spec.new_snapshot(sn_mor) snapshot.set_attribute_type(sn_mor.get_attribute_type()) spec.set_element_snapshot(snapshot) if linked and snapshot: location.set_element_diskMoveType("createNewChildDiskBacking") if not template and customize: if data is None: raise VIApiException( "Cannot use Customization without data") customization = spec.new_customization() spec.set_element_customization(customization) globalIPSettings = customization.new_globalIPSettings() customization.set_element_globalIPSettings(globalIPSettings) # nicSettingMap nicSetting = customization.new_nicSettingMap() adapter = nicSetting.new_adapter() nicSetting.set_element_adapter(adapter) ipAddress = data.get('ip') netmask = data.get('netmask') gateway = data.get('gateway') fixedip = VI.ns0.CustomizationFixedIp_Def( "ipAddress").pyclass() fixedip.set_element_ipAddress(ipAddress) #dhcp = VI.ns0.CustomizationDhcpIpGenerator_Def("ip").pyclass() adapter.set_element_ip(fixedip) adapter.set_element_subnetMask(netmask) #help(adapter.set_element_gateway([gateway,])) adapter.set_element_gateway([ gateway, ]) nicSetting.set_element_adapter(adapter) customization.set_element_nicSettingMap([ nicSetting, ]) if customize == "SYSPREP": # here starts windows identity = VI.ns0.CustomizationSysprep_Def( "identity").pyclass() customization.set_element_identity(identity) guiUnattended = identity.new_guiUnattended() guiUnattended.set_element_autoLogon(True) guiUnattended.set_element_autoLogonCount(1) passw = guiUnattended.new_password() guiUnattended.set_element_password(passw) passw.set_element_value(data["adminpw"]) passw.set_element_plainText(True) # http://msdn.microsoft.com/en-us/library/ms912391(v=winembedded.11).aspx # 85 is GMT Standard Time timeZone = data.get("timezone", 85) guiUnattended.set_element_timeZone(timeZone) identity.set_element_guiUnattended(guiUnattended) userData = identity.new_userData() userData.set_element_fullName( data.get("fullName", "PyShere")) userData.set_element_orgName( data.get("orgName", "PySphere")) userData.set_element_productId("") computerName = VI.ns0.CustomizationFixedName_Def( "computerName").pyclass() computerName.set_element_name(name.replace("_", "")) userData.set_element_computerName(computerName) identity.set_element_userData(userData) identification = identity.new_identification() if data.get("joinDomain", False): # join the domain identification.set_element_domainAdmin( data["domainAdmin"]) domainAdminPassword = identification.new_domainAdminPassword( ) domainAdminPassword.set_element_plainText(True) domainAdminPassword.set_element_value( data["domainAdminPassword"]) identification.set_element_domainAdminPassword( domainAdminPassword) identification.set_element_joinDomain( data["joinDomain"]) identity.set_element_identification(identification) elif customize == "SYSPREPTEXT": identity = VI.ns0.CustomizationSysprepText_Def( "identity").pyclass() customization.set_element_identity(identity) identity.set_element_value(data["value"]) elif customize == "LINUX": identity = VI.ns0.CustomizationLinuxPrep_Def( "identity").pyclass() customization.set_element_identity(identity) identity.set_element_domain(data["domain"]) hostName = VI.ns0.CustomizationFixedName_Def( "hostName").pyclass() hostName.set_element_name(name.replace("_", "")) identity.set_element_hostName(hostName) spec.set_element_location(location) spec.set_element_template(template) request.set_element_spec(spec) task = self._server._proxy.CloneVM_Task(request)._returnval vi_task = VITask(task, self._server) if sync_run: status = vi_task.wait_for_state( [vi_task.STATE_SUCCESS, vi_task.STATE_ERROR]) if status == vi_task.STATE_ERROR: raise VIException(vi_task.get_error_message(), FaultTypes.TASK_ERROR) return mianbao(self._server, vi_task.get_result()._obj) return vi_task except (VI.ZSI.FaultException), e: raise VIApiException(e)
def connect(self, host, user=None, password=None, passthrough=False, trace_file=None, sock_timeout=None): """Opens a session to a VC/ESX server with the given credentials: @host: is the server's hostname or address. If the web service uses another protocol or port than the default, you must use the full service URL (e.g. http://myhost:8888/sdk) @user: (optional) username to connect with @password: (optional) password to authenticate the session @passthrough: (optional) use Windows session credentials or MIT Kerberos credentials to connect. User should provide user/password pair OR set passthrough to True @trace_file: (optional) a file path to log SOAP requests and responses @sock_timeout: (optional) only for python >= 2.6, sets the connection timeout for sockets, in python 2.5 you'll have to use socket.setdefaulttimeout(secs) to change the global setting. """ if (((user is None or password is None) and not passthrough) or ((user is not None or password is not None) and passthrough)): raise TypeError("connect() takes user/password pair OR passthrough=True") self.__user = user self.__password = password #Generate server's URL if not isinstance(host, basestring): raise VIException("'host' should be a string with the ESX/VC url." ,FaultTypes.PARAMETER_ERROR) elif (host.lower().startswith('http://') or host.lower().startswith('https://')): server_url = host.lower() else: server_url = 'https://%s/sdk' % host try: #get the server's proxy locator = VI.VimServiceLocator() args = {'url':server_url} if trace_file: trace=open(trace_file, 'w') args['tracefile'] = trace if sock_timeout and sys.version_info >= (2, 6): args['transdict'] = {'timeout':sock_timeout} self._proxy = locator.getVimPortType(**args) for header, value in self.__initial_headers.iteritems(): self._proxy.binding.AddHeader(header, value) #get service content from service instance request = VI.RetrieveServiceContentRequestMsg() mor_service_instance = request.new__this('ServiceInstance') mor_service_instance.set_attribute_type(MORTypes.ServiceInstance) request.set_element__this(mor_service_instance) self._do_service_content = self._proxy.RetrieveServiceContent( request)._returnval self.__server_type = self._do_service_content.About.Name self.__api_version = self._do_service_content.About.ApiVersion self.__api_type = self._do_service_content.About.ApiType if not passthrough: #login with user/password request = VI.LoginRequestMsg() mor_session_manager = request.new__this( self._do_service_content.SessionManager) mor_session_manager.set_attribute_type(MORTypes.SessionManager) request.set_element__this(mor_session_manager) request.set_element_userName(user) request.set_element_password(password) self.__session = self._proxy.Login(request)._returnval self.__logged = True else: fqdn, aliases, addrs = gethostbyaddr(urlparse(server_url).netloc) if os.name == 'nt': #login with Windows session credentials try: from sspi import ClientAuth except ImportError: raise ImportError("To enable passthrough authentication please"\ " install pywin32 (available for Windows only)") spn = "host/%s" % fqdn client = ClientAuth("Kerberos", targetspn=spn) def get_token(serverToken=None): if serverToken is not None: serverToken = b64decode(serverToken) err, bufs = client.authorize(serverToken) return b64encode(bufs[0].Buffer) else: #login with MIT Kerberos credentials try: import kerberos except ImportError: raise ImportError("To enable passthrough authentication please"\ " install python bindings for kerberos") spn = "host@%s" % fqdn flags = kerberos.GSS_C_INTEG_FLAG|kerberos.GSS_C_SEQUENCE_FLAG|\ kerberos.GSS_C_REPLAY_FLAG|kerberos.GSS_C_CONF_FLAG errc, client = kerberos.authGSSClientInit(spn, gssflags=flags) def get_token(serverToken=''): cres = kerberos.authGSSClientStep(client, serverToken) return kerberos.authGSSClientResponse(client) token = get_token() while not self.__logged: try: request = VI.LoginBySSPIRequestMsg() mor_session_manager = request.new__this( self._do_service_content.SessionManager) mor_session_manager.set_attribute_type(MORTypes.SessionManager) request.set_element__this(mor_session_manager) request.set_element_base64Token(token) self.__session = self._proxy.LoginBySSPI(request)._returnval self.__logged = True except (VI.ZSI.FaultException), e: if e.fault.string == "fault.SSPIChallenge.summary": serverToken = e.fault.detail[0].Base64Token token = get_token(serverToken) else: raise e except (VI.ZSI.FaultException), e: raise VIApiException(e)
def relocate(self, sync_run=True, priority='default', datastore=None, resource_pool=None, host=None, transform=None, disks=None): """ Cold or Hot relocates this virtual machine's virtual disks to a new datastore. @sync_run: If True (default) waits for the task to finish, and returns (raises an exception if the task didn't succeed). If False the task is started an a VITask instance is returned. @priority: either 'default', 'high', or 'low': priority of the task that moves the vm. Note this priority can affect both the source and target hosts. @datastore: The target datastore to which the virtual machine's virtual disks are intended to migrate. @resource_pool: The resource pool to which this virtual machine should be attached. If the argument is not supplied, the current resource pool of virtual machine is used. @host: The target host for the virtual machine. If not specified, * if resource pool is not specified, current host is used. * if resource pool is specified, and the target pool represents a stand-alone host, the host is used. * if resource pool is specified, and the target pool represents a DRS-enabled cluster, a host selected by DRS is used. * if resource pool is specified and the target pool represents a cluster without DRS enabled, an InvalidArgument exception be thrown. @transform: If specified, the virtual machine's virtual disks are transformed to the datastore using the specificed method; must be either 'flat' or 'sparse'. """ try: if priority not in ['default', 'low', 'high']: raise VIException( "priority must be either 'default', 'low', or 'high'.", FaultTypes.PARAMETER_ERROR) if transform and transform not in [None, 'flat', 'sparse']: raise VIException( "transform, if set, must be either '%s' or '%s'." % ('flat', 'sparse'), FaultTypes.PARAMETER_ERROR) request = VI.RelocateVM_TaskRequestMsg() _this = request.new__this(self._mor) _this.set_attribute_type(self._mor.get_attribute_type()) request.set_element__this(_this) spec = request.new_spec() if datastore: if not VIMor.is_mor(datastore): ds = VIMor(datastore, MORTypes.Datastore) datastore = spec.new_datastore(ds) datastore.set_attribute_type(ds.get_attribute_type()) spec.set_element_datastore(datastore) if resource_pool: if not VIMor.is_mor(resource_pool): rp = VIMor(resource_pool, MORTypes.ResourcePool) resource_pool = spec.new_pool(rp) resource_pool.set_attribute_type(rp.get_attribute_type()) spec.set_element_pool(resource_pool) if host: if not VIMor.is_mor(host): h = VIMor(host, MORTypes.HostSystem) host = spec.new_host(h) host.set_attribute_type(h.get_attribute_type()) spec.set_element_host(host) if transform: spec.set_element_transform(transform) if disks and isinstance(disks, dict): disk_spec = [] for k, disk_ds in disks.iteritems(): if not VIMor.is_mor(disk_ds): disk_ds = VIMor(disk_ds, MORTypes.Datastore) disk = spec.new_disk() disk.DiskId = k ds = disk.new_datastore(disk_ds) ds.set_attribute_type(disk_ds.get_attribute_type()) disk.Datastore = ds disk_spec.append(disk) spec.Disk = disk_spec request.set_element_priority(priority + "Priority") request.set_element_spec(spec) task = self._server._proxy.RelocateVM_Task(request)._returnval vi_task = VITask(task, self._server) if sync_run: status = vi_task.wait_for_state( [vi_task.STATE_SUCCESS, vi_task.STATE_ERROR]) if status == vi_task.STATE_ERROR: raise VIException(vi_task.get_error_message(), FaultTypes.TASK_ERROR) return return vi_task except (VI.ZSI.FaultException), e: raise VIApiException(e)