Skip to content

Commit e1c010a

Browse files
committed
Extract scripts: Added ability to handle images by SHA256 (no attempt to download)
1 parent 7ce7232 commit e1c010a

File tree

2 files changed

+75
-32
lines changed

2 files changed

+75
-32
lines changed

scripts/container-extract.sh

+39-8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@
22

33
# This script is authored by Robert Altman, OptumRx
44
5-
# Version 1.1.1
5+
# Version 1.2.0
66
# https://github.com/optum-rx-tech-ops/devsecops-team/blob/main/Docker/Scripts/container-extract.sh
77

88
# Requirements:
99
# * Docker Desktop, or docker cli
1010

11+
is_sha256()
12+
{
13+
# Validate parameters
14+
if [ -z "$1" ]; then
15+
return 0
16+
fi
17+
18+
[[ "$1" =~ ^[a-f0-9]{12}$|^[a-f0-9]{64}$ ]]
19+
return $?
20+
}
21+
1122
# Validate parameters
1223
# TBD
1324

@@ -31,20 +42,40 @@ fi
3142
# Create image folder
3243
mkdir -p ${image_folder}
3344

45+
if is_sha256 "${image_name}"; then
46+
echo "Image name is a sha256 hash"
47+
else
48+
# Check for the docker image and download if needed
49+
if [ -z "$(docker image ls -q ${image_name} 2> /dev/null)" ]; then
50+
echo Pulling image ${image_name}
51+
docker image pull "${image_name}"
52+
if [ $? -ne 0 ]; then
53+
echo Could not pull docker image; exiting
54+
rm -rf "${image_folder}"
55+
exit $?
56+
fi
57+
fi
58+
fi
59+
3460
# start docker image and container in background; captute the new container ID
3561
echo Starting container for image ${image_name}
36-
container_id=$(docker run --rm --interactive --detach --entrypoint="sh" "${image_name}" )
62+
container_id=$(docker container run --rm --interactive --detach --entrypoint "sh" "${image_name}" )
3763
if [ $? -ne 0 ]
3864
then
39-
echo Container could not be started; exiting
40-
exit $?
41-
else
42-
echo Container ID: $container_id
65+
container_id=$(docker container run --rm --interactive --detach "${image_name}" )
66+
if [ $? -ne 0 ]
67+
then
68+
echo Container could not be started; exiting
69+
rm -rf "${image_folder}"
70+
exit $?
71+
fi
4372
fi
4473

74+
echo Container ID: $container_id
75+
4576
# Export the file system to a tar file
4677
echo Writing filesystem to ${image_tar}
47-
docker export --output="${image_tar}" ${container_id}
78+
docker container export --output="${image_tar}" ${container_id}
4879
if [ $? -ne 0 ]
4980
then
5081
echo Export failed; exiting
@@ -53,7 +84,7 @@ fi
5384

5485
# Stop the container; no cleanup needed since we used the --rm flag
5586
echo Stopping container ${contained_id}
56-
docker stop $container_id
87+
docker container stop $container_id
5788
if [ $? -ne 0 ]
5889
then
5990
echo Error stopping the conatainer; continuing anyway

scripts/image-extract.sh

+36-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# This script is authored by Robert Altman, OptumRx
44
5-
# Version 1.1.1
5+
# Version 1.2.0
66
# https://github.com/optum-rx-tech-ops/devsecops-team/blob/main/Docker/Scripts/image-extract.sh
77

88
# Requirements:
@@ -12,21 +12,32 @@
1212
# Format a JSON file; tests file existence and type, so it is safe to use on any filename
1313
jq_format_file()
1414
{
15-
# Validate paramters
15+
# Validate parameters
1616
if [ -z "$1" ]; then
17-
return 0
17+
return 0
1818
fi
1919

2020
tmp_file=jq-temp.json
2121
the_file="${1}"
2222

2323
# Check if the file is JSON; if it is, format it to a temp file and then replace the original with the formatted file; clean up afterwards
2424
if file --brief "${the_file}" | grep -q "JSON"; then
25-
echo Formatting ${the_file} as JSON
26-
jq "." "${the_file}" > "${tmp_file}" && cp -f "${tmp_file}" "${the_file}" && rm "${tmp_file}"
25+
echo Formatting ${the_file} as JSON
26+
jq "." "${the_file}" > "${tmp_file}" && cp -f "${tmp_file}" "${the_file}" && rm "${tmp_file}"
2727
fi
2828
}
2929

30+
is_sha256()
31+
{
32+
# Validate parameters
33+
if [ -z "$1" ]; then
34+
return 0
35+
fi
36+
37+
[[ "$1" =~ ^[a-f0-9]{12}$|^[a-f0-9]{64}$ ]]
38+
return $?
39+
}
40+
3041
# Validate parameters
3142
# TBD
3243

@@ -39,10 +50,10 @@ image_tar="${image_folder}".tar
3950
blobs_path="blobs/sha256/"
4051
blobs_path_len=${#blobs_path}
4152

42-
#echo image_name: ${image_name}
43-
#echo image_folder: ${image_folder}
44-
#echo image_tar: ${image_tar}
45-
#echo tmp_file: ${tmp_file}
53+
echo image_name: ${image_name}
54+
echo image_folder: ${image_folder}
55+
echo image_tar: ${image_tar}
56+
echo tmp_file: ${tmp_file}
4657

4758
# Check if folder exists; if it does, query user and remove it
4859
# TBD - query user before continuing
@@ -54,31 +65,32 @@ fi
5465
# Create image folder
5566
mkdir -p ${image_folder}
5667

57-
# Clear display
58-
#read -s -k '?Press enter to continue.'
59-
#clear
60-
61-
# Check for the docker image and download if needed
62-
if [ -z "$(docker images -q ${image_name} 2> /dev/null)" ]; then
63-
echo Pulling image ${image_name}
64-
docker pull "${image_name}"
65-
if [ $? -ne 0 ]
66-
then
67-
echo Could not pull docker image; exiting
68-
exit $?
68+
if is_sha256 "${image_name}"; then
69+
echo "Image name is a sha256 hash"
70+
else
71+
# Check for the docker image and download if needed
72+
if [ -z "$(docker image ls -q ${image_name} 2> /dev/null)" ]; then
73+
echo Pulling image ${image_name}
74+
docker image pull "${image_name}"
75+
if [ $? -ne 0 ]; then
76+
echo Could not pull docker image; exiting
77+
rm -rf "${image_folder}"
78+
exit $?
79+
fi
6980
fi
7081
fi
7182

7283
# Display layer info (visual nicety)
73-
docker history ${image_name}
74-
docker history --no-trunc --format 'table {{.ID}}\t{{printf "%.10s" .CreatedAt}}\t{{.Size}}\t{{.Comment}}\n{{.CreatedBy}}\n' "${image_name}" > "${image_folder}/${image_folder}"_history.txt
84+
docker image history ${image_name}
85+
docker image history --no-trunc --format 'table {{.ID}}\t{{printf "%.10s" .CreatedAt}}\t{{.Size}}\t{{.Comment}}\n{{.CreatedBy}}\n' "${image_name}" > "${image_folder}/${image_folder}"_history.txt
7586

7687
# Export the image
7788
echo Exporting image ...
78-
docker save "${image_name}" -o "${image_tar}"
89+
docker image save "${image_name}" -o "${image_tar}"
7990
if [ $? -ne 0 ]
8091
then
8192
echo Save failed; exiting
93+
rm -rf "${image_folder}"
8294
exit $?
8395
fi
8496

0 commit comments

Comments
 (0)