-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvolcano.py
More file actions
217 lines (194 loc) · 5.46 KB
/
volcano.py
File metadata and controls
217 lines (194 loc) · 5.46 KB
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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author : firefoxbug
# E-Mail : wanghuafire@gmail.com
# Blog : www.firefoxbug.net
import os
import sys
import signal
from put_file import PutFile
from exec_cmd import ExecCMD
from put_dir import PutDIR
from report_log import ReportLog
import rlcompleter, readline
class GetConf(object):
@classmethod
def read_local_conf(self):
conf_file = "./conf/volcano.conf"
tmp_conf_file = "./conf/volcano.conf.tmp"
if not os.path.exists(conf_file):
ReportLog.sys_info("%s does not exists !"%(conf_file))
cmd = "sed \'/^$/D\' %s | grep -v \"^[[:blank:]]*#\" > %s"%(conf_file,tmp_conf_file)
os.system(cmd)
fd = open(tmp_conf_file)
self.hosts_list = []
while True:
hosts = {}
line = fd.readline()
if not line :
break
hosts['ip'] = line.split()[0]
hosts['passwd'] = line.split()[1]
hosts['port'] = 22
hosts['username'] = "root"
# put_dir_test(hosts)
exec_cmd_test(hosts)
self.hosts_list.append(hosts)
fd.close()
os.system("rm -f %s"%tmp_conf_file)
return self.hosts_list
def read_mysql_conf(self):
pass;
class Volcano(object):
"""docstring for Volcano"""
def __init__(self):
self.fun_map = {1:ExecCMD.exec_cmd,2:PutFile.put_file,3:PutDIR.put_dir}
# self.print_choose()
# self.job_call = {}
self.hosts_list = []
def submit_job(self):
self.read_local_conf()
self.get_choose()
def get_choose(self):
while True:
# '''execute commands'''
resultes = dict(OK=[],NO=[])
self.print_choose()
opt = raw_input("Please input choice : ").strip()
if opt == "1":
cmd2exec = self.get_execute2cmds()
if not cmd2exec:
# print "Error..."
continue
for hosts in self.hosts_list:
hosts['cmd2exec'] = cmd2exec
if ExecCMD.exec_cmd(hosts):
resultes["OK"].append(hosts['ip'])
else:
resultes["NO"].append(hosts['ip'])
# '''transfer file'''
elif opt == "2":
(local_file_path,remote_file_path) = self.get_file2transfer()
for hosts in self.hosts_list:
hosts['file2put'] = local_file_path
hosts['file2save'] = remote_file_path
if PutFile.put_file(hosts):
resultes["OK"].append(hosts['ip'])
else:
resultes["NO"].append(hosts['ip'])
# '''transfer dictory'''
elif opt == "3":
(local_dir_path,remote_dir_path) = self.get_dir2transfer()
for hosts in self.hosts_list:
hosts['dir2put'] = local_dir_path
hosts['dir2save'] = remote_dir_path
if PutDIR.put_dir(hosts):
resultes["OK"].append(hosts['ip'])
else:
resultes["NO"].append(hosts['ip'])
elif opt == "4":
pass
else :
continue
self.print_results(resultes)
# self.job_call = dict(func=self.fun_map[opt])
def print_results(self,resultes):
print "Final resulte :"
print "======Successfully======"
for ip in resultes['OK']:
print ip
print "========Failed=========="
for ip in resultes['NO']:
print ip
print
def print_choose(self):
print '''\
[1]. Excetue commands on remote nodes
[2]. Transmission file form localhost to remote nodes
[3]. Transmission dictory form localhost to remote nodes
[4]. Download file from remote node to localhost
'''
def get_execute2cmds(self):
while True:
cmd2exec = raw_input("Please input commands : ")
if cmd2exec :
break
return cmd2exec
def get_file2transfer(self):
remote_file_path=''
local_file_path=''
local_file_path = raw_input("Please input local path : ")
if not os.path.isfile(local_file_path):
ReportLog.sys_info("%s does not exists !"%(local_file_path))
sys.exit(1)
remote_file_path = raw_input("Please input remote path : ")
return (local_file_path,remote_file_path)
def get_dir2transfer(self):
remote_dir_path=''
local_dir_path=''
local_dir_path = raw_input("Please input local path : ")
if not os.path.isdir(local_dir_path):
ReportLog.sys_info("%s does not exists !"%(local_dir_path))
sys.exit(1)
remote_dir_path = raw_input("Please input remote path : ")
return (local_dir_path,remote_dir_path)
def read_local_conf(self):
conf_file = "./conf/volcano.conf"
tmp_conf_file = "./conf/volcano.conf.tmp"
if not os.path.exists(conf_file):
ReportLog.sys_info("%s does not exists !"%(conf_file))
cmd = "sed \'/^$/D\' %s | grep -v \"^[[:blank:]]*#\" > %s"%(conf_file,tmp_conf_file)
os.system(cmd)
fd = open(tmp_conf_file)
while True:
hosts = {}
line = fd.readline()
if not line :
break
hosts['ip'] = line.split()[0]
hosts['passwd'] = line.split()[1]
hosts['port'] = 22
hosts['username'] = "root"
self.hosts_list.append(hosts)
fd.close()
os.system("rm -f %s"%tmp_conf_file)
# print self.hosts_list
# return self.hosts_list
class Watcher:
def __init__(self):
""" Creates a child thread, which returns. The parent
thread waits for a KeyboardInterrupt and then kills
the child thread.
"""
self.child = os.fork()
if self.child == 0:
return
else:
self.watch()
def watch(self):
try:
os.wait()
except KeyboardInterrupt:
print ' exit...'
self.kill()
sys.exit()
def kill(self):
try:
os.kill(self.child, signal.SIGKILL)
except OSError:
pass
def put_dir_test(hosts):
hosts['dir2put'] = "/home/firefoxbug/server/OpenCDN2/node/Tengine/conf"
hosts['dir2save'] = "/usr/local/nginx"
PutDIR.put_dir(hosts)
def exec_cmd_test(hosts):
hosts['cmd2exec'] = "cp -raf /usr/local/nginx /usr/local/nginx2"
ExecCMD.exec_cmd(hosts)
def main():
readline.parse_and_bind('tab: complete')
vol = Volcano()
vol.submit_job()
pass;
if __name__ == '__main__':
w = Watcher()
main()