示例#1
0
 def _create_vim_service(cls,
                         domain,
                         username=None,
                         password=None,
                         cookie=None,
                         raise_exceptions=False,
                         use_cache=False):
     #First check if there even is a service instance
     if use_cache:
         if cls._cache_and_populate_connection_properties(domain):
             return True
         else:
             logger.info(
                 "[Connection] could not load connection from cache for domain=%s, rebuilding from scratch",
                 domain)
     if cls.svcInstance is None:
         logger.info(
             "[Connection] svcInstance was destroyed correctly, proceeding with creation"
         )
         #Here we set the domain key which is used for caching purposes
         cls._domain_key = domain
         #if no service instance make a new one
         #First check if the wsdl's been loaded, if not, load it.
         if cls.vim25client == None:
             logger.info("[Connection] WSDL not yet loaded, loading...")
             cls._create_vim25client(domain)
         #now try to build the serviceInstance
         #first try from session_key
         if cookie is not None:
             logger.info(
                 "[Connection] Trying to build creation from session key")
             try:
                 cls._create_vim_service_from_cookie(
                     domain, cookie, raise_exceptions=raise_exceptions)
                 return cls.is_service_instance_valid()
             except Exception as e:
                 #this means that the cookie was invalid
                 msg = "[Connection] Cookie invalid. Failing for domain"
                 logger.exception("%s %s: %s", msg, domain, e)
                 if raise_exceptions: raise e
         #if session key failed try the username and password
         if username is not None and password is not None:
             logger.info(
                 "[Connection] Trying to build creation from User/Password")
             try:
                 cls._create_vim_service_from_username_password(
                     domain,
                     username,
                     password,
                     raise_exceptions=raise_exceptions)
                 return cls.is_service_instance_valid()
             except Exception as e:
                 msg = "[Connection] something went wrong when trying to create a new vim service for domain "
                 logger.exception("%s %s: %s", msg, domain, e)
                 if raise_exceptions: raise e
                 #username and password were bad, so we return False
                 return False
示例#2
0
    def collectEvents(self, eventSpec):
        '''Preforms a full collection of the specList passed.  Will not retain any information
		or update status.
		'''
        try:
            events = self.targetEventManager.queryEvents(eventSpec)
            return events
        except Exception as e:
            logger.exception(e)
示例#3
0
	def destroyView(cls, viewListIndex):
		''' Used to remove a view that's been created.  Specify the index list of the target
		view to remove.  Views are accessed on PCollector.cViewRefList
		'''
		try:
			cls.cViewRefList[viewListIndex].destroyView()
			cls.updateViewList()
		except Exception as e:
			logger.error("Missing View, or view list index out of range: %s", str(e))
			logger.exception(e)
示例#4
0
	def destroyFilter(self, filterListIndex):
		''' Used to remove a filter that's been created.  Specify the index list of the target
		filter to remove.  Filters are accessed on PCollector.filterList
		'''
		try:
			self.filterList[filterListIndex].destroyPropertyFilter()
			self.updateFilterList()
		except Exception as e:
			logger.error("Missing filter, or filter list out of range: %s", str(e))
			logger.exception(e)
示例#5
0
	def dumpCollector(self):
		'''Preforms a full dump of the currently set fSpecList.
		'''
		ro = Connection.vim25client.new('RetrieveOptions')
		try:
			props = self.targetCollector.retrievePropertiesEx(specSet=self.fSpecList, options=ro)
			return props
		except Exception as e:
			logger.error("dump collector took a dump: %s", str(e))
			logger.exception(e)
示例#6
0
 def buildTaskCollector(self):
     try:
         if self.taskCollector:
             logger.error("Collector already exists")
         else:
             self.taskCollector = self.targetTaskManager.createCollectorForTasks(
                 self.taskSpec)
     except Exception as e:
         logger.exception(
             "taskSpec does not exist, or there was an error during collector creation."
         )
         logger.exception(e)
示例#7
0
def CollectTasks(startTime=None, endTime=None):
    ''' Collects Tasks for the specified start and end times.
	returns a list of events in key order.
	'''
    try:
        taskListCollector = TaskCollector()
        taskListCollector.buildTaskQuerySpec()
        taskListCollector.buildTimeSpec(startTime, endTime)
        taskList = taskListCollector.collectTasks()
        return taskList

    except Exception as e:
        logger.error("error in collect tasks")
        logger.exception(e)
        logger.exception(traceback.print_exc())
