Complete the diff generating functionality.
This commit is contained in:
parent
decb324411
commit
a1164269be
6 changed files with 113 additions and 32 deletions
|
@ -1,4 +1,3 @@
|
|||
|
||||
import logging
|
||||
import json
|
||||
|
||||
|
@ -11,7 +10,7 @@ import storage
|
|||
|
||||
from app import app
|
||||
from auth.auth import process_auth, extract_namespace_repo_from_session
|
||||
from util import checksums
|
||||
from util import checksums, changes
|
||||
from auth.permissions import (ReadRepositoryPermission,
|
||||
ModifyRepositoryPermission)
|
||||
from data import model
|
||||
|
@ -315,6 +314,49 @@ def delete_repository_storage(namespace, repository):
|
|||
|
||||
|
||||
def process_image_changes(namespace, repository, image_id):
|
||||
return
|
||||
logger.debug('Generating diffs for image: %s' % image_id)
|
||||
|
||||
image_diffs_path = store.image_file_diffs_path(namespace, repository,
|
||||
image_id)
|
||||
if store.exists(image_diffs_path):
|
||||
logger.debug('Diffs already exist for image: %s' % image_id)
|
||||
return
|
||||
|
||||
image = model.get_image_by_id(namespace, repository, image_id)
|
||||
model.get_parent_images()
|
||||
parents = model.get_parent_images(image)
|
||||
|
||||
# Compute the diffs and fs for the parent first if necessary
|
||||
parent_trie_path = None
|
||||
if parents:
|
||||
parent_trie_path = process_image_changes(namespace, repository,
|
||||
parents[-1].docker_image_id)
|
||||
|
||||
# Read in the collapsed layer state of the filesystem for the parent
|
||||
parent_trie = changes.empty_fs()
|
||||
if parent_trie_path:
|
||||
with store.stream_read_file(parent_trie_path) as parent_trie_stream:
|
||||
parent_trie.read(parent_trie_stream)
|
||||
|
||||
# Read in the file entries from the layer tar file
|
||||
layer_path = store.image_layer_path(namespace, repository, image_id)
|
||||
with store.stream_read_file(layer_path) as layer_tar_stream:
|
||||
removed_files = set()
|
||||
layer_files = changes.files_and_dirs_from_tar(layer_tar_stream,
|
||||
removed_files)
|
||||
|
||||
new_metadata = changes.compute_new_diffs_and_fs(parent_trie, layer_files,
|
||||
removed_files)
|
||||
(new_trie, added, changed, removed) = new_metadata
|
||||
|
||||
# Write out the new trie
|
||||
new_trie_path = store.image_file_trie_path(namespace, repository, image_id)
|
||||
store.put_content(new_trie_path, new_trie.tobytes())
|
||||
|
||||
# Write out the diffs
|
||||
diffs = {}
|
||||
sections = ('added', 'changed', 'removed')
|
||||
for section, source_trie in zip(sections, new_metadata[1:]):
|
||||
diffs[section] = list(source_trie)
|
||||
store.put_content(image_diffs_path, json.dumps(diffs, indent=2))
|
||||
|
||||
return new_trie_path
|
||||
|
|
Reference in a new issue