Beispiel #1
0
def day_upload_req():
    """

        定时任务 每天执行一次    将redis内的数据写入db
    """
    with flask_app.app_context():
        day = (datetime.today() - timedelta(days=1)).strftime("%Y%m%d")
        ip_count = cache.scard(ConstCacheKey.IP_DAY_SET % day)
        req_count = int(cache.get(ConstCacheKey.REQ_DAY_COUNT % day) or 0)
        instance = RequestState(day=day, ip_count=ip_count, req_count=req_count)
        db.session.add(instance)
        db.session.commit()
Beispiel #2
0
def build_delay(task: int,
                host,
                build_type,
                tag,
                admin,
                pt=None,
                dockerfile=None):
    """
        编译镜像
    """
    with flask_app.app_context():
        task = db.query(TaskList).get(task)
        instance = db.session.query(Host).filter(Host.id == host).one_or_none()
        cli = APIClient(base_url='tcp://{}'.format(instance.addr))
        if build_type == 'tar':
            f = open(pt, 'rb')
            for line in cli.build(fileobj=f,
                                  rm=True,
                                  tag=tag,
                                  custom_context=True):
                print(line)
                task_add_log(task.id, line)
            task.status = task.STATUS_DONE
        elif build_type == 'pull':
            for line in cli.pull(tag, stream=True, decode=True):
                task_add_log(task.id, line)
            task.status = task.STATUS_DONE
        else:
            try:
                f = BytesIO(dockerfile.encode('utf-8'))
                for line in cli.build(fileobj=f, rm=True, tag=tag):
                    task_add_log(task.id, line)
                task.status = task.STATUS_DONE
            except docker_error.DockerException as e:
                task.status = task.STATUS_ERROR
                task.remark = str(e)
        db.session.commit()