示例#8
0
def CollectEvents(startTime=None, endTime=None):
    ''' Collects Events for the specified start and end times.
	returns a list of events in key order.
	'''
    try:
        Collector = EventCollector()
        Collector.buildEventQuerySpec()
        Collector.buildTimeSpec(startTime, endTime)
        events = Collector.collectEvents(Collector.eventSpec)
        return events

    except Exception as e:
        logger.error("error in collect events")
        logger.exception(e)
        logger.exception(traceback.print_exc())
示例#9
0
	def collectPropertiesEx(self, specList):
		'''Preforms a full collection of the specList passed.  Will not retain any information
		or update status.  Any filter created during this process is destroyed after collection.
		this method will be most often used with buildFilterList.
		'''
		ro = Connection.vim25client.new('RetrieveOptions')
		try:
			props = self.targetCollector.retrievePropertiesEx(specSet=specList, options=ro)
			yield props.objects
			while hasattr(props, 'token'):
				lastToken = str(props.token)
				props = self.targetCollector.continueRetrievePropertiesEx(token=lastToken)
				yield props.objects
		except Exception as e:
			logger.error("collect properties ex fail: %s", str(e))
			logger.exception(e)
示例#10
0
    def _create_vim_service_from_cookie(cls,
                                        domain,
                                        cookie,
                                        raise_exceptions=False):
        """
		Given a domain and a session key try to create a vim service from them
		"""
        try:
            cls.vim25client.createServiceInstance(server_url=domain,
                                                  sessioncookie=cookie)
            cls.svcInstance = cls.vim25client.serviceInstance
            cls._populate_connection_properties()
        except Exception as e:
            #this means that the cookie was invalid
            msg = "[Connection] something went wrong when trying to create a new connection for domain "
            logger.exception("%s %s: %s", msg, domain, e)
            if raise_exceptions: raise e
示例#11
0
    def _create_vim_service_from_username_password(cls,
                                                   domain,
                                                   username,
                                                   password,
                                                   raise_exceptions=False):
        """
		Given a domain, username and password create a vim service from them.
		"""
        # Create an empty serviceInstance first, this is to properly populate with an empty serverConnection
        try:
            cls.vim25client.createServiceInstance(server_url=domain,
                                                  username=username,
                                                  password=password)
            cls.svcInstance = cls.vim25client.serviceInstance
            cls._populate_connection_properties()
        except Exception as e:
            msg = "[Connection] something went wrong when trying to create a new connection for domain "
            logger.exception("%s %s: %s", msg, domain, e)
            if raise_exceptions: raise e
示例#12
0
    def is_service_instance_valid(cls):
        """
		Check if the current service instance bound to the static class is valid
		"""
        try:
            logger.debug(
                "[Connection] Checking for current time on the service instance."
            )
            cls.svcInstance.currentTime()
            logger.debug("[Connection] Time returned properly.")
            return True
        except Exception as e:
            logger.error(
                "[Connection] I can't check the time for domain %s.  Exception below.  Returning False on valid session. Killing all the rebel scum!"
                % (cls.domain))
            logger.exception(e)
            logger.info("[Connection] Destroying bad session for domain %s" %
                        (cls.domain))
            cls._destroy_vim_service()
            return False
示例#13
0
    def checkForUpdates(self, ver=None, maxObjUpdatesWaitOp=None):
        ''' Used to check for any updated changes on ALL created filters.  An optional version
		can be specified to check for a difference between the specified version and the current
		configuration.  This method will return null if there are no differences between the 
		the specified versions.  Submitting a version of "None" will cause a full dump of the filters
		at the creation of the first checkForUpdates() call
		'''
        try:
            lastVersion = ver
            waitOptions = Connection.vim25client.new(
                'WaitOptions',
                maxWaitSeconds=0,
                maxObjectUpdates=maxObjUpdatesWaitOp)
            data = self.targetCollector.waitForUpdatesEx(version=ver,
                                                         options=waitOptions)
            if data:
                lastVersion = getattr(data, 'version')
                # Version may have _ hence trim it
                if lastVersion.find('_') >= 0:
                    lastVersion = lastVersion[0:lastVersion.find('_')]
                    # When result set in chunks then we need to do this hack
                    # If there is subversion, it always start with oldversion_subversion and last truncated
                    # result data contains actual increased version so increasing version by 1 to have right
                    # count for all object
                    lastVersion = int(str(lastVersion)) + 1
                yield lastVersion, data
                while hasattr(data, 'truncated'):
                    version = data.version
                    data = self.targetCollector.waitForUpdatesEx(
                        version=version, options=waitOptions)
                    # Version may have _, hence trim it, also increase by 1 as described above
                    if version.find('_') >= 0:
                        mainVer = version[0:version.find('_')]
                        mainVer = int(str(mainVer)) + 1
                    else:
                        mainVer = version
                    yield mainVer, data
        except Exception as e:
            logger.error("[Inventory] checkForUpdates Failure")
            logger.exception(e)
