-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhotbox-build
More file actions
executable file
·210 lines (172 loc) · 4.78 KB
/
Copy pathhotbox-build
File metadata and controls
executable file
·210 lines (172 loc) · 4.78 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/sh
set -eu ; set -o | grep -q pipefail && set -o pipefail
HOTBOX=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)
PATH="$HOTBOX:$HOTBOX/lib:$PATH"
. hotbox-use-shell.sh
. hotbox-use-state.sh
. hotbox-use-txn.sh
. hotbox-use-sources.sh
refresh=
commandline_sources=
while [ $# -gt 0 ] ; do
case $1 in
--source)
shift
[ $# -gt 0 ] || die "Expected <source>"
commandline_sources="$commandline_sources $1"
shift
;;
--refresh)
shift
refresh=1
;;
-*)
die "Unrecognised option '$1'"
;;
*)
break
;;
esac
done
[ $# -gt 0 ] || die "Expected <spec>"
spec="$1" ; shift
[ $# -eq 0 ] || die "Unexpected argument '$1'"
for source in $commandline_sources ; do
hotbox-sources-add "$source"
done
sources_snapshot="$HOTBOX_STATE/sources/$spec.next"
rm -rf "$sources_snapshot"
mkdir -p "$sources_snapshot"
cp "$HOTBOX_SOURCES"/* "$sources_snapshot" || true
heading "Reading '$spec' spec"
baseimage=$(hotbox-spec-read $spec baseimage | tail -n 1 || checkpipe)
[ "$baseimage" ] || die "No baseimage specified in $spec spec"
user="$(current_user)"
uid="$(current_uid)"
gid="$(current_gid)"
docker_gid="$(getent group docker | cut -d: -f3 || checkpipe)"
image="hotbox-$spec:next"
heading "Checking for existing $image container image"
echo_on
imageid="$(docker image ls -q $image)"
echo_off
if [ "$imageid" ] ; then
heading "Deleting existing $image container image"
echo_on
docker image rm -f $image
echo_off
fi
if [ "$refresh" ] ; then
heading "Pulling latest $baseimage image"
echo_on
docker image pull $baseimage
echo_off
fi
heading "Forking $image container image from $baseimage"
echo_on
echo "FROM $baseimage" | \
docker build \
--label hotbox \
--tag "$image" \
- \
|| checkpipe
echo_off
hotbox_mount_options="$(hotbox-feature-run-command container-hotbox run)"
heading "Setting up system features in container"
echo_on
container=$(\
docker create \
--network host \
--hostname hotbox-$spec \
$hotbox_mount_options \
--env HOTBOX_REFRESH=$refresh \
--env HOTBOX_SOURCES=/hotbox-state/sources/$spec.next \
--env HOTBOX_USER=$user \
--env HOTBOX_UID=$uid \
--env HOTBOX_GID=$gid \
--env HOTBOX_DOCKER_GID=$docker_gid \
--user root \
--workdir /root \
$image \
/hotbox/hotbox-spec-apply --container --system $spec \
)
docker start -ai $container
docker commit $container $image
echo_off
heading "Rebuilding /hotbox mount point in container"
echo_on
container=$(\
docker create \
--network host \
--user root \
--workdir /root \
$image \
/bin/sh -c "rm -rf /hotbox && mkdir /hotbox && chown $user:$user /hotbox && chmod 755 /hotbox" \
)
docker start -ai $container
docker commit $container $image
echo_off
heading "Rebuilding /hotbox-state mount point in container"
echo_on
container=$(\
docker create \
--network host \
--user root \
--workdir /root \
$image \
/bin/sh -c "rm -rf /hotbox-state && mkdir /hotbox-state && chown $user:$user /hotbox-state && chmod 755 /hotbox-state" \
)
docker start -ai $container
docker commit $container $image
echo_off
heading "Setting up features in container"
echo_on
container=$(\
docker create \
--network host \
$hotbox_mount_options \
--env HOTBOX_REFRESH=$refresh \
--env HOTBOX_SOURCES=/hotbox-state/sources/$spec.next \
--env HOTBOX_USER=$user \
--env HOTBOX_UID=$uid \
--env HOTBOX_GID=$gid \
--env HOTBOX_DOCKER_GID=$docker_gid \
--user $user \
--workdir /home/$user \
$image \
/hotbox/hotbox-spec-apply --container $spec \
)
docker start -ai $container
docker commit $container $image
echo_off
heading "Setting final container metadata"
echo_on
container=$(\
docker create \
--env HOTBOX_REFRESH= \
--env HOTBOX_SOURCES= \
--env HOTBOX_USER= \
--env HOTBOX_UID= \
--env HOTBOX_GID= \
--env HOTBOX_DOCKER_GID= \
--env HOTBOX_STATE= \
--user $user \
--workdir /home/$user \
$image \
/bin/sh -l \
)
docker start -ai $container
docker commit $container $image
echo_off
heading "Committing final $spec container image"
echo_on
docker tag $image hotbox-$spec
rm -rf $HOTBOX_STATE/sources/$spec
mv $sources_snapshot $HOTBOX_STATE/sources/$spec
docker image rm -f $image
echo_off
heading "Pruning orphaned hotbox containers and images"
echo_on
docker container prune -f --filter label=hotbox
docker image prune -f --filter label=hotbox
echo_off