def _start_server(self, conf_template, udp_enabled): # create a temporary dir to store all needed files # for the influxdb server instance : self.temp_dir_base = tempfile.mkdtemp() # "temp_dir_base" will be used for conf file and logs, # while "temp_dir_influxdb" is for the databases files/dirs : tempdir = self.temp_dir_influxdb = tempfile.mkdtemp( dir=self.temp_dir_base) # find a couple free ports : free_ports = get_free_ports(4) ports = {} for service in 'http', 'admin', 'meta', 'udp': ports[service + '_port'] = free_ports.pop() if not udp_enabled: ports['udp_port'] = -1 conf_data = dict( meta_dir=os.path.join(tempdir, 'meta'), data_dir=os.path.join(tempdir, 'data'), wal_dir=os.path.join(tempdir, 'wal'), cluster_dir=os.path.join(tempdir, 'state'), handoff_dir=os.path.join(tempdir, 'handoff'), logs_file=os.path.join(self.temp_dir_base, 'logs.txt'), udp_enabled='true' if udp_enabled else 'false', ) conf_data.update(ports) self.__dict__.update(conf_data) conf_file = os.path.join(self.temp_dir_base, 'influxdb.conf') with open(conf_file, "w") as fh: with open(conf_template) as fh_template: fh.write(fh_template.read().format(**conf_data)) # now start the server instance: self.proc = subprocess.Popen([self.influxd_path, '-config', conf_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE) print("%s > Started influxdb bin in %r with ports %s and %s.." % (datetime.datetime.now(), self.temp_dir_base, self.admin_port, self.http_port)) # wait for it to listen on the broker and admin ports: # usually a fresh instance is ready in less than 1 sec .. timeout = time.time() + 10 # so 10 secs should be enough, # otherwise either your system load is high, # or you run a 286 @ 1Mhz ? try: while time.time() < timeout: if (is_port_open(self.http_port) and is_port_open(self.admin_port)): # it's hard to check if a UDP port is open.. if udp_enabled: # so let's just sleep 0.5 sec in this case # to be sure that the server has open the port time.sleep(0.5) break time.sleep(0.5) if self.proc.poll() is not None: raise RuntimeError('influxdb prematurely exited') else: self.proc.terminate() self.proc.wait() raise RuntimeError('Timeout waiting for influxdb to listen' ' on its ports (%s)' % ports) except RuntimeError as err: data = self.get_logs_and_output() data['reason'] = str(err) data['now'] = datetime.datetime.now() raise RuntimeError( "%(now)s > %(reason)s. RC=%(rc)s\n" "stdout=%(out)s\nstderr=%(err)s\nlogs=%(logs)r" % data)
def _start_server(self, conf_template, udp_enabled): # create a temporary dir to store all needed files # for the influxdb server instance : self.temp_dir_base = tempfile.mkdtemp() # "temp_dir_base" will be used for conf file and logs, # while "temp_dir_influxdb" is for the databases files/dirs : tempdir = self.temp_dir_influxdb = tempfile.mkdtemp( dir=self.temp_dir_base) # find a couple free ports : free_ports = get_free_ports(4) ports = {} for service in 'http', 'admin', 'meta', 'udp': ports[service + '_port'] = free_ports.pop() if not udp_enabled: ports['udp_port'] = -1 conf_data = dict( meta_dir=os.path.join(tempdir, 'meta'), data_dir=os.path.join(tempdir, 'data'), wal_dir=os.path.join(tempdir, 'wal'), cluster_dir=os.path.join(tempdir, 'state'), handoff_dir=os.path.join(tempdir, 'handoff'), logs_file=os.path.join(self.temp_dir_base, 'logs.txt'), udp_enabled='true' if udp_enabled else 'false', ) conf_data.update(ports) self.__dict__.update(conf_data) conf_file = os.path.join(self.temp_dir_base, 'influxdb.conf') with open(conf_file, "w") as fh: with open(conf_template) as fh_template: fh.write(fh_template.read().format(**conf_data)) # now start the server instance: self.proc = subprocess.Popen( [self.influxd_path, '-config', conf_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) print( "%s > Started influxdb bin in %r with ports %s and %s.." % ( datetime.datetime.now(), self.temp_dir_base, self.admin_port, self.http_port ) ) # wait for it to listen on the broker and admin ports: # usually a fresh instance is ready in less than 1 sec .. timeout = time.time() + 10 # so 10 secs should be enough, # otherwise either your system load is high, # or you run a 286 @ 1Mhz ? try: while time.time() < timeout: if (is_port_open(self.http_port) and is_port_open(self.admin_port)): # it's hard to check if a UDP port is open.. if udp_enabled: # so let's just sleep 0.5 sec in this case # to be sure that the server has open the port time.sleep(0.5) break time.sleep(0.5) if self.proc.poll() is not None: raise RuntimeError('influxdb prematurely exited') else: self.proc.terminate() self.proc.wait() raise RuntimeError('Timeout waiting for influxdb to listen' ' on its ports (%s)' % ports) except RuntimeError as err: data = self.get_logs_and_output() data['reason'] = str(err) data['now'] = datetime.datetime.now() raise RuntimeError("%(now)s > %(reason)s. RC=%(rc)s\n" "stdout=%(out)s\nstderr=%(err)s\nlogs=%(logs)r" % data)