mdserver-web/class/core/common.py

147 lines
4.3 KiB
Python
Raw Normal View History

2018-12-27 21:49:15 -05:00
# coding: utf-8
2022-11-17 21:29:25 -05:00
# ---------------------------------------------------------------------------------
# MW-Linux面板
# ---------------------------------------------------------------------------------
# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved.
# ---------------------------------------------------------------------------------
# Author: midoks <midoks@163.com>
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# 公共操作
# ---------------------------------------------------------------------------------
2018-12-27 21:49:15 -05:00
import os
import sys
import time
import string
import json
import hashlib
import shlex
import datetime
import subprocess
import re
import hashlib
2018-12-27 22:31:08 -05:00
from random import Random
2018-12-27 21:49:15 -05:00
2020-07-10 03:57:25 -04:00
import mw
2018-12-27 21:49:15 -05:00
import db
2019-02-21 10:05:09 -05:00
from flask import redirect
2018-12-27 22:31:08 -05:00
def init():
initDB()
2019-01-15 02:38:00 -05:00
initUserInfo()
2022-06-25 07:14:19 -04:00
initInitD()
2022-11-16 01:50:49 -05:00
initInitTask()
2024-01-08 12:56:25 -05:00
return True
2018-12-27 22:31:08 -05:00
2019-02-21 22:47:56 -05:00
def local():
result = checkClose()
if result:
return result
2019-02-21 10:05:09 -05:00
# 检查面板是否关闭
def checkClose():
if os.path.exists('data/close.pl'):
return redirect('/close')
2018-12-27 22:31:08 -05:00
def initDB():
try:
sql = db.Sql().dbfile('default')
2020-07-10 03:57:25 -04:00
csql = mw.readFile('data/sql/default.sql')
2018-12-27 22:31:08 -05:00
csql_list = csql.split(';')
for index in range(len(csql_list)):
sql.execute(csql_list[index], ())
2018-12-28 01:08:49 -05:00
2021-04-30 23:45:29 -04:00
except Exception as ex:
print(str(ex))
2018-12-27 22:31:08 -05:00
2022-06-17 03:52:52 -04:00
def doContentReplace(src, dst):
content = mw.readFile(src)
2020-07-10 03:57:25 -04:00
content = content.replace("{$SERVER_PATH}", mw.getRunDir())
2022-06-17 03:52:52 -04:00
mw.writeFile(dst, content)
2018-12-27 22:31:08 -05:00
2022-06-17 03:52:52 -04:00
def initInitD():
2020-07-14 22:43:10 -04:00
2022-06-17 03:52:52 -04:00
# systemctl
2022-11-10 09:26:45 -05:00
# sysCfgDir = mw.systemdCfgDir()
# if os.path.exists(sysCfgDir) and mw.getOsName() == 'centos' and mw.getOsID() == '9':
# systemd_mw = sysCfgDir + '/mw.service'
# systemd_mw_task = sysCfgDir + '/mw-task.service'
2022-11-10 07:32:17 -05:00
2022-11-10 09:26:45 -05:00
# systemd_mw_tpl = mw.getRunDir() + '/scripts/init.d/mw.service.tpl'
# systemd_mw_task_tpl = mw.getRunDir() + '/scripts/init.d/mw-task.service.tpl'
2022-11-10 07:32:17 -05:00
2022-11-10 09:26:45 -05:00
# if os.path.exists(systemd_mw):
# os.remove(systemd_mw)
# if os.path.exists(systemd_mw_task):
# os.remove(systemd_mw_task)
2022-11-10 08:02:06 -05:00
2022-11-10 09:26:45 -05:00
# doContentReplace(systemd_mw_tpl, systemd_mw)
# doContentReplace(systemd_mw_task_tpl, systemd_mw_task)
2022-11-10 07:32:17 -05:00
2022-11-10 09:26:45 -05:00
# mw.execShell('systemctl enable mw')
# mw.execShell('systemctl enable mw-task')
# mw.execShell('systemctl daemon-reload')
2022-06-17 03:52:52 -04:00
script = mw.getRunDir() + '/scripts/init.d/mw.tpl'
script_bin = mw.getRunDir() + '/scripts/init.d/mw'
doContentReplace(script, script_bin)
mw.execShell('chmod +x ' + script_bin)
2022-06-20 08:41:17 -04:00
# 在linux系统中,确保/etc/init.d存在
2022-11-10 09:26:45 -05:00
if not mw.isAppleSystem() and not os.path.exists("/etc/rc.d/init.d"):
mw.execShell('mkdir -p /etc/rc.d/init.d')
2022-06-20 08:41:17 -04:00
2022-11-19 23:06:24 -05:00
if not mw.isAppleSystem() and not os.path.exists("/etc/init.d"):
mw.execShell('mkdir -p /etc/init.d')
2022-06-17 03:52:52 -04:00
# initd
2022-11-10 09:26:45 -05:00
if os.path.exists('/etc/rc.d/init.d'):
initd_bin = '/etc/rc.d/init.d/mw'
2018-12-27 22:31:08 -05:00
if not os.path.exists(initd_bin):
2019-01-19 02:20:51 -05:00
import shutil
2018-12-27 22:31:08 -05:00
shutil.copyfile(script_bin, initd_bin)
2020-07-10 03:57:25 -04:00
mw.execShell('chmod +x ' + initd_bin)
2022-11-19 23:06:51 -05:00
# 加入自启动
2022-06-17 03:52:52 -04:00
mw.execShell('which chkconfig && chkconfig --add mw')
2022-11-19 23:06:24 -05:00
if os.path.exists('/etc/init.d'):
initd_bin = '/etc/init.d/mw'
if not os.path.exists(initd_bin):
import shutil
shutil.copyfile(script_bin, initd_bin)
mw.execShell('chmod +x ' + initd_bin)
2022-11-19 23:07:51 -05:00
# 加入自启动
2022-06-17 06:06:23 -04:00
mw.execShell('which update-rc.d && update-rc.d -f mw defaults')
2019-01-12 08:29:07 -05:00
2022-06-25 07:14:19 -04:00
# 获取系统IPV4
mw.setHostAddr(mw.getLocalIp())
2019-01-12 08:29:07 -05:00
2022-11-16 01:50:49 -05:00
def initInitTask():
# 创建证书同步命令
import cert_api
api = cert_api.cert_api()
api.createCertCron()
2019-01-12 08:29:07 -05:00
def initUserInfo():
2020-07-10 03:57:25 -04:00
data = mw.M('users').where('id=?', (1,)).getField('password')
2019-01-15 02:38:00 -05:00
if data == '21232f297a57a5a743894a0e4a801fc3':
2023-09-07 01:36:54 -04:00
2020-07-10 03:57:25 -04:00
pwd = mw.getRandomString(8).lower()
file_pw = mw.getRunDir() + '/data/default.pl'
mw.writeFile(file_pw, pwd)
mw.M('users').where('id=?', (1,)).setField(
'password', mw.md5(pwd))