forked from nooodw/ServerStatus-Toyo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autodeploy.py
246 lines (225 loc) · 8.33 KB
/
autodeploy.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# -*- coding: utf-8 -*-
# Author : https://github.com/tenyue/ServerStatus
# ServerStatus 自动部署脚本
import os
import sys
import json
import datetime
import hashlib
# 全新部署服务端_客户端
def NewDeploy():
os.system('yum -y install expect')
os.system('apt-get -y install expect')
os.system('clear')
webdir = ''
while True:
print 'Please input the website path for CloudMonitor(Eg: /home/wwwroot/default):'
webdir = raw_input('the website path is:')
if os.path.exists(webdir) == True:
break
else:
print 'the website path is\'t exist!'
if webdir[-1] == '/':
webdir = webdir[:-1]
else:
webdir = webdir
server_ipstr = raw_input('the server\'s ip address:')
count = 0
config_list = []
while True:
count += 1
print 'Deploy the %s client for CloudMonitor: ' % count
client_ipstr = raw_input('the client\'s ip address: ')
rootstr = raw_input('the client\'s root: ')
passwdstr = raw_input('the client\'s password: ')
name = raw_input('the client\'s name: ')
type = raw_input('the client\'s type: ')
host = raw_input('the client\'s host: ')
location = raw_input('the client\'s location: ')
xstr = ('CloudMonitor%s' + client_ipstr + rootstr + passwdstr+ str(datetime.datetime.now()))% count
hash_user = 'CloudMonitor%s' % count
hash_passwd = hashlib.md5(xstr).hexdigest()
xjson = {
"username": hash_user,
"name": name,
"type": type,
"host": host,
"location": location,
"password": hash_passwd,
}
config_list.append(xjson)
new_config_str = ''
# 配置服务端
mvcomment = 'mv web/* %s' % webdir
os.system(mvcomment)
# 读取客户端配置
with open('client/client.py','r') as f:
for line in f.readlines():
if 'SERVER = "127.0.0.1"' in line:
new_config_str = new_config_str + ('SERVER = "%s"') % server_ipstr + '\n'
elif 'USER = "USER"' in line:
new_config_str = new_config_str + ('USER = "%s"') % hash_user + '\n'
elif 'PASSWORD = "PASSWORD"' in line:
new_config_str = new_config_str + ('PASSWORD = "%s"') % hash_passwd + '\n'
else:
new_config_str = new_config_str + line
# 写入临时客户端配置
with open('client/client-temp.py','wt') as f:
f.write(new_config_str)
# 上传配置到客户机,并执行脚本
scp_put('client/client-temp.py', '/root/', client_ipstr, rootstr, passwdstr)
# 清理临时客户端配置
os.remove('client/client-temp.py')
is_add = raw_input('Whether to continue to add the client(y/n): ')
if is_add == 'n' or is_add == 'N' or is_add == 'not' or is_add == 'NOT':
break
config_dict = {"servers": config_list}
config_json = json.dumps(config_dict)
with open('config.json','wt') as f:
f.write(config_json)
# 启动主服务
os.chdir('server')
os.system('make')
os.chdir('..')
if os.path.exists('server/sergate') == True:
os.system('mv server/sergate %s' % webdir)
os.system('mv config.json %s' % webdir)
task_sergate = 'nohup %s/sergate --config=%s/config.json --web-dir=%s &> /dev/null &' % (webdir, webdir, webdir)
# 立即启动服务
os.system(task_sergate)
print 'Successed start CloudMonitor!'
else:
print 'Failed start CloudMonitor!'
# 增加客户端
def AddClient():
while True:
print 'Please input the website path for CloudMonitor(Eg: /home/wwwroot/default):'
webdir = raw_input('the website path is:')
if os.path.exists(webdir) == True:
break
else:
print 'the website path is\'t exist!'
if webdir[-1] == '/':
webdir = webdir[:-1]
else:
webdir = webdir
count = 0
with open('%s/config.json' % webdir, 'rt') as f:
old_config = json.loads(f)
for i in old_config["servers"]:
tempvalue = int(i["username"].split('CloudMonitor')[1])
if tempvalue > count:
count = tempvalue
server_ipstr = raw_input('the server\'s ip address:')
while True:
count += 1
print 'Add the %s client for CloudMonitor: ' % count
client_ipstr = raw_input('the client\'s ip address: ')
rootstr = raw_input('the client\'s root: ')
passwdstr = raw_input('the client\'s password: ')
name = raw_input('the client\'s name: ')
type = raw_input('the client\'s type: ')
host = raw_input('the client\'s host: ')
location = raw_input('the client\'s location: ')
xstr = ('CloudMonitor%s' + client_ipstr + rootstr + passwdstr + str(datetime.datetime.now())) % count
hash_user = 'CloudMonitor%s' % count
hash_passwd = hashlib.md5(xstr).hexdigest()
xjson = {
"username": hash_user,
"name": name,
"type": type,
"host": host,
"location": location,
"password": hash_passwd,
}
old_config["servers"].append(xjson)
new_config_str = ''
# 读取客户端配置
with open('client/client.py', 'r') as f:
for line in f.readlines():
if 'SERVER = "127.0.0.1"' in line:
new_config_str = new_config_str + ('SERVER = "%s"') % server_ipstr + '\n'
elif 'USER = "USER"' in line:
new_config_str = new_config_str + ('USER = "%s"') % hash_user + '\n'
elif 'PASSWORD = "PASSWORD"' in line:
new_config_str = new_config_str + ('PASSWORD = "%s"') % hash_passwd + '\n'
else:
new_config_str = new_config_str + line
# 写入临时客户端配置
with open('client/client-temp.py', 'wt') as f:
f.write(new_config_str)
# 上传配置到客户机,并执行脚本
scp_put('client/client-temp.py', '/root/', client_ipstr, rootstr, passwdstr)
# 清理临时客户端配置
os.remove('client/client-temp.py')
is_add = raw_input('Whether to continue to add the client(y/n): ')
if is_add == 'n' or is_add == 'N' or is_add == 'not' or is_add == 'NOT':
break
with open('%s/config.json' % webdir, 'wt') as f:
f.write(json.dumps(old_config))
def scp_put(local_path, remote_path, host, root, passwd):
scp_put = '''
set timeout -1
spawn scp %s %s@%s:%s
expect "(yes/no)?" {
send "yes\r"
expect "password:"
send "%s\r"
} "password:" {send "%s\r"}
expect eof
exit'''
os.system("echo '%s' > scp_put.cmd" % (
scp_put % (os.path.expanduser(local_path), root, host, remote_path, passwd, passwd)))
os.system('expect scp_put.cmd')
os.system('rm scp_put.cmd')
shell_exe = '''
set timeout -1
spawn ssh %s@%s
expect "(yes/no)?" {
send "yes\r"
expect "password:"
send "%s\r"
} "password:" {send "%s\r"}
expect "#"
send "mv /root/client-temp.py /root/client.py\r"
expect "#"
send "yum -y install epel-release\r"
expect "#"
send "yum -y install python-pip\r"
expect "#"
send "yum clean all\r"
expect "#"
send "yum -y install gcc\r"
expect "#"
send "yum -y install python-devel\r"
expect "#"
send "pip install psutil\r"
expect "#"
send "apt-get -y install python-setuptools python-dev build-essential\r"
expect "#"
send "apt-get -y install python-pip\r"
expect "#"
send "pip install psutil\r"
expect "#"
send "nohup python /root/client.py &> /dev/null &\r"
expect "#"
send "exit\r"
expect eof
exit'''
os.system("echo '%s' > shell_exe.cmd" % (
shell_exe % (root, host, passwd, passwd)))
os.system('expect shell_exe.cmd')
os.system('rm shell_exe.cmd')
if __name__ == '__main__':
print '******* CloudMonitor ********'
print '* 1, Install CloudMonitor *'
print '* 2, Add Client *'
print '*****************************'
sel = raw_input("input number: ")
if sel not in ['1', '2']:
print 'error input!'
exit(1)
elif sel == '1':
NewDeploy()
elif sel == '2':
AddClient()