示例#1
0
    def chg_iptable(self, wars_with_md5, confs_with_md5):
        if not self.__run1() and not self.__run2():
            return False, "Nothing running cann't chg"
        if IptableWrap.is_iptables_tom2():
            if not self.__run1():
                return False, "current iptables is tom2, but tom1 is not running, cann't chg"

            if self.tomcat1.check_md5(wars_with_md5, confs_with_md5):
                log_util.info("[start] chg iptable for tom8080")
                result = util.run_cmd(
                    "iptables -t nat -D PREROUTING -p tcp --dport 8080 -j REDIRECT --to-ports 8180"
                )
                if result["code"] > 0:
                    return False, result["info"][1]
                self.__kill2()
            else:
                return False, "check tom1's md5 fail, cann't chg"
        else:
            if not self.__run2():
                return False, "current iptables is tom1, but tom2 is not running, cann't chg"

            if self.tomcat2.check_md5(wars_with_md5, confs_with_md5):
                log_util.info("[start] chg iptable for tom8180")
                result = util.run_cmd(
                    "iptables -t nat -A PREROUTING -p tcp --dport 8080 -j REDIRECT --to-ports 8180"
                )
                if result["code"] > 0:
                    return False, result["info"][1]
                self.__kill1()
            else:
                return False, "check tom2's md5 fail, cann't chg"

        return True, "success"
示例#2
0
 def __kill1(self):
     if self.__run1():
         log_util.info("[start] kill tom8080")
         self.tomcat1.wait_for_deal_pre_resp()
         self.tomcat1.force_kill_pre()
     else:
         log_util.warn("Previous tomcat is not run,didn`t need kill")
示例#3
0
    def start_deploy(self, wars_with_md5, confs_with_md5):
        if IptableWrap.is_iptables_tom2():
            if self.__run1():
                log_util.info("tom1 is already running start to kill")
                self.tomcat1.force_kill_pre()
            if not self.__run1():
                code, info = self.tomcat1.deploy_new(wars_with_md5,
                                                     confs_with_md5)
                if code > 0:
                    self.tomcat1.force_kill_current()
            else:
                code, info = (111, "kill tom1 fail")
        else:
            if self.__run2():
                log_util.info("tom2 is already running start to kill")
                self.tomcat2.force_kill_pre()
            if not self.__run2():
                code, info = self.tomcat2.deploy_new(wars_with_md5,
                                                     confs_with_md5)
                if code > 0:
                    self.tomcat2.force_kill_current()
            else:
                code, info = (111, "kill tom2 fail")

        if code > 0:
            log_util.error("Deploy error: %s" % info)
            return False, info
        return True, "success"
示例#4
0
 def force_kill_pre(self):
     # pid_cmd = "ps -lfC java | grep [t]omcat | awk '{print $4}'"
     # pid = util.run_cmd(pid_cmd)["info"][0]
     # if pid == '':
     #     print "pid not exist"
     #     return
     log_util.info("try to kill pid: %s", self.__run_pid)
     if self.__run_pid != "":
         util.force_kill(int(self.__run_pid))
示例#5
0
 def is_iptables_tom2():
     log_util.info("check iptables")
     result = util.run_cmd("iptables-save")
     iptables_info = result["info"][0]
     if result["code"] > 0:
         raise Exception("run iptables-save error")
     return re.search(
         "-A PREROUTING -p tcp -m tcp --dport 8080 -j REDIRECT --to-ports 8180",
         iptables_info)
示例#6
0
    def __is_tomcat_start(self):
        def __tomcat_not_start():
            return not util.is_request_200(self.heart_url,
                                           self.__request_timeout)

        rt, cost = util.polling_check(__tomcat_not_start,
                                      conf.TOMCAT__CHECK_START_TIMEOUT)
        log_util.info("check tomcat start cost: %s s" % cost)
        return rt
示例#7
0
    def __init__(self, tomcat_path, conf_path, host, heart_url):
        log_util.info("init deploy tomcat")
        self.tomcat_path = util.append_black_slash(tomcat_path)
        self.conf_path = util.append_black_slash(conf_path)
        self.heart_url = heart_url

        self.__request_timeout = 1
        self.__tomcat_war_dir_path = "%swebapps/" % self.tomcat_path
        self.__tomcatStatus = TomcatStatus(
            host, "Basic c2t5OGNoaTpjaGl0aWFueGlhbmc=")
        self.__run_pid = self.get_pid()
示例#8
0
def single(wars_with_md5=util.get_wars_with_md5(conf.SRC_WAR_PATH),
           confs_with_md5=util.get_confs_with_md5(conf.SRC_CONF_PATH)):

    init()
    start = time.time()

    deploy_tom = TomcatDeploy(conf.TOMCAT1__PATH, conf.TOMCAT1__CONF_PATH, conf.TOMCAT1__HOST, conf.TOMCAT1__HEART_URL)
    result = deploy_tom.deploy(wars_with_md5, confs_with_md5)

    log_util.info("Final result: %s" % result[1])
    log_util.info("total cost: %s " % (time.time() - start))
