码迷,mamicode.com
首页 > 编程语言 > 详细

结合rpyc使用python实现动态升级的方法

时间:2015-02-12 22:40:01      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

动态升级,就是程序不退出的情况下,将其代码更新的策略。假设集群含有多个机器,然后每个机器部署一套程序,当升级的时候就要去所有的上面部署一把。

(1)有个包装程序专门负责接口并检查是否需要更新,当需要更新的时候,下载下来。

(2)动态引用,将实质程序放到独立文件和文件夹下面,通过动态引用,调用的时候reload;

客户端代码:

import rpyc
import sys
def update(remoteHost):
        c=rpyc.connect(remoteHost,12233)
        content="".join(file("Test.py","r").readlines())
        print c.root.update(content)
        c.close()


ipList=["10.101.92.211","10.101.90.203","10.101.91.239"]
for ip in ipList:
        update(ip)

服务器端:

import time,subprocess
from rpyc import Service
from rpyc.utils.server import ThreadedServer
import sys
g_module=None

class TimeService(Service):
    def exposed_sum(self,a,b):
        return a+b
    def exposed_show(self,cmd):
        #cmd=‘‘‘grep "FAIL"  /home/admin/alisatasknode/taskinfo/*/*/*/*/*/*/*.log   ‘‘‘
        return self.do_cmd(cmd)[1]
    def do_cmd(self,cmd):
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = p.communicate()
        return p.returncode, out, err
    def exposed_process(self,starttime):
        self.modulePath="/home/admin/rpyc-3.3.0/server.py"
        global g_module
        if g_module is None:
            print("none")
            g_module=__import__(Test)
        else:
            print("reload")
            g_module=reload(g_module)
        Test=getattr(g_module,"Test")
        t=Test()
        return t.getFailure(starttime)
    def exposed_update(self,content):
        outfile=open("Test.py","w")
        outfile.write(content)
        return "OK"

s=ThreadedServer(TimeService,port=12233,auto_register=False)
s.start()

 在Test.py里面实现真正的逻辑

import subprocess
import os,time

class Test():
    def do_cmd(self,cmd):
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = p.communicate()
        return p.returncode, out, err

    def getFailure(self,starttime):
        key="Table not found"
        keyfile= key.replace(" ","_")+".txt"
        timeArray = time.strptime(starttime,"%Y:%m:%d:%H:%M:%S")
        timeStamp = int(time.mktime(timeArray))
        print("timeStamp:%s" % (timeStamp))
        cmd="grep ‘%s‘ /home/admin/alisatasknode/taskinfo/20141222/phoenix/20141221/*/*/*/*.log" % (key)
        print("cmd:%s" % (cmd))
        r=self.do_cmd(cmd)
        print(r[0])
        infos=""
        if r[0] == 0:
            filelist=r[1].split("\n")
            for info in filelist:
                filename=info.split(":")[0]
                #print("filename:%s" % (filename))
                if filename.find("taskinfo") < 0:
                    continue
                filestamp=os.path.getmtime(filename)
                if filestamp >= timeStamp:
                    print("timeStamp:%s\tfilestamp:%s\ninfo:%s" % (timeStamp,filestamp,info))
                    if info.find(key):
                        infos=infos+"\n"+info
        return infos
~                     

 

结合rpyc使用python实现动态升级的方法

标签:

原文地址:http://www.cnblogs.com/laodageblog/p/4288948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!