From 770f574f7ae83acf127c3410a830a2b35db75c63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Eckel?= Date: Fri, 8 Apr 2022 13:34:27 +0200 Subject: [PATCH] simplified the tarantool files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jürgen Eckel --- planetmint/backend/tarantool/connection.py | 44 +++++++---------- planetmint/backend/tarantool/utils.py | 57 ++-------------------- 2 files changed, 21 insertions(+), 80 deletions(-) diff --git a/planetmint/backend/tarantool/connection.py b/planetmint/backend/tarantool/connection.py index 2f0767a..75548e0 100644 --- a/planetmint/backend/tarantool/connection.py +++ b/planetmint/backend/tarantool/connection.py @@ -4,13 +4,12 @@ # Code is Apache-2.0 and docs are CC-BY-4.0 import logging - import tarantool + from planetmint.config import Config logger = logging.getLogger(__name__) - class TarantoolDB: def __init__(self, host: str = None, port: int = None, user: str = None, password: str = None, reset_database: bool = False): @@ -18,42 +17,35 @@ class TarantoolDB: self.port = port # TODO add user support later on #self.db_connect = tarantool.connect(host=host, port=port, user=user, password=password) - self.db_connect = tarantool.connect(host=host, port=port) - self._load_setup_files() + self.db_connect = tarantool.connect(host=self.host , port=self.port) + self.init_path = Config().get()["database"]["init_config"]["absolute_path"] + self.drop_path = Config().get()["database"]["drop_config"]["absolute_path"] if reset_database: self.drop_database() self.init_database() - def _load_setup_files(self): - self.init_path = Config().get()["database"]["init_config"]["absolute_path"] - self.drop_path = Config().get()["database"]["drop_config"]["absolute_path"] - #self.drop_commands = self.__read_commands(file_path=init_path) - #self.init_commands = self.__read_commands(file_path=drop_path) - def space(self, space_name: str): return self.db_connect.space(space_name) def get_connection(self): return self.db_connect - def __read_commands(self, file_path): - with open(file_path, "r") as cmd_file: - commands = [line.strip() for line in cmd_file.readlines() if len(str(line)) > 1] - cmd_file.close() - return commands - def drop_database(self): - from planetmint.backend.tarantool.utils import run2 db_config = Config().get()["database"] - # drop_config = db_config["drop_config"] - # f_path = "%s%s" % (drop_config["relative_path"], drop_config["drop_file"]) - # commands = self.__read_commands(file_path=f_path) - run2(commands=self.drop_path, config=db_config) + self.run_cmd(commands=self.drop_path, config=db_config) def init_database(self): - from planetmint.backend.tarantool.utils import run2 db_config = Config().get()["database"] - # init_config = db_config["init_config"] - # f_path = "%s%s" % (init_config["relative_path"], init_config["init_file"]) - # commands = self.__read_commands(file_path=f_path) - run2(commands=self.init_path, config=db_config) + self.run_cmd(commands=self.init_path, config=db_config) + + def run_cmd(commands: list, config: dict): + import subprocess + + ret = subprocess.Popen( + ['%s %s:%s < %s' % ("tarantoolctl connect", "localhost", "3303", "planetmint/backend/tarantool/init.lua")], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + universal_newlines=True, + bufsize=0, + shell=True) + return True if ret >= 0 else False \ No newline at end of file diff --git a/planetmint/backend/tarantool/utils.py b/planetmint/backend/tarantool/utils.py index e4894f3..88b9b99 100644 --- a/planetmint/backend/tarantool/utils.py +++ b/planetmint/backend/tarantool/utils.py @@ -1,62 +1,11 @@ import subprocess -from planetmint.config import Config - - - -def run2(commands: list, config: dict): - sshProcess = subprocess.Popen( +def run_cmd(commands: list, config: dict): + ret = subprocess.Popen( ['%s %s:%s < %s' % ("tarantoolctl connect", "localhost", "3303", "planetmint/backend/tarantool/init.lua")], stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True, bufsize=0, shell=True) - - for cmd in commands: - try: - sshProcess.stdin.write(cmd) - except Exception as cmd_err: - print(str(cmd_err)) - sshProcess.stdin.close() - # TODO To add here Exception Handler for stdout - # for line in sshProcess.stdout: - # print(line) -# -#def run(commands: list, config: dict): -# sshProcess = subprocess.Popen( -# ['%s %s:%s@%s:%s' % (config["service"], config["login"], config["password"], config["host"], config["port"])], -# stdin=subprocess.PIPE, -# stdout=subprocess.PIPE, -# universal_newlines=True, -# bufsize=0, -# shell=True) -# -# for cmd in commands: -# try: -# sshProcess.stdin.write(cmd) -# except Exception as cmd_err: -# print(str(cmd_err)) -# sshProcess.stdin.close() -# # TODO To add here Exception Handler for stdout -# # for line in sshProcess.stdout: -# # print(line) -# -# -#def __read_commands(file_path): -# with open(file_path, "r") as cmd_file: -# commands = [line.strip()+'\n' for line in cmd_file.readlines() if len(str(line)) > 1] -# cmd_file.close() -# return commands -# -# -#def _load_setup_files(): -# drop_commands = __read_commands(file_path="planetmint/backend/tarantool/drop.lua") -# init_commands = __read_commands(file_path="planetmint/backend/tarantool/init.lua") -# return init_commands, drop_commands -# -# -#init, drop = _load_setup_files() -#db_config = Config().get()["database"] -#run(commands=drop, config=db_config) -# \ No newline at end of file + return True if ret >= 0 else False