-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathbuild.py
125 lines (109 loc) · 3.68 KB
/
build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Created by zhouwang on 2018/6/9.
import os
from settings import MYSQL_DB
import subprocess
import random
import string
import hashlib
import time
NEW_SUPERADMIN = False
def tools():
print('Step1: Install Tools')
command = 'yum install -y openldap openldap-devel'
print('-->', command)
status = os.system(command)
if status != 0:
exit()
print('Step1: End.\n')
def python_packages():
print('Step2: Install Python Packages')
command = 'pip3 install -r requirements.txt'
print('-->', command)
status = os.system(command)
if status != 0:
exit()
print('Step2: End.\n')
def mysql_db():
print('Step3: Build Mysql Database')
command = 'mysql -h%s -P%d -u%s -p%s -e \'CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARSET utf8 COLLATE utf8_general_ci;\'' \
% (MYSQL_DB['host'], MYSQL_DB['port'], MYSQL_DB['user'], MYSQL_DB['password'], MYSQL_DB['db'])
print('-->', command)
status = os.system(command)
if status != 0:
exit()
print('Step3: End.\n')
def mysql_tables():
print('Step4: Build MySQL Tables:')
command = 'mysql -h%s -P%d -u%s -p%s loggrove < tables.sql' % \
(MYSQL_DB['host'], MYSQL_DB['port'], MYSQL_DB['user'], MYSQL_DB['password'])
print('-->', command)
status = os.system(command)
if status != 0:
exit()
print('Step4: End.\n')
def loggrove_admin():
global NEW_SUPERADMIN
print('Step5: Create Loggrove Superadmin')
status, output = subprocess.getstatusoutput(
'mysql -h%s -P%d -u%s -p%s loggrove -e \'select "Existence of superadmin" from user where username="admin"\'' %
(MYSQL_DB['host'], MYSQL_DB['port'], MYSQL_DB['user'], MYSQL_DB['password']))
if status != 0:
print(output)
exit()
if output.find('Existence of superadmin') < 0:
salt = ''.join(random.sample(string.ascii_letters, 8))
password = '%s%s' % (salt, hashlib.md5((salt + 'loggrove').encode('UTF-8')).hexdigest())
sqltext = '''
INSERT INTO user (username, password, fullname, email, join_time, status, role)
VALUES ("admin", "%s", "Admin", "[email protected]", now(), "1", "1");
''' % password
command = 'mysql -h%s -P%d -u%s -p%s loggrove -e \'%s\'' % \
(MYSQL_DB['host'], MYSQL_DB['port'], MYSQL_DB['user'], MYSQL_DB['password'], sqltext)
print('-->', command)
status = os.system(command)
if status != 0:
exit()
NEW_SUPERADMIN = True
else:
print('Existence of Superadmin, Skip.\n')
print('Step5: End.\n')
def main():
print('### Loggrove Build ###')
print('''
要求:
1: 已安装 Python36、PIP3、MySQL57,并保证 python3、pip3、mysql、yum 命令可用;
2: 已完成 settings.py > MYSQL_DB host、port、user、password, SSH username、password、port 等配置;
步骤:
1: 安装依赖工具(yum)
2: 安装Python包 (pip3)
3: 创建MySQL db,若存在则不进行创建动作
4: 创建or更新 MySQL tables 结构
5: 创建超级管理员,若存在则不进行创建动作
''')
while True:
put = input('开始(y/n):')
if put == 'y':
break
if put == 'n':
exit()
print('\n')
print('>>> Begin:\n')
tools()
python_packages()
mysql_db()
mysql_tables()
loggrove_admin()
print('End and successful. <<< \n')
if NEW_SUPERADMIN:
print(
'''
# # # # Super Admin # # # #
# #
# username: admin #
# password: loggrove #
# #
# # # # # # # # # # # # # #
'''
)
if __name__ == '__main__':
main()