diff --git a/planetmint/__init__.py b/planetmint/__init__.py index 11e5051..73b10ae 100644 --- a/planetmint/__init__.py +++ b/planetmint/__init__.py @@ -55,6 +55,12 @@ config = { log_config['handlers']['console']['level']).lower(), 'workers': None, # if None, the value will be cpu_count * 2 + 1 }, + "ctl_config": { + "login": "admin", + "host": "admin:pass@127.0.0.1:3301", + "service": "tarantoolctl connect", + "init_file": "init_db.txt" + }, 'wsserver': { 'scheme': 'ws', 'host': 'localhost', diff --git a/planetmint/backend/connection_tarantool.py b/planetmint/backend/connection_tarantool.py index 64da960..cf87ed0 100644 --- a/planetmint/backend/connection_tarantool.py +++ b/planetmint/backend/connection_tarantool.py @@ -11,7 +11,6 @@ import tarantool import os import pathlib -from time import sleep from planetmint.backend.tarantool.utils import run @@ -27,37 +26,30 @@ BACKENDS = { # This is path to MongoDBClass logger = logging.getLogger(__name__) -def init_tarantool(): - init_lua_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tarantool", "init_db.lua") - tarantool_root = os.path.join(pathlib.Path.home(), 'tarantool') - snap = os.path.join(pathlib.Path.home(), 'tarantool_snap') - if os.path.exists(tarantool_root) is not True: - run(["mkdir", tarantool_root]) - run(["mkdir", snap]) - run(["tarantool", init_lua_path], tarantool_root) - else: - raise Exception("There is a instance of tarantool already created in %s" + snap) - - -def drop_tarantool(): - drop_lua_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tarantool", "drop_db.lua") - tarantool_root = os.path.join(pathlib.Path.home(), 'tarantool') - init_lua = os.path.join(tarantool_root, 'init_db.lua') - if os.path.exists(init_lua) is not True: - run(["tarantool", drop_lua_path]) - else: - raise Exception("There is no tarantool spaces to drop") - - class TarantoolDB: - def __init__(self, host: str, port: int, user: str, password: str): - # init_tarantool() + def __init__(self, host: str, port: int, user: str, password: str, reset_database: bool = False): + if reset_database: + self.init_database() self.db_connect = None self.db_connect = tarantool.connect(host=host, port=port, user=user, password=password) + def __init_tarantool(self): + from planetmint.backend.tarantool.utils import run + config = get_planetmint_config_value_or_key_error("ctl_config") + with open(config["init_file"], 'r') as file: + commands = [line + '\n' for line in file.readlines() if len(str(line)) > 1] + file.close() + run(commands=commands, config=config) + + def __drop_tarantool(self): + pass + def get_connection(self): return self.db_connect + def init_database(self): + pass + def connect(host: str = None, port: int = None, username: str = "admin", password: str = "pass", backend: str = None): backend = backend or get_planetmint_config_value_or_key_error('backend') # TODO Rewrite Configs diff --git a/planetmint/backend/tarantool/drop_db.lua b/planetmint/backend/tarantool/drop_db.lua deleted file mode 100644 index 8b8579e..0000000 --- a/planetmint/backend/tarantool/drop_db.lua +++ /dev/null @@ -1,15 +0,0 @@ -box.space.transactions.drop() -box.space.output.drop() -box.space.inputs.drop() -box.space.keys.drop() -box.space.abci_chains.drop() -box.space.assets.drop() -box.space.blocks.drop() -box.space.blocks_tx.drop() -box.space.elections.drop() -box.space.meta_datas.drop() -box.space.pre_commits.drop() -box.space.validators.drop() -box.space. -box.space. -box.snapshot() \ No newline at end of file diff --git a/planetmint/backend/tarantool/init_db.lua b/planetmint/backend/tarantool/init_db.txt similarity index 85% rename from planetmint/backend/tarantool/init_db.lua rename to planetmint/backend/tarantool/init_db.txt index 58e9366..cae67ca 100644 --- a/planetmint/backend/tarantool/init_db.lua +++ b/planetmint/backend/tarantool/init_db.txt @@ -31,12 +31,12 @@ meta_datas:create_index('id_search', { type='hash' , parts={'transaction_id'}}) pre_commits = box.schema.space.create('pre_commits' , {engine='memtx' , is_sync=false}) pre_commits:format({{name='commit_id', type='string'}, {name='height',type='integer'}, {name='transactions',type=any}}) pre_commits:create_index('id_search', {type ='hash' , parts={'commit_id'}}) -pre_commits:create_index('height_search', {type ='tree',unique=false, parts={'height'}}) +pre_commits:create_index('height_search', {type ='tree',unique=true, parts={'height'}}) validators = box.schema.space.create('validators' , {engine = 'memtx' , is_sync = false}) validators:format({{name='validator_id' , type='string'},{name='height',type='integer'},{name='validators' , type='any'}}) validators:create_index('id_search' , {type='hash' , parts={'validator_id'}}) -validators:create_index('height_search' , {type='tree', unique=false, parts={'height'}}) +validators:create_index('height_search' , {type='tree', unique=true, parts={'height'}}) transactions = box.schema.space.create('transactions',{engine='memtx' , is_sync=false}) transactions:format({{name='transaction_id' , type='string'}, {name='operation' , type='string'}, {name='version' ,type='string'}, {name='asset_id', type='string'}}) @@ -48,8 +48,8 @@ transactions:create_index('both_search' , {type = 'tree',unique=false, parts={'a inputs = box.schema.space.create('inputs') inputs:format({{name='transaction_id' , type='string'}, {name='fulfillment' , type='string'}, {name='owners_before' , type='array'}, {name='fulfills_transaction_id', type = 'string'}, {name='fulfills_output_index', type = 'string'}, {name='input_id', type='string'}}) -inputs:create_index('spent_search' , {type = 'hash', parts={'fulfills_transaction_id', 'fulfills_output_index'}}) inputs:create_index('delete_search' , {type = 'hash', parts={'input_id'}}) +inputs:create_index('spent_search' , {type = 'tree', unique=false, parts={'fulfills_transaction_id', 'fulfills_output_index'}}) inputs:create_index('id_search', {type = 'tree', unique=false, parts = {'transaction_id'}}) outputs = box.schema.space.create('outputs') @@ -58,10 +58,10 @@ outputs:create_index('unique_search' ,{type='hash', parts={'output_id'}}) outputs:create_index('id_search' ,{type='tree', unique=false, parts={'transaction_id'}}) keys = box.schema.space.create('keys') -keys:format({{name = 'transaction_id', type = 'string'} ,{name = 'output_id', type = 'string'}, {name = 'public_key', type = 'string'}}) -keys:create_index('keys_search', {type = 'hash', parts={'public_key'}}) +keys:format({{name = 'id', type='string'}, {name = 'transaction_id', type = 'string'} ,{name = 'output_id', type = 'string'}, {name = 'public_key', type = 'string'}}) +keys:create_index('id_search', {type = 'hash', parts={'id'}}) +keys:create_index('keys_search', {type = 'tree', unique=false, parts={'public_key'}}) keys:create_index('txid_search', {type = 'tree', unique=false, parts={'transaction_id'}}) -keys:create_index('id_search', {type = 'tree', unique=false, parts={'output_id'}}) +keys:create_index('output_search', {type = 'tree', unique=false, parts={'output_id'}}) -local console = require('console') -console.start() \ No newline at end of file +box.schema.user.passwd('pass') diff --git a/planetmint/backend/tarantool/utils.py b/planetmint/backend/tarantool/utils.py index 233dfd1..fe67279 100644 --- a/planetmint/backend/tarantool/utils.py +++ b/planetmint/backend/tarantool/utils.py @@ -1,22 +1,17 @@ -import os import subprocess -def run(command, path=None): - if path is not None: - os.chdir(path) - p = subprocess.Popen( - command - ) +def run(commands: list, config: dict): + sshProcess = subprocess.Popen(['%s %s' % (config["service"], config["host"])], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + universal_newlines=True, + bufsize=0, + shell=True) - # output, error = p.communicate() # TODO Here was problem that we are waiting for outputs - # if p.returncode != 0: - # print(str(p.returncode) + "\n" + str(output) + "\n" + str(error)) - else: - p = subprocess.Popen( - command - ) - - # output, error = p.communicate() - # if p.returncode != 0: - # print(str(p.returncode) + "\n" + str(output) + "\n" + str(error)) + for cmd in commands: + sshProcess.stdin.write(cmd) + sshProcess.stdin.close() + # TODO To add here Exception Handler for stdout + # for line in sshProcess.stdout: + # print(line)