Merge ~ilasc/launchpad-buildd:upload-gzipped-oci-layers into launchpad-buildd:master

Proposed by Ioana Lasc
Status: Merged
Approved by: Ioana Lasc
Approved revision: c00ee74eec3fb7cfdfee8a8e6fd410077a4d93b6
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~ilasc/launchpad-buildd:upload-gzipped-oci-layers
Merge into: launchpad-buildd:master
Diff against target: 78 lines (+24/-14)
1 file modified
lpbuildd/oci.py (+24/-14)
Reviewer Review Type Date Requested Status
Tom Wardill (community) Approve
Review via email: mp+404367@code.launchpad.net

Commit message

Upload oci layers to librarian as tar.gz

To post a comment you must log in.
Revision history for this message
Tom Wardill (twom) wrote :

This LGTM.
We can't deploy this until the matching LP change is in production though.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lpbuildd/oci.py b/lpbuildd/oci.py
2index 53198c8..15bf5b8 100644
3--- a/lpbuildd/oci.py
4+++ b/lpbuildd/oci.py
5@@ -5,6 +5,7 @@ from __future__ import print_function
6
7 __metaclass__ = type
8
9+import gzip
10 import hashlib
11 import json
12 import os
13@@ -168,7 +169,7 @@ class OCIBuildManager(SnapBuildProxyMixin, DebianBuildManager):
14 raise
15
16 current_dir = ''
17- directory_tar = None
18+ gzip_layer = None
19 symlinks = []
20 try:
21 # The tarfile is a stream and must be processed in order
22@@ -178,14 +179,9 @@ class OCIBuildManager(SnapBuildProxyMixin, DebianBuildManager):
23 # directly, so keep track of what dir we're in.
24 if file.isdir():
25 current_dir = file.name
26- if directory_tar:
27+ if gzip_layer:
28 # Close the old directory if we have one
29- directory_tar.close()
30- # We're going to add the layer.tar to a gzip
31- directory_tar = tarfile.open(
32- os.path.join(
33- extract_path, '{}.tar.gz'.format(file.name)),
34- 'w|gz')
35+ gzip_layer.close()
36 if file.issym():
37 # symlinks can't be extracted or derefenced from a stream
38 # as you can't seek backwards.
39@@ -197,10 +193,23 @@ class OCIBuildManager(SnapBuildProxyMixin, DebianBuildManager):
40 symlinks.append(file)
41 continue
42 if current_dir and file.name.endswith('layer.tar'):
43- # This is the actual layer data, we want to add it to
44- # the directory gzip
45- file.name = file.name.split('/')[1]
46- directory_tar.addfile(file, tar.extractfile(file))
47+ # This is the actual layer data.
48+ # Instead of adding the layer.tar to a gzip directory
49+ # we add the contents of untarred layer.tar to a gzip.
50+ # Now instead of having a gz directory in the form:
51+ # directory.tar.gz/layer.tar/contents
52+ # we will have: layer.tar.gz/contents. This final gz format
53+ # will have to have the name of the directory
54+ # (directory_name.tar.gz/contents) otherwise we will endup
55+ # with multiple gzips with the same name "layer.tar.gz".
56+ fileobj = tar.extractfile(file)
57+ name = os.path.join(extract_path,
58+ '{}.tar.gz'.format(current_dir))
59+ with gzip.GzipFile(name, 'wb') as gzip_layer:
60+ byte = fileobj.read(1)
61+ while len(byte) > 0:
62+ gzip_layer.write(byte)
63+ byte = fileobj.read(1)
64 elif current_dir and file.name.startswith(current_dir):
65 # Other files that are in the layer directories,
66 # we don't care about
67@@ -212,8 +221,9 @@ class OCIBuildManager(SnapBuildProxyMixin, DebianBuildManager):
68 self._builder.log("Tar file processing failed: {}".format(e))
69 raise
70 finally:
71- if directory_tar is not None:
72- directory_tar.close()
73+ if gzip_layer is not None:
74+ gzip_layer.close()
75+ fileobj.close()
76
77 # deal with any symlinks we had
78 for symlink in symlinks:

Subscribers

People subscribed via source and target branches