Пример #1
0
    def run(self):
        while True:

            # 打开连接
            start_connect(sqlite_db_use=False)

            try:
                container_monitor.init()
                login_log("container_monitor_script", "完成一次应用状态扫描!")
            except Exception as err:
                login_log("container_monitor_script_error", err.args[0])
                time.sleep(5)

            try:
                if not task_execute.init():
                    login_log("task_script", "任务队列没有任务")
                login_log("task_script", "完成一次任务调度!")
            except Exception as err:
                login_log("task_script_error", err.args[0])
                time.sleep(5)

            # 关闭数据库
            close_connect(sqlite_db_use=False)

            time.sleep(5)
Пример #2
0
def get_a_api_url():
    """
    获取一个API服务器地址
    :return:
    """

    objs = ContainerServerModel.select().where(ContainerServerModel.status == 0).order_by(ContainerServerModel.sort.desc())
    d = [999999, 999999, None, None]
    for obj in objs:
        url = "http://%s:%d/container/" % (obj.server_host, obj.server_port)
        r = request_api(url+"analy", {})
        if r.status_code != 200:
            continue

        response_data_obj = json.loads(r.text)
        if response_data_obj['code'] != 0:
            continue
        if response_data_obj['result']['count'] < d[0] and response_data_obj['result']['totalMemory'] < d[1]:
            d[2] = obj
            d[0] = response_data_obj['result']['count']
            d[1] = response_data_obj['result']['totalMemory']
            d[3] = {"url": url, "server_host": obj.server_host}
    if d[2] is None:
        return None
    login_log("containerAPI", "获取到的API服务器为:%s:%d,当前容器数:%d,容器内存:%dM" %(d[2].server_host, d[2].server_port, d[0], d[1]))
    return d[3]['url'], d[3]['server_host']
Пример #3
0
def remove_some_container(task, number):
    """
    删除若干个容器
    :param task:
    :param number:
    :return:
    """

    containers = AppContainModel.select().where(AppContainModel.app_id == task.app_id).limit(number)
    for obj in containers:
        api_url = obj.api_url + "remove"
        data = {
            "containerId": obj.container_id
        }
        r = request_api(api_url, data)
        if r.status_code == 200:
            d = json.loads(r.text)
            if d['code'] == 0:
                # 删除容器成功
                dao = AppContainModel.delete().where(AppContainModel.id == obj.id)
                dao.execute()
                login_log("common", "删除容器成功,触发应用id:%d,容器id:%s,容器服务器域名:%s" % (task.app_id, obj.container_id, obj.host))
                continue
        # 删除出错处理
        login_log("error", "删除容器失败,触发应用id:%d,容器id:%s,容器服务器域名:%s" % (task.app_id, obj.container_id, obj.host))

    return None
Пример #4
0
def get_a_api_url():
    """
    获取一个API服务器地址
    :return:
    """

    objs = ContainerServerModel.select().where(
        ContainerServerModel.status == 0).order_by(
            ContainerServerModel.sort.desc())
    d = [999999, 999999, None, None]
    for obj in objs:
        url = "http://%s:%d/container/" % (obj.server_host, obj.server_port)
        r = request_api(url + "analy", {})
        if r.status_code != 200:
            continue

        response_data_obj = json.loads(r.text)
        if response_data_obj['code'] != 0:
            continue
        if response_data_obj['result']['count'] < d[0] and response_data_obj[
                'result']['totalMemory'] < d[1]:
            d[2] = obj
            d[0] = response_data_obj['result']['count']
            d[1] = response_data_obj['result']['totalMemory']
            d[3] = {"url": url, "server_host": obj.server_host}
    if d[2] is None:
        return None
    login_log(
        "containerAPI", "获取到的API服务器为:%s:%d,当前容器数:%d,容器内存:%dM" %
        (d[2].server_host, d[2].server_port, d[0], d[1]))
    return d[3]['url'], d[3]['server_host']
Пример #5
0
def remove_some_container(task, number):
    """
    删除若干个容器
    :param task:
    :param number:
    :return:
    """

    containers = AppContainModel.select().where(
        AppContainModel.app_id == task.app_id).limit(number)
    for obj in containers:
        api_url = obj.api_url + "remove"
        data = {"containerId": obj.container_id}
        r = request_api(api_url, data)
        if r.status_code == 200:
            d = json.loads(r.text)
            if d['code'] == 0:
                # 删除容器成功
                dao = AppContainModel.delete().where(
                    AppContainModel.id == obj.id)
                dao.execute()
                login_log(
                    "common", "删除容器成功,触发应用id:%d,容器id:%s,容器服务器域名:%s" %
                    (task.app_id, obj.container_id, obj.host))
                continue
        # 删除出错处理
        login_log(
            "error", "删除容器失败,触发应用id:%d,容器id:%s,容器服务器域名:%s" %
            (task.app_id, obj.container_id, obj.host))

    return None
