-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpush-iso-to-dockerhub.sh
executable file
·123 lines (105 loc) · 3.15 KB
/
push-iso-to-dockerhub.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
#!/bin/bash
set -e
cd "`dirname \"$0\"`"
date_of_start=$(date +"%s")
function duration() {
local now=$(date +"%s")
local _duration=$(($now-$date_of_start))
echo "# Duration: $(($_duration / 60))m $(($_duration % 60))s"
}
image="$1"
dockerfile_filesystem="docker/filesystem"
cache="docker/filesystem.origin"
if [ -f "$cache" ]
then
target="`cat \"$cache\"`"
echo "# Found previous file system in $cache. Moving it to \"$target\"."
rm "$cache"
sudo mv "$dockerfile_filesystem" "$target"
fi
if [ -z "$image" ]
then
echo "# Please pass the image as the first argument!"
exit 1
fi
echo "# Installing tools ... "
install=""
if ! [ -e "/usr/bin/realpath" ]
then
install="$install realpath"
fi
if ! [ -e "/usr/bin/unsquashfs" ]
then
install="$install squashfs-tools"
fi
if [ -n "$install" ]
then
sudo apt-get -y -qq install $install
fi
echo "# Reading ISO contents ... "
duration
image_name="`basename \"$image\"`"
mount_point="$image_name-mount"
filesystem="$image_name-filesystem"
echo "# Mounting iso to $mount_point"
duration
mkdir -p "$mount_point"
sudo umount "$mount_point" 2> /tmp/mount-error || true
if ! sudo mount -o loop "$image" "$mount_point"
then
cat /tmp/mount-error
exit 1
fi
echo "# Unpacking the file system to $filesystem"
duration
relative_filesystem_squashfs="`( cd \"$mount_point\" && find -name filesystem.squashfs )`"
if [ -z "$relative_filesystem_squashfs" ]
then
echo "# ERROR: did not find filesystem.squashfs in $mount_point"
duration
exit 1
fi
if [ -z "`ls \"$filesystem\" 2>/dev/null`" ]
then
filesystem_squashfs="$mount_point/$relative_filesystem_squashfs"
echo "# extracting filesystem from $filesystem_squashfs"
mkdir -p "$filesystem"
sudo unsquashfs -f -d "$filesystem" "$filesystem_squashfs"
else
echo "# This was done before, doing nothing."
fi
echo "# Copying iso to docker folder"
duration
dockerfile_iso_path="docker/iso"
sudo rm -rf "$dockerfile_iso_path"
mkdir -p "$dockerfile_iso_path"
echo "# Removing filesytem.squashfs since it is not needed in container."
duration
for source in `( cd "$mount_point" ; find . -not -name filesystem.squashfs )`
do
if [ -d "$mount_point/$source" ]
then
mkdir -p "$dockerfile_iso_path/$source"
else
cp -t "$dockerfile_iso_path/`dirname \"$source\"`" "$mount_point/$source"
fi
done
echo "# Moving file system to docker folder"
duration
sudo rm -rf "$dockerfile_filesystem"
sudo mv "$filesystem" "$dockerfile_filesystem"
echo -n "$filesystem" > "$cache"
echo "# Setting relative filesystem.squashfs path."
echo -n "`dirname \"$relative_filesystem_squashfs\"`" > "docker/toiso/filesystem.squashfs.directory"
echo "# Creating docker image name accoring to"
echo "# https://github.com/docker/docker/blob/master/image/spec/v1.md"
dockerhub_organization="codersosimages"
docker_image_name="`echo \"${image_name%.*}\" | tr -c '[:alnum:]._-' _ | head -c -1`"
docker_image_name="$dockerhub_organization/$docker_image_name"
echo "# labels: $docker_image_name and $full_docker_image_name"
duration
sudo docker rmi -f "$docker_image_name" 2>>/dev/null || true
sudo docker build -t "$docker_image_name" docker
echo "# Pushing the image to dockerhub"
duration
docker push "$docker_image_name"