示例#14
0
	def __init__(self, mor=None):
		try:
			CurrentViewManager.updateViewMgr()
			CurrentViewManager.updateViewList()
			if mor:
				if type(mor) != str:
					raise
				else:
					tempMOR = ManagedObjectReference(value=mor, _type="PropertyCollector")
					# tempMOR = Connection.vim25client.new('_this', value=mor, _type="PropertyCollector")
					self.targetCollector = Connection.vim25client.createExactManagedObject(mor=tempMOR)
			else:
				self.targetCollector = CurrentViewManager.propColl.createPropertyCollector()
			if not CurrentViewManager.cViewRef:
				CurrentViewManager.buildView(targetConfig.targetEntities)
			self.oSpec = None
			self.tSpec = CurrentViewManager.tSpec
			self.filterList = self.targetCollector.getFilter('filter')
			self.fSpecList = []
			self.pSpecList = []
		except Exception as e:
			logger.error("Error, when specifying an mor, please pass just the mor value, not an object: %s", str(e))
			logger.exception(e)
示例#15
0
    def collectTasks(self):
        '''Preforms a full collection of the self.specList passed.  Will not retain any information
		or update status.  Should be ran after specs are created and before create collector.
		this method will destroy the collector after collection.
		'''
        try:
            taskList = []
            self.buildTaskCollector()
            if self.taskCollector:
                while True:
                    tasks = self.taskCollector.readNextTasks(maxCount=100)
                    if tasks == None or len(tasks) == 0:
                        break
                    taskList.extend(tasks)
                    if len(tasks) < 100:
                        break
            else:
                logger.info("TaskCollector for target time range is empty.")
            return taskList
        except Exception as e:
            logger.exception(e)
        finally:
            self.destroyTaskCollector()
示例#16
0
def Jsonify(target):
	try:
		return json.dumps(target)
	except Exception as e:
		logger.error("Error trying to jsonify object")
		logger.exception(e)
示例#17
0
    def update_connection(cls,
                          url,
                          username=None,
                          password=None,
                          session_key=None,
                          cookie=None,
                          raise_exceptions=False):
        """
		Create/update the static class Connection with the provided credentials and url.
		
		RETURNS True if successful, False if not
		"""
        if (username is None or password is None) and cookie is None:
            raise Exception(
                "Must provide either a username-password and/or a cookie for domain : %s"
                % url)
        if url.startswith("https://") or url.startswith("http://"):
            domain = url.lstrip("htps:").lstrip("/")
        else:
            domain = url

        logger.debug(
            "[Connection] Update called with url=%s username=%s password=[REDACTED] session_key=%s cookie=%s",
            url, username, session_key, cookie)

        #Create a cookie, mainly so I don't have to change unit tests
        if cookie is None and session_key is not None:
            logger.warning(
                "[Connection] null cookie passed into update connection, creating an artificial cookie based on session_key, this will break for short named urls"
            )
            cookie = cls.create_session_cookie(domain, session_key)

        #First check if there even is a service instance
        if cls.svcInstance is None:
            logger.debug(
                "[Connection] Update called with no active svcInstance, building new service instance for domain=%s",
                domain)
            return cls._create_vim_service(domain, username, password, cookie,
                                           raise_exceptions)
        else:
            logger.info("[Connection] Connection has a vimservice, testing it")
            try:
                #Check if URL has changed from the currently loaded wsdl, if so reload it
                if Connection.svcInstance.serverConnection.getUrl() != domain:
                    logger.info(
                        "[Connection] swapping connection from old_domain=%s to new_domain=%s",
                        Connection.svcInstance.serverConnection.getUrl(),
                        domain)
                    return cls._create_vim_service(
                        domain=domain,
                        username=username,
                        password=password,
                        cookie=cookie,
                        use_cache=True,
                        raise_exceptions=raise_exceptions)
                elif Connection.svcInstance.serverConnection.getUrl(
                ) == domain:
                    logger.info(
                        "[Connection] Update was called with a connection that is already in place with the same url. Validating that the current session is still valid"
                    )
                    old_session_key = cls.session_key
                    session_valid = cls.is_service_instance_valid()
                    if session_valid and cookie is None and password is None:
                        logger.info(
                            "[Connection] current_session_key=%s was valid and happy happy happy",
                            cls.session_key)
                        return True
                    elif password is not None:
                        logger.info(
                            "[Connection] Connection update called with password on an already created object. Updating with password."
                        )
                        cls._destroy_vim_service()
                        return cls._create_vim_service(
                            domain=domain,
                            username=username,
                            password=password,
                            raise_exceptions=raise_exceptions)
                    elif cookie is not None:
                        logger.info(
                            "[Connection] was passed a session key/cookie, checking if it's what I'm already using"
                        )
                        if cookie.value != old_session_key:
                            #Got a new session key, remake the connection:
                            logger.info(
                                "[Connection] was passed a different session key, making a new connection with session_key=%s",
                                cookie.value)
                            cls._destroy_vim_service()
                            return cls._create_vim_service(
                                domain=domain,
                                cookie=cookie,
                                raise_exceptions=raise_exceptions)
                        else:
                            logger.info(
                                "[Connection] urls are the same, and the session key is the same. returning current session status=%s",
                                session_valid)
                            return session_valid
            except Exception as e:
                logger.exception(
                    "[Connection] Could not update connection for domain: %s %s",
                    url, e)
                if raise_exceptions:
                    raise e
                return False
