mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-25 06:55:45 +00:00
simplified the tarantool files
Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
ec074553fd
commit
770f574f7a
@ -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
|
||||
@ -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)
|
||||
#
|
||||
return True if ret >= 0 else False
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user