优化代码。
This commit is contained in:
parent
459b5cc92a
commit
a60ba5b3b7
|
|
@ -1,16 +1,15 @@
|
||||||
from Common import *
|
from Common import *
|
||||||
|
|
||||||
|
current_dir = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else os.path.dirname(
|
||||||
|
os.path.abspath(__file__))
|
||||||
|
log_dir = os.path.join(current_dir, "logs")
|
||||||
|
|
||||||
class InstallMariaDb:
|
class InstallMariaDb:
|
||||||
|
|
||||||
def __init__(self, install_path):
|
def __init__(self):
|
||||||
self.service_name = 'ComacMariaDB'
|
self.service_name = 'ComacMariaDB'
|
||||||
self.root_path = install_path
|
self.mariadb_data_path = os.path.join(current_dir, 'mariadb', 'data')
|
||||||
self.mariadb_data_path = os.path.join(install_path, 'mariadb', 'data')
|
self.init_sql = os.path.join(current_dir, 'mariadb', 'data', 'init.sql')
|
||||||
self.mariadb_port = mariadb_port
|
|
||||||
self.passowrd = mariadb_passowrd
|
|
||||||
self.init_schema = mariadb_init_schema
|
|
||||||
self.init_sql = os.path.join(install_path, 'mariadb', 'data', 'init.sql')
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def start_install_mariadb(self):
|
def start_install_mariadb(self):
|
||||||
|
|
@ -40,11 +39,11 @@ class InstallMariaDb:
|
||||||
logger.info("服务启动成功")
|
logger.info("服务启动成功")
|
||||||
|
|
||||||
def __register_service(self):
|
def __register_service(self):
|
||||||
args1 = self.root_path + '/mariadb/bin/mysql_install_db.exe'
|
args1 = current_dir + '/mariadb/bin/mysql_install_db.exe'
|
||||||
args2 = '--datadir=' + self.mariadb_data_path
|
args2 = '--datadir=' + self.mariadb_data_path
|
||||||
args3 = '--service=' + self.service_name
|
args3 = '--service=' + self.service_name
|
||||||
args4 = '--password=' + self.passowrd
|
args4 = '--password=' + mariadb_passowrd
|
||||||
args5 = '--port=' + str(self.mariadb_port)
|
args5 = '--port=' + str(mariadb_port)
|
||||||
res = subprocess.run([args1, args2, args3, args4, args5], text=True, capture_output=True)
|
res = subprocess.run([args1, args2, args3, args4, args5], text=True, capture_output=True)
|
||||||
if res.returncode != 0:
|
if res.returncode != 0:
|
||||||
logger.info(res.stdout)
|
logger.info(res.stdout)
|
||||||
|
|
@ -54,14 +53,14 @@ class InstallMariaDb:
|
||||||
logger.info("服务注册成功")
|
logger.info("服务注册成功")
|
||||||
|
|
||||||
def __init_db_user(self):
|
def __init_db_user(self):
|
||||||
command1 = fr'{self.root_path}\mariadb\bin\mysql.exe -u root -p{self.passowrd} -P {self.mariadb_port} -e "create database if not exists {self.init_schema}; use {self.init_schema};"'
|
command1 = fr'{current_dir}\mariadb\bin\mysql.exe -u root -p{mariadb_passowrd} -P {mariadb_port} -e "create database if not exists {mariadb_init_schema}; use {mariadb_init_schema};"'
|
||||||
command2 = fr'{self.root_path}\mariadb\bin\mysql.exe -u root -p****** -P {self.mariadb_port} -e "create database if not exists {self.init_schema}; use {self.init_schema};"'
|
command2 = fr'{current_dir}\mariadb\bin\mysql.exe -u ****** -p****** -P { mariadb_port} -e "create database if not exists {mariadb_init_schema}; use {mariadb_init_schema};"'
|
||||||
logger.info(command2)
|
logger.info(command2)
|
||||||
with os.popen(command1) as stream:
|
with os.popen(command1) as stream:
|
||||||
res = stream.read()
|
res = stream.read()
|
||||||
logger.info(res)
|
logger.info(res)
|
||||||
|
|
||||||
schema_dir = fr'{self.root_path}\mariadb\data\{self.init_schema}'
|
schema_dir = fr'{current_dir}\mariadb\data\{mariadb_init_schema}'
|
||||||
if not os.path.exists(schema_dir):
|
if not os.path.exists(schema_dir):
|
||||||
logger.error("数据库初始化失败")
|
logger.error("数据库初始化失败")
|
||||||
raise MyCustomError("数据库安装失败")
|
raise MyCustomError("数据库安装失败")
|
||||||
|
|
@ -69,9 +68,6 @@ class InstallMariaDb:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
current_dir = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else os.path.dirname(
|
|
||||||
os.path.abspath(__file__))
|
|
||||||
log_dir = os.path.join(current_dir, "logs")
|
|
||||||
ensure_dir(log_dir)
|
ensure_dir(log_dir)
|
||||||
logger.add(
|
logger.add(
|
||||||
sink=os.path.join(log_dir, "InstallMariaDB_{time}.log"), # 文件路径模板
|
sink=os.path.join(log_dir, "InstallMariaDB_{time}.log"), # 文件路径模板
|
||||||
|
|
@ -81,5 +77,5 @@ if __name__ == '__main__':
|
||||||
enqueue=True, # 线程安全写入
|
enqueue=True, # 线程安全写入
|
||||||
format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}" # 自定义格式
|
format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}" # 自定义格式
|
||||||
)
|
)
|
||||||
InstallMariaDb(current_dir).start_install_mariadb()
|
InstallMariaDb().start_install_mariadb()
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue