Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 02c0e8c

Browse files
committed
add gameops.py
1 parent b820b16 commit 02c0e8c

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121

2222
* 远程执行命令、远程添加信任、远程自动分区挂盘:
2323
[*ssh_cmd.py*](https://github.com/honglongwei/python-scripts/blob/master/ssh_cmd.py)
24+
25+
* 基于saltstack的nested输出的运维脚本:
26+
[*gameops.py*](https://github.com/honglongwei/python-scripts/blob/master/gameops.py)

gameops.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf8 -*-
3+
4+
__author__ = 'honglongwei'
5+
6+
import os
7+
import sys
8+
import time
9+
import zipfile
10+
import datetime
11+
import subprocess
12+
import salt.client
13+
from salt.output.nested import NestDisplay
14+
from salt.utils import get_colors
15+
16+
#script path
17+
sct_lt = {'start': '/home/game/startup.sh',
18+
'stop': '/home/game/shutdown.sh'
19+
}
20+
#pid path
21+
ser_pid = '/home/game/game.pid'
22+
#src update package path
23+
src_path = '/home/update'
24+
#des update package path
25+
des_path = '/home'
26+
#src backup package path
27+
bsrc_path_lt = ['/home/game/config',
28+
'/home/game/data',
29+
'/home/game/hibernate']
30+
#des backup package path
31+
bdes_path = '/home/backup'
32+
33+
34+
# call salt output class
35+
class NestPut(NestDisplay):
36+
def __init__(self):
37+
self.colors = get_colors(True)
38+
self.__dict__.update(get_colors(True))
39+
self.strip_colors = True
40+
41+
def Prest(data):
42+
'''
43+
Display ret data
44+
'''
45+
nest = NestPut()
46+
print '\n'.join(nest.display(data, 0, '', []))
47+
48+
49+
def start():
50+
'''
51+
Define func start server
52+
'''
53+
if os.path.exists(ser_pid):
54+
return Prest('GameServer is already running !!!')
55+
else:
56+
cmd = sct_lt['start']
57+
proc = subprocess.Popen(cmd, shell=True)
58+
return Prest('Start GameServer is successful !!!')
59+
60+
61+
def stop():
62+
'''
63+
Define func stop server
64+
'''
65+
if os.path.exists(ser_pid):
66+
cmd = sct_lt['stop']
67+
proc = subprocess.Popen(cmd, shell=True)
68+
return Prest('Stop GameServer is successful !!!')
69+
else:
70+
return Prest('GameServer is already stopped !!!')
71+
72+
73+
def status():
74+
'''
75+
Define func status server
76+
'''
77+
if os.path.exists(ser_pid):
78+
return Prest('GameServer is not running !!!')
79+
else:
80+
cmd = 'ps -ef|grep \'{0}\'|grep -v grep'.format('server')
81+
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
82+
ret = proc.stdout.read()
83+
return Prest(ret)
84+
85+
86+
def backup():
87+
'''
88+
Define func backup server
89+
'''
90+
bakname = 'gs_' + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + '.zip'
91+
zipname = os.path.join(bdes_path, bakname)
92+
f = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
93+
for bsrc_path in bsrc_path_lt:
94+
bac_path = os.path.dirname(bsrc_path)
95+
ls_path = bac_path + '/'
96+
zg_path = bsrc_path.split(ls_path)[1]
97+
os.chdir(bac_path)
98+
for dirpath, dirnames, filenames in os.walk(zg_path):
99+
for filename in filenames:
100+
f.write(os.path.join(dirpath, filename))
101+
f.close()
102+
return 'Backup is successful !'
103+
104+
105+
def update(pkg):
106+
'''
107+
Define func update server
108+
'''
109+
if pkg:
110+
fl = os.path.join(src_path, pkg)
111+
try:
112+
zfile = zipfile.ZipFile(fl,'r')
113+
for filename in zfile.namelist():
114+
zfile.extract(filename, des_path)
115+
return 'Update is successful !'
116+
except IOError:
117+
return 'The package is invalid !!!'
118+
else:
119+
return 'The package is invalid !!!'
120+
121+
122+
123+
if __name__== "__main__":
124+
# check arguments
125+
opts = sys.argv
126+
if len(opts) < 2:
127+
print 'start|stop|status|update'
128+
sys.exit(0)
129+
elif len(opts) == 2:
130+
if opts[1]=='start':
131+
start()
132+
elif opts[1]=='stop':
133+
stop()
134+
elif opts[1]=='status':
135+
status()
136+
elif opts[1]=='backup':
137+
backup()
138+
else:
139+
print 'Script Parameter Error !!!'
140+
elif len(opts) == 3:
141+
if opts[1]=='update':
142+
update(opts[2])
143+
else:
144+
print 'Script Parameter Error !!!'
145+
else:
146+
print 'Script Parameter Error !!!'

0 commit comments

Comments
 (0)