Example #1
0
File: Log.py Project: zhmsg/dms
class LogManager:

    def __init__(self):
        if env == "Development":
            service_mysql = "192.168.120.2"
            self.data_db_name = "clinic_basic"
        else:
            service_mysql = "rdsikqm8sr3rugdu1muh3.mysql.rds.aliyuncs.com"
            self.data_db_name = "clinic"
        self.db = DB(host=service_mysql, mysql_user="******", mysql_password="******", mysql_db=self.data_db_name)
        self.local_db = DB()
        self.api_log = "api_log_2"
        self.login_server = "login_server"
        self.log_cols = ["log_no", "run_begin", "host", "url", "method", "account", "ip", "level", "info", "run_time"]
        self.login_cols = ["login_no", "server_ip", "server_name", "user_ip", "user_name", "login_time"]
        self.log_level = ["error", "base_error", "bad_req", "http_error", "info"]
        self.log_task = TaskManager(1)
        self.login_task = TaskManager(2)
        self.basic_time = 1473350400  # 2016/09/09 00:00:00

    def _select_log(self, where_sql, limit_num=250):
        select_sql = "SELECT %s FROM %s WHERE %s ORDER BY log_no DESC LIMIT %s;" \
                     % (",".join(self.log_cols), self.api_log, where_sql, limit_num)
        self.db.execute(select_sql)
        log_records = []
        for item in self.db.fetchall():
            log_item = {}
            for i in range(len(self.log_cols)):
                log_item[self.log_cols[i]] = item[i]
            log_records.append(log_item)
        return True, log_records

    def select_log(self, log_no):
        where_sql = "log_no=%s" % long(log_no)
        return self._select_log(where_sql, limit_num=1)

    def show_log(self, start_time=None, end_time=None, level=None, search_url="", search_account=""):
        run_end = time()
        run_begin = run_end - timedelta(hours=1).total_seconds()
        require = {}
        if start_time is not None and start_time > run_begin:
            run_begin = start_time
            require["start_time"] = start_time
        if end_time is not None and end_time < run_end:
            run_end = end_time
            require["end_time"] = end_time
        where_sql_list = ["log_no>=%s " % long(run_begin * 10000), "log_no<=%s " % long(run_end * 10000)]
        if level is not None:
            if level not in self.log_level:
                return False, "Bad level"
            where_sql_list.append("level = '%s'" % level)
        if search_url is not None and search_url != "":
            search_url = check_sql_character(search_url)
            where_sql_list.append("url like '%s%%'" % search_url)
        if search_account is not None and search_account != "":
            search_account = check_sql_character(search_account)
            where_sql_list.append("account = '%s'" % search_account)
        where_sql = " AND ".join(where_sql_list)
        result, log_records = self._select_log(where_sql)
        if result is False:
            return False, log_records
        return True, {"log_records": log_records, "require": require}

    def select_daily_log(self):
        result, info = self.log_task.select_scheduler_status()
        if result is False:
            return False, info
        if info["task_status"] is None:
            run_end = long(time() * 10000)
            run_begin = long(run_end - timedelta(days=1).total_seconds() * 10000)
            require = {"start_time": run_begin, "end_time": run_end}
            where_sql = "log_no >= %s AND log_no <= %s AND level <> 'info'" % (run_begin, run_end)
        else:
            log_no = long(info["task_status"])
            require = {"log_no": log_no}
            where_sql = "log_no > %s AND level <> 'info'" % log_no
        result, log_records = self._select_log(where_sql)
        if result is False:
            return False, log_records
        if len(log_records) > 0:
            self.log_task.update_scheduler_status(log_records[0]["log_no"], "system", "daily log")
        return True, {"log_records": log_records, "require": require}

    def register_daily_task(self):
        user_name = "system"
        reason = u"每日运行日志"
        reason_desc = u"每天8:30,将一天前到现在所有的不正确或者未正确执行的请求日志发送给相关权限人员。"
        task_no = (int(time()) - self.basic_time) / 86400
        return self.log_task.register_new_task(task_no, user_name=user_name, reason=reason, reason_desc=reason_desc)

    def _select_login(self, where_sql, limit_num=250):
        select_sql = "SELECT %s FROM %s WHERE %s ORDER BY login_no DESC LIMIT %s;" \
                     % (",".join(self.login_cols), self.login_server, where_sql, limit_num)
        self.local_db.execute(select_sql)
        login_records = []
        for item in self.local_db.fetchall():
            login_item = {}
            for i in range(len(self.login_cols)):
                login_item[self.login_cols[i]] = item[i]
            login_records.append(login_item)
        return True, login_records

    def insert_login_server(self, server_ip, server_name, user_ip, user_name, login_time):
        if check_int(server_ip, 1, sys.maxint) is False:
            return False, "Bad server ip"
        if check_int(user_ip, 1, sys.maxint) is False:
            return False, "Bad user ip"
        now_time = int(time())
        if check_int(login_time, now_time - 100, now_time + 100) is False:
            return False, "Bad login time"
        user_name = check_sql_character(user_name)[:50]
        server_name = check_sql_character(server_name)[:20]
        insert_sql = "INSERT INTO %s (server_ip,server_name,user_ip,user_name,login_time) VALUES (%s,'%s',%s,'%s',%s);" \
                     % (self.login_server, server_ip, server_name, user_ip, user_name, login_time)
        self.local_db.execute(insert_sql)
        return True, "success"

    def select_login_log(self):
        result, info = self.login_task.select_scheduler_status()
        if result is False:
            return False, info
        if info["task_status"] is None:
            run_end = time()
            run_begin = run_end - timedelta(days=1).total_seconds()
            where_sql = "login_time >= %s AND login_time <= %s" % (run_begin, run_end)
        else:
            login_no = int(info["task_status"])
            where_sql = "login_no > %s" % login_no
        result, login_records = self._select_login(where_sql)
        if result is False:
            return False, login_records
        if len(login_records) > 0:
            self.login_task.update_scheduler_status(login_records[0]["login_no"], "system", "")
        return True, {"login_records": login_records}

    def register_login_task(self):
        user_name = "system"
        reason = u"登录记录"
        reason_desc = u"每天9-15点5分,将服务器登录记录送给相关权限人员。"
        task_no = (int(time()) - self.basic_time) / 3600
        return self.login_task.register_new_task(task_no, user_name=user_name, reason=reason, reason_desc=reason_desc)