Пример #6
0
    def run(self):
        while True:

            # 打开连接
            start_connect(sqlite_db_use=False)

            try:
                container_monitor.init()
                login_log("container_monitor_script", "完成一次应用状态扫描!")
            except Exception as err:
                login_log("container_monitor_script_error", err.args[0])
                time.sleep(5)

            try:
                if not task_execute.init():
                    login_log("task_script", "任务队列没有任务")
                login_log("task_script", "完成一次任务调度!")
            except Exception as err:
                login_log("task_script_error", err.args[0])
                time.sleep(5)

            # 关闭数据库
            close_connect(sqlite_db_use=False)

            time.sleep(5)
Пример #7
0
def handle_task(task):
    """
    处理每一个任务
    :param task: 任务对象
    :return:
    """
    if task.command_code == 1:
        # 添加一个容器
        r, obj = add_one_container(task)

        if r:
            # 保存容器服务器申请到的容器
            dao = AppContainModel(
                app_id=task.app_id,
                host=obj['host'],
                port=obj['r']['result']['port'],
                container_id=obj['r']['result']['containerId'],
                create_time=datetime.datetime.now(),
                api_url=obj['api_url'])
            dao.save()
            login_log(
                "common", "申请容器成功,触发应用id:%d,申请到的容器域名:%s,端口:%d,id:%s" %
                (task.app_id, obj['host'], obj['r']['result']['port'],
                 obj['r']['result']['containerId']))
        else:
            login_log("error", "申请容器失败,触发应用id:%d" % task.app_id)
    elif task.command_code == 2:
        # 移除一个容器
        remove_some_container(task, 1)
    elif task.command_code == 3:
        # 移除所有容器
        count = AppContainModel.select().where(
            AppContainModel.app_id == task.app_id).count()
        remove_some_container(task, count)
    else:
        # 未定义任务码
        login_log("error",
                  "未定义任务码:%d,触发应用id:%d" % (task.command_code, task.app_id))

    build_nginx_config(task.app_id)

    # 删除任务队列
    dao = TaskQueueModel.delete().where(TaskQueueModel.id == task.id)
    dao.execute()
Пример #8
0
def handle_task(task):
    """
    处理每一个任务
    :param task: 任务对象
    :return:
    """
    if task.command_code == 1:
        # 添加一个容器
        r, obj = add_one_container(task)

        if r:
            # 保存容器服务器申请到的容器
            dao = AppContainModel(app_id=task.app_id, host=obj['host'], port=obj['r']['result']['port'],
                                  container_id=obj['r']['result']['containerId'], create_time=datetime.datetime.now(),
                                  api_url=obj['api_url'])
            dao.save()
            login_log("common", "申请容器成功,触发应用id:%d,申请到的容器域名:%s,端口:%d,id:%s" % (task.app_id,
                                                                              obj['host'], obj['r']['result']['port'],
                                                                              obj['r']['result']['containerId']))
        else:
            login_log("error", "申请容器失败,触发应用id:%d"%task.app_id)
    elif task.command_code == 2:
        # 移除一个容器
        remove_some_container(task, 1)
    elif task.command_code == 3:
        # 移除所有容器
        count = AppContainModel.select().where(AppContainModel.app_id == task.app_id).count()
        remove_some_container(task, count)
    else:
        # 未定义任务码
        login_log("error", "未定义任务码:%d,触发应用id:%d" % (task.command_code, task.app_id))

    build_nginx_config(task.app_id)

    # 删除任务队列
    dao = TaskQueueModel.delete().where(TaskQueueModel.id == task.id)
    dao.execute()
Пример #9
0

# 清除孤儿容器
print("开始清除孤儿容器")
start_connect(sqlite_db_use=False)
c = db.execute_sql("select id, api_url, container_id, app_id, host, port  from app_container where app_id not in "
                   "(select id from app)")
objs = c.fetchall()
for t in objs:
    obj['id'] = t[0]
    obj['api_url'] = t[1]
    obj['container_id'] = t[2]
    obj['app_id'] = t[3]
    obj['host'] = t[4]
    obj['port'] = t[5]
    login_log("maintain", "发现孤儿容器,所属应用id:%d,容器id:%s,容器域名:%s,容器端口:%d" % (obj['app_id'],
                                                                        obj['container_id'], obj['host'], obj['port']))
    api_url = obj['api_url'] + "remove"
    data = {
        "containerId": obj['container_id']
    }
    r = request_api(api_url, data)
    if r.status_code == 200:
        d = json.loads(r.text)
        if d['code'] == 0:
            # 删除容器成功
            dao = AppContainModel.delete().where(AppContainModel.id == obj['id'])
            dao.execute()
            login_log("maintain", "删除孤儿容器成功,所属应用id:%d,容器id:%s,容器域名:%s,容器端口:%d" % (obj['app_id'],
                                                                                  obj['container_id'], obj['host'],
                                                                                  obj['port']))
            continue