-
Notifications
You must be signed in to change notification settings - Fork 5
/
build-lxc.sh
executable file
·230 lines (198 loc) · 6.99 KB
/
build-lxc.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/bash -e
PROGRAM_NAME=`basename $0`
display_help () {
echo "Usage: $PROGRAM_NAME [options] [command]"
echo ""
echo "Available commands:"
echo " help - Display this help."
echo " delete - Delete a container."
echo " shell - Enter a container."
echo " clean - Clean the snap build dir."
echo "Options:"
echo " --help, -h - Display this help."
echo " --branch=<value> - Use given branch to check out sources (Default: master)"
}
variables() {
RED='\033[0;31m'
GREEN='\033[0;32m'
LIGHT_RED='\033[1;31m'
LIGHT_GREEN='\033[1;32m'
NC='\033[0m'
ERROR_COLOR=$LIGHT_RED
POSITIVE_COLOR=$GREEN
USERNAME=`id --user --name`
GROUPNAME=$USERNAME
USERID=150000
GROUPID=150000
PACKAGE=`basename "$PWD"`
LXD_IMAGE_NAME=jammy
LXD_IMAGE=ubuntu:$LXD_IMAGE_NAME
# LXD_IMAGE=$LXD_IMAGE_NAME
LXD_CONTAINER=builder-$CONTAINER_PREFIX$LXD_IMAGE_NAME
DEPENDENCIES="npm python3-git"
USERDIR=/home/$USERNAME
MOUNTED_DIRECTORY=$PWD
MOUNT_POINT=$USERDIR/$PACKAGE
SOURCE_PATH_CONTAINER=$MOUNT_POINT
}
new_container() {
if lxc info $LXD_CONTAINER > /dev/null 2>&1 ; then
echo "${POSITIVE_COLOR}LXD container $LXD_CONTAINER already exists.${NC}"
# FIXME: check if the container is already started
lxc start $LXD_CONTAINER || true
else
echo "${POSITIVE_COLOR}Creating LXD container $LXD_CONTAINER using $LXD_IMAGE.${NC}"
lxc remote --public=true --accept-certificate=true add nymea https://jenkins.nymea.io || true
lxc init $LXD_IMAGE $LXD_CONTAINER
# Note: check the lxc version for lxc.id_map vs lxc.idmap
LXCVERSION=$(lxc --version)
LXCMAJORVERSION=$(echo ${LXCVERSION} | cut -d. -f1)
if [ "${LXCMAJORVERSION}" -lt "3" ]; then
echo "${POSITIVE_COLOR}Using lxc version 2 compatibility$LXD_IMAGE.${NC}"
printf "lxc.id_map = g $GROUPID `id --group` 1\nlxc.id_map = u $USERID `id --user` 1" | lxc config set $LXD_CONTAINER raw.lxc -
else
printf "uid `id --user` 150000\ngid `id --group` 150000" | lxc config set $LXD_CONTAINER raw.idmap -
# echo "Mapping uids: lxc.idmap = g $GROUPID `id --group` 1\nlxc.idmap = u $USERID `id --user` 1"
# printf "lxc.idmap = g `id --group` 1\nlxc.idmap = u $USERID `id --user` 1" | lxc config set $LXD_CONTAINER raw.lxc -
fi
lxc start $LXD_CONTAINER
lxc exec --env GROUPID=$GROUPID --env GROUPNAME=$GROUPNAME $LXD_CONTAINER -- deluser $USERNAME || true
lxc exec --env GROUPID=$GROUPID --env GROUPNAME=$GROUPNAME $LXD_CONTAINER -- delgroup $GROUPNAME || true
lxc exec $LXD_CONTAINER -- rm -rf /home/ubuntu
lxc exec --env GROUPID=$GROUPID --env GROUPNAME=$GROUPNAME $LXD_CONTAINER -- addgroup --gid $GROUPID $GROUPNAME
lxc exec --env GROUPID=$GROUPID --env USERNAME=$USERNAME --env USERID=$USERID $LXD_CONTAINER -- adduser --disabled-password --gecos "" --uid $USERID --gid $GROUPID $USERNAME
lxc exec --env USERNAME=$USERNAME $LXD_CONTAINER -- usermod -aG sudo $USERNAME
exec_container_root passwd --delete $USERNAME
fi
if ! lxc config device get $LXD_CONTAINER current_dir_mount disk 2> /dev/null ; then
echo "${POSITIVE_COLOR}Mounting $MOUNTED_DIRECTORY in container $MOUNT_POINT.${NC}"
lxc config device add $LXD_CONTAINER current_dir_mount disk source=$MOUNTED_DIRECTORY path=$MOUNT_POINT
else
lxc config device set $LXD_CONTAINER current_dir_mount source $MOUNTED_DIRECTORY
fi
if ! lxc config device get $LXD_CONTAINER gradle_cache_dir_mount disk 2> /dev/null ; then
echo "${POSITIVE_COLOR}Mounting gradle cache dir ($HOME/.gradle) in container.${NC}"
if [ ! -d $HOME/.gradle ]; then mkdir $HOME/.gradle; fi
lxc config device add $LXD_CONTAINER gradle_cache_dir_mount disk source=$HOME/.gradle/ path=$HOME/.gradle
fi
exec_container_root chgrp -R 150000 /opt/ || true
}
delete_container () {
echo "${POSITIVE_COLOR}Deleting LXD container $LXD_CONTAINER.${NC}"
lxc delete -f $LXD_CONTAINER
}
install_dependencies() {
exec_container_root apt update
exec_container_root apt install --yes $DEPENDENCIES
}
shell_container () {
echo "${POSITIVE_COLOR}Entering shell in LXD container $LXD_CONTAINER.${NC}"
lxc exec $LXD_CONTAINER -- su --login $USERNAME
}
check_command_parameter_count () {
MIN_PARAMETERS=$1
MAX_PARAMETERS=$2
COUNT=$(echo $PARAMETERS | wc -w)
if [ -n $MIN_PARAMETERS ] && [ $COUNT -lt $MIN_PARAMETERS ] ; then
display_help
echo ""
echo "${ERROR_COLOR}Command '$COMMAND' requires a minimum of $MIN_PARAMETERS parameters. $$
exit 1
fi
if [ -n $MAX_PARAMETERS ] && [ $COUNT -gt $MAX_PARAMETERS ] ; then
display_help
echo ""
echo "${ERROR_COLOR}Command '$COMMAND' accepts at most $MAX_PARAMETERS parameters. $COUNT $
exit 1
fi
}
exec_container_root () {
command="$@"
#echo lxc exec $LXD_CONTAINER "$@"
lxc exec $LXD_CONTAINER -- sh -c "cd $SOURCE_PATH_CONTAINER; $command"
}
exec_container () {
command="$@"
#echo lxc exec $LXD_CONTAINER "$@"
lxc exec $LXD_CONTAINER -- su -l -c "cd $SOURCE_PATH_CONTAINER; $command" $USERNAME
}
build () {
exec_container ./build.sh --branch=$BRANCH
}
clean () {
exec_container "rm builddir -rf"
}
BRANCH="master"
while [ "$1" != "" ]; do
OPTION=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
VALUEVALUE=`echo $1 | awk -F= '{print $3}'`
case $1 in
-*)
case $OPTION in
--container-prefix)
CONTAINER_PREFIX=$VALUE
;;
--branch)
echo "Setting branch to $VALUE"
BRANCH=$VALUE
;;
--help | -h | help )
display_help
exit 0
;;
*)
display_help
echo ""
echo "error: unknown option: $OPTION"
exit 1
;;
esac
shift
;;
*)
break
;;
esac
done
variables
COMMAND=$1
if [ -n "$COMMAND" ] ; then
shift
fi
if [ "$COMMAND" = "help" ] ; then
display_help
exit 0
fi
if [ -z "$COMMAND" ] ; then
new_container
install_dependencies
build
else
PARAMETERS=$@
case "$COMMAND" in
delete)
check_command_parameter_count 0 1
delete_container
;;
shell)
check_command_parameter_count 0 1
new_container
shell_container
;;
build)
new_container
build
;;
clean)
new_container
clean $@
;;
*)
display_help
echo ""
echo "${ERROR_COLOR}error: unknown command: $COMMAND${NC}"
exit 1
;;
esac
fi