Пример #1
0
def delete(aid=None):
    aid = aid if aid is not None else Q('ids', type=int)
    LOG.debug("客户端请求删除 ID=%d 的应用..." % aid)

    app = __loadApp(aid)
    db.session.delete(app)
    db.session.commit()
    LOG.info("删除 ID=%d 的应用成功" % aid)
    return jsonify(Result.ok())
Пример #2
0
def clean(aid):
    LOG.debug("客户端请求清空 ID=%s 的应用数据..." % aid)

    app = __loadApp(aid)
    app_dir = services.detect_app_dir(app)
    if os.path.exists(app_dir):
        shutil.rmtree(app_dir)
        LOG.info("删除 id={} 的应用数据:{}".format(aid, app_dir))

    return jsonify(Result.ok())
Пример #3
0
def checkDocker():
    with db.app.app_context():
        __tip("开始检测 docker connection...")
        if docker.client is None:
            LOG.debug("检测到 docker.client 未实例化,即将进行初始化...")
            docker.setup(current_app.config)
            LOG.info("^.^ docker.client 初始化成功 ^.^")
        else:
            try:
                LOG.debug("^.^ docker server 通讯正常 : ping=%s ^.^", docker.client.ping())
            except Exception as e:
                LOG.info("调用 docker.client.ping() 时出错:%s", str(e))
                docker.cleanup()
        __tip()
Пример #4
0
def __tip(msg=None):
    """

    :param msg:
    :return:
    """
    LOG.debug("================== SCHEDULE ==================")
    if msg is not None:
        stack = inspect.stack()
        the_method = stack[1][0].f_code.co_name
        LOG.debug("| method: %s", the_method)
        LOG.debug("| %s", msg)
Пример #5
0
def load_from_file(file_path: str, application: Application, update=False,  **kwargs):
    """
    从指定的目录加载文件(用户上传的文件要求为`zip`格式的压缩文件)
    :param application:
    :param update: True = 迭代更新,False = 全新部署
    :param file_path:
    :param kwargs:
        remove  是否移除同名的旧容器,默认 True

    :return:
    """
    if file_path is None or not os.path.exists(file_path):
        raise ServiceException("参数 file_path 未定义或者找不到相应的文件:%s" % file_path)

    if not file_path.endswith(ZIP):
        raise ServiceException("load_from_file 只支持 %s 结尾的文件" % ZIP)

    app_dir = detect_app_dir(application)

    if update:
        files = update_app_with_zip(file_path, app_dir)
        return application.name, files

    unzip_dir, files = unzip(file_path)
    LOG.info("解压到 %s" % unzip_dir)

    container_name = application.name

    for file in [str(f) for f in files]:
        LOG.debug("processing %s", file)
        if file.endswith(TAR):
            LOG.info("检测到 %s 文件 %s,即将导入该镜像..." % (TAR, file))
            docker.loadImage(os.path.join(unzip_dir, file))
            LOG.info("docker 镜像导入成功")

        if file.endswith(ZIP):
            """对于 zip 格式的文件,解压到程序根目录"""
            unzip(os.path.join(unzip_dir, file), app_dir)
            LOG.info("解压 %s 到 %s" % (file, app_dir))

        if file in ["{}-{}.jar".format(application.name, application.version), "{}.jar".format(application.name)]:
            """
            对于 {application.name}.jar 、 {application.name}-{version}.jar 的文件,直接复制到 app_dir
            通常经过 spring-boot 打包的 jar 可以直接运行
            """
            copyFileToDir(os.path.join(unzip_dir, file), app_dir)

        if file == APP_JSON:
            with open(os.path.join(unzip_dir, file)) as app_json:
                content = __transform_placeholder(app_json.read(), application)
                LOG.info("获取并填充 %s :%s" % (APP_JSON, content))

                app_ps = json.loads(content)
                if 'args' not in app_ps:
                    app_ps['args'] = {}
                if 'image' not in app_ps:
                    raise ServiceException("{} 中必须定义 'image' 属性,否则无法创建容器".format(APP_JSON))

            container_name = detect_app_name(app_ps['args'], app_ps['image'])
            LOG.info("检测到 容器名:%s", container_name)

            # 判断是否需要移除旧的 container
            old_container = None
            if kwargs.pop("remove", True):
                try:
                    old_container = docker.getContainer(container_name)
                    LOG.info("name={} 的容器已经存在:{}, id={}".format(container_name, old_container, old_container.id))
                except Exception:
                    pass

                if old_container is not None:
                    try:
                        old_container.remove()
                        LOG.info("成功删除name=%s 的容器" % container_name)
                    except Exception as e:
                        raise ServiceException("无法删除 name={} 的容器: {}".format(container_name, str(e)))

            network = docker.createDefaultNetwork()
            app_ps['args']['network'] = network.name
            docker.createContainer(app_ps['image'], container_name, app_ps['cmd'], app_ps['args'])
            LOG.info("APP 容器 创建成功(image=%s,name=%s, network=%s)" % (app_ps['image'], container_name, network.name))

    shutil.rmtree(unzip_dir)

    return container_name, files
Пример #6
0
 def index():
     """"
     跳转到 static/index.html
     """
     LOG.debug("visit index page %s", config.SERVER_INDEX_PAGE)
     return app.send_static_file(config.SERVER_INDEX_PAGE)