示例#18
0
def CreateHierarchyCollector(managedObjectReference=None,
                             targetConfigObject=None,
                             updateType="update",
                             version=None,
                             oneTime=False,
                             addlTargetConfig=None):
    ''' Primary method to create collecting inventory.  managedObjectReference is to reference a propertyCollector
	that has already been created.  You can pass the MOR value to this method, and it will create a reference
	to that existing property collector.  You do not need to specify targetEntityType when using a MOR reference.
	For all new property collectors (like the first run), you should use targetEntityType.  This will reference
	the entity out of the targetConfig class for what properties to collect.  updateType is to be used in
	conjunction with an existing MOR, "update" will allow the user to submit a version for checking differences,
	while "recycle" will destroy the property collector and then create a new property collector.
	"addlTargetConfig" will include any additional properties that need to be collected, as specified in the conf file.
	It also create filterspec and filter as well before return property collector object
	
	Returns: version, property collector object, targetConfigObject (the returned configuration target for chaining to a formatter), mor (The returned MOR to
	the created / updated property collector, version
	'''
    try:
        logger.debug(
            "[Inventory] CollectInventory called with these options: managedObjectReference:{0}, targetConfigObject:{1}, updateType:{2}, version:{3}, oneTime:{4}, addlTargetConfig: {5}"
            .format(managedObjectReference, targetConfigObject, updateType,
                    version, oneTime, addlTargetConfig))

        configObject = getattr(targetConfig, targetConfigObject)
        if addlTargetConfig:
            configObject['HostSystem'].extend(addlTargetConfig)
            logger.debug(
                "[Inventory] CollectInventory: Addl properties added for 'HostSystem', configObject now contains:"
                + str(configObject))

        if managedObjectReference:
            logger.debug(
                "[Inventory] Received an MOR.  Rebuilding old collector.")
            hierarchyCollector = PropCollector(mor=managedObjectReference)
            if updateType == "recycle":
                try:
                    logger.debug("[Inventory] Old collector being recycled")
                    hierarchyCollector.targetCollector.destroyPropertyCollector(
                    )
                except WebFault as wf:
                    if str(
                            wf
                    ) == "Server raised fault: 'The object has already been deleted or has not been completely created'":
                        logger.debug(
                            "[Inventory] Destroy Collector called with a non-existing collector, assuming deleted and creating new."
                        )
                    else:
                        raise wf
                mor = None
                version = None
                hierarchyCollector = PropCollector()
                mor = str(hierarchyCollector.targetCollector.getMOR().value)
                logger.debug(
                    "[Inventory] Recycled collector MOR:{0}".format(mor))
                hierarchyCollector.buildFilterSpecList(configObject)
                hierarchyCollector.buildFilter(hierarchyCollector.fSpecList,
                                               partUpdates=True)
            if oneTime == True:
                logger.debug(
                    "[Inventory] OneTime collection is set to true with existing MOR."
                )
                hierarchyCollector.buildFilterSpecList(configObject)
            mor = str(hierarchyCollector.targetCollector.getMOR().value)
            return version, hierarchyCollector, targetConfigObject, mor
        else:
            logger.debug("[Inventory] Creating new collector.")
            hierarchyCollector = PropCollector()
            mor = str(hierarchyCollector.targetCollector.getMOR().value)
            logger.debug("[Inventory] New collector MOR:{0}".format(mor))
            hierarchyCollector.buildFilterSpecList(configObject)
            hierarchyCollector.buildFilter(hierarchyCollector.fSpecList,
                                           partUpdates=True)
            return version, hierarchyCollector, targetConfigObject, mor
    except Exception as e:
        logger.error("error in creating hierarchy collector")
        logger.exception(e)
        raise e