22
22
from getpass import getpass
23
23
24
24
import tenacity
25
- import yaml
26
25
import bcrypt
27
26
import json
28
27
from elasticsearch import Elasticsearch
29
28
30
- from kibble .settings import KIBBLE_YAML
29
+ from kibble .configuration import conf
31
30
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
34
34
35
35
if sys .version_info <= (3 , 3 ):
36
36
print ("This script requires Python 3.4 or higher" )
@@ -42,60 +42,53 @@ def get_parser():
42
42
arg_parser = argparse .ArgumentParser ()
43
43
arg_parser .add_argument (
44
44
"-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" ),
54
48
)
55
49
arg_parser .add_argument (
56
50
"-d" ,
57
51
"--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" ) ,
60
54
)
61
55
arg_parser .add_argument (
62
56
"-s" ,
63
57
"--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" ) ,
66
60
)
67
61
arg_parser .add_argument (
68
62
"-r" ,
69
63
"--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" ) ,
72
66
)
73
67
arg_parser .add_argument (
74
68
"-m" ,
75
69
"--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" ) ,
78
72
)
79
73
arg_parser .add_argument (
80
74
"-a" ,
81
75
"--autoadmin" ,
82
76
action = "store_true" ,
83
- help = "Generate generic admin account (docker setups) . Default: False" ,
77
+ help = "Generate generic admin account. Default: False" ,
84
78
default = False ,
85
79
)
86
80
arg_parser .add_argument (
87
81
"-k" ,
88
82
"--skiponexist" ,
89
83
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" ,
91
85
default = True ,
92
86
)
93
87
return arg_parser
94
88
95
89
96
90
def create_es_index (
97
- hostname : str ,
98
- port : int ,
91
+ conn_uri : str ,
99
92
dbname : str ,
100
93
shards : int ,
101
94
replicas : int ,
@@ -114,11 +107,7 @@ def create_es_index(
114
107
with open (mappings_json , "r" ) as f :
115
108
mappings = json .load (f )
116
109
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 )
122
111
123
112
es_version = es .info ()["version" ]["number" ]
124
113
es6 = int (es_version .split ("." )[0 ]) >= 6
@@ -223,51 +212,6 @@ def create_es_index(
223
212
print ("Account created!" )
224
213
225
214
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 <[email protected] >" ,
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
-
271
215
def get_user_input (msg : str , secure : bool = False ):
272
216
value = None
273
217
while not value :
@@ -279,8 +223,7 @@ def print_configuration(args):
279
223
print (
280
224
"Configuring Apache Kibble elasticsearch instance with the following arguments:"
281
225
)
282
- print (f"- hostname: { args .hostname } " )
283
- print (f"- port: { int (args .port )} " )
226
+ print (f"- conn_uri: { args .conn_uri } " )
284
227
print (f"- dbname: { args .dbname } " )
285
228
print (f"- shards: { int (args .shards )} " )
286
229
print (f"- replicas: { int (args .replicas )} " )
@@ -311,7 +254,7 @@ def main():
311
254
312
255
# Create Elasticsearch index
313
256
# Retry in case ES is not yet up
314
- print (f"Elasticsearch: { args .hostname } : { args . port } " )
257
+ print (f"Elasticsearch: { args .conn_uri } " )
315
258
for attempt in tenacity .Retrying (
316
259
retry = tenacity .retry_if_exception_type (exception_types = Exception ),
317
260
wait = tenacity .wait_fixed (10 ),
@@ -321,8 +264,7 @@ def main():
321
264
with attempt :
322
265
print ("Trying to create ES index..." )
323
266
create_es_index (
324
- hostname = args .hostname ,
325
- port = int (args .port ),
267
+ conn_uri = args .conn_uri ,
326
268
dbname = args .dbname ,
327
269
shards = int (args .shards ),
328
270
replicas = int (args .replicas ),
@@ -331,15 +273,6 @@ def main():
331
273
skiponexist = args .skiponexist ,
332
274
)
333
275
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 ()
343
276
print ("All done, Kibble should...work now :)" )
344
277
345
278
0 commit comments