Example #1
0
 def gen_fs_hash(self):
     '''Get the filesystem hash if the image class was created with a
     tar_file'''
     if self.__tar_file:
         fs_dir = rootfs.get_untar_dir(self.__tar_file)
         tar_file = rootfs.get_layer_tar_path(self.__tar_file)
         # remove the fs directory if it already exists
         if os.path.isdir(fs_dir):
             rootfs.root_command(rootfs.remove, fs_dir)
         rootfs.extract_layer_tar(tar_file, fs_dir)
         self.__fs_hash = rootfs.calc_fs_hash(fs_dir)
Example #2
0
def setup(working_dir=None):
    """Environment setup
    working_dir: a directory path other than the default directory"""
    # create the top directory and cache file
    logger.debug("Setting up...")
    top_dir = general.get_top_dir(working_dir)
    if not os.path.isdir(top_dir):
        os.makedirs(top_dir)
    # set the working directory according to user input
    rootfs.set_working_dir(working_dir)
    # load the cache
    cache.load()
    # required to run in a container natively on Windows
    fs_hash_path = pkg_resources.resource_filename("tern", "tools/fs_hash.sh")
    rootfs.root_command(["chmod", "+x", fs_hash_path])
Example #3
0
def recover():
    """Undo all the mounts and clean up directories"""
    try:
        rootfs.undo_mount()
    except subprocess.CalledProcessError:
        pass
    try:
        rootfs.unmount_rootfs()
    except subprocess.CalledProcessError:
        pass
    # we nuke all the directories after mounting
    rootfs.clean_up()
    working_dir = rootfs.get_working_dir()
    if os.path.exists(working_dir):
        rootfs.root_command(rootfs.remove, working_dir)
Example #4
0
def apply_layers(image_obj, top_layer):
    """Apply image diff layers without using a kernel snapshot driver"""
    # All merging happens in the merge directory
    target = os.path.join(rootfs.get_working_dir(), constants.mergedir)
    layer_dir = image_obj.layers[top_layer].get_untar_dir()
    layer_contents = layer_dir + '/*'
    # Account for whiteout files
    for fd in image_obj.layers[top_layer].files:
        if fd.is_whiteout:
            # delete the corresponding file or directory in the target
            # directory as well as the layer contents directory
            deleted = fd.name.replace('.wh.', '')
            delpath = os.path.join(target, os.path.dirname(fd.path), deleted)
            if os.path.exists(delpath):
                if os.path.isfile(delpath):
                    rootfs.root_command(['rm'], delpath)
                else:
                    rootfs.root_command(rootfs.remove, delpath)
                os.remove(os.path.join(layer_dir, fd.path))
    # Finally, bulk copy the layer contents into the target directory
    # if there are any files to move
    if os.listdir(layer_dir):
        rootfs.root_command(['cp', '-r'] + glob.glob(layer_contents), target)
    return target
Example #5
0
def clean_image_tars(image_obj):
    """Given an image object, clean up all the image layer contents"""
    for layer in image_obj.layers:
        fspath = rootfs.get_untar_dir(layer.tar_file)
        if os.path.exists(fspath):
            rootfs.root_command(rootfs.remove, fspath)
Example #6
0
def clean_image_tars(image_obj):
    '''Clean up untar directories'''
    for layer in image_obj.layers:
        fspath = rootfs.get_untar_dir(layer.tar_file)
        if os.path.exists(fspath):
            rootfs.root_command(rootfs.remove, fspath)
Example #7
0
def cleanup():
    """Clean up the working directory"""
    rootfs.clean_up()
    rootfs.root_command(rootfs.remove, rootfs.get_working_dir())