-
Notifications
You must be signed in to change notification settings - Fork 97
/
containerExtract.sh
executable file
·112 lines (99 loc) · 3.87 KB
/
containerExtract.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash -e
DELETE_LOCAL_IMAGE=""
usage ()
{
echo "Usage: $0 CONTAINER [--override-arch s390x] [--tar-flags tar-extraction-flags] [--delete-before] [--delete-after]"
echo "Usage: $0 quay.io/crw/operator-bundle:latest"
echo "Usage: $0 quay.io/crw/udi-rhel8:2.4"
echo "Usage: $0 quay.io/crw/pluginregistry-rhel8:latest --tar-flags var/www/html/*/external_images.txt"
echo "Usage: $0 quay.io/crw/devfileregistry-rhel8:latest --tar-flags var/www/html/*/external_images.txt --override-arch ppc64le"
echo "
Options:
--delete-before remove any local images before attempting to pull and extract a new copy
--delete-after remove any local images after attempting to pull and extract the container"
exit
}
if [[ $# -lt 1 ]]; then usage; fi
while [[ "$#" -gt 0 ]]; do
case $1 in
'--override-arch') ARCH_OVERRIDE="--override-arch $2"; shift 1;;
'--delete-after') DELETE_LOCAL_IMAGE="${DELETE_LOCAL_IMAGE} after";;
'--delete-before') DELETE_LOCAL_IMAGE="${DELETE_LOCAL_IMAGE} before";;
'--tar-flags' ) TAR_FLAGS="$2"; shift 1;;
'-h') usage;;
*) container="$1";;
esac
shift 1
done
# echo "ARCH_OVERRIDE = $ARCH_OVERRIDE"
# echo "TAR_FLAGS = $TAR_FLAGS"
# echo "container = $container"
PODMAN=$(command -v podman)
if [[ ! -x $PODMAN ]]; then
echo "[WARNING] podman is not installed."
PODMAN=$(command -v docker)
if [[ ! -x $PODMAN ]]; then
echo "[ERROR] docker is not installed. Aborting."; exit 1
fi
fi
if [[ ${ARCH_OVERRIDE} == "" ]] && [[ ${container} == *"-openj9"* ]]; then
ARCH_OVERRIDE="--override-arch s390x"
fi
if [[ $container == *"@"* ]]; then
tmpcontainer="$(echo "$container" | tr "/:@" "--")"
else
tmpcontainer="$(echo "$container" | tr "/:" "--")-$(date +%s)"
fi
unpackdir="/tmp/${tmpcontainer}"
container_alt=""
for container_ref in "$container" "localhost/$container:latest" "localhost/$container"; do
if [[ $DELETE_LOCAL_IMAGE == *"before"* ]]; then
${PODMAN} rmi $container_ref 2>/dev/null >/dev/null || true
fi
container_check="$(${PODMAN} images "$container_ref" -q)"
if [[ $container_check ]]; then
container_alt="$container_check"
echo "[INFO] Using local $container_ref ($container_alt)..."
break
fi
done
if [[ ! $container_alt ]]; then
# get remote image
echo "[INFO] Pulling $container ..."
# shellcheck disable=SC2086
${PODMAN} pull ${ARCH_OVERRIDE} "$container" 2>&1
# throw the same error code that a failed pull throws, in case we're running this in a nested bash shell
${PODMAN} image exists "$container" || exit 125
fi
# create local container
${PODMAN} rm -f "${tmpcontainer}" >/dev/null 2>&1 || true
# use sh for regular containers or ls for scratch containers
if [[ $container_alt ]]; then
${PODMAN} create --name="${tmpcontainer}" "$container_alt" sh >/dev/null 2>&1 || ${PODMAN} create --name="${tmpcontainer}" "$container_alt" ls >/dev/null 2>&1
else
${PODMAN} create --name="${tmpcontainer}" "$container" sh >/dev/null 2>&1 || ${PODMAN} create --name="${tmpcontainer}" "$container" ls >/dev/null 2>&1
fi
# export and unpack
${PODMAN} export "${tmpcontainer}" > "/tmp/${tmpcontainer}.tar"
rm -fr "$unpackdir" || true
mkdir -p "$unpackdir"
# shellcheck disable=SC2086 disable=SC2116
TAR_FLAGS="$(echo $TAR_FLAGS)" # squash duplicate spaces
echo "[INFO] Extract from container (${TAR_FLAGS}) ..."
# shellcheck disable=SC2086
tar xf "/tmp/${tmpcontainer}.tar" --wildcards -C "$unpackdir" ${TAR_FLAGS} || exit 1 # fail if we can't unpack the tar
# cleanup
${PODMAN} rm -f "${tmpcontainer}" >/dev/null 2>&1 || true
rm -fr "/tmp/${tmpcontainer}.tar" || true
if [[ $container_alt ]]; then
echo "[INFO] Container $container ($container_alt) unpacked to $unpackdir"
else
echo "[INFO] Container $container unpacked to $unpackdir"
fi
if [[ $DELETE_LOCAL_IMAGE == *"after"* ]]; then
if [[ $container_alt ]]; then
${PODMAN} rmi $container_alt || true
else
${PODMAN} rmi $container || true
fi
fi