示例#9
0
class TomcatStatus(object):
    def __init__(self, host, auth):
        self.host = host
        self.auth = auth
        self.__url = ''.join(["http://", host, "/manager/status"])
        self.__self_verify_request = "GET /manager/status HTTP/1.1"
        self.__request_timeout = 1
    
    def __request_url(self):
        req = urllib2.Request(self.__url)
        req.add_header("Authorization", self.auth)
        response = urllib2.urlopen(req, timeout=self.__request_timeout)
        self.__body = response.read()
        response.close()
    
    def has_user_connect(self):
        try:
            self.__request_url()
        except Exception, e:
            log_util.error("%s : %s" % (Exception, e))
            return True
        has_connect = False

        # 因页面table会发生变化,固用正则先抓取大概范围
        # soup = BeautifulSoup(self.__body, "html.parser")
        # tables = soup.findAll('table')
        # status_table = tables[-2]

        http_info = re.search(r"(<h1>http-\d+</h1>[\s\S]*?</table>)", self.__body).group(1)
        soup = BeautifulSoup(http_info, "html.parser")
        tables = soup.findAll('table')
        status_table = tables[0]

        trs = status_table.findAll("tr")
        trs_len = len(trs)
        for (idx, th) in enumerate(trs[0].findAll("th")):
            th_txt = th.getText()
            if th_txt == "Stage":
                stage_idx = idx
            elif th_txt == "Request":
                request_idx = idx
            log_util.debug("connect[ stage: %s, request: %s ]" % (idx, th.getText()))

        log_util.debug("stageIdx: %s, requestIdx: %s, trsLen: %s" % (stage_idx, request_idx, trs_len))
        for tr in trs[1:trs_len]:
            tds = tr.findAll("td")
            stage = tds[stage_idx].getText()
            request = tds[request_idx].getText()
            log_util.debug("current connects [ stage: %s, request: %s ]" % (stage, request))
            if (stage == "K" or request != "?") and request != self.__self_verify_request:
                log_util.info("Some url is connected: stage[%s], request[%s]" % (stage, request))
                has_connect = True
                break
        return has_connect
示例#10
0
def multi(cmd="start",
          wars_with_md5=util.get_wars_with_md5(conf.SRC_WAR_PATH),
          confs_with_md5=util.get_confs_with_md5(conf.SRC_CONF_PATH)):

    init()
    start = time.time()

    deploy_tom = TomcatDeploy(conf.TOMCAT1__PATH, conf.TOMCAT1__CONF_PATH, conf.TOMCAT1__HOST, conf.TOMCAT1__HEART_URL)
    deploy_tom2 = TomcatDeploy(conf.TOMCAT2__PATH, conf.TOMCAT2__CONF_PATH, conf.TOMCAT2__HOST, conf.TOMCAT2_HEART__URL)
    iptable_wrap = IptableWrap(deploy_tom, deploy_tom2)
    if cmd == "start":
        is_success, info = iptable_wrap.start_deploy(wars_with_md5, confs_with_md5)
    elif cmd == "chg":
        is_success, info = iptable_wrap.chg_iptable(wars_with_md5, confs_with_md5)
    else:
        is_success, info = (False, "Unknown cmd for: %s" % cmd)

    if not is_success:
        log_util.error(info)
    log_util.info("%s => total cost: %s" % (cmd, (time.time() - start)))
    return is_success, info
示例#11
0
 def wait_for_deal_pre_resp(self):
     rst, cost = util.polling_check(
         self.__tomcatStatus.has_user_connect,
         conf.TOMCAT_STATUS__CHECK_CONNECT_TIMEOUT)
     log_util.info("wait_for_deal_pre_resp cost: %s s", cost)
     return rst
示例#12
0
    def deploy_new(self, wars_with_md5, conf_with_md5):
        log_util.info("[start] deploy tomcat new")

        log_util.info("[start] remove unpack")
        if not self.__remove_unpack(wars_with_md5):
            return code_info.REMOVE_UNPACK_FAIL

        log_util.info("[start] remove work")
        if not self.__remove_work():
            return code_info.REMOVE_WORK_FAIL

        log_util.info("[start] copy war")
        if not self.__copy_war(wars_with_md5):
            return code_info.COPY_WAR_FAIL

        log_util.info("[start] copy conf")
        if not self.__copy_conf(conf_with_md5):
            return code_info.COPY_CONF_FAIL

        log_util.info("[start] check md5")
        if not self.check_md5(wars_with_md5, conf_with_md5):
            return code_info.CHECK_FAIL

        log_util.info("[start] start tomcat")
        if not self.__start_tomcat():
            return code_info.START_SHELL_FAIL

        log_util.info("[start] start check deploy")
        if not self.__is_tomcat_start():
            return code_info.START_CHECK_FAIL

        log_util.info("[end] success")
        return code_info.SUCCESS
示例#13
0
    def deploy(self, wars_with_md5, conf_with_md5):
        log_util.info("[start] deploy tomcat")

        log_util.info("[start] wait for deal pre resp")
        self.wait_for_deal_pre_resp()

        log_util.info("[start] force kill")
        self.force_kill_pre()

        log_util.info("[start] remove unpack")
        if not self.__remove_unpack(wars_with_md5):
            return code_info.REMOVE_UNPACK_FAIL

        log_util.info("[start] remove work")
        if not self.__remove_work():
            return code_info.REMOVE_WORK_FAIL

        log_util.info("[start] copy war")
        if not self.__copy_war(wars_with_md5):
            return code_info.COPY_WAR_FAIL

        log_util.info("[start] copy conf")
        if not self.__copy_conf(conf_with_md5):
            return code_info.COPY_CONF_FAIL

        log_util.info("[start] check md5")
        if not self.check_md5(wars_with_md5, conf_with_md5):
            return code_info.CHECK_FAIL

        log_util.info("[start] start tomcat")
        if not self.__start_tomcat():
            return code_info.START_SHELL_FAIL

        log_util.info("[start] start check deploy")
        if not self.__is_tomcat_start():
            return code_info.START_CHECK_FAIL

        log_util.info("[end] success")
        return code_info.SUCCESS
示例#14
0
def force_kill(pid):
    try:
        os.kill(pid, 9)
        log_util.info('已杀死pid为%s的进程' % pid)
    except OSError, e:
        log_util.error("%s %s 没有如此进程!!!" % (OSError, e))