更新
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Register the existing project database in BaoTa without changing MySQL data."""
|
||||
import json
|
||||
import os
|
||||
import shlex
|
||||
import sqlite3
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
PANEL = Path('/www/server/panel')
|
||||
if os.geteuid() != 0 or not PANEL.is_dir():
|
||||
raise SystemExit('Run on the BaoTa host as root.')
|
||||
os.chdir(str(PANEL))
|
||||
sys.path.insert(0, str(PANEL))
|
||||
sys.path.insert(0, str(PANEL / 'class'))
|
||||
import public
|
||||
import database
|
||||
|
||||
dsn = None
|
||||
for line in Path('/etc/xingyu-im.env').read_text().splitlines():
|
||||
if line.startswith('IM_DB_DSN='):
|
||||
dsn = shlex.split(line.split('=', 1)[1])[0]
|
||||
assert dsn is not None
|
||||
credentials, endpoint = dsn.split('@tcp(', 1)
|
||||
username, password = credentials.split(':', 1)
|
||||
address, database_part = endpoint.split(')/', 1)
|
||||
host, port = address.rsplit(':', 1)
|
||||
name = database_part.split('?', 1)[0]
|
||||
assert host == '127.0.0.1' and port == '3307'
|
||||
assert name.lower() == 'im'
|
||||
|
||||
existing = public.M('databases').where("name=? AND LOWER(type)=LOWER('mysql')", (name,)).find()
|
||||
if not existing:
|
||||
backup = Path('/www/backup/xingyu-panel-takeover-database-' + time.strftime('%Y%m%d-%H%M%S'))
|
||||
backup.mkdir(mode=0o700, parents=True, exist_ok=False)
|
||||
os.chmod(str(backup), 0o700)
|
||||
with sqlite3.connect(str(PANEL / 'data/db/database.db')) as connection:
|
||||
with sqlite3.connect(str(backup / 'database.db')) as destination:
|
||||
connection.backup(destination)
|
||||
get = public.dict_obj()
|
||||
get.db_host = host
|
||||
get.db_port = int(port)
|
||||
get.db_user = username
|
||||
get.db_name = name
|
||||
get.db_password = password
|
||||
get.db_ps = '星遇 IM · 独立 MySQL 8.4 · 127.0.0.1:3307'
|
||||
result = database.database().AddCloudDatabase(get)
|
||||
if not result.get('status'):
|
||||
raise RuntimeError('BaoTa database registration failed; no business data has been modified.')
|
||||
|
||||
record = public.M('databases').where("name=? AND LOWER(type)=LOWER('mysql')", (name,)).find()
|
||||
config = json.loads(record['conn_config'])
|
||||
assert config['db_host'] == host and int(config['db_port']) == int(port)
|
||||
assert config['db_name'] == name
|
||||
check = public.dict_obj()
|
||||
for key, value in config.items():
|
||||
setattr(check, key, value)
|
||||
assert database.database().CheckCloudDatabase(check) is True
|
||||
print(json.dumps({'status': 'DATABASE_REGISTERED_AND_CONNECTED', 'database': name,
|
||||
'panel_id': record['id'], 'host': host, 'port': int(port)}, ensure_ascii=False))
|
||||
Reference in New Issue
Block a user