This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbuild
executable file
·171 lines (135 loc) · 4.21 KB
/
build
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
#!/bin/bash
set -e -u
function sanitize_cgroups() {
mkdir -p /sys/fs/cgroup
mountpoint -q /sys/fs/cgroup || \
mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup
mount -o remount,rw none /sys/fs/cgroup
sed -e 1d /proc/cgroups | while read sys hierarchy num enabled; do
if [ "$enabled" != "1" ]; then
# subsystem disabled; skip
continue
fi
grouping="$(cat /proc/self/cgroup | cut -d: -f2 | grep "\\<$sys\\>")" || true
if [ -z "$grouping" ]; then
# subsystem not mounted anywhere; mount it on its own
grouping="$sys"
fi
mountpoint="/sys/fs/cgroup/$grouping"
mkdir -p "$mountpoint"
# clear out existing mount to make sure new one is read-write
if mountpoint -q "$mountpoint"; then
umount "$mountpoint"
fi
mount -n -t cgroup -o "$grouping" cgroup "$mountpoint"
if [ "$grouping" != "$sys" ]; then
if [ -L "/sys/fs/cgroup/$sys" ]; then
rm "/sys/fs/cgroup/$sys"
fi
ln -s "$mountpoint" "/sys/fs/cgroup/$sys"
fi
done
if ! test -e /sys/fs/cgroup/systemd ; then
mkdir /sys/fs/cgroup/systemd
mount -t cgroup -o none,name=systemd none /sys/fs/cgroup/systemd
fi
}
sanitize_cgroups
stty columns 80
ran_progress="false"
function progress() {
if [ "$ran_progress" = "true" ]; then
echo ""
fi
ran_progress="true"
echo $'\e[1m'"$@"$'\e[0m'
}
TAG=${TAG:-latest}
TAG_FILE=${TAG_FILE:-}
CONTEXT=${CONTEXT:-.}
DOCKERFILE=${DOCKERFILE:-$CONTEXT/Dockerfile}
TARGET=${TARGET:-}
TARGET_FILE=${TARGET_FILE:-}
BUILD_ARGS_OPT=$(env | awk '/BUILD_ARG_/ {gsub(/BUILD_ARG_/, "--build-arg "); printf "%s ",$0}')
BUILD_ARGS_FILE=${BUILD_ARGS_FILE:-}
tag_name=""
if [ -n "$TAG_FILE" ]; then
if [ ! -f "$TAG_FILE" ]; then
echo "tag file '$TAG_FILE' does not exist"
exit 1
fi
tag_name="$(cat $TAG_FILE)"
else
tag_name="$TAG"
fi
target_arg=""
if [ -n "$TARGET_FILE" ]; then
if [ ! -f "$TARGET_FILE" ]; then
echo "target file '$TARGET_FILE' does not exist"
exit 1
fi
target_arg="--target $(cat $TARGET_FILE)"
elif [ -n "$TARGET" ]; then
target_arg="--target $TARGET"
fi
build_args=""
if [ -n "$BUILD_ARGS_FILE" ]; then
if [ ! -f "$BUILD_ARGS_FILE" ]; then
echo "build_args file '$BUILD_ARGS_FILE' does not exist"
exit 1
fi
expanded_build_args=()
while IFS= read -r line || [ -n "$line" ];
do
if [[ ! -z "$line" ]]; then
expanded_build_args+=("--build-arg")
expanded_build_args+=("${line}")
fi
done < $BUILD_ARGS_FILE
build_args="${expanded_build_args[@]}"
else
build_args="$BUILD_ARGS_OPT"
fi
if [ -d ./cache/state ]; then
progress "syncing cache to state"
rsync -a ./cache/state/ /scratch/state
fi
if [ -e ./cache/whiteouts ]; then
cat ./cache/whiteouts | while read path; do
echo "restoring whiteout: $path"
mknod -m0 $path c 0 0
done
fi
progress "building"
img build --no-console -s /scratch/state -t $REPOSITORY:$tag_name -f $DOCKERFILE $target_arg $build_args $CONTEXT
if [ -d ./cache ]; then
progress "syncing state to cache"
> ./cache/whiteouts
find /scratch/state -type c | while read device; do
types=$(stat -c '%t,%T' "$device")
if [ "$types" = "0,0" ]; then
echo "recording whiteout: $device"
echo $device >> ./cache/whiteouts
rm $device
else
echo "caching non-whiteout char device ($types): $device"
fi
done
rsync -a /scratch/state/ ./cache/state
fi
if ! [ -d ./image ] && ! [ -d ./rootfs ]; then
echo "neither 'image' nor 'rootfs' are specified, finishing with no outputs"
exit 0
fi
mkdir -p image
progress "saving image"
img save -s /scratch/state -o image/image.tar $REPOSITORY:$tag_name
if [ -d ./rootfs ]; then
progress "unpacking rootfs"
imageConfig=$(tar -Oxf image/image.tar manifest.json | jq -r '.[0].Config')
tar -Oxf image/image.tar $imageConfig | jq '{"user":.config.User,"env":.config.Env}' > ./rootfs/metadata.json
RUNC_PATH=$(echo /tmp/img-runc*) # Can be removed once this is fixed https://github.com/genuinetools/img/issues/233
PATH=$RUNC_PATH:$PATH img unpack -s /scratch/state -o /scratch/rootfs $REPOSITORY:$tag_name
# unpacking to a btrfs backed baggageclaim volume directly hangs, so we unpack to /scratch and copy
rsync -a /scratch/rootfs/ ./rootfs/rootfs
fi