-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquash.sh
186 lines (139 loc) · 5.94 KB
/
squash.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#Docker Command
DOCKER="docker"
clear
#Current Container Name that is being used to Squash into a Final Image
echo "####################################################"
echo "TYPE THE NAME OF THE CONTAINER TO COMMIT/SQUASH"
echo "####################################################"
read CONTAINER
clear
#Docker Repository to Associate Final Image with
echo "####################################################"
echo "TYPE THE NAME OF THE REPOSITORY TO COMMIT TO"
echo "####################################################"
read REPOSITORY
clear
#Docker Tag to Associate Final Image with
echo "####################################################"
echo "TYPE THE NAME OF THE TAG TO COMMIT TO"
echo "####################################################"
read TAG
clear
#Final Image Name after work is done
IMAGE_TO_BUILD="$REPOSITORY:$TAG"
TEMP_DIRECTORY="/opt/"
#Intermediary Image Name for Raw, Flattened Container Filesystem Export
IMAGE_TO_BUILD_RAW=${IMAGE_TO_BUILD}_raw
#Dynamic Dockerfile that Contains Image Configuration
DYNAMIC_DOCKER_FILE_PATH=$(mktemp --tmpdir="$TEMP_DIRECTORY")
#Container Filesystem that is flattened but does not have Docker configurations attached
CONTAINER_EXPORT_PATH=$(mktemp --tmpdir="$TEMP_DIRECTORY")
#Image Export that Contains the Container Filesystem and the generated Dockerfile Configurations
IMAGE_EXPORT_PATH=$(mktemp --tmpdir="$TEMP_DIRECTORY")
outputArray()
{
VARIABLE_RAW_ARRAY_TO_PARSE="$1"
PREFIX="$2"
declare -a TEMP_ARRAY
TEMP_ARRAY=($VARIABLE_RAW_ARRAY_TO_PARSE)
for ITEM in "${TEMP_ARRAY[@]}"
do
if [ -n "${ITEM}" ]; then
VARIABLE_OUTPUT="$PREFIX"
VARIABLE_OUTPUT+="$ITEM"
echo "$VARIABLE_OUTPUT"
fi
done
}
getDockerInspectionValues()
{
VARIABLE_DOCKER_IMAGE_REPO_TAG="$1"
VARIABLE_DOCKER_INSPECTION_VALUE="$2"
VARIABLE_INSPECTION_VALUE_DOCKERFILE_PREFIX="$3"
VARIABLE_FORMAT_VARIABLE_ARRAY="{{range $VARIABLE_DOCKER_INSPECTION_VALUE}}{{println .}}{{end}}"
VARIABLE_FORMAT_VARIABLE_OBJECT="{{println $VARIABLE_DOCKER_INSPECTION_VALUE}}"
VARIABLE_DOCKER_INSPECTION_OUTPUT=$(docker inspect --format="$VARIABLE_FORMAT_VARIABLE_ARRAY" "$VARIABLE_DOCKER_IMAGE_REPO_TAG" 2>/dev/null)
if [ "$?" == "1" ]; then
#Object was not array and failed the GO formatting
#We do this because there is no way to determine what kind of type the object is in
#the current docker implementation of GO for this command
VARIABLE_DOCKER_INSPECTION_OUTPUT=$(docker inspect --format="$VARIABLE_FORMAT_VARIABLE_OBJECT" "$VARIABLE_DOCKER_IMAGE_REPO_TAG" 2>/dev/null)
fi
outputArray "$VARIABLE_DOCKER_INSPECTION_OUTPUT" "$VARIABLE_INSPECTION_VALUE_DOCKERFILE_PREFIX"
}
getDockerInspectionValues_AsJson()
{
VARIABLE_DOCKER_IMAGE_REPO_TAG=$1
VARIABLE_INSPECTION_VALUE=$2
VARIABLE_INSPECTION_VALUE_DOCKERFILE_PREFIX=$3
VARIABLE_FORMAT_VARIABLE_OBJECT="{{json $VARIABLE_INSPECTION_VALUE}}"
VARIABLE_DOCKER_INSPECTION_OUTPUT=$VARIABLE_INSPECTION_VALUE_DOCKERFILE_PREFIX
VARIABLE_DOCKER_INSPECTION_OUTPUT+=$(docker inspect --format="$VARIABLE_FORMAT_VARIABLE_OBJECT" "$VARIABLE_DOCKER_IMAGE_REPO_TAG" 2>/dev/null)
echo $VARIABLE_DOCKER_INSPECTION_OUTPUT
}
getDockerInspectionValues_RawOutput()
{
VARIABLE_DOCKER_IMAGE_REPO_TAG=$1
VARIABLE_INSPECTION_VALUE=$2
VARIABLE_INSPECTION_VALUE_DOCKERFILE_PREFIX=$3
VARIABLE_FORMAT_VARIABLE_OBJECT="{{ $VARIABLE_INSPECTION_VALUE }}"
VARIABLE_DOCKER_INSPECTION_OUTPUT=$VARIABLE_INSPECTION_VALUE_DOCKERFILE_PREFIX
VARIABLE_DOCKER_INSPECTION_OUTPUT+=$(docker inspect --format="$VARIABLE_FORMAT_VARIABLE_OBJECT" "$VARIABLE_DOCKER_IMAGE_REPO_TAG" 2>/dev/null)
echo $VARIABLE_DOCKER_INSPECTION_OUTPUT
}
getDockerInspectionValues_Healthcheck()
{
VARIABLE_DOCKER_IMAGE_REPO_TAG=$1
VARIABLE_DOCKER_INSPECTION_OUTPUT="HEALTHCHECK "
VARIABLE_DOCKER_INSPECTION_OUTPUT+=$(docker inspect --format="{{ .Config.Healthcheck.Test }}" "$VARIABLE_DOCKER_IMAGE_REPO_TAG" 2>/dev/null)
VARIABLE_DOCKER_INSPECTION_OUTPUT="${VARIABLE_DOCKER_INSPECTION_OUTPUT/[/}"
VARIABLE_DOCKER_INSPECTION_OUTPUT="${VARIABLE_DOCKER_INSPECTION_OUTPUT/]/}"
VARIABLE_DOCKER_INSPECTION_OUTPUT="${VARIABLE_DOCKER_INSPECTION_OUTPUT/CMD-SHELL/CMD}"
echo $VARIABLE_DOCKER_INSPECTION_OUTPUT
}
exportContainer()
{
$DOCKER export $CONTAINER | $DOCKER import - $IMAGE_TO_BUILD_RAW
}
buildImage()
{
truncate --size=0 "$DYNAMIC_DOCKER_FILE_PATH"
echo "Dynamic Docker File located at $DYNAMIC_DOCKER_FILE_PATH"
#Get the image the container is based on
CONTAINER_IMAGE_ID=$(getDockerInspectionValues $CONTAINER ".Image" "")
echo "FROM $IMAGE_TO_BUILD_RAW" >> "$DYNAMIC_DOCKER_FILE_PATH"
getDockerInspectionValues $CONTAINER_IMAGE_ID ".Config.User" "USER " >> "$DYNAMIC_DOCKER_FILE_PATH"
getDockerInspectionValues $CONTAINER_IMAGE_ID ".Config.Env" "ENV " >> "$DYNAMIC_DOCKER_FILE_PATH"
getDockerInspectionValues $CONTAINER_IMAGE_ID ".Config.WorkingDir" "WORKDIR " >> "$DYNAMIC_DOCKER_FILE_PATH"
getDockerInspectionValues_Healthcheck $CONTAINER_IMAGE_ID >> "$DYNAMIC_DOCKER_FILE_PATH"
getDockerInspectionValues_AsJson $CONTAINER_IMAGE_ID ".Config.Cmd" "CMD " >> "$DYNAMIC_DOCKER_FILE_PATH"
DOCKER_BUILD_CONTEXT_FOLDER=$(mktemp -d --tmpdir="$TEMP_DIRECTORY")
$DOCKER build $DOCKER_BUILD_CONTEXT_FOLDER --compress --file "$DYNAMIC_DOCKER_FILE_PATH" --tag $IMAGE_TO_BUILD
#Save Dat HD Space
$DOCKER rmi $IMAGE_TO_BUILD_RAW
}
exportImage()
{
$DOCKER save --output $IMAGE_EXPORT_PATH $IMAGE_TO_BUILD
}
cleanUp()
{
rm -f $DYNAMIC_DOCKER_FILE_PATH
rm -f $CONTAINER_EXPORT_PATH
rm -f $IMAGE_EXPORT_PATH
docker image prune --force
docker volume prune --force
docker network prune --force
}
main()
{
echo "Exporting Container and Importing Raw..."
exportContainer
echo "Building New Image..."
buildImage
#echo "Exporting New Image..."
#exportImage
echo "Cleaning up..."
cleanUp
}
main