Example #2
0
File: Release.py Project: zhmsg/dms
class ReleaseManager:

    def __init__(self, release_dir):
        self.db = DB()
        self.release_task = "release_task"
        self.basic_time = datetime.strptime("2016-09-02 00:00:00", TIME_FORMAT)
        self.latest_branch = "master"
        self.api_work_dir = release_dir + "/ih_GATCAPI"
        self.web_work_dir = release_dir + "/ih_GATCWeb"
        self.api_task = TaskManager(3)
        self.web_task = TaskManager(4)
        self.pull_request_man = PullRequestManager()
        self.wx = WeiXinManager(wx_service)

    def new_release_task(self, user_name, reason, restart_service, reason_desc):
        # return False, "领导禁止重启"
        release_time = datetime.now() - self.basic_time
        release_no = release_time.seconds / 3600 + release_time.days * 24
        status_info = "%s1" % int(time())
        args = dict(release_no=release_no, user_name=user_name, reason=reason, restart_service=restart_service,
                    reason_desc=reason_desc, status_info=status_info)
        result = self.db.execute_insert(self.release_task, args=args, ignore=True)
        if result <= 0:
            return False, u"任务已存在"
        return True, args

    def update_release_task(self, release_no, run_result=True):
        status_info = "|%s%s" % (int(time()), 1 if run_result is True else 0)
        update_sql = "UPDATE %s SET status_info=CONCAT(status_info, '%s') WHERE release_no=%s;" \
                     % (self.release_task, status_info, release_no)
        self.db.execute(update_sql)
        return True, "success"

    def select_release_task(self, user_name=None):
        release_time = datetime.now() - self.basic_time
        min_release_no = release_time.days * 24
        cols = ["release_no", "user_name", "restart_service", "reason", "reason_desc", "status_info"]
        if user_name is None:
            zero = 1
        else:
            zero = 0
        select_sql = "SELECT %s FROM %s WHERE release_no>=%s AND (user_name='%s' OR %s) ORDER BY release_no DESC;" \
                     % (",".join(cols), self.release_task, min_release_no, user_name, zero)
        self.db.execute(select_sql)
        db_r = self.db.fetchall()
        task_list = []
        for item in db_r:
            task_info = {}
            for i in range(len(cols)):
                task_info[cols[i]] = item[i]
            task_list.append(task_info)
        return True, task_list

    def send_wx_msg(self, msg):
        result, user_list = self.wx.user_info()
        if result is False:
            return False, user_list
        for user_info in user_list:
            # self.wx.send_status(u"后台开发", "ochiws2EiR0cq3qzXYjQkw0m9jdE", "Test", msg)
            # break
            if user_info["groupid"] == 100:
                self.wx.send_status(u"后台开发", user_info["openid"], "Test", msg)
            elif user_info["groupid"] == 101:
                self.wx.send_status(u"前端开发", user_info["openid"], "Test", msg)
            elif user_info["groupid"] == 102:
                self.wx.send_status(u"产品设计", user_info["openid"], "Test", msg)
        return True, "success"

    def select_api_pull_request(self):
        result, scheduler_info = self.api_task.select_scheduler_status()
        task_status = scheduler_info["task_status"]
        return self.pull_request_man.select_pull_request(action_no=task_status, repository="GATCAPI", merged=True, base_branch="master")

    def select_web_pull_request(self):
        result, scheduler_info = self.web_task.select_scheduler_status()
        task_status = scheduler_info["task_status"]
        return self.pull_request_man.select_pull_request(action_no=task_status, repository="GATCWeb", merged=True, base_branch="master")

    def _restart_api(self):
        _pull_code(self.api_work_dir, self.latest_branch)
        with cd(self.api_work_dir):
            run('rm -rf *.log')
            run("sh stop.sh")
            run('ssh service "sh /home/msg/GATCAPI/restart_service.sh"', quiet=True)
            run('nohup gunicorn -b 0.0.0.0:8100 -t 3600 -w 5 -k "gevent" --backlog 2048 -p "/tmp/api_gunicorn_test.pid" --chdir API run:app 1>> API.log 2>> API.log & sleep 3')
            run('cat /tmp/api_gunicorn_test.pid >> service.pid')
        self.api_task.update_scheduler_status(int(time()), "system", "restart ih api")

    def _restart_web(self):
        _pull_code(self.web_work_dir, self.latest_branch)
        with cd(self.web_work_dir):
            run('find -name "*.log" | xargs rm -rf')
            run("sh stop.sh")
            run('nohup gunicorn -b 0.0.0.0:9101 -t 3600 -w 5 -k "gevent" --backlog 2048 -p "/tmp/web_gunicorn_test.pid" --chdir Web2 Webstart:app 1>> WEB.log 2>> WEB.log & sleep 3')
            run('cat /tmp/web_gunicorn_test.pid >> service.pid')
        self.web_task.update_scheduler_status(int(time()), "system", "restart ih web")

    def _release_api(self, user_name, release_no, reason):
        reason_desc = u"%s 重启API测试环境 %s\n" % (user_name, reason)
        wx_msg = reason_desc
        # 获得提交的pull request信息
        result, pull_requests = self.select_api_pull_request()
        if len(pull_requests) <= 0:
            return False, u"API无更新"
        else:
            wx_msg += u"API更新如下:\n"
            for i in range(len(pull_requests) - 1, -1, -1):
                wx_msg += u"%s、%s\n" % (i+1, pull_requests[i]["request_title"])
        print("start restart")
        self._restart_api()
        self.update_release_task(release_no, True)
        print("start test")
        self.update_release_task(release_no, True)
        print("start push")
        _push_code(self.api_work_dir, reason_desc)
        self.update_release_task(release_no, True)
        self.send_wx_msg(wx_msg)
        return True, "success"

    def _release_web(self, user_name, release_no, reason):
        reason_desc = u"%s 重启WEB测试环境 %s\n" % (user_name, reason)
        wx_msg = reason_desc
        # 获得提交的pull request信息
        result, pull_requests = self.select_web_pull_request()
        if len(pull_requests) <= 0:
            return False, u"WEB无更新"
        else:
            wx_msg += u"WEB更新如下:\n"
            for i in range(len(pull_requests) - 1, -1, -1):
                wx_msg += u"%s、%s\n" % (i+1, pull_requests[i]["request_title"])
        print("start restart")
        self._restart_web()
        self.update_release_task(release_no, True)
        print("start test")
        self.update_release_task(release_no, True)
        print("start push")
        _push_code(self.web_work_dir, reason_desc)
        self.update_release_task(release_no, True)
        self.send_wx_msg(wx_msg)
        return True, "success"

    def _release_ih(self, user_name, release_no, reason):
        reason_desc = u"%s 重启API&WEB测试环境 %s\n" % (user_name, reason)
        wx_msg = reason_desc
        # 获得提交的pull request信息
        result, pull_requests = self.select_web_pull_request()
        if len(pull_requests) <= 0:
            return False, u"WEB无更新"
        else:
            wx_msg += u"WEB更新如下:\n"
            for i in range(len(pull_requests) - 1, -1, -1):
                wx_msg += u"%s、%s\n" % (i+1, pull_requests[i]["request_title"])
        result, pull_requests = self.select_api_pull_request()
        if len(pull_requests) <= 0:
            return False, u"API无更新"
        else:
            wx_msg += u"API更新如下:\n"
            for i in range(len(pull_requests) - 1, -1, -1):
                wx_msg += u"%s、%s\n" % (i+1, pull_requests[i]["request_title"])
        print("start restart")
        self._restart_api()
        self._restart_web()
        self.update_release_task(release_no, True)
        print("start test")
        self.update_release_task(release_no, True)
        print("start push")
        _push_code(self.api_work_dir, reason_desc)
        _push_code(self.web_work_dir, reason_desc)
        self.update_release_task(release_no, True)
        self.send_wx_msg(wx_msg)
        return True, "success"

    def release_ih(self):
        # 获得任务
        result, info = self.select_release_task()
        if result is False:
            return False, info
        if len(info) <= 0:
            return False, info
        release_time = datetime.now() - self.basic_time
        release_no = (release_time.seconds - 600) / 3600 + release_time.days * 24
        if info[0]["release_no"] != release_no:
            return False, "No Task"
        restart_service = info[0]["restart_service"]
        user_name = info[0]["user_name"]
        reason = info[0]["reason"]
        print("start run release %s" % release_no)
        self.update_release_task(release_no, True)

        if restart_service == 0:
            return self._release_ih(user_name, release_no, reason)
        elif restart_service == 1:
            return self._release_api(user_name, release_no, reason)
        elif restart_service == 2:
            return self._release_web(user_name, release_no, reason)
        else:
            return False, "invalid restart service code"