-
Notifications
You must be signed in to change notification settings - Fork 1
/
save-images.sh
133 lines (118 loc) · 2.58 KB
/
save-images.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
# Cleanup on trap
cleanup()
{
spin_stop
exit 100
}
# Spinning loading dot
spin()
{
local spinstr='|/-\'
while :
do
local temp=${spinstr#?}
printf " [%c] " "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep 0.05
printf "\b\b\b\b\b\b"
done
}
# Start the Spinner
spin_start()
{
spin &
# Make a note of its Process ID (PID):
SPIN_PID=$!
}
# Stop the Spinner
spin_stop()
{
if [ -z $SPIN_PID ]; then
echo "Spinner is already down...";
else
#echo killing Spinner $SPIN_PID
kill -n 9 $SPIN_PID &> /dev/null
unset SPIN_PID;
fi
}
usage()
{
echo -e "\nUsage: $0 [-n <namespace>] [-o <output_path>]" 1>&2;
echo "
Description:
This script will use your local install of kubectl,
will search and find all images used by containers in specific namespace,
and will save them all to .tar files."
echo "
Options:
-h help
-n <namespace> namespace for seaching images from (Default: default)
-o <output_path> DIR path for saveing all images to (Default: ./)
"
exit 0;
}
# Get all images
get_images()
{
echo -e "\e[93mSearching for all images in $NAMESPACE namespace...\e[0m"
spin_start
IMAGES=$(kubectl get pods -n $NAMESPACE -o jsonpath="{..image}" |tr -s '[[:space:]]' '\n' |uniq |sort)
if (( $(echo $IMAGES|wc -w) == 0 )); then
echo "NO images found in $NAMESPACE namespace"
spin_stop
return 1;
fi
spin_stop
}
# Echo all images
list_images()
{
echo -e "\e[93mImage List:\n\e[0m$IMAGES"
}
# Save all images to files
save_images()
{
echo -e "\e[0m........\e[92m Saving Images \e[0m........"
for image in $IMAGES
do
image_name=$FILES_PATH$(echo $image| tr /. _|tr : -).tar
echo "Pulling $image:"
spin_start
docker pull $image 1> /dev/null
spin_stop
echo "Saving: $image_name"
spin_start
docker save -o $image_name $image
spin_stop
done
}
main()
{
# Kill the spinner on any signal, including our own exit.
trap 'echo "Cought trap!" ; cleanup' 1 2 3 6 9 14 15
# Get images from kubernetes
get_images
# Check if any images found
if [ $? == 0 ]; then
list_images
save_images;
else
exit 101;
fi
}
# Set default vars
NAMESPACE=default
FILES_PATH=.
# Set vars from user input
while getopts n:o:h option
do
case "${option}"
in
n) NAMESPACE=${OPTARG};;
o) FILES_PATH=${OPTARG};;
h|*) usage;;
esac
done
# Run main
main