def ensure_proper_shared_image(self): # 在 registry 以及本地寻找合适可用的 shared prepare # 如果找到则保证本地和 registry 里此 image 均可用 # 上述行为成功后返回此 prepare image name # 两处都没有合适的 image name 则返回 None remote_images = self._get_prepare_shared_image_names(True).items() if remote_images: remote_latest = remote_images[0] else: remote_latest = None local_images = self._get_prepare_shared_image_names(False).items() if local_images: local_latest = local_images[0] else: local_latest = None if remote_latest and local_latest: info("found shared prepare image at remote and local, sync ...") if remote_latest[0] > local_latest[0]: if mydocker.pull(remote_latest[1]) != 0: error("FAILED: docker pull {}".format(remote_latest[1])) raise Exception("remote prepare fetching failed.") return remote_latest[1] elif remote_latest[0] < local_latest[0]: if mydocker.push(local_latest[1]) != 0: warn("FAILED: docker push {}".format(local_latest[1])) return local_latest[1] else: return local_latest[1] if remote_latest and local_latest is None: info("found shared prepare image at remote.") if mydocker.pull(remote_latest[1]) != 0: error("FAILED: docker pull {}".format(remote_latest[1])) raise Exception("remote prepare fetching failed.") return remote_latest[1] if remote_latest is None and local_latest: info("found shared prepare image at local.") if mydocker.push(local_latest[1]) != 0: warn("FAILED: docker push {}".format(local_latest[1])) return local_latest[1] if remote_latest is None and local_latest is None: warn( "found no proper shared prepare image neither at local nor remote, rebuild ..." ) return None
def ensure_proper_shared_image(self): # 在 registry 以及本地寻找合适可用的 shared prepare # 如果找到则保证本地和 registry 里此 image 均可用 # 上述行为成功后返回此 prepare image name # 两处都没有合适的 image name 则返回 None remote_images = self._get_prepare_shared_image_names(True).items() if remote_images: remote_latest = remote_images[0] else: remote_latest = None local_images = self._get_prepare_shared_image_names(False).items() if local_images: local_latest = local_images[0] else: local_latest = None if remote_latest and local_latest: info("found shared prepare image at remote and local, sync ...") if remote_latest[0] > local_latest[0]: if mydocker.pull(remote_latest[1]) != 0: error("FAILED: docker pull {}".format(remote_latest[1])) raise Exception("remote prepare fetching failed.") return remote_latest[1] elif remote_latest[0] < local_latest[0]: if mydocker.push(local_latest[1]) != 0: warn("FAILED: docker push {}".format(local_latest[1])) return local_latest[1] else: return local_latest[1] if remote_latest and local_latest is None: info("found shared prepare image at remote.") if mydocker.pull(remote_latest[1]) != 0: error("FAILED: docker pull {}".format(remote_latest[1])) raise Exception("remote prepare fetching failed.") return remote_latest[1] if remote_latest is None and local_latest: info("found shared prepare image at local.") if mydocker.push(local_latest[1]) != 0: warn("FAILED: docker push {}".format(local_latest[1])) return local_latest[1] if remote_latest is None and local_latest is None: warn( "found no proper shared prepare image neither at local nor remote, rebuild ...") return None
def _prepare_act(self, ignore_prepare=False): if self.act is True: return if self.yaml_path is None: raise Exception( 'self.yaml_path not set, can not perform action, only fields defined in lain.yaml is accessible') if ignore_prepare is False: if mydocker.pull(self.build.base) != 0: raise Exception('docker pull {} failed'.format(self.build.base)) self.build_base_image = mydocker.get_image(self.build.base) self.ctx = file_parent_dir(self.yaml_path) self.workdir = DOCKER_APP_ROOT + '/' # '/' is need for COPY in Dockefile self.ignore = ['.git', LAIN_CACHE_DIR, '.vagrant'] self.gen_name = partial(mydocker.gen_image_name, appname=self.appname, meta_version=meta_version(self.ctx)) phases = ('prepare', 'build', 'release', 'test', 'meta') self.img_names = {phase: self.gen_name( phase=phase) for phase in phases} if ignore_prepare or self.build.prepare is None: shared_prepare_image_name = None del self.img_names['prepare'] phases = filter(lambda x: x != 'prepare', phases) else: shared_prepare_image_name = self.ensure_proper_shared_image() if shared_prepare_image_name is None: if self.build.prepare.version is None: shared_prepare_image_name = self._gen_prepare_auto_version_image_name() else: shared_prepare_image_name = self.gen_prepare_shared_image_name() self.img_names['prepare'] = shared_prepare_image_name j2temps = { 'prepare': 'build_dockerfile.j2', 'build': 'build_dockerfile.j2', 'release': 'release_dockerfile.j2', 'test': 'build_dockerfile.j2', 'meta': 'meta_dockerfile.j2' } self.img_temps = {phase: load_template( j2temps[phase]) for phase in phases} self.img_builders = { phase: partial(mydocker.build, name=self.img_names[phase], ignore=self.ignore, template=self.img_temps[phase]) for phase in phases } self.prepare_updater = partial( mydocker.build, ignore=self.ignore, template=load_template('build_dockerfile.j2')) self.act = True
def _prepare_act(self, ignore_prepare=False): if self.act is True: return if self.yaml_path is None: raise Exception( 'self.yaml_path not set, can not perform action, only fields defined in lain.yaml is accessible' ) if ignore_prepare is False: if mydocker.pull(self.build.base) != 0: raise Exception('docker pull {} failed'.format( self.build.base)) self.build_base_image = mydocker.get_image(self.build.base) self.ctx = file_parent_dir(self.yaml_path) self.workdir = DOCKER_APP_ROOT + '/' # '/' is need for COPY in Dockefile self.ignore = ['.git', LAIN_CACHE_DIR, '.vagrant'] self.gen_name = partial(mydocker.gen_image_name, appname=self.appname, meta_version=meta_version(self.ctx)) phases = ('prepare', 'build', 'release', 'test', 'meta') self.img_names = { phase: self.gen_name(phase=phase) for phase in phases } if ignore_prepare or self.build.prepare is None: shared_prepare_image_name = None del self.img_names['prepare'] phases = filter(lambda x: x != 'prepare', phases) else: shared_prepare_image_name = self.ensure_proper_shared_image() if shared_prepare_image_name is None: if self.build.prepare.version is None: shared_prepare_image_name = self._gen_prepare_auto_version_image_name( ) else: shared_prepare_image_name = self.gen_prepare_shared_image_name( ) self.img_names['prepare'] = shared_prepare_image_name j2temps = { 'prepare': 'build_dockerfile.j2', 'build': 'build_dockerfile.j2', 'release': 'release_dockerfile.j2', 'test': 'build_dockerfile.j2', 'meta': 'meta_dockerfile.j2' } self.img_temps = { phase: load_template(j2temps[phase]) for phase in phases } self.img_builders = { phase: partial(mydocker.build, name=self.img_names[phase], ignore=self.ignore, template=self.img_temps[phase]) for phase in phases } self.prepare_updater = partial( mydocker.build, ignore=self.ignore, template=load_template('build_dockerfile.j2')) self.act = True