| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
# |
| 15 |
# Authors: |
| 16 |
# Chandan Singh <[email protected]> |
| 17 |
|
| 18 |
# This is a helper script to generate Docker images using checkouts of |
| 19 |
# BuildStream elements. |
| 20 |
|
| 21 |
usage() { |
| 22 |
cat <<EOF |
| 23 |
|
| 24 |
USAGE: $(basename "$0") [-c BST_CMD] [-m MESSAGE] [-t TAG] [-h] ELEMENT |
| 25 |
|
| 26 |
Create a Docker image from bst checkout of an element. |
| 27 |
|
| 28 |
OPTIONS: |
| 29 |
-c BST_CMD Path to BuildStream command (default: bst). |
| 30 |
-m MESSAGE Commit message for the imported image. |
| 31 |
-t TAG Tag of the imported image. |
| 32 |
-h Print this help text and exit. |
| 33 |
|
| 34 |
EXAMPLES: |
| 35 |
|
| 36 |
# Import hello.bst as a Docker image with tag "bst-hello" and message "hello" |
| 37 |
$(basename "$0") -m hello -t bst-hello hello.bst |
| 38 |
|
| 39 |
# Import hello.bst as a Docker image with tag "bst-hello" using bst-here |
| 40 |
$(basename "$0") -c bst-here -t bst-hello hello.bst |
| 41 |
|
| 42 |
EOF |
| 43 |
exit "$1" |
| 44 |
} |
| 45 |
|
| 46 |
die() { |
| 47 |
echo "FATAL: $1" >&2 |
| 48 |
exit 1 |
| 49 |
} |
| 50 |
|
| 51 |
bst_cmd=bst |
| 52 |
docker_import_cmd=(docker import) |
| 53 |
docker_image_tag= |
| 54 |
|
| 55 |
while getopts c:m:t:h arg |
| 56 |
do |
| 57 |
case $arg in |
| 58 |
c) |
| 59 |
bst_cmd="$OPTARG" |
| 60 |
;; |
| 61 |
m) |
| 62 |
docker_import_cmd+=('-m' "$OPTARG") |
| 63 |
;; |
| 64 |
t) |
| 65 |
docker_image_tag="$OPTARG" |
| 66 |
;; |
| 67 |
h) |
| 68 |
usage 0 |
| 69 |
;; |
| 70 |
\?) |
| 71 |
usage 1 |
| 72 |
esac |
| 73 |
done |
| 74 |
|
| 75 |
shift $((OPTIND-1)) |
| 76 |
if [[ "$#" != 1 ]]; then |
| 77 |
echo "$0: No element specified" >&2 |
| 78 |
usage 1 |
| 79 |
fi |
| 80 |
element="$1" |
| 81 |
|
| 82 |
# Dump to a temporary file in the current directory. |
| 83 |
# NOTE: We use current directory to try to ensure compatibility with scripts |
| 84 |
# like bst-here, assuming that the current working directory is mounted |
| 85 |
# inside the container. |
| 86 |
|
| 87 |
checkout_tar="bst-checkout-$(basename "$element")-$RANDOM.tar" |
| 88 |
|
| 89 |
echo "INFO: Checking out $element ..." >&2 |
| 90 |
$bst_cmd artifact checkout "$element" --tar "$checkout_tar" || die "Failed to checkout $element" |
| 91 |
echo "INFO: Successfully checked out $element" >&2 |
| 92 |
|
| 93 |
echo "INFO: Importing Docker image ..." >&2 |
| 94 |
"${docker_import_cmd[@]}" "$checkout_tar" "$docker_image_tag" || die "Failed to import Docker image from tarball" |
| 95 |
echo "INFO: Successfully import Docker image $docker_image_tag" >&2 |
| 96 |
|
| 97 |
echo "INFO: Cleaning up ..." >&2 |
| 98 |
rm "$checkout_tar" || die "Failed to remove $checkout_tar" |
| 99 |
echo "INFO: Clean up finished" >&2 |