8
8
import time
9
9
import zipfile
10
10
import datetime
11
+ import getpass
12
+ import logging
11
13
import subprocess
12
14
from subprocess import Popen
13
15
14
16
15
17
#src update package path
16
- src_path = '/tmp/backup'
18
+ src_path = '/home/mrdTomcat/update'
19
+
17
20
#des update package path
18
- des_path = {'tomcat' : '/tmp/222' ,
19
- 'apache' : '/tmp' ,
20
- 'websocket' : '/tmp' }
21
+ des_path = {'webap' : '/home/app/webap/tomcat/webapps' ,
22
+ 'apache' : '/usr/local/apache' ,
23
+ 'websocket' : '/home/app/websocket/webapps' ,
24
+ 'batch' : '/home/app/tomcat/webapps' ,
25
+ 'memcache' : '/home/app/memcached' ,
26
+ 'sdk' : '/home/app/sdk/tomcat/webapps' }
27
+
21
28
#src backup package path
22
29
bsrc_path_lt = {'tomcat' : ['/tmp/2' , '/tmp/1' ],
23
- 'apache' : ['/tmp ' ],
30
+ 'apache' : ['/usr/local/apache2/conf ' ],
24
31
'websocket' : ['/tmp' ]}
32
+
25
33
#des backup package path
26
- bdes_path = '/tmp/backup'
34
+ bdes_path = '/home/mrdTomcat/version_bak'
35
+
27
36
#service name and server start bin
28
- srv_up = {'tomcat' : '/home/mrdTcomat/app/tomcat/bin/start.sh' ,
29
- 'apache' : '' ,
30
- 'websocket' : '' }
37
+ srv_up = {'webap' : '/home/app/webap/tomcat/bin/startup.sh' ,
38
+ 'apache' : '/usr/local/apache/bin/apachectl start' ,
39
+ 'websocket' : '/home/app/websocket/bin/jetty.sh start' ,
40
+ 'batch' : '/home/app/tomcat/bin/startup.sh' ,
41
+ 'memcache' : '/home/app/memcached/bin/start.sh' ,
42
+ 'sdk' : '/home/app/tomcat/bin/startup.sh' }
43
+
31
44
#service name and server stop bin
32
- srv_down = {'tomcat' : '/home/mrdTcomat/app/tomcat/bin/shutdown.sh' ,
33
- 'apache' : '' ,
34
- 'websocket' : '' }
45
+ srv_down = {'webap' : '/home/app/tomcat/bin/shutdown.sh' ,
46
+ 'apache' : '/usr/local/apache/bin/apachectl stop' ,
47
+ 'websocket' : '/home/app/websocket/bin/jetty.sh stop' ,
48
+ 'batch' : '/home/app/tomcat/bin/shutdown.sh' ,
49
+ 'memcache' : '/home/app/memcached/bin/stop.sh' ,
50
+ 'sdk' : '/home/app/tomcat/bin/shutdown.sh' }
51
+
52
+ #server pidfile path
53
+ srv_pidfile = {'webap' : '/var/run/webap/webap.pid' ,
54
+ 'apache' : '' ,
55
+ 'websocket' : '' ,
56
+ 'batch' : '' ,
57
+ 'memcache' : '' ,
58
+ 'sdk' : '' }
35
59
36
60
37
61
#change return color
@@ -50,9 +74,14 @@ def start(ServiceName):
50
74
CLI Example:
51
75
opsmod.py ServiceName start
52
76
'''
77
+ pid = srv_pidfile [ServiceName ]
53
78
cmd = srv_up [ServiceName ]
54
- proc = Popen (cmd , shell = True )
55
- return G ('Start GameServer is successful !' )
79
+ logging .info ('{0} start' .format (ServiceName ))
80
+ if os .path .exists (pid ):
81
+ return R ('GameServer is already running !' )
82
+ else :
83
+ proc = Popen (cmd , shell = True )
84
+ return G ('Start GameServer is successful !' )
56
85
57
86
58
87
def stop (ServiceName ):
@@ -62,9 +91,14 @@ def stop(ServiceName):
62
91
CLI Example:
63
92
opsmod.py ServiceName stop
64
93
'''
94
+ pid = srv_pidfile [ServiceName ]
65
95
cmd = srv_down [ServiceName ]
66
- proc = Popen (cmd , shell = True )
67
- return G ('Stop GameServer is running...,please wait !' )
96
+ logging .info ('{0} stop' .format (ServiceName ))
97
+ if os .path .exists (pid ):
98
+ proc = Popen (cmd , shell = True )
99
+ return G ('Stop GameServer is running...,please wait !' )
100
+ else :
101
+ return R ('GameServer is already stopped !' )
68
102
69
103
70
104
def status (ServiceName ):
@@ -74,11 +108,13 @@ def status(ServiceName):
74
108
CLI Example:
75
109
opsmod.py ServiceName status
76
110
'''
77
- cmd = 'ps -ef|grep {0}|grep -v grep' .format (ServiceName )
111
+ cmd = 'ps -ef|grep " {0}" |grep -v grep' .format (ServiceName )
78
112
proc = Popen (cmd , stdout = subprocess .PIPE , shell = True )
79
- item = proc .stdout .read ()
80
- cot = len (item .split ('\n ' )) - int (1 )
81
- ret = item + '\n ' + '*' * 80 + '\n ' + 'The total of process is {0} !' .format (cot )
113
+ item = proc .stdout .read ().split ('\n ' )[:- 2 ]
114
+ its = '\n ' .join (item )
115
+ cot = len (item )
116
+ ret = its + '\n ' + '*' * 80 + '\n ' + 'The total of process is {0} !' .format (cot )
117
+ logging .info ('{0} status' .format (ServiceName ))
82
118
return G (ret )
83
119
84
120
@@ -89,6 +125,7 @@ def update(ServiceName, Pkg):
89
125
CLI Example:
90
126
opsmod.py ServiceName update Pkg
91
127
'''
128
+ logging .info ('{0} update {1}' .format (ServiceName , Pkg ))
92
129
if Pkg :
93
130
fl = os .path .join (src_path , Pkg )
94
131
try :
@@ -109,6 +146,7 @@ def backup(ServiceName):
109
146
CLI Example:
110
147
opsmod.py ServiceName backup
111
148
'''
149
+ logging .info ('{0} backup' .format (ServiceName ))
112
150
bakname = ServiceName + '_' + datetime .datetime .now ().strftime ('%Y%m%d%H%M%S' ) + '.zip'
113
151
zipname = os .path .join (bdes_path , bakname )
114
152
f = zipfile .ZipFile (zipname , 'w' , zipfile .ZIP_DEFLATED )
@@ -125,6 +163,17 @@ def backup(ServiceName):
125
163
126
164
127
165
if __name__ == "__main__" :
166
+ if os .path .exists ('./logs' ):
167
+ pass
168
+ else :
169
+ os .makedirs ('./logs' )
170
+ log_ft = datetime .datetime .now ().strftime ('%Y-%m-%d-%H' )
171
+ user_cmd = getpass .getuser ()
172
+ logging .basicConfig (level = logging .DEBUG ,
173
+ format = '%(asctime)s {0} %(levelname)s: %(message)s' .format (user_cmd ),
174
+ datefmt = '%Y-%m-%d %H:%M:%S' ,
175
+ filename = './logs/control{0}.log' .format (log_ft ),
176
+ filemode = 'a' )
128
177
opts = sys .argv
129
178
try :
130
179
if opts [1 ]== '-d' or opts [1 ]== '--help' :
@@ -147,3 +196,4 @@ def backup(ServiceName):
147
196
print R ('Script Parameter Error !!!' )
148
197
except IndexError :
149
198
print R ('Script Parameter Error !!!' )
199
+
0 commit comments