2
2
3
3
# This script is authored by Robert Altman, OptumRx
4
4
5
- # Version 1.1.1
5
+ # Version 1.2.0
6
6
# https://github.com/optum-rx-tech-ops/devsecops-team/blob/main/Docker/Scripts/image-extract.sh
7
7
8
8
# Requirements:
12
12
# Format a JSON file; tests file existence and type, so it is safe to use on any filename
13
13
jq_format_file ()
14
14
{
15
- # Validate paramters
15
+ # Validate parameters
16
16
if [ -z " $1 " ]; then
17
- return 0
17
+ return 0
18
18
fi
19
19
20
20
tmp_file=jq-temp.json
21
21
the_file=" ${1} "
22
22
23
23
# 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
24
24
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} "
27
27
fi
28
28
}
29
29
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
+
30
41
# Validate parameters
31
42
# TBD
32
43
@@ -39,10 +50,10 @@ image_tar="${image_folder}".tar
39
50
blobs_path=" blobs/sha256/"
40
51
blobs_path_len=${# blobs_path}
41
52
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}
46
57
47
58
# Check if folder exists; if it does, query user and remove it
48
59
# TBD - query user before continuing
54
65
# Create image folder
55
66
mkdir -p ${image_folder}
56
67
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
69
80
fi
70
81
fi
71
82
72
83
# 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
75
86
76
87
# Export the image
77
88
echo Exporting image ...
78
- docker save " ${image_name} " -o " ${image_tar} "
89
+ docker image save " ${image_name} " -o " ${image_tar} "
79
90
if [ $? -ne 0 ]
80
91
then
81
92
echo Save failed; exiting
93
+ rm -rf " ${image_folder} "
82
94
exit $?
83
95
fi
84
96
0 commit comments