本地服务更新Python代码 如要使用请适当的修改,python代码,用python代码编写的
本地服务更新Python代码 如要使用请适当的修改,python代码,用python代码编写的
用python代码编写的本地服务更新程序,如要使用还请依自己情况做适当的修改。此代码仅供参考!#coding=utf-8import osimport timefrom datetime import dateimport commands #用于回显系统命令输出结果import sysimport shutil #copy file '''注:只是针对SMCDserver更新用(本地没有环境,只用临时文件替代)。 只需要把要更新的压缩包 解压后的文件放入以今天日期为目录的文件中(目录格式:20150522 。只更新当天日期所对应的目录中的内容),自动对照MD5,复制文件到要更新的位置,删除原链接,建立新软连接。重启服务(因用httpd 和 keepalived测试,所以只是重新启动httpd和keepalived服务)查看当天最新的日志(这个日志 执行脚本的最后一步会显示到屏幕上)。如果没问题 ctrl + C 退出缺点:当时写脚本的时候未考虑到 函数不支持远程操作(后续会用python 对应的模块解决这个问题),所以此脚本只能在同台机器上执行。以下函数名 如果看不懂的 基本上都是拼音的缩写,屏幕输出不人性化'''now = date.today().strftime("%Y%m%d") #file_dir = "/root/Desktop/aaa/" #要更新的文件所在地d_path = "/tmp/" #要更新的目录log_path = "/var/newlog/" #日志存放目录log_name_1 = "message.log." #日志固定前缀log_time = date.today().strftime("%Y%m%d")[-6:]#截取当前日子 格式年月日 从后面数保留6位server_name = "httpd" #要启动的服务名keepalived = "keepalived"#判断md5值是否一样def pd_md5(file_md5,source_file_md5,f_path): if file_md5 == source_file_md5 : cp_file(f_path,d_path) #md5值一样复制文件 else : print "md5 error ,exit " sys.exit() #md5值不一样 .退出#copy file def cp_file(f_path,d_path): shutil.copy2(f_path,d_path) #本机拷贝 update_file_name = d_path + f_path.split("/")[-1] #获取要更新的文件名 if os.path.exists(d_path + f_path.split("/")[-1]): #判断文件是否存在 ex_file(update_file_name) #执行更新 else : print "file %s exist ..... exit" %update_file_name sys.exit()#删除并重新创新建软连接def ex_file(update_file_name): os.chdir(d_path) #进入要跟新的目录 update_file_name = update_file_name.split('/')[-1] #获取要更新的文件名 #source_ln = commands.getstatusoutput("find . -type l")[-1].strip().split('/') [-1] #搜索目录中的软连接,因项目每个目录中只有一个超链接,所以此方法可行 #if len(commands.getstatusoutput("find . -type l |xargs rm -rf {}")[-1]) ==0: #查找并删除软连接 for i in os.listdir("d_path"): if i == "SMCDServer": os.system("ln -s %s SMCDServer" %update_file_name) # if commands.getstatusoutput("ln -s %s SMCDServer && find . -type l " %update_file_name)[-1].strip().split('/')[-1] =="SMCDServer": #重新建立软连接,并查找软连接是否存在 restart_server()#链接建立好重启服务 else : sys.exit() else : sys.exit()#start keepaliveddef start_keepalive(): if len(commands.getstatusoutput("ps -ef | grep %s |grep -v grep" %keepalived)[-1]) == 0: os.system("/etc/init.d/%s start" %keepalived) if len(commands.getstatusoutput("ps -ef | grep %s |grep -v grep" %keepalived)[-1]) > 0: print " %s start .. " %keepalived# 关闭keepaliveddef close_keepalive(): if len(commands.getstatusoutput("ps -ef | grep %s |grep -v grep" %keepalived)[-1]) > 0: if len(commands.getstatusoutput("killall -9 %s " %keepalived)[-1]) == 0: print " %s closed .." %keepalived#重新启动服务def restart_server(): if len(commands.getstatusoutput("ps -ef | grep %s |grep -v grep" %server_name)[-1]) == 0:# 判断服务是否在运行 等于0 没运行 os.system("service %s start " %server_name) #启动服务 if len(commands.getstatusoutput("ps -ef | grep %s |grep -v grep" %keepalived)[-1]) == 0: #keepalived 没启动 print "--------------------------------------------------------------" start_keepalive() #启动keepalived check_log() #查看当天最新日志 print "---------------------------------------------------------------" else : print "killall -9 %s....." %server_name os.system("ps -ef | grep %s | grep -v grep|awk -F' ' '{print $8}'|xargs killall -9 &> /dev/null" %server_name) # 不在屏幕上显示 restart_server()#分析日志 判断是否更新成功 def check_log(): log_num= [] for i in os.listdir(log_path): log_num.append(i.split('-')[-1]) log_name = log_name_1 + log_time + "-" + max(log_num) #获取当天最新日志名 print "check log file %s " %(log_path+log_name) os.system("tailf %s " %(log_path+log_name)) #观察日志,看文件更新是否成功#稍后给成函数for i in os.listdir(file_dir+now): close_keepalive() #先关闭keepalived if '.' not in i: print "Current directory : %s " %commands.getstatusoutput(file_dir+now)[1].split(":")[1].strip() #操作的目录 file_path = file_dir+now # 要操作文件的绝对路径 os.chdir(file_path) #进入目录 file_md5=commands.getstatusoutput("md5sum %s" %i)[1].split(" ")[0] # 生成文件的md5值 source_file_md5 = commands.getstatusoutput("cat %s.md5" %i)[1].split(" ")[0] #已给出的md5值 pd_md5(file_md5,source_file_md5,f_path=file_path+"/" +i) #对比md5值是否相同其中有个删除 SMCDServer 的地方错了 我贴改正的代码for i in os.listdir("d_path"): if i == "SMCDServer": os.system("rm -rf SMCDServer") else : os.system("ln -s %s SMCDServer" %update_file_name)
代码中的python相关方法使用详解:python list append、python type、python assert
本文原创自www.iplaypy.com编橙之家会员:liu382337172
编橙之家文章,
相关内容
- python抽奖 系统算法代码的简单实现,python抽奖算法代码
- 如何用Python代码实现自动比较两个文件中的代码变化?
- python logging 日志模块使用方法学习,pythonlogging,本文为
- Python近期使用较少算法实现方法,python近期较少算法
- 用Python来处理中文分句的方法_【源码精华】,,我在用
- 用Python生成随机的中文验证码图片,,在登录很多网站的
- Python方法批量重命名MP3文件,python重命名,Python方法批量
- Python threading多线程断点下载文件的方法,pythonthreadin
- python3.2 数据存储pickle模块的使用方法详解,python3.2pi
- 用Python为Vim编写的小插件源码分享,pythonvim编写源码
评论关闭