2222from getpass import getpass
2323
2424import tenacity
25- import yaml
2625import bcrypt
2726import json
2827from elasticsearch import Elasticsearch
2928
30- from kibble .settings import KIBBLE_YAML
29+ from kibble .configuration import conf
3130
32- KIBBLE_VERSION = "0.1.0" # ABI/API compat demarcation.
33- KIBBLE_DB_VERSION = 2 # Second database revision
31+
32+ KIBBLE_VERSION = conf .get ("api" , "version" )
33+ KIBBLE_DB_VERSION = conf .get ("api" , "database" ) # database revision
3434
3535if sys .version_info <= (3 , 3 ):
3636 print ("This script requires Python 3.4 or higher" )
@@ -42,60 +42,53 @@ def get_parser():
4242 arg_parser = argparse .ArgumentParser ()
4343 arg_parser .add_argument (
4444 "-e" ,
45- "--hostname" ,
46- help = "Pre-defined hostname for ElasticSearch (docker setups). Default: localhost" ,
47- default = "localhost" ,
48- )
49- arg_parser .add_argument (
50- "-p" ,
51- "--port" ,
52- help = "Pre-defined port for ES (docker setups). Default: 9200" ,
53- default = 9200 ,
45+ "--conn-uri" ,
46+ help = "Pre-defined connection uri for ElasticSearch." ,
47+ default = conf .get ("elasticsearch" , "conn_uri" ),
5448 )
5549 arg_parser .add_argument (
5650 "-d" ,
5751 "--dbname" ,
58- help = "Pre-defined Database prefix (docker setups) . Default: kibble" ,
59- default = "kibble" ,
52+ help = "Pre-defined Database prefix. Default: kibble" ,
53+ default = conf . get ( "elasticsearch" , "dbname" ) ,
6054 )
6155 arg_parser .add_argument (
6256 "-s" ,
6357 "--shards" ,
64- help = "Predefined number of ES shards (docker setups) , Default: 5" ,
65- default = 5 ,
58+ help = "Predefined number of ES shards, Default: 5" ,
59+ default = conf . get ( "elasticsearch" , "shards" ) ,
6660 )
6761 arg_parser .add_argument (
6862 "-r" ,
6963 "--replicas" ,
70- help = "Predefined number of replicas for ES (docker setups) . Default: 1" ,
71- default = 1 ,
64+ help = "Predefined number of replicas for ES. Default: 1" ,
65+ default = conf . get ( "elasticsearch" , "replicas" ) ,
7266 )
7367 arg_parser .add_argument (
7468 "-m" ,
7569 "--mailhost" ,
76- help = "Pre-defined mail server host (docker setups) . Default: localhost:25" ,
77- default = "localhost:25" ,
70+ help = "Pre-defined mail server host. Default: localhost:25" ,
71+ default = conf . get ( "mail" , "mailhost" ) ,
7872 )
7973 arg_parser .add_argument (
8074 "-a" ,
8175 "--autoadmin" ,
8276 action = "store_true" ,
83- help = "Generate generic admin account (docker setups) . Default: False" ,
77+ help = "Generate generic admin account. Default: False" ,
8478 default = False ,
8579 )
8680 arg_parser .add_argument (
8781 "-k" ,
8882 "--skiponexist" ,
8983 action = "store_true" ,
90- help = "Skip DB creation if DBs exist (docker setups) . Defaul: True" ,
84+ help = "Skip DB creation if DBs exist. Defaul: True" ,
9185 default = True ,
9286 )
9387 return arg_parser
9488
9589
9690def create_es_index (
97- hostname : str ,
98- port : int ,
91+ conn_uri : str ,
9992 dbname : str ,
10093 shards : int ,
10194 replicas : int ,
@@ -114,11 +107,7 @@ def create_es_index(
114107 with open (mappings_json , "r" ) as f :
115108 mappings = json .load (f )
116109
117- es = Elasticsearch (
118- [{"host" : hostname , "port" : port , "use_ssl" : False , "url_prefix" : "" }],
119- max_retries = 5 ,
120- retry_on_timeout = True ,
121- )
110+ es = Elasticsearch ([conn_uri ], max_retries = 5 , retry_on_timeout = True )
122111
123112 es_version = es .info ()["version" ]["number" ]
124113 es6 = int (es_version .split ("." )[0 ]) >= 6
@@ -223,51 +212,6 @@ def create_es_index(
223212 print ("Account created!" )
224213
225214
226- def get_kibble_yaml () -> str :
227- """Resolve path to kibble config yaml"""
228- kibble_yaml = KIBBLE_YAML
229- if os .path .exists (kibble_yaml ):
230- print (f"{ kibble_yaml } already exists! Writing to { kibble_yaml } .tmp instead" )
231- kibble_yaml = kibble_yaml + ".tmp"
232- return kibble_yaml
233-
234-
235- def save_config (mlserver : str , hostname : str , port : int , dbname : str ):
236- """Save kibble config to yaml file"""
237- if ":" in mlserver :
238- try :
239- mailhost , mailport = mlserver .split (":" )
240- except ValueError :
241- raise ValueError (
242- "mailhost argument must be in form of `host:port` or `host`"
243- )
244- else :
245- mailhost = mlserver
246- mailport = 25
247-
248- config = {
249- "api" : {"version" : KIBBLE_VERSION , "database" : KIBBLE_DB_VERSION },
250- "elasticsearch" : {
251- "host" : hostname ,
252- "port" : port ,
253- "ssl" : False ,
254- "dbname" : dbname ,
255- },
256- "mail" : {
257- "mailhost" : mailhost ,
258- "mailport" : int (mailport ),
259- "sender" : "Kibble <noreply@kibble.kibble>" ,
260- },
261- "accounts" : {"allowSignup" : True , "verify" : True },
262- }
263-
264- kibble_yaml = get_kibble_yaml ()
265- print (f"Writing Kibble config to { kibble_yaml } " )
266- with open (kibble_yaml , "w" ) as f :
267- f .write (yaml .dump (config , default_flow_style = False ))
268- f .close ()
269-
270-
271215def get_user_input (msg : str , secure : bool = False ):
272216 value = None
273217 while not value :
@@ -279,8 +223,7 @@ def print_configuration(args):
279223 print (
280224 "Configuring Apache Kibble elasticsearch instance with the following arguments:"
281225 )
282- print (f"- hostname: { args .hostname } " )
283- print (f"- port: { int (args .port )} " )
226+ print (f"- conn_uri: { args .conn_uri } " )
284227 print (f"- dbname: { args .dbname } " )
285228 print (f"- shards: { int (args .shards )} " )
286229 print (f"- replicas: { int (args .replicas )} " )
@@ -311,7 +254,7 @@ def main():
311254
312255 # Create Elasticsearch index
313256 # Retry in case ES is not yet up
314- print (f"Elasticsearch: { args .hostname } : { args . port } " )
257+ print (f"Elasticsearch: { args .conn_uri } " )
315258 for attempt in tenacity .Retrying (
316259 retry = tenacity .retry_if_exception_type (exception_types = Exception ),
317260 wait = tenacity .wait_fixed (10 ),
@@ -321,8 +264,7 @@ def main():
321264 with attempt :
322265 print ("Trying to create ES index..." )
323266 create_es_index (
324- hostname = args .hostname ,
325- port = int (args .port ),
267+ conn_uri = args .conn_uri ,
326268 dbname = args .dbname ,
327269 shards = int (args .shards ),
328270 replicas = int (args .replicas ),
@@ -331,15 +273,6 @@ def main():
331273 skiponexist = args .skiponexist ,
332274 )
333275 print ()
334-
335- # Create Kibble configuration file
336- save_config (
337- mlserver = args .mailhost ,
338- hostname = args .hostname ,
339- port = int (args .port ),
340- dbname = args .dbname ,
341- )
342- print ()
343276 print ("All done, Kibble should...work now :)" )
344277
345278
0 commit comments