def ensure_elasticsearch(self): """Start elasticsearch if it's not already running""" if self.elasticsearch_pid: # Use the existing instance return # Create a random directory and get or assign a port elasticsearch_root_dir = tempfile.mkdtemp(prefix='elastic') es_options = { 'node.local': 'true', 'index.number_of_shards': 1, 'index.number_of_replicas': 0, 'network.host': '127.0.0.1', 'http.port': self.elasticsearch_port, 'discovery.zen.ping.multicast.enabled': 'false', 'path.data': elasticsearch_root_dir, 'path.conf': elasticsearch_root_dir, 'path.logs': elasticsearch_root_dir, 'action.auto_create_index': 'false', 'script.engine.groovy.inline.update': 'on' } # Set JVM options exec_env = {'ES_HEAP_SIZE': '50m'} cmd = 'elasticsearch ' cmd += ' '.join('--%s=%s' % kv for kv in es_options.items()) # Fork and retain the PID self.elasticsearch_pid = test_utils.fork_exec(cmd, logfile=os.devnull, exec_env=exec_env) # Register a shutdown function def _stop_elasticsearch(): if not self.elasticsearch_pid: raise Exception('why is this being called? elasticsearch') os.kill(self.elasticsearch_pid, signal.SIGTERM) rc = test_utils.wait_for_fork(self.elasticsearch_pid, raise_error=False) self.elasticsearch_pid = None # Delete the temporary directory shutil.rmtree(elasticsearch_root_dir) return (rc, '', '') atexit.register(_stop_elasticsearch) # Wait for elasticsearch to spin up; it takes a while to initialize http = httplib2.Http() es_url = 'http://localhost:%s' % self.elasticsearch_port for _ in range(6): try: response, content = http.request(es_url) if response.status == 200: break except socket.error: # Expect 'connection refused' a couple of times pass time.sleep(5) else: raise Exception("Elasticsearch failed to start")
def ensure_elasticsearch(self): """Start elasticsearch if it's not already running""" if self.elasticsearch_pid: # Use the existing instance return # Create a random directory and get or assign a port elasticsearch_root_dir = tempfile.mkdtemp(prefix='elastic') es_options = { 'node.local': 'true', 'index.number_of_shards': 1, 'index.number_of_replicas': 0, 'network.host': '127.0.0.1', 'http.port': self.elasticsearch_port, 'discovery.zen.ping.multicast.enabled': 'false', 'path.data': elasticsearch_root_dir, 'path.conf': elasticsearch_root_dir, 'path.logs': elasticsearch_root_dir, 'action.auto_create_index': 'false', 'script.engine.groovy.inline.update': 'on' } # Set JVM options exec_env = { 'ES_HEAP_SIZE': '64m' } cmd = 'elasticsearch ' cmd += ' '.join('--%s=%s' % kv for kv in es_options.items()) # Fork and retain the PID self.elasticsearch_pid = test_utils.fork_exec(cmd, logfile=os.devnull, exec_env=exec_env) # Register a shutdown function def _stop_elasticsearch(): if not self.elasticsearch_pid: raise Exception('why is this being called? elasticsearch') os.kill(self.elasticsearch_pid, signal.SIGTERM) rc = test_utils.wait_for_fork(self.elasticsearch_pid, raise_error=False) self.elasticsearch_pid = None # Delete the temporary directory shutil.rmtree(elasticsearch_root_dir) return (rc, '', '') atexit.register(_stop_elasticsearch) # Wait for elasticsearch to spin up; it takes a while to initialize es_url = 'http://localhost:%s' % self.elasticsearch_port time.sleep(10) for _ in range(10): try: response = requests.get(es_url) if response.status_code == 200: break except requests.exceptions.RequestException as e: LOG.debug("ES startup: Request Exception occured: %s" % e) time.sleep(10) else: raise Exception("Elasticsearch failed to start")
def start(self, expect_exit=True, expected_exitcode=0, **kwargs): """ Starts the server. Any kwargs passed to this method will override the configuration value in the conf file used in starting the servers. """ # Ensure the configuration file is written self.write_conf(**kwargs) elasticsearch_wrapper.ensure_elasticsearch() cmd = ("%(server_module)s --config-file %(conf_file_name)s" % { "server_module": self.server_module, "conf_file_name": self.conf_file_name }) cmd = "%s -m %s" % (sys.executable, cmd) # close the sock and release the unused port closer to start time if self.exec_env: exec_env = self.exec_env.copy() else: exec_env = {} pass_fds = set() if self.sock: if not self.fork_socket: self.sock.close() self.sock = None else: fd = os.dup(self.sock.fileno()) exec_env[utils.SEARCHLIGHT_TEST_SOCKET_FD_STR] = str(fd) pass_fds.add(fd) self.sock.close() self.process_pid = test_utils.fork_exec(cmd, logfile=os.devnull, exec_env=exec_env, pass_fds=pass_fds) self.stop_kill = not expect_exit if self.pid_file: pf = open(self.pid_file, 'w') pf.write('%d\n' % self.process_pid) pf.close() if not expect_exit: rc = 0 try: os.kill(self.process_pid, 0) except OSError: raise RuntimeError("The process did not start") else: rc = test_utils.wait_for_fork(self.process_pid, expected_exitcode=expected_exitcode) # avoid an FD leak if self.sock: os.close(fd) self.sock = None return (rc, '', '')
def start(self, expect_exit=True, expected_exitcode=0, **kwargs): """ Starts the server. Any kwargs passed to this method will override the configuration value in the conf file used in starting the servers. """ # Ensure the configuration file is written self.write_conf(**kwargs) elasticsearch_wrapper.ensure_elasticsearch() cmd = ("%(server_module)s --config-file %(conf_file_name)s" % {"server_module": self.server_module, "conf_file_name": self.conf_file_name}) cmd = "%s -m %s" % (sys.executable, cmd) # close the sock and release the unused port closer to start time if self.exec_env: exec_env = self.exec_env.copy() else: exec_env = {} pass_fds = set() if self.sock: if not self.fork_socket: self.sock.close() self.sock = None else: fd = os.dup(self.sock.fileno()) exec_env[utils.SEARCHLIGHT_TEST_SOCKET_FD_STR] = str(fd) pass_fds.add(fd) self.sock.close() self.process_pid = test_utils.fork_exec(cmd, logfile=os.devnull, exec_env=exec_env, pass_fds=pass_fds) self.stop_kill = not expect_exit if self.pid_file: pf = open(self.pid_file, 'w') pf.write('%d\n' % self.process_pid) pf.close() if not expect_exit: rc = 0 try: os.kill(self.process_pid, 0) except OSError: raise RuntimeError("The process did not start") else: rc = test_utils.wait_for_fork( self.process_pid, expected_exitcode=expected_exitcode) # avoid an FD leak if self.sock: os.close(fd) self.sock = None return (rc, '', '')