def setUp(self): addr = '127.0.0.1' port = self.__port = get_free_port() try: self.__client = HTTPClient(addr, port) self.__client.timeout = 2000 self.__client.data_timeout = 20000 self.__mocked_app = mock.MagicMock() self.__dispatcher = Interface(self.__mocked_app) self.__server = HTTPDaemon(addr, port, http_interface=self.__dispatcher, use_ssl=False, ca_cert=None, ssl_key=None, ssl_cert=None, daemon_thread_pool_size=4 ) #self.__server.register(self.__dispatcher) self.__process = Process(target=self._run_http_server) self.__process.start() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) while True: try: if sock.connect_ex((addr, port)) == 0: # print("http_server started", file=sys.stderr) break except: pass # print("Waiting http_server", file=sys.stderr) time.sleep(1) else: raise Exception() except: # nosetest doesn't call tearDown if setUp raise something, # but I want to be really clean, so: self.tearDown() raise
def create_connection(self): """Initialize HTTP connection with a satellite (con attribute) and set uri attribute :return: None """ self.con = HTTPClient(address=self.arb_satmap['address'], port=self.arb_satmap['port'], timeout=self.timeout, data_timeout=self.data_timeout, use_ssl=self.use_ssl, strong_ssl=self.hard_ssl_name_check ) self.uri = self.con.uri
def do_pynag_con_init(self, s_id, i_type='scheduler'): """Initialize or re-initialize connection with scheduler or arbiter if type == arbiter :param s_id: s_id :type s_id: int :param i_type: type of item :type i_type: str :return: None """ # Get the good links tab for looping.. links = self.get_links_from_type(i_type) if links is None: logger.debug('Type unknown for connection! %s', i_type) return # default timeout for daemons like pollers/reactionners/... timeout = 3 data_timeout = 120 if i_type == 'scheduler': # If sched is not active, I do not try to init # it is just useless is_active = links[s_id]['active'] if not is_active: return # schedulers also got real timeout to respect timeout = links[s_id]['timeout'] data_timeout = links[s_id]['data_timeout'] # If we try to connect too much, we slow down our tests if self.is_connection_try_too_close(links[s_id]): return # Ok, we can now update it links[s_id]['last_connection'] = time.time() # DBG: print "Init connection with", links[s_id]['uri'] running_id = links[s_id]['running_id'] # DBG: print "Running id before connection", running_id uri = links[s_id]['uri'] try: con = links[s_id]['con'] = HTTPClient(uri=uri, strong_ssl=links[s_id]['hard_ssl_name_check'], timeout=timeout, data_timeout=data_timeout) except HTTPEXCEPTIONS, exp: # But the multiprocessing module is not compatible with it! # so we must disable it immediately after logger.info("Connection problem to the %s %s: %s", i_type, links[s_id]['name'], str(exp)) links[s_id]['con'] = None return
def __init__(self): self.name = '' self.type = '' self.app = None self.stats = {} # There are two modes that are not exclusive # first the kernel mode self.api_key = '' self.secret = '' self.http_proxy = '' self.con = HTTPClient(uri='http://kernel.alignak.io') # then the statsd one self.statsd_sock = None self.statsd_addr = None
def create_connection(self): """Initialize HTTP connection with a satellite (con attribute) and set its uri attribute This is called on the satellite link initialization :return: None """ # Create the HTTP client for the connection try: self.con = HTTPClient(address=self.satellite_map['address'], port=self.satellite_map['port'], short_timeout=self.short_timeout, long_timeout=self.long_timeout, use_ssl=self.satellite_map['use_ssl'], strong_ssl=self.satellite_map['hard_ssl_name_check']) self.uri = self.con.uri except HTTPClientException as exp: # logger.error("Error with '%s' when creating client: %s", self.name, str(exp)) # Set the satellite as dead self.set_dead() raise LinkError("Error with '%s' when creating client: %s" % (self.name, str(exp)))
def create_connection(self): """Initialize HTTP connection with a satellite (con attribute) and set uri attribute :return: None """ self.con = None # Create the HTTP client for the connection try: self.con = HTTPClient(address=self.arb_satmap['address'], port=self.arb_satmap['port'], timeout=self.timeout, data_timeout=self.data_timeout, use_ssl=self.use_ssl, strong_ssl=self.hard_ssl_name_check) self.uri = self.con.uri # Set the satellite as alive self.set_alive() except HTTPClientException as exp: logger.error("Error with '%s' when creating client: %s", self.get_name(), str(exp)) # Set the satellite as dead self.set_dead()
def do_pynag_con_init(self, s_id): """Initialize a connection with scheduler having '_id' Return the new connection to the scheduler if it succeeded, else: any error OR sched is inactive: return None. NB: if sched is inactive then None is directly returned. :param s_id: scheduler s_id to connect to :type s_id: int :return: scheduler connection object or None :rtype: alignak.http.client.HTTPClient """ sched = self.schedulers[s_id] if not sched['active']: return sname = sched['name'] uri = sched['uri'] running_id = sched['running_id'] timeout = sched['timeout'] data_timeout = sched['data_timeout'] logger.info("[%s] Init connection with %s at %s (%ss,%ss)", self.name, sname, uri, timeout, data_timeout) try: sch_con = sched['con'] = HTTPClient( uri=uri, strong_ssl=sched['hard_ssl_name_check'], timeout=timeout, data_timeout=data_timeout) except HTTPEXCEPTIONS, exp: logger.warning( "[%s] Scheduler %s is not initialized or has network problem: %s", self.name, sname, str(exp)) sched['con'] = None return