package cn.reghao.autodop.dmaster.app.service.bd.tools.packer;

import cn.reghao.autodop.common.docker.Docker;
import cn.reghao.autodop.dmaster.app.model.po.config.build.PackerConfig;
import lombok.extern.slf4j.Slf4j;

/**
 * 打包为 docker 应用
 *
 * @author reghao
 * @date 2019-10-14 22:02:04
 */
@Slf4j
public class DockerPack implements CodePacker {
    private final String dockerfile;
    private final String targetPath;

    public DockerPack(PackerConfig packerConfig, String dockerfile) {
        this.dockerfile = dockerfile;
        this.targetPath = packerConfig.getTargetPath();
    }

    @Override
    public String pack(String appId, String commitId, String appCompileHome) throws Exception {
        try (Docker docker = new Docker()) {
            String repo = targetPath + "/" + appId;
            String image = repo + ":" + commitId;

            docker.build(image, appCompileHome, dockerfile);
            // TODO push 没有报错，但有时 pull 会提示 image 不存在
            docker.push(image);
            return image;
        }
    }
}
