def export_container_image(self, filename, transport, image_ref, additional_refs=None): """ Exports the working container to a container image archive :param str filename: The resulting filename :param str transport: The archive format :param str image_name: Name of the exported image :param str image_tag: Tag of the exported image :param list additional_tags: List of additional references """ extra_tags_opt = [] if additional_refs: for ref in additional_refs: extra_tags_opt.extend(['--additional-tag', ref]) # make sure the target tar file does not exist # skopeo doesn't support force overwrite Path.wipe(filename) if self.working_image: export_image = self.working_image elif self.imported_image: export_image = self.imported_image else: raise KiwiBuildahError("There is no image to export defined") # we are using 'skopeo copy' to export images instead of 'buildah push' # because buildah does not support multiple tags Command.run([ 'skopeo', 'copy', 'containers-storage:{0}'.format(export_image), '{0}:{1}:{2}'.format(transport, filename, image_ref) ] + extra_tags_opt)
def import_container_image(self, container_image_ref): """ Imports container image reference to the OCI containers storage. :param str container_image_ref: container image reference """ if not self.imported_image: self.imported_image = 'kiwi-image-{0}:{1}'.format( self._random_string_generator(), Defaults.get_container_base_image_tag() ) else: raise KiwiBuildahError( "Image already imported, called: '{0}'".format( self.imported_image ) ) # We are making use of skopeo instead of only calling 'buildah from' # because we want to control the image name loaded into the containers # storage. This way we are certain to not leave any left over after the # build. Command.run( [ 'skopeo', 'copy', container_image_ref, 'containers-storage:{0}'.format(self.imported_image) ] ) if not self.working_container: self.working_container = 'kiwi-container-{0}'.format( self._random_string_generator() ) else: raise KiwiBuildahError( "Container already initated, called: '{0}'".format( self.working_container ) ) Command.run( [ 'buildah', 'from', '--name', self.working_container, 'containers-storage:{0}'.format(self.imported_image) ] )
def init_container(self): """ Initialize a new container in OCI containers storage """ if not self.working_container: self.working_container = 'kiwi-container-{0}'.format( self._random_string_generator()) else: raise KiwiBuildahError( "Image already imported or initated at '{0}' container".format( self.working_container)) Command.run( ['buildah', 'from', '--name', self.working_container, 'scratch'])
def post_process(self): """ Commits the OCI container into an OCI image """ if not self.working_image and self.working_container: self.working_image = 'kiwi-image-{0}:{1}'.format( self._random_string_generator(), 'tag-{0}'.format(self._random_string_generator())) else: raise KiwiBuildahError( "No container to commit or container already committed") output = Command.run([ 'buildah', 'commit', '--rm', '--format', 'oci', self.working_container, self.working_image ]) self.working_image = output.output.rstrip() self.working_container = None