-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
executable file
·201 lines (147 loc) · 4.39 KB
/
index.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
# coding:utf-8
import sys
import io
import os
import time
sys.path.append(os.getcwd() + "/class/core")
import mw
app_debug = False
if mw.isAppleSystem():
app_debug = True
def getPluginName():
return 'gerapy'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
def getInitDTpl():
path = getPluginDir() + "/scripts/init.d/" + getPluginName() + ".tpl"
return path
def getInitDScrapydTpl():
path = getPluginDir() + "/scripts/init.d/scrapyd.tpl"
return path
def getArgs():
args = sys.argv[2:]
tmp = {}
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
tmp[t[0]] = t[1]
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
return tmp
def status():
data = mw.execShell(
"ps -ef|grep gerapy |grep -v grep | grep -v mdserver-web | awk '{print $2}'")
if data[0] == '':
return 'stop'
return 'start'
def initDreplace():
file_tpl = getInitDTpl()
service_path = os.path.dirname(os.getcwd())
initD_path = getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
file_bin = initD_path + '/' + getPluginName()
# initd replace
content = mw.readFile(file_tpl)
content = content.replace('{$SERVER_PATH}', service_path)
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
file_tpl = getInitDScrapydTpl()
file_bin_scr = initD_path + '/scrapyd'
content = mw.readFile(file_tpl)
content = content.replace('{$SERVER_PATH}', service_path)
mw.writeFile(file_bin_scr, content)
mw.execShell('chmod +x ' + file_bin_scr)
return file_bin, file_bin_scr
def start():
file, file_scr = initDreplace()
data = mw.execShell(file + ' start')
mw.execShell(file_scr + ' start')
if data[1] == '':
return 'ok'
return 'fail'
def stop():
file, file_scr = initDreplace()
data = mw.execShell(file + ' stop')
mw.execShell(file_scr + ' stop')
if data[1] == '':
return 'ok'
return 'fail'
def reload():
file, file_scr = initDreplace()
data = mw.execShell(file + ' reload')
mw.execShell(file_scr + ' reload')
if data[1] == '':
return 'ok'
return 'fail'
def initdStatus():
if not app_debug:
if mw.isAppleSystem():
return "Apple Computer does not support"
initd_bin = getInitDFile()
if not os.path.exists(initd_bin):
return 'fail'
if not os.path.exists("/etc/init.d/scrapyd"):
return 'fail'
return 'ok'
def initdInstall():
import shutil
if not app_debug:
if mw.isAppleSystem():
return "Apple Computer does not support"
source_bin = initDreplace()
initd_bin = getInitDFile()
shutil.copyfile(source_bin[0], initd_bin)
mw.execShell('chmod +x ' + initd_bin)
mw.execShell('chkconfig --add ' + getPluginName())
shutil.copyfile("/www/server/gerapy/init.d/scrapyd", "/etc/init.d/scrapyd")
mw.execShell("chmod +x /etc/init.d/scrapyd")
mw.execShell('chkconfig --add scrapyd')
return 'ok'
def initdUinstall():
if not app_debug:
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('chkconfig --del ' + getPluginName())
initd_bin = getInitDFile()
os.remove(initd_bin)
mw.execShell('chkconfig --del scrapyd')
os.remove("/etc/init.d/scrapyd")
return 'ok'
def runLog():
return getServerDir() + '/logs.pl'
def scrLog():
return getServerDir() + '/scrapyd.pl'
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
print(status())
elif func == 'start':
print(start())
elif func == 'stop':
print(stop())
elif func == 'restart':
print(reload())
elif func == 'reload':
print(reload())
elif func == 'initd_status':
print(initdStatus())
elif func == 'initd_install':
print(initdInstall())
elif func == 'initd_uninstall':
print(initdUinstall())
elif func == 'run_log':
print(runLog())
elif func == 'scr_log':
print(scrLog())
else:
print('error')