python运维脚本_运维通用python简易脚本

1.[代码]运维通用python简易脚本

#!/usr/bin/env python

# -*- coding: utf-8 -*-

__author__ = 'honglongwei'

import os

import sys

import time

import zipfile

import datetime

import getpass

import logging

import subprocess

from subprocess import Popen

#src update package path

src_path = '/home/mrdTomcat/update'

#des update package path

des_path = {'webap': '/home/app/webap/tomcat/webapps',

'apache': '/usr/local/apache',

'websocket': '/home/app/websocket/webapps',

'batch': '/home/app/tomcat/webapps',

'memcache': '/home/app/memcached',

'sdk': '/home/app/sdk/tomcat/webapps'}

#src backup package path

bsrc_path_lt = {'tomcat': ['/tmp/2', '/tmp/1'],

'apache': ['/usr/local/apache2/conf'],

'websocket': ['/tmp']}

#des backup package path

bdes_path = '/home/mrdTomcat/version_bak'

#service name and server start bin

srv_up = {'webap': '/home/app/webap/tomcat/bin/startup.sh',

'apache': '/usr/local/apache/bin/apachectl start',

'websocket': '/home/app/websocket/bin/jetty.sh start',

'batch': '/home/app/tomcat/bin/startup.sh',

'memcache': '/home/app/memcached/bin/start.sh',

'sdk': '/home/app/tomcat/bin/startup.sh'}

#service name and server stop bin

srv_down = {'webap': '/home/app/tomcat/bin/shutdown.sh',

'apache': '/usr/local/apache/bin/apachectl stop',

'websocket': '/home/app/websocket/bin/jetty.sh stop',

'batch': '/home/app/tomcat/bin/shutdown.sh',

'memcache': '/home/app/memcached/bin/stop.sh',

'sdk': '/home/app/tomcat/bin/shutdown.sh'}

#server pidfile path

srv_pidfile = {'webap': '/var/run/webap/webap.pid',

'apache': '',

'websocket': '',

'batch': '',

'memcache': '',

'sdk': ''}

#change return color

def G(s):

return "%s[32;2m%s%s[0m"%(chr(27), s, chr(27))

def A(s):

return "%s[36;2m%s%s[0m"%(chr(27), s, chr(27))

def R(s):

return "%s[31;2m%s%s[0m"%(chr(27), s, chr(27))

def start(ServiceName):

'''

Desc: Start GameServer

CLI Example:

czqstmod.py ServiceName start

'''

pid = srv_pidfile[ServiceName]

cmd = srv_up[ServiceName]

logging.info('{0} start'.format(ServiceName))

if os.path.exists(pid):

return R('GameServer is already running !')

else:

proc = Popen(cmd, shell=True)

return G('Start GameServer is successful !')

def stop(ServiceName):

'''

Desc: Stop GameServer

CLI Example:

czqstmod.py ServiceName stop

'''

pid = srv_pidfile[ServiceName]

cmd = srv_down[ServiceName]

logging.info('{0} stop'.format(ServiceName))

if os.path.exists(pid):

proc = Popen(cmd, shell=True)

return G('Stop GameServer is running...,please wait !')

else:

return R('GameServer is already stopped !')

def status(ServiceName):

'''

Desc: Check GameServer Status

CLI Example:

czqstmod.py ServiceName status

'''

cmd = 'ps -ef|grep "{0}"|grep -v grep'.format(ServiceName)

proc = Popen(cmd, stdout=subprocess.PIPE, shell=True)

item = proc.stdout.read().split('\n')[:-2]

its = '\n'.join(item)

cot = len(item)

ret = its + '\n' + '*'*80 + '\n' + 'The total of process is {0} !'.format(cot)

logging.info('{0} status'.format(ServiceName))

return G(ret)

def update(ServiceName, Pkg):

'''

Desc: Update GameServer

CLI Example:

czqstmod.py ServiceName update Pkg

'''

logging.info('{0} update {1}'.format(ServiceName, Pkg))

if Pkg:

fl = os.path.join(src_path, Pkg)

try:

zfile = zipfile.ZipFile(fl,'r')

for filename in zfile.namelist():

zfile.extract(filename, des_path[ServiceName])

return G('Update is successful !')

except IOError:

return R('The package is invalid !!!')

else:

return R('The package is invalid !!!')

def backup(ServiceName):

'''

Desc: Backup GameServer

CLI Example:

czqstmod.py ServiceName backup

'''

logging.info('{0} backup'.format(ServiceName))

bakname = ServiceName + '_' + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + '.zip'

zipname = os.path.join(bdes_path, bakname)

f = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)

for bsrc_path in bsrc_path_lt[ServiceName]:

bac_path = os.path.dirname(bsrc_path)

ls_path = bac_path + '/'

zg_path = bsrc_path.split(ls_path)[1]

os.chdir(bac_path)

for dirpath, dirnames, filenames in os.walk(zg_path):

for filename in filenames:

f.write(os.path.join(dirpath, filename))

f.close()

return G('Backup is successful !')

if __name__== "__main__":

if os.path.exists('./logs'):

pass

else:

os.makedirs('./logs')

log_ft = datetime.datetime.now().strftime('%Y-%m-%d-%H')

user_cmd = getpass.getuser()

logging.basicConfig(level=logging.DEBUG,

format='%(asctime)s {0} %(levelname)s: %(message)s'.format(user_cmd),

datefmt='%Y-%m-%d %H:%M:%S',

filename='./logs/control{0}.log'.format(log_ft),

filemode='a')

opts = sys.argv

try:

if opts[1]=='-d' or opts[1]=='--help':

print G('start :') + R('{0}'.format(start.__doc__))

print G('stop :') + R('{0}'.format(stop.__doc__))

print G('status :') + R('{0}'.format(status.__doc__))

print G('update :') + R('{0}'.format(update.__doc__))

print G('backup :') + R('{0}'.format(backup.__doc__))

elif opts[2]=='start':

print start(opts[1])

elif opts[2]=='stop':

print stop(opts[1])

elif opts[2]=='status':

print status(opts[1])

elif opts[2]=='backup':

print backup(opts[1])

elif opts[2]=='update':

print update(opts[1], opts[3])

else:

print R('Script Parameter Error !!!')

except IndexError:

print R('Script Parameter Error !!!')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值