This repository has been archived by the owner on Nov 19, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsiridb-manage.py
executable file
·1160 lines (923 loc) · 34.1 KB
/
siridb-manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3 -OO
import sys
import os
import argparse
import signal
import functools
import asyncio
import shutil
import logging
import getpass
import uuid
import pickle
import qpack
import time
from settings import Settings
from constants import DEFAULT_TIMEZONE
from constants import DEFAULT_DROP_THRESHOLD
from constants import DEFAULT_BUFFER_SIZE
from constants import DURATIONS
from constants import DBNAME_VALID_NAME
from constants import MAX_BUFFER_SIZE
from constants import DEFAULT_CONFIG
from constants import DEFAULT_CLIENT_PORT
from constants import DBPROPS
from constants import MAX_NUMBER_DB
from version import __version__
from version import __version_info__
from version import __email__
from version import __maintainer__
from siridb.connector import connect
from siridb.connector import SiriDBProtocol
from siridb.connector import async_server_info
from siridb.connector.lib.protomap import CPROTO_REQ_LOADDB
from siridb.connector.lib.exceptions import ServerError
from siridb.connector.lib.exceptions import PoolError
from siridb.connector.lib.exceptions import UserAuthError
from siridb.connector.lib.exceptions import AuthenticationError
settings = Settings()
PROMPT = '> '
FULL_AUTH = 'full'
siri = None
local_siridb_info = None
remote_siridb_info = None
def check_valid_dbname(dbname):
if not isinstance(dbname, str):
raise ValueError(
'Need a string value for dbname, got {}'.format(
type(dbname).__name__))
if not DBNAME_VALID_NAME.match(dbname):
raise ValueError(
'Database name should be 2 to 20 characters, stating with an '
'alphabetic and ending with an alphabetic or number character. '
'In the middle hyphens are allowed.')
def mk_path(path):
if not os.path.exists(path):
os.makedirs(path)
elif os.listdir(path):
raise OSError('path is not empty: {}'.format(path))
def create_database(
dbname,
dbpath,
time_precision='ms',
duration_log='1d',
duration_num='1w',
timezone=DEFAULT_TIMEZONE,
drop_threshold=DEFAULT_DROP_THRESHOLD,
buffer_size=DEFAULT_BUFFER_SIZE,
config={},
_uuid=None,
_pool=0):
'''
Note: duration_log and duration_num can both be integer or string.
get_duration() understands both.
'''
check_valid_dbname(dbname)
_config = {
'buffer_path': dbpath
}
_config.update(config)
if time_precision not in ['s', 'ms', 'us', 'ns']:
raise ValueError('time_precision must be either \'s\' (seconds), '
'\'ms\' (milliseconds), \'us\' (microseconds) '
'or \'ns\' (nanoseconds) but received {!r}'
.format(time_precision))
time_precision = get_time_precision(time_precision)
duration_num = get_duration(time_precision, duration_num)
duration_log = get_duration(time_precision, duration_log)
if _uuid is None:
_uuid = uuid.uuid1()
with open(os.path.join(dbpath, 'database.conf'),
'w',
encoding='utf-8') as f:
f.write(DEFAULT_CONFIG.format(
comment_buffer_path='# '
if _config['buffer_path'] == dbpath else '',
**_config))
db_obj = [
1, # shema version
_uuid.bytes, # uuid
dbname, # dbname
time_precision, # time precision
buffer_size, # buffer size
duration_num, # duration num
duration_log, # duration log
timezone, # timezone
drop_threshold, # drop threshold
]
with open(os.path.join(dbpath, 'database.dat'), 'wb') as f:
f.write(qpack.packb(db_obj))
def color_red(s):
return '\x1b[31m{}\x1b[0m'.format(s)
def color_yellow(s):
return '\x1b[33m{}\x1b[0m'.format(s)
def color_purple(s):
return '\033[95m{}\x1b[0m'.format(s)
def color_blue(s):
return '\033[94m{}\x1b[0m'.format(s)
def print_header(title, desciption, has_default):
print('\n',
color_blue(title),
'(enter to use default)' if has_default else '')
if desciption:
print(desciption)
def print_error(s):
print('\n', color_yellow(s), '\n')
def print_action(action):
print(' {}'.format(color_yellow(action)))
def get_input(default):
return input('[{}] {}'.format(color_red(default), PROMPT)
if default is not None
else PROMPT).strip()
def get_pass(default):
return getpass.getpass('[{}] {}'.format(color_red(default), PROMPT)
if default is not None
else PROMPT).strip()
class Options:
def __init__(self, options):
self.options = options
def get_options(self):
return [option['option'] for option in self.options]
def options_as_text(self):
return ' or '.join([s
for s in [
', '.join(self.get_options()[:-1]),
self.get_options()[-1]] if s])
def __getitem__(self, key):
return self.options[key]
def not_empty(s):
if not s:
raise ValueError('Empty value is not allowed')
def quit_manage(exit_code=0, msg='Exit manage SiriDB... bye!'):
if siri:
logging.debug('Close siridb connection')
siri.close()
if exit_code:
logging.error(msg)
else:
logging.info(msg)
sys.exit(exit_code)
def menu(title, options, description='', default=None):
print_header(title, description, default is not None)
while True:
for option in options:
print(' [{}] - {}'.format(color_red(option['option']),
option['text']))
inp = get_input(default)
if not inp:
inp = default
if inp not in options.get_options():
print('\nInvalid option: {}, options are: {}'.format(
color_red(inp), options.options_as_text()))
else:
return inp
def ask_string(title,
description='',
default=None,
func=lambda x: None,
is_password=False):
print_header(title, description, default is not None)
while True:
inp = get_pass(default) if is_password else get_input(default)
if not inp:
inp = default
try:
func(inp)
except Exception as e:
print('\n', e)
else:
return inp
def ask_int(title, description='', default=None, func=lambda x: None):
print_header(title, description, default is not None)
while True:
inp = get_input(default)
if not inp:
inp = default
try:
inp = int(inp)
except ValueError:
print('\nExpecting an integer value but got {!r}'.format(inp))
else:
try:
func(inp)
except Exception as e:
print('\n', e)
else:
return inp
def check_valid_buffer_size(i):
if i % 512 != 0:
raise ValueError('Please use a multiple of 512 as a buffer size, '
'got {}'.format(i))
check_min_max(i, 512, MAX_BUFFER_SIZE)
def check_min_max(i, mi, ma, s='a value'):
if i < mi or i > ma:
raise ValueError('Expecting {} between {} and {} but got {}'.format(
s, mi, ma, i))
class SiriDBInfo():
def __init__(self, version, dblist):
self.version = version
self.dblist = dblist
async def set_local_siridb_info(host, port):
global local_siridb_info
try:
result = await async_server_info(host, port)
except Exception as e:
logging.error('Connection error: {}'.format(e))
sys.exit(1)
else:
if result:
local_siridb_info = SiriDBInfo(*result)
async def set_remote_siridb_info(host, port):
global remote_siridb_info
try:
result = await async_server_info(host, port)
except:
result = None
if result:
remote_siridb_info = SiriDBInfo(*result)
if remote_siridb_info is None:
raise RuntimeError(
'Error retreiving SiriDB info from {}:{}'.format(host, port))
if not remote_siridb_info.dblist:
raise RuntimeError('No databases found in {}:{}'.format(host, port))
if local_siridb_info.version != remote_siridb_info.version:
raise RuntimeError(
'Local version ({}) not equal to remote version ({})'
.format(local_siridb_info.version, remote_siridb_info.version))
def check_dbname(s):
check_valid_dbname(s)
if s in local_siridb_info.dblist:
raise ValueError('Database {!r} already exists'.format(s))
if len(local_siridb_info.dblist) >= MAX_NUMBER_DB:
raise ValueError(
'Cannot create {!r} because the maximum number of '
'databases is reached. (max={})'.format(s, MAX_NUMBER_DB))
def check_loaded(dbname):
asyncio.get_event_loop().run_until_complete(set_local_siridb_info(
settings.localhost,
settings.listen_client_port))
if dbname not in local_siridb_info.dblist:
raise ValueError('Database {!r} is not loaded, please check the '
'SiriDB logging to see what went wrong. (possible '
'cause: SiriDB has no access to the database '
'folder)'.format(dbname))
class SiriDBLoadProtocol(SiriDBProtocol):
def connection_made(self, transport):
def finished(future):
pass
self.transport = transport
self.remote_ip, self.port = transport.get_extra_info('peername')[:2]
self.future = self.send_package(CPROTO_REQ_LOADDB,
data=self._dbname,
timeout=10)
self.future.add_done_callback(finished)
async def load_database(dbpath, host, port):
if dbpath[-1] != '/':
dbpath += '/'
loop = asyncio.get_event_loop()
client = loop.create_connection(
lambda: SiriDBLoadProtocol(None, None, dbpath),
host=host,
port=port)
transport, protocol = await asyncio.wait_for(client, timeout=10)
await protocol.future
transport.close()
def connect_other(dbname, host, port, username, password):
global siri
siri = connect(username, password, dbname, host, port)
def connect_to_siridb(dbname, address, port, username, password):
try:
connect_other(dbname, address, port, username, password)
except Exception as e:
raise ConnectionError('Error while connecting: {}'.format(e))
try:
result = siri.query('show version')
except QueryError:
raise ConnectionError('User {!r} has no {!r} privileges'.format(
username, FULL_AUTH))
version = result['data'][0]['value']
if tuple(map(int, version.split('.')))[:2] != __version_info__[:2]:
raise ValueError('SiriDB Server is running version {}, '
'we are using {}'.format(version, __version__))
result = siri.query('list users name, access')
for user in result['users']:
if user[0] == username and user[1] != FULL_AUTH:
raise ConnectionError('User {!r} has no {!r} privileges'.format(
username, FULL_AUTH))
def join_database():
other_address = None
other_port = DEFAULT_CLIENT_PORT
username = None
dbname = None
while True:
other_address = ask_string(
title='Remote host or IP-address',
default=other_address,
func=not_empty,
description='If your database has already more than one server '
'you can just choose one')
other_port = ask_int(
title='Remote client port',
default=other_port,
func=functools.partial(check_min_max, mi=1, ma=65535))
try:
asyncio.get_event_loop().run_until_complete(set_remote_siridb_info(
other_address,
other_port))
except Exception as e:
print_error(e)
continue
db = menu(
title='Database',
options=Options([
{'option': str(i), 'text': s}
for i, s in enumerate(remote_siridb_info.dblist)]),
default='0')
dbname = remote_siridb_info.dblist[int(db)]
if dbname in local_siridb_info.dblist:
print_error('Database "{}" already exist on this server'.format(
dbname))
continue
username = ask_string(
title='User name',
default=username,
func=not_empty,
description='The given user name should have {!r} '
'privileges'.format(FULL_AUTH))
password = ask_string(
title='Password',
is_password=True)
try:
connect_to_siridb(dbname,
other_address,
other_port,
username,
password)
except Exception as e:
print_error(e)
else:
break
print_action('Please verify your input and try again...')
dbpath = os.path.join(settings.default_db_path, dbname)
mk_path(dbpath)
buffer_path = ask_buffer_path(dbpath)
while True:
try:
result = siri.query('list pools pool, servers, series ')
except QueryError as e:
print_error(e)
answer = menu(
title='Do you want to retry?',
options=Options([
{'option': 'r', 'text': 'Retry'},
{'option': 'q', 'text': 'Quit'}]),
default='r')
if answer == 'r':
continue
quit_manage(1, e)
break
pools = sorted(result['pools'], key=lambda t: t[0])
pool_or_replica(pools, dbpath, buffer_path)
def show_pool_status(pools):
print(color_blue('{}{}{}'.format('pool'.ljust(10),
'servers'.ljust(10),
'series')))
for pool in pools:
print('{}{}{}'.format(str(pool[0]).ljust(10),
str(pool[1]).ljust(10),
str(pool[2])))
def create_joined_database(dbpath, buffer_path, pool, new_pool, action_str):
dbconfig = siri.query('show {}'.format(','.join(DBPROPS)))
props = {prop['name']: prop['value'] for prop in dbconfig['data']}
cfg = {}
ask_buffer_size(cfg)
cfg['buffer_path'] = buffer_path
dbname = props['dbname']
answer = menu(
title='Are you sure you want to continue and {}?'.format(action_str),
options=Options([
{'option': 'y', 'text': 'Yes, I\'m sure'},
{'option': 'n', 'text': 'No, go back'}]),
default='n')
if answer == 'n':
return None
create_and_register_server(dbname,
dbpath,
pool,
props,
cfg,
new_pool,
allow_retry=True)
def get_time_precision(s):
_map = ['s', 'ms', 'us', 'ns']
return _map.index(s)
def get_duration(tp, duration):
return duration if isinstance(duration, int) \
else DURATIONS[duration][0] * (1000**tp)
def create_and_register_server(dbname,
dbpath,
pool,
props,
cfg,
new_pool,
allow_retry=True):
def rollback(*args):
logging.warning('Roll-back create database...')
shutil.rmtree(dbpath)
quit_manage(*args)
address = settings.server_address
port = settings.listen_backend_port
_uuid = uuid.uuid1()
create_database(
dbname=dbname,
dbpath=dbpath,
time_precision=props['time_precision'],
duration_log=props['duration_log'],
duration_num=props['duration_num'],
timezone=props['timezone'],
drop_threshold=props['drop_threshold'],
config=cfg,
_uuid=_uuid,
_pool=pool)
logging.info('Added database {!r}'.format(props['dbname']))
for fn in ('servers.dat',
'users.dat',
'groups.dat'):
try:
content = siri._get_file(fn)
except ServerError as e:
rollback(1, e)
with open(os.path.join(dbpath, fn), 'wb') as f:
f.write(content)
if new_pool:
with open(os.path.join(dbpath, '.reindex'), 'wb') as f:
pass
with open(os.path.join(dbpath, 'servers.dat'), 'rb') as f:
qp_servers = f.read()
servers_obj = qpack.unpackb(qp_servers)
server = [_uuid.bytes, bytes(address, 'utf-8'), port, pool]
servers_obj.append(server)
with open(os.path.join(dbpath, 'servers.dat'), 'wb') as f:
qp_servers = qpack.packb(servers_obj)
f.write(qp_servers)
result = siri.query('list servers name, status')
expected = 'running'
for srv in result['servers']:
if srv[1] != expected:
rollback(
1,
'All servers must have status {!r} '
'before we can continue. As least {!r} has status {!r}'
.format(expected, srv[0], srv[1]))
asyncio.get_event_loop().run_until_complete(load_database(
dbpath,
settings.localhost,
settings.listen_client_port))
time.sleep(1)
try:
check_loaded(dbname)
except Exception as e:
rollback(1, e)
else:
logging.info(
'Database loaded... now register the server'.format(dbname))
while True:
try:
siri._register_server(server)
except Exception as e:
if allow_retry:
print_error(e)
answer = menu(
title='Do you want to retry the registration?',
options=Options([
{'option': 'r', 'text': 'Retry'},
{'option': 'q', 'text': 'Quit'}]),
default='r')
if answer == 'r':
continue
rollback(0, None)
else:
rollback(1, e)
break
quit_manage(0, 'Finished joining database {!r}...'.format(dbname))
def create_new_pool(pools, dbpath, buffer_path):
pool = len(pools)
create_joined_database(dbpath,
buffer_path,
pool,
True,
'create a new pool: {}'.format(pool))
def create_new_replica(pools, dbpath, buffer_path):
opts = [{
'option': str(pool[0]),
'text': 'Pool ID {}'.format(pool[0])
} for pool in pools if pool[1] == 1]
pool = menu(
title='For which pool do you want to create a replica?',
description=None
if opts
else '(All available pools already have a replica)',
options=Options(opts + [{'option': 'b', 'text': 'Back'}])
)
if pool == 'b':
return None
pool = int(pool)
create_joined_database(dbpath,
buffer_path,
pool,
False,
'create a replica for pool {}'.format(pool))
def pool_or_replica(pools, dbpath, buffer_path):
while True:
action = menu(
title='New pool or extend and existing pool (replica)?',
options=Options([
{'option': 'p',
'text': 'Create a new pool'},
{'option': 'r',
'text': 'Create a replica for an existing pool'},
{'option': 's',
'text': 'Show current pools'},
{'option': 'q',
'text': 'quit'}]))
{
'q': quit_manage,
'p': lambda: create_new_pool(pools, dbpath, buffer_path),
'r': lambda: create_new_replica(pools, dbpath, buffer_path),
's': lambda: show_pool_status(pools)
}[action]()
def ask_buffer_path(dbpath):
return ask_string(
title='Location to store the buffer file',
description='It can be useful to store the buffer file on a separate '
'(fast) disk, for example a Solid State Drive (SSD).'.format(
args.config),
default=dbpath,
func=mk_path)
def ask_buffer_size(cfg):
cfg['buffer_size'] = ask_int(
title='Buffer size',
default=DEFAULT_BUFFER_SIZE,
func=check_valid_buffer_size)
def _arg_dbname(parser):
parser.add_argument(
'--dbname',
type=str,
required=True,
help='Database name.')
def _arg_buffer_path(parser):
parser.add_argument(
'--buffer-path',
type=str,
default="",
help='Alternative location for storing the buffer file.')
def _arg_time_precision(parser):
parser.add_argument(
'--time-precision',
type=str,
choices=['s', 'ms', 'us', 'ns'],
default='ms',
help='Time precision for the records in the database in milliseconds '
'or seconds.')
def _arg_duration_num(parser):
parser.add_argument(
'--duration-num',
type=str,
choices=list(DURATIONS.keys()),
default='1w',
help='Time span used for number (float and integer) shards.')
def _arg_duration_log(parser):
parser.add_argument(
'--duration-log',
type=str,
choices=list(DURATIONS.keys()),
default='1d',
help='Time span used for log (string) shards .')
def _arg_buffer_size(parser):
parser.add_argument(
'--buffer-size',
type=int,
default=DEFAULT_BUFFER_SIZE,
help='Size in bytes per series for storing points in memory. Use a '
'multiple of 512 as a buffer size.')
def _arg_remote_address(parser):
parser.add_argument(
'--remote-address',
type=str,
required=True,
help='Remote host or IP-address of one of the servers in the SiriDB '
'cluster you want to join.')
def _arg_remote_port(parser):
parser.add_argument(
'--remote-port',
type=int,
default=9000,
help='Remote port of one of the servers in the SiriDB cluster you '
'want to join.')
def _arg_user(parser):
parser.add_argument(
'--user',
type=str,
required=True,
help='User for connecting to the SiriDB cluster. The user should '
'have {!r} privileges.'.format(FULL_AUTH))
def _arg_password(parser):
parser.add_argument(
'--password',
type=str,
default='',
help='You will be prompted for a password when leaving this empty.')
def _arg_pool(parser):
parser.add_argument(
'--pool',
type=int,
required=True,
help='Pool ID for which you want to create the replica. A pool can '
'only have two servers so you must choose a pool with exactly one '
'server. (use \'\' for an overview)')
def form_create_new_database():
dbname = ask_string(
title='Type a name for the new database',
description='Note: this value cannot be changed after the database '
'has been created',
func=check_dbname)
dbpath = os.path.join(settings.default_db_path, dbname)
try:
mk_path(dbpath)
except Exception as e:
quit_manage(1, e)
buffer_path = ask_buffer_path(dbpath)
time_precision = menu(
title='Time precision',
options=Options([
{'option': 's', 'text': 'seconds'},
{'option': 'ms', 'text': 'milliseconds'},
{'option': 'us', 'text': 'microseconds'},
{'option': 'ns', 'text': 'nanoseconds'}
]),
default='ms')
duration_options = [
{'option': k, 'text': v[1]} for k, v in DURATIONS.items()]
duration_num = menu(
title='Number (float and integer) sharding duration',
options=Options(duration_options),
default='1w')
duration_log = menu(
title='Log (string) sharding duration',
options=Options(duration_options),
default='1d')
cfg = {'buffer_path': buffer_path}
ask_buffer_size(cfg)
create_new_database(dbname,
dbpath,
time_precision,
duration_log,
duration_num,
cfg.pop('buffer_size'),
cfg)
def create_new_database(dbname,
dbpath,
time_precision,
duration_log,
duration_num,
buffer_size,
cfg):
create_database(
dbname=dbname,
dbpath=dbpath,
time_precision=time_precision,
duration_log=duration_log,
duration_num=duration_num,
buffer_size=buffer_size,
config=cfg)
logging.info('Created database {!r}'.format(dbname))
asyncio.get_event_loop().run_until_complete(load_database(
dbpath,
settings.localhost,
settings.listen_client_port))
time.sleep(1)
try:
check_loaded(dbname)
except Exception as e:
quit_manage(1, e)
else:
quit_manage(0, 'Database "{}" created succesfully'.format(dbname))
def main_menu():
choice = menu(
title='Tell me what you plan to do:',
options=Options([
{'option': 'c', 'text': 'create a new database'},
{'option': 'j', 'text': 'join an existing SiriDB database'},
{'option': 'q', 'text': 'quit'}
]))
{
'q': quit_manage,
'j': join_database,
'c': form_create_new_database
}[choice]()
def signal_handler(s, f):
quit_manage(2, '\nyou pressed ctrl+c, quiting...\n')
def parse_create_new(args):
try:
check_dbname(args.dbname)
check_valid_buffer_size(args.buffer_size)
dbpath = os.path.join(settings.default_db_path, args.dbname)
mk_path(dbpath)
buffer_path = args.buffer_path or dbpath
mk_path(buffer_path)
except Exception as e:
quit_manage(1, e)
cfg = {'buffer_path': buffer_path}
create_new_database(args.dbname,
dbpath,
args.time_precision,
args.duration_log,
args.duration_num,
args.buffer_size,
cfg)
def parse_create_replica_or_pool(args):
if not args.password:
password = ask_string(
title='Password',
is_password=True)
else:
password = args.password
try:
asyncio.get_event_loop().run_until_complete(set_remote_siridb_info(
args.remote_address,
args.remote_port))
check_dbname(args.dbname)
check_valid_buffer_size(args.buffer_size)
dbpath = os.path.join(settings.default_db_path, args.dbname)
mk_path(dbpath)
buffer_path = args.buffer_path or dbpath
mk_path(buffer_path)
connect_to_siridb(args.dbname,
args.remote_address,
args.remote_port,
args.user,
password)
result = siri.query('list pools pool, servers, series')
if hasattr(args, 'pool'):
pools = {pool[0]: pool[1] for pool in result['pools']}
if args.pool not in pools:
raise ValueError('Pool ID {} does not exists'.format(
args.pool))
if pools[args.pool] != 1:
raise ValueError('A pool can only have two servers. '
'Pool ID {} already has {} servers.'
.format(args.pool, pools[args.pool]))
pool = args.pool
else:
pool = len(result['pools'])
dbconfig = siri.query('show {}'.format(','.join(DBPROPS)))
except Exception as e:
quit_manage(1, e)
props = {prop['name']: prop['value'] for prop in dbconfig['data']}
cfg = {
'buffer_path': buffer_path,
'buffer_size': args.buffer_size,
}
create_and_register_server(args.dbname,
dbpath,
pool,
props,
cfg,
new_pool=not hasattr(args, 'pool'),
allow_retry=False)