11import os .path
2+ import shutil
23import sys
34from time import sleep
45try :
89
910
1011# These are set in .circleci/config
11- CIRCLE_BUILD = os .environ .get ('BASH_ENV' ) == '/home/circleci/.bashrc'
12- CICLE_ES_VERSION = os .environ .get ('ES_MAJOR_VERSION' )
12+ _CIRCLE_BUILD = os .environ .get ('BASH_ENV' ) == '/home/circleci/.bashrc'
13+ _CICLE_ES_VERSION = os .environ .get ('ES_MAJOR_VERSION' )
14+ # Encoded configuraion repo
15+ _ENCD_CONFIG_DIR = os .environ .get ('ENCD_CONFIG_DIR' )
16+ # This is required for local osx testing, circle version is sepearte
17+ _OSX_ES_VERSION = 5
1318
1419
15- def _get_args_and_env ():
16- pass
17-
18-
19- def server_process (
20- datadir ,
21- configdir = None ,
22- echo = False ,
23- host = '127.0.0.1' ,
24- port = 9201 ,
25- prefix = '' ,
26- version = 5 ,
27- ):
20+ def _get_args_and_env (esdata , esdata_override , kwargs ):
2821 env = os .environ .copy ()
29- if configdir :
30- # How elasticsearch 6+ sets config path
31- env ["ES_PATH_CONF" ] = configdir
32- args = [
33- 'elasticsearch' ,
34- ]
35- if version <= 5 :
22+ args = [
23+ os .path .join (kwargs .get ('prefix' , '' ), 'elasticsearch' ),
24+ ]
25+ if esdata_override :
26+ # ES configuration dir is specified, not default location.
27+ # 'esdata' is ignored and kwargs values are overidden by elasticsearch.yml
28+ esconfig = f"{ esdata_override } /config"
29+ if kwargs ['version' ] <= 5 :
3630 # How elasticsearch 5 sets config path
37- args .append (f"-Epath.conf={ configdir } " )
38- else :
39- args = [
40- os .path .join (prefix , 'elasticsearch' ),
41- '-Enetwork.host=%s' % host ,
42- '-Ehttp.port=%d' % port ,
43- '-Epath.data=%s' % os .path .join (datadir , 'data' ),
44- '-Epath.logs=%s' % os .path .join (datadir , 'logs' ),
45- '-Epath.conf=./conf' ,
46- ]
31+ args .append (f"-Epath.conf={ esconfig } " )
32+ else :
33+ # How elasticsearch 6+ sets config path
34+ env ["ES_PATH_CONF" ] = esconfig
35+ jvm_options = _get_jvm_options (f"{ esconfig } /jvm.options" )
36+ if jvm_options :
37+ env ['ES_JAVA_OPTS' ] = jvm_options
38+ return args , env
39+ # Default 'esdata' location
40+ args .extend ([
41+ '-Enetwork.host=%s' % kwargs .get ('host' , '127.0.0.1' ),
42+ '-Ehttp.port=%d' % kwargs .get ('port' , 9201 ),
43+ '-Epath.data=%s' % os .path .join (esdata , 'data' ),
44+ '-Epath.logs=%s' % os .path .join (esdata , 'logs' ),
45+ ])
46+ if _CIRCLE_BUILD and int (_CICLE_ES_VERSION ) == 5 :
47+ args .append ('-Epath.conf=./conf' )
48+ return args , env
49+
50+
51+ def _get_config_override (esdata , kwargs ):
52+ if _ENCD_CONFIG_DIR :
53+ # If found, elasticsearch.yml config overrides vars like logs/data/host/port/etc...
54+ es_data_dir = f"{ _ENCD_CONFIG_DIR } /elasticsearch/es{ kwargs ['version' ]} "
55+ if kwargs .get ('local_test' , False ):
56+ es_data_dir += 'test'
57+ if os .path .exists (f"{ es_data_dir } /config/elasticsearch.yml" ):
58+ return es_data_dir
59+ return None
60+
61+
62+ def _get_jvm_options (jvm_options_path ):
63+ with open (jvm_options_path , 'r' ) as fileh :
64+ return ' ' .join ([
65+ line .strip ()
66+ for line in fileh .readlines ()
67+ ])
68+
69+
70+ def _clear (esdata , esdata_override ):
71+ rm_dirs = [esdata ]
72+ if esdata_override :
73+ esconfig = f"{ esdata_override } /config"
74+ rm_dirs .extend ([f"{ esdata_override } /data" , f"{ esdata_override } /logs" , f"{ esconfig } /scripts" ])
75+ es_keystore_path = f"{ esconfig } /elasticsearch.keystore"
76+ if os .path .exists (es_keystore_path ):
77+ os .remove (es_keystore_path )
78+ for dir_path in rm_dirs :
79+ if os .path .exists (dir_path ):
80+ shutil .rmtree (dir_path )
81+
82+
83+ def server_process (datadir , ** kwargs ):
84+ kwargs ['version' ] = kwargs .get ('version' , _OSX_ES_VERSION )
85+ esdata = os .path .join (datadir , 'data' )
86+ esdata_override = _get_config_override (esdata , kwargs )
87+ if kwargs .get ('clear' , False ):
88+ _clear (esdata , esdata_override )
89+ args , env = _get_args_and_env (esdata , esdata_override , kwargs )
4790 process = subprocess .Popen (
4891 args ,
4992 close_fds = True ,
@@ -55,7 +98,7 @@ def server_process(
5598 SUCCESS_LINE = b'started\n '
5699 lines = []
57100 for line in iter (process .stdout .readline , b'' ):
58- if echo :
101+ if kwargs . get ( ' echo' , False ) :
59102 sys .stdout .write (line .decode ('utf-8' ))
60103 lines .append (line )
61104 if line .endswith (SUCCESS_LINE ):
@@ -66,7 +109,7 @@ def server_process(
66109 msg = ('Process return code: %d\n ' % code ) + b'' .join (lines ).decode ('utf-8' )
67110 raise Exception (msg )
68111
69- if not echo :
112+ if not kwargs . get ( ' echo' , False ) :
70113 process .stdout .close ()
71114 print ('returning process' )
72115 return process
0 commit comments