def monitor_host(): global DOCKER_MONITOR session = None host = None # container monitoring can get a smaller slice of the CPU time os.nice(10) signal.signal(signal.SIGTERM, interrupt_handler) signal.signal(signal.SIGINT, interrupt_handler) while True: try: session = api_helper.get_local_api_session() client = api_helper.LocalXenAPIClient() # need to refresh the host, in case we just joined a pool host = api_helper.Host(client, api_helper.get_this_host_ref(session)) if not DOCKER_MONITOR: DOCKER_MONITOR = DockerMonitor(host) else: DOCKER_MONITOR.set_host(host) log.info("Monitoring host %s" % (host.get_id())) try: # Avoid race conditions - get a current event token event_from = session.xenapi.event_from(["vm"], '', 0.0) token_from = event_from['token'] # Now load the VMs that are enabled for monitoring DOCKER_MONITOR.refresh() while True: event_from = session.xenapi.event_from( ["vm"], token_from, EVENT_FROM_TIMEOUT_S) token_from = event_from['token'] events = event_from['events'] for event in events: if (event['operation'] == 'mod' and 'snapshot' in event): # At this point the monitor may need to # refresh it's monitoring state of a particular # vm. DOCKER_MONITOR.process_vmrecord(event['ref'], event['snapshot']) elif event['operation'] == 'del': DOCKER_MONITOR.process_vm_del(event['ref']) finally: try: session.xenapi.session.logout() except XenAPI.Failure: log.exception("Failed when trying to logout") except (socket.error, XenAPI.Failure, xmlrpclib.ProtocolError) as e: if session is not None: log.exception(e) log.error("Could not connect to XAPI - Is XAPI running? " + "Will retry in %d" % (XAPIRETRYSLEEPINS)) else: log.exception("Recovering from XAPI failure - Is XAPI " + "restarting? Will retry in %d." % (XAPIRETRYSLEEPINS)) time.sleep(XAPIRETRYSLEEPINS) api_helper.reinit_global_xapi_session()
def decorated(*args, **kwargs): try: return func(*args, **kwargs) except Exception, exception: log.error("Caught exception '%s'. Retrying with new session." % (str(exception))) reinit_global_xapi_session() # Return the func undecorated return func(*args, **kwargs)
def missing_host_key(self, client, hostname, key): hostkey = key.get_base64() remembered_hostkey = api_helper.get_ssh_hostkey(self._session, self._vm_uuid) if remembered_hostkey: # We have a key on record if hostkey == remembered_hostkey: # all good - continue return else: # bad - throw error because of mismatch message = ("Key for VM %s does not match the known public key." % (self._vm_uuid)) log.error(message) raise VmHostKeyException(message) else: # we don't have key on record. Let's remember this one for next # time log.debug("No public key on record found for %s. Will remember." % hostkey) api_helper.set_ssh_hostkey(self._session, self._vm_uuid, hostkey) # all good - continue return
def missing_host_key(self, client, hostname, key): hostkey = key.get_base64() remembered_hostkey = api_helper.get_ssh_hostkey( self._session, self._vm_uuid) if remembered_hostkey: # We have a key on record if hostkey == remembered_hostkey: # all good - continue return else: # bad - throw error because of mismatch message = ( "Key for VM %s does not match the known public key." % (self._vm_uuid)) log.error(message) raise VmHostKeyException(message) else: # we don't have key on record. Let's remember this one for next # time log.debug("No public key on record found for %s. Will remember." % hostkey) api_helper.set_ssh_hostkey(self._session, self._vm_uuid, hostkey) # all good - continue return