updatewebserver.py
#!/usr/bin/python
#2014-10-15 by jastme
from optparse import OptionParser
import paramiko
from twisted.internet import reactor, defer
import os,time
parser = OptionParser(usage="%prog",version="version %prog 1.0.0")
parser.add_option("-f", "--files",action="store", type="string", dest="files", help="give the file's name")
(options, args) = parser.parse_args()
def Update(hostname,username):
# paramiko.util.log_to_file('/tmp/update.log')
s=paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
s.connect(hostname=hostname,username=username)
stdin,stdout,stderr=s.exec_command('python /script/update.py %s' %options.files)
except Exception as e:
return '%s %s' %(hostname,e)
result=stdout.read()
s.close()
# return '%s %s' %(hostname,result.strip('\n'))
return '%s\n%s' %(hostname,result.strip('\n'))
def updatedf(hostname,username):
d=defer.Deferred()
reactor.callLater(1,d.callback,Update(hostname,username))
return d
def printData(x):
if os.path.isfile('/root/update.log'):
f=open('/root/update.log','a')
f.write('%s\n%s\n####################################\n' %(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),x))
f.close()
else:
f=open('/root/update.log','w')
f.write('%s\n%s\n####################################\n' %(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),x))
f.close()
print x
if __name__ == '__main__':
serverlist=['192.168.0.110','192.168.0.218','192.168.0.215']
for i in serverlist:
dd=updatedf(i,'root')
dd.addCallback(printData)
reactor.callLater(1, reactor.stop)
reactor.run()
这个相当于服务端,去调用客户端上的脚本,主要应用的defer的异步回调
update.py
#!/usr/bin/python
#2014-10-15 by jastme
import os,sys,time
def Backup_PaySystem():
"""
backup the website PaySystem
"""
reslut=os.system('tar zcf /backup/PaySystem-%s.tar.gz -P /var/www/html/pay1pay/PaySystem --exclude=BerbonHotSpot' %time.strftime('%Y-%m-%d-%H-%M-%S'))
try:
if reslut==0:
print '\033[36m PaySystem had been compressed in /backup \033[0m'
else:
print '\033[36m compressed PaySystem failed \033[0m'
except Exception as e:
print e
sys.exit(111)
def Ftp_Download(p):
"""
wget the package from ftp server
"""
ftp='wget ftp://jastme:jastme@192.168.1.1/%s' %p
try:
if os.system(ftp)==0:
print '\033[36m %s download \033[0m' %p
except Exception as e:
print e
sys.exit(222)
def Update(p):
"""
update the webroot
"""
os.system('alias cp=cp')
try:
os.system('tar xf %s' %p)
except Exception as e:
print e
for root,dirs,files in os.walk(p.split('_')[0]):
for i in files:
os.system('md5sum %s/%s >> md5.txt' %(root,i))
os.system('chown -R www-data:www-data %s' %p.split('_')[0])
os.system('cp -afr %s /var/www/html/pay1pay/' %p.split('_')[0])
os.system('mv md5.txt /var/www/html/pay1pay/')
os.chdir('/var/www/html/pay1pay/')
try:
if os.system('md5sum -c md5.txt')==0:
os.remove('md5.txt')
print '\033[36m update OK \033[0m'
else:
os.remove('md5.txt')
print '\033[36m update Failed,check the files or update by yourself \033[0m'
except Exception as e:
print e
sys.exit(333)
os.chdir('/root')
os.system('rm %s %s -rf' %(p,p.split('_')[0]))
if __name__ == '__main__':
Backup_PaySystem()
Ftp_Download(sys.argv[1])
Update(sys.argv[1])
这个脚本写的很丑,调用了很多shell命令,不过为了速度,丑就丑吧,这个主要还是一些命令的合集。