-
Notifications
You must be signed in to change notification settings - Fork 691
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add container run and commit layer command
- Loading branch information
Showing
5 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Copyright 2020 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Extracts the last layer of a docker image out of an image tarball | ||
Takes three arguments, the path to the tarball, and the output file for the layer, and the output file for the layer diffID | ||
""" | ||
|
||
|
||
from __future__ import print_function | ||
from json import JSONDecoder | ||
import hashlib | ||
import sys | ||
import tarfile | ||
|
||
|
||
def extract_last_layer(tar_path, layer_path, diffid_path): | ||
"""Extracts the last layer from a docker image from an image tarball | ||
Args: | ||
tar_path: str path to the tarball | ||
layer_path: str path for the output layer | ||
diffid_path: str path for the layer diff ID | ||
Returns: | ||
str the diff ID of the layer | ||
""" | ||
tar = tarfile.open(tar_path, mode="r") | ||
|
||
decoder = JSONDecoder() | ||
try: | ||
# Extracts it as a file object (not to the disk) | ||
manifest = tar.extractfile("manifest.json").read().decode("utf-8") | ||
except Exception as e: | ||
print(( | ||
"Unable to extract manifest.json, make sure {} " | ||
"is a valid docker image.\n").format(tar_path), | ||
e, | ||
file=sys.stderr) | ||
exit(1) | ||
|
||
# Get the manifest dictionary from JSON | ||
manifest = decoder.decode(manifest)[0] | ||
|
||
layers = manifest["Layers"] | ||
|
||
last_layer_path = layers[-1] | ||
|
||
layer_id = last_layer_path.split("/")[0] | ||
|
||
diff_id = hashlib.sha256() | ||
|
||
try: | ||
last_layer = tar.extractfile(last_layer_path) | ||
with open(layer_path, "wb") as f: | ||
while True: | ||
buf = last_layer.read(4096) | ||
if buf: | ||
diff_id.update(buf) | ||
f.write(buf) | ||
else: | ||
break | ||
except Exception as e: | ||
print(( | ||
"Unable to extract last layer {} to {}, make sure {} " | ||
"is a valid docker image and that the layer path is writable\n").format(layer_id, layer_path, tar_path), | ||
e, | ||
file=sys.stderr) | ||
exit(1) | ||
|
||
diff_id_digest = diff_id.hexdigest() | ||
try: | ||
with open(diffid_path, "w") as f: | ||
f.write(diff_id_digest) | ||
except Exception as e: | ||
print("Unable to write layer Diff ID {} to {}, make sure the path is writeable\n".format(diff_id_digest, diffid_path), e, file=sys.stderr) | ||
exit(1) | ||
|
||
return layer_id | ||
|
||
|
||
if __name__ == "__main__": | ||
print(extract_last_layer(sys.argv[1], sys.argv[2], sys.argv[3])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/bin/bash | ||
|
||
set -ex | ||
|
||
# Load utils | ||
source %{util_script} | ||
|
||
# Resolve the docker tool path | ||
DOCKER="%{docker_tool_path}" | ||
DOCKER_FLAGS="%{docker_flags}" | ||
|
||
if [[ -z "$DOCKER" ]]; then | ||
echo >&2 "error: docker not found; do you need to manually configure the docker toolchain?" | ||
exit 1 | ||
fi | ||
|
||
# Load the image and remember its name | ||
image_id=$(%{image_id_extractor_path} %{image_tar}) | ||
$DOCKER $DOCKER_FLAGS load -i %{image_tar} | ||
|
||
id=$($DOCKER $DOCKER_FLAGS run -d %{docker_run_flags} $image_id %{commands}) | ||
# Actually wait for the container to finish running its commands | ||
retcode=$($DOCKER $DOCKER_FLAGS wait $id) | ||
# Trigger a failure if the run had a non-zero exit status | ||
if [ $retcode != 0 ]; then | ||
$DOCKER $DOCKER_FLAGS logs $id && false | ||
fi | ||
OUTPUT_IMAGE_TAR="%{output_layer_tar}.image.tar" | ||
reset_cmd $image_id $id %{output_image} | ||
$DOCKER $DOCKER_FLAGS save %{output_image} -o $OUTPUT_IMAGE_TAR | ||
$DOCKER $DOCKER_FLAGS rm $id | ||
$DOCKER $DOCKER_FLAGS rmi %{output_image} | ||
|
||
%{image_last_layer_extractor_path} $OUTPUT_IMAGE_TAR %{output_layer_tar} %{output_diff_id} | ||
|
||
rm $OUTPUT_IMAGE_TAR | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters