def _push(self, image): cmd = "{0} push {1}".format(DOCKER_BIN, image) logger.info("mq_work.image_manual", cmd) retry = 2 while retry: try: p = shell.start(cmd) while p.is_running(): line = p.readline() self.log.debug(line.rstrip('\n').lstrip('\x1b[1G'), step="push-image") for line in p.unread_lines: self.log.debug(line, step="push-image") if p.exit_with_err(): self.log.error("上传镜像失败。" + ("开始进行重试." if retry > 0 else ""), step="push-image", status="failure") retry -= 1 continue return True except shell.ExecException, e: self.log.error("上传镜像发生错误。{}" + ( "开始进行重试." if retry > 0 else "").format(e.message)) logger.error(e) retry -= 1
def _push(self, image): cmd = "{} push {}".format(DOCKER_BIN, image) logger.info("mq_work.app_image", cmd) retry = 2 while retry: try: p = shell.start(cmd) while p.is_running(): line = p.readline() self.log.debug( line.rstrip('\n').lstrip('\x1b[1G'), step="push-image") for line in p.unread_lines: self.log.debug(line, step="push-image") if p.exit_with_err(): self.log.error( "上传镜像失败。" + ("开始进行重试." if retry > 0 else ""), step="push-image", status="failure") retry -= 1 continue return True except shell.ExecException, e: self.log.error("上传镜像发生错误。{0}" + ("开始进行重试." if retry > 0 else "").format(e.message)) logger.error(e) retry -= 1
def build_code(self): self.log.info("开始编译代码包", step="build_code") package_name = '{0}/{1}.tgz'.format(self.tgz_dir, self.deploy_version) self.logfile = '{0}/{1}.log'.format(self.tgz_dir, self.deploy_version) repos = self.repo_url.split(" ") self.log.debug("repos=" + repos[1], step="build_code") #master no_cache = self.build_envs.pop('NO_CACHE', False) if no_cache: try: shutil.rmtree(self.cache_dir) os.makedirs(self.cache_dir) os.chown(self.cache_dir, 200, 200) self.log.debug("清理缓存目录{0}".format(self.cache_dir), step="build_code") except Exception as e: self.log.error("清理缓存目录{0}失败{1}".format(self.cache_dir, e.message), step="build_code") pass try: cmd = "perl {0} -b {1} -s {2} -c {3} -d {4} -v {5} -l {6} -tid {7} -sid {8} --name {9}".format( self.build_cmd, repos[1], self.source_dir, self.cache_dir, self.tgz_dir, self.deploy_version, self.logfile, self.tenant_id, self.service_id, self.build_name) if self.build_envs: build_env_string = ':::'.join( map(lambda x: "{}='{}'".format(x, self.build_envs[x]), self.build_envs.keys())) cmd += " -e {}".format(build_env_string) p = shell.start(cmd) while p.is_running(): line = p.readline() self.log.debug(line.rstrip('\n').lstrip('\x1b[1G'), step="build_code") for line in p.unread_lines: self.log.debug(line, step="build_code") if p.exit_with_err(): self.log.error("编译代码包失败。", step="build_code", status="failure") return False self.log.debug("编译代码包完成。", step="build_code", status="success") except shell.ExecException, e: self.log.error("编译代码包过程遇到异常,{}".format(e.message), step="build_code", status="failure") return False
def build_code(self): self.log.info("开始编译代码包", step="build_code") package_name = '{0}/{1}.tgz'.format(self.tgz_dir, self.deploy_version) self.logfile = '{0}/{1}.log'.format(self.tgz_dir, self.deploy_version) repos = self.repo_url.split(" ") self.log.debug("repos=" + repos[1], step="build_code") #master no_cache = self.build_envs.pop('NO_CACHE', False) if no_cache: try: shutil.rmtree(self.cache_dir) os.makedirs(self.cache_dir) self.log.debug( "清理缓存目录{0}".format(self.cache_dir), step="build_code") except Exception as e: self.log.error( "清理缓存目录{0}失败{1}".format(self.cache_dir, e.message), step="build_code") pass try: cmd = "perl {0} -b {1} -s {2} -c {3} -d {4} -v {5} -l {6} -tid {7} -sid {8} --name {9}".format( self.build_cmd, repos[1], self.source_dir, self.cache_dir, self.tgz_dir, self.deploy_version, self.logfile, self.tenant_id, self.service_id, self.build_name) if self.build_envs: build_env_string = ':::'.join( map(lambda x: "{}='{}'".format(x, self.build_envs[x]), self.build_envs.keys())) cmd += " -e {}".format(build_env_string) p = shell.start(cmd) while p.is_running(): line = p.readline() self.log.debug( line.rstrip('\n').lstrip('\x1b[1G'), step="build_code") for line in p.unread_lines: self.log.debug(line, step="build_code") if p.exit_with_err(): self.log.error("编译代码包失败。", step="build_code", status="failure") return False self.log.debug("编译代码包完成。", step="build_code", status="success") except shell.ExecException, e: self.log.error( "编译代码包过程遇到异常,{}".format(e.message), step="build_code", status="failure") return False
def build_image(self): # self.write_build_log(u"开始编译Dockerfile") ''' insert_lines = [ 'RUN which wget || (apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y wget) || (yum install -y wget)', 'RUN which curl || (apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y curl) || (yum install -y curl)', 'RUN mkdir -pv /opt/bin', 'ADD ./.goodrain/init /opt/bin/init', '', 'RUN wget http://lang.goodrain.me/public/gr-listener -O /opt/bin/gr-listener -q && \\', ' chmod 755 /opt/bin/*', '', 'RUN rm -rf /var/lib/dpkg/* /var/lib/apt/*', 'ENTRYPOINT ["/opt/bin/init"]', ] ''' dockerfile = '{0}/{1}'.format(self.source_dir, 'Dockerfile') update_items, entrypoint, cmd = self.get_dockerfile_items(dockerfile) # 重新解析dockerfile pdf = None try: self.log.info("开始解析Dockerfile", step="build_image") pdf = ParseDockerFile(dockerfile) except ValueError as e: self.log.error("用户自定义的volume路径包含相对路径,必须为绝对路径!", step="build_image", status="failure") logger.exception(e) return False except Exception as e: self.log.error("解析Dockerfile发生异常", step="build_image", status="failure") logger.exception(e) s = self.repo_url regex = re.compile(r'.*(?:\:|\/)([\w\-\.]+)/([\w\-\.]+)\.git') m = regex.match(s) account, project = m.groups() _name = '_'.join([self.service_id[:12], account, project]) _tag = self.deploy_version build_image_name = '{0}/{1}:{2}'.format(REGISTRY_DOMAIN, _name, _tag) # image name must be lower build_image_name = build_image_name.lower() self.log.debug("构建镜像名称为{0}".format(build_image_name), step="build_image") #build_image_name="" no_cache = self.build_envs.pop('NO_CACHE', False) if no_cache: build_cmd = "{0} build -t {1} --no-cache .".format( DOCKER_BIN, build_image_name) else: build_cmd = "{0} build -t {1} .".format(DOCKER_BIN, build_image_name) p = shell.start(build_cmd, cwd=self.source_dir) while p.is_running(): line = p.readline() self.log.debug(line, step="build_image") for line in p.unread_lines: self.log.debug(line, step="build_image") if p.exit_with_err(): self.log.error("构建失败,请检查Debug日志排查!", step="build_image", status="failure") return False self.log.debug("镜像构建成功。开始推送", step="build_image", status="success") try: shell.call("{0} push {1}".format(DOCKER_BIN, build_image_name)) except shell.ExecException, e: self.log.error("镜像推送失败。{}".format(e.message), step="push_image", status="failure") return False
def build_image(self): # self.write_build_log(u"开始编译Dockerfile") ''' insert_lines = [ 'RUN which wget || (apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y wget) || (yum install -y wget)', 'RUN which curl || (apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y curl) || (yum install -y curl)', 'RUN mkdir -pv /opt/bin', 'ADD ./.goodrain/init /opt/bin/init', '', 'RUN wget http://lang.goodrain.me/public/gr-listener -O /opt/bin/gr-listener -q && \\', ' chmod 755 /opt/bin/*', '', 'RUN rm -rf /var/lib/dpkg/* /var/lib/apt/*', 'ENTRYPOINT ["/opt/bin/init"]', ] ''' dockerfile = '{0}/{1}'.format(self.source_dir, 'Dockerfile') update_items, entrypoint, cmd = self.get_dockerfile_items(dockerfile) # 重新解析dockerfile pdf = None try: self.log.info("开始解析Dockerfile", step="build_image") pdf = ParseDockerFile(dockerfile) except ValueError as e: self.log.error( "用户自定义的volume路径包含相对路径,必须为绝对路径!", step="build_image", status="failure") logger.exception(e) return False except Exception as e: self.log.error( "解析Dockerfile发生异常", step="build_image", status="failure") logger.exception(e) s = self.repo_url regex = re.compile(r'.*(?:\:|\/)([\w\-\.]+)/([\w\-\.]+)\.git') m = regex.match(s) account, project = m.groups() _name = '_'.join([self.service_id[:12], account, project]) _tag = self.deploy_version build_image_name = '{0}/{1}:{2}'.format(REGISTRY_DOMAIN, _name, _tag) # image name must be lower build_image_name = build_image_name.lower() self.log.debug( "构建镜像名称为{0}".format(build_image_name), step="build_image") #build_image_name="" no_cache = self.build_envs.pop('NO_CACHE', False) if no_cache: build_cmd = "{0} build -t {1} --no-cache .".format( DOCKER_BIN, build_image_name) else: build_cmd = "{0} build -t {1} .".format(DOCKER_BIN, build_image_name) p = shell.start(build_cmd, cwd=self.source_dir) while p.is_running(): line = p.readline() self.log.debug(line, step="build_image") for line in p.unread_lines: self.log.debug(line, step="build_image") if p.exit_with_err(): self.log.error( "构建失败,请检查Debug日志排查!", step="build_image", status="failure") return False self.log.debug("镜像构建成功。开始推送", step="build_image", status="success") try: shell.call("{0} push {1}".format(DOCKER_BIN, build_image_name)) except shell.ExecException, e: self.log.error( "镜像推送失败。{}".format(e.message), step="push_image", status="failure") return False