on master help sign in
LXC: tooling box - add boilerplate to containers

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>

commite8cf22504650f742da247bc923abebccec869676 authorMarkus Heiser <markus.heiser@darmarit.de> dateSun 16 Feb 2020, 18:18 (7 years ago) parent0b2b325
1 file changed+80−29
utils/lxc.sh+80−29
utils/lxc.sh+80−29
@@ -27,12 +27,18 @@ TEST_IMAGES=(
2727 #"$LINUXCONTAINERS_ORG_NAME:fedora/31" "fedora31"
2828)
2929
30+ubu1804_boilerplate="
31+export DEBIAN_FRONTEND=noninteractive
32+apt-get install -y git curl wget
33+"
34+ubu1904_boilerplate="$ubu1804_boilerplate"
35+
3036REMOTE_IMAGES=()
3137LOCAL_IMAGES=()
3238
3339for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
3440 REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${TEST_IMAGES[i]}")
35− LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${TEST_IMAGES[i+1]}")
41+ LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${HOST_PREFIX}-${TEST_IMAGES[i+1]}")
3642done
3743
3844HOST_USER="${SUDO_USER:-$USER}"
@@ -72,9 +78,26 @@ EOF
7278 [ ! -z "${1+x}" ] && err_msg "$1"
7379}
7480
81+lxd_info() {
82+
83+ cat <<EOF
84+
85+LXD is needed, to install run::
86+
87+ snap install lxd
88+ lxc init --auto
89+
90+EOF
91+}
92+
7593main() {
7694
77− required_commands lxc || exit
95+ local exit_val
96+
97+ if ! required_commands lxc; then
98+ lxd_info
99+ exit 42
100+ fi
78101
79102 local _usage="unknown or missing $1 command $2"
80103
@@ -118,8 +141,13 @@ main() {
118141 sudo_or_exit
119142 shift
120143 for i in "${LOCAL_IMAGES[@]}"; do
121− info_msg "lxc exec ${_BBlue}${HOST_PREFIX}-${i}${_creset} -- ${_BGreen}${*}${_creset}"
122− lxc exec "${HOST_PREFIX}-${i}" -- "$@"
144+ info_msg "call ${_BBlue}${i}${_creset} -- ${_BGreen}${*}${_creset}"
145+ wait_key 3
146+ lxc exec "${i}" -- "$@"
147+ exit_val=$?
148+ if [ $exit_val -ne 0 ]; then
149+ err_msg "$exit_val ${_BBlue}${i}${_creset} -- ${_BGreen}${*}${_creset}"
150+ fi
123151 done
124152 ;;
125153 *)
@@ -129,10 +157,18 @@ main() {
129157
130158build_instances() {
131159 rst_title "Build LXC instances"
160+
161+ rst_title "copy images" section
162+ echo
132163 lxc_copy_images_localy
133− #lxc image list local: && wait_key
164+ lxc image list local: && wait_key
165+ echo
166+ rst_title "build containers" section
167+ echo
134168 lxc_init_containers
135169 lxc_config_containers
170+ lxc_boilerplate_containers
171+ echo
136172 lxc list "$HOST_PREFIX"
137173}
138174
@@ -142,7 +178,8 @@ delete_instances() {
142178 if ask_yn "Do you really want to delete all images"; then
143179 lxc_delete_containers
144180 fi
145− # lxc list "$HOST_PREFIX"
181+ echo
182+ lxc list "$HOST_PREFIX"
146183 # lxc image list local: && wait_key
147184}
148185
@@ -150,14 +187,13 @@ delete_instances() {
150187# ------
151188
152189lxc_copy_images_localy() {
153− echo
154190 for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
155191 if lxc image info "local:${TEST_IMAGES[i+1]}" &>/dev/null; then
156192 info_msg "image ${TEST_IMAGES[i]} already copied --> ${TEST_IMAGES[i+1]}"
157193 else
158194 info_msg "copy image locally ${TEST_IMAGES[i]} --> ${TEST_IMAGES[i+1]}"
159195 lxc image copy "${TEST_IMAGES[i]}" local: \
160− --alias "${TEST_IMAGES[i+1]}" prefix_stdout
196+ --alias "${TEST_IMAGES[i+1]}" | prefix_stdout
161197 fi
162198 done
163199}
@@ -175,53 +211,68 @@ lxc_delete_images_localy() {
175211# ---------
176212
177213lxc_cmd() {
178− echo
179214 for i in "${LOCAL_IMAGES[@]}"; do
180− info_msg "lxc $* $HOST_PREFIX-$i"
181− lxc "$@" "$HOST_PREFIX-$i"
215+ info_msg "lxc $* $i"
216+ lxc "$@" "$i"
182217 done
183218}
184219
185220lxc_init_containers() {
186− echo
187221 for i in "${LOCAL_IMAGES[@]}"; do
188− if lxc info "$HOST_PREFIX-$i" &>/dev/null; then
189− info_msg "conatiner '$HOST_PREFIX-$i' already exists"
222+ if lxc info "$i" &>/dev/null; then
223+ info_msg "conatiner '$i' already exists"
190224 else
191− info_msg "create conatiner instance: $HOST_PREFIX-$i"
192− lxc init "local:$i" "$HOST_PREFIX-$i"
225+ info_msg "create conatiner instance: $i"
226+ lxc init "local:$i" "$i"
193227 fi
194228 done
195229}
196230
197231lxc_config_containers() {
198− echo
199232 for i in "${LOCAL_IMAGES[@]}"; do
233+ info_msg "configure container: ${_BBlue}${i}${_creset}"
200234
201− info_msg "map uid/gid from host to conatiner: $HOST_PREFIX-$i"
235+ info_msg "map uid/gid from host to container"
202236 # https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps
203237 echo -e -n "uid $HOST_USER_ID 1000\\ngid $HOST_GROUP_ID 1000"\
204− | lxc config set "$HOST_PREFIX-$i" raw.idmap -
238+ | lxc config set "$i" raw.idmap -
205239
206− info_msg "share ${REPO_ROOT} (repo_share) from HOST into container: $HOST_PREFIX-$i"
240+ info_msg "share ${REPO_ROOT} (repo_share) from HOST into container"
207241 # https://lxd.readthedocs.io/en/latest/instances/#type-disk
208− lxc config device add "$HOST_PREFIX-$i" repo_share disk \
242+ lxc config device add "$i" repo_share disk \
209243 source="${REPO_ROOT}" \
210− path="/share/$(basename "${REPO_ROOT}")"
244+ path="/share/$(basename "${REPO_ROOT}")" &>/dev/null
245+ # lxc config show "$i" && wait_key
246+ done
247+}
211248
212− # lxc config show "$HOST_PREFIX-$i" && wait_key
249+lxc_boilerplate_containers() {
250+ local shortname
251+ local boilerplate_script
252+ for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
253+ shortname="${TEST_IMAGES[i+1]}"
254+ info_msg "install boilerplate: ${_BBlue}${HOST_PREFIX}-${shortname}${_creset}"
255+ lxc start -q "${HOST_PREFIX}-${shortname}" &>/dev/null
256+ boilerplate_script="${shortname}_boilerplate"
257+ boilerplate_script="${!boilerplate_script}"
258+ if [[ ! -z "${boilerplate_script}" ]]; then
259+ echo "$boilerplate_script" \
260+ | lxc exec "${HOST_PREFIX}-${shortname}" -- bash \
261+ | prefix_stdout " ${HOST_PREFIX}-${shortname} | "
262+ else
263+ warn_msg "no boilerplate for instance '$i'"
264+ fi
213265 done
214266}
215267
216268lxc_delete_containers() {
217− echo
218269 for i in "${LOCAL_IMAGES[@]}"; do
219− if lxc info "$HOST_PREFIX-$i" &>/dev/null; then
220− info_msg "stop & delete instance '$HOST_PREFIX-$i'"
221− lxc stop "$HOST_PREFIX-$i" &>/dev/null
222− lxc delete "$HOST_PREFIX-$i" | prefix_stdout
270+ if lxc info "$i" &>/dev/null; then
271+ info_msg "stop & delete instance '$i'"
272+ lxc stop "$i" &>/dev/null
273+ lxc delete "$i" | prefix_stdout
223274 else
224− warn_msg "instance '$HOST_PREFIX-$i' does not exist / can't delete :o"
275+ warn_msg "instance '$i' does not exist / can't delete :o"
225276 fi
226277 done
227278}