on master help sign in
LXC: improved console messages & prompt

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

commitccb96544f77814273bc0fbe2342633df69433669 authorMarkus Heiser <markus.heiser@darmarit.de> dateTue 18 Feb 2020, 18:20 (7 years ago) parent59e4026
1 file changed+49−28
utils/lxc.sh+49−28
utils/lxc.sh+49−28
@@ -17,6 +17,9 @@ source_dot_config
1717LINUXCONTAINERS_ORG_NAME="${LINUXCONTAINERS_ORG_NAME:-images}"
1818HOST_PREFIX="${HOST_PREFIX:-searx}"
1919
20+# where all folders from HOST are mounted
21+LXC_SHARE_FOLDER="/share"
22+
2023TEST_IMAGES=(
2124 "$LINUXCONTAINERS_ORG_NAME:ubuntu/18.04" "ubu1804"
2225 "$LINUXCONTAINERS_ORG_NAME:ubuntu/19.04" "ubu1904"
@@ -61,7 +64,7 @@ usage::
6164 $(basename "$0") cmd ...
6265
6366build / remove
64− :containers: build and remove all LXC containers
67+ :containers: build & launch (or remove) all LXC containers
6568add / remove
6669 :subordinate: lxd permission to map ${HOST_USER}'s user/group id through
6770start/stop
@@ -129,7 +132,10 @@ main() {
129132 sudo_or_exit
130133 case $2 in
131134 containers) lxc_cmd "$1" ;;
132− *) usage "$_usage"; exit 42;;
135+ *)
136+ info_msg "lxc $1 $2"
137+ lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
138+ ;;
133139 esac ;;
134140 inspect)
135141 sudo_or_exit
@@ -142,12 +148,11 @@ main() {
142148 sudo_or_exit
143149 shift
144150 for i in "${LOCAL_IMAGES[@]}"; do
145− info_msg "call ${_BBlue}${i}${_creset} -- ${_BGreen}${*}${_creset}"
146− wait_key 3
151+ info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${*}${_creset}"
147152 lxc exec "${i}" -- "$@"
148153 exit_val=$?
149− if [ $exit_val -ne 0 ]; then
150− err_msg "$exit_val ${_BBlue}${i}${_creset} -- ${_BGreen}${*}${_creset}"
154+ if [[ $exit_val -ne 0 ]]; then
155+ err_msg "[${_BBlue}${i}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
151156 fi
152157 done
153158 ;;
@@ -162,7 +167,7 @@ build_instances() {
162167 rst_title "copy images" section
163168 echo
164169 lxc_copy_images_localy
165− lxc image list local: && wait_key
170+ # lxc image list local: && wait_key
166171 echo
167172 rst_title "build containers" section
168173 echo
@@ -175,6 +180,7 @@ build_instances() {
175180
176181remove_instances() {
177182 rst_title "Remove LXC instances"
183+ lxc list "$HOST_PREFIX"
178184 echo -en "\\nLXC containers(s)::\\n\\n ${LOCAL_IMAGES[*]}\\n" | $FMT
179185 if ask_yn "Do you really want to delete all images"; then
180186 lxc_delete_containers
@@ -214,64 +220,79 @@ lxc_delete_images_localy() {
214220lxc_cmd() {
215221 for i in "${LOCAL_IMAGES[@]}"; do
216222 info_msg "lxc $* $i"
217− lxc "$@" "$i"
223+ lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
218224 done
219225}
220226
221227lxc_init_containers() {
222− local shortname
228+
229+ local image_name
230+ local container_name
231+
223232 for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
224− shortname="${TEST_IMAGES[i+1]}"
225− if lxc info "${HOST_PREFIX}-${shortname}" &>/dev/null; then
226− info_msg "conatiner '$i' already exists"
233+
234+ image_name="${TEST_IMAGES[i+1]}"
235+ container_name="${HOST_PREFIX}-${image_name}"
236+
237+ if lxc info "${container_name}" &>/dev/null; then
238+ info_msg "container '${container_name}' already exists"
227239 else
228− info_msg "create conatiner instance: $i"
229− lxc init "local:${shortname}" "${HOST_PREFIX}-${shortname}"
240+ info_msg "create conatiner instance: ${container_name}"
241+ lxc init "local:${image_name}" "${container_name}"
230242 fi
231243 done
232244}
233245
234246lxc_config_containers() {
235247 for i in "${LOCAL_IMAGES[@]}"; do
236− info_msg "configure container: ${_BBlue}${i}${_creset}"
248+ info_msg "[${_BBlue}${i}${_creset}] configure container ..."
237249
238− info_msg "map uid/gid from host to container"
250+ info_msg "[${_BBlue}${i}${_creset}] map uid/gid from host to container"
239251 # https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps
240252 echo -e -n "uid $HOST_USER_ID 1000\\ngid $HOST_GROUP_ID 1000"\
241253 | lxc config set "$i" raw.idmap -
242254
243− info_msg "share ${REPO_ROOT} (repo_share) from HOST into container"
255+ info_msg "[${_BBlue}${i}${_creset}] share ${REPO_ROOT} (repo_share) from HOST into container"
244256 # https://lxd.readthedocs.io/en/latest/instances/#type-disk
245257 lxc config device add "$i" repo_share disk \
246258 source="${REPO_ROOT}" \
247− path="/share/$(basename "${REPO_ROOT}")" &>/dev/null
259+ path="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")" &>/dev/null
248260 # lxc config show "$i" && wait_key
249261 done
250262}
251263
252264lxc_boilerplate_containers() {
253− local shortname
265+
266+ local image_name
267+ local container_name
254268 local boilerplate_script
269+
255270 for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
256− shortname="${TEST_IMAGES[i+1]}"
257− info_msg "install boilerplate: ${_BBlue}${HOST_PREFIX}-${shortname}${_creset}"
258− lxc start -q "${HOST_PREFIX}-${shortname}" &>/dev/null
259− boilerplate_script="${shortname}_boilerplate"
271+
272+ image_name="${TEST_IMAGES[i+1]}"
273+ container_name="${HOST_PREFIX}-${image_name}"
274+ boilerplate_script="${image_name}_boilerplate"
260275 boilerplate_script="${!boilerplate_script}"
276+
277+ info_msg "[${_BBlue}${container_name}${_creset}] install boilerplate"
278+ if lxc start -q "${container_name}" &>/dev/null; then
279+ sleep 5 # guest needs some time to come up and get an IP
280+ fi
261281 if [[ -n "${boilerplate_script}" ]]; then
262− echo "$boilerplate_script" \
263− | lxc exec "${HOST_PREFIX}-${shortname}" -- bash \
264− | prefix_stdout " ${HOST_PREFIX}-${shortname} | "
282+ echo "${boilerplate_script}" \
283+ | lxc exec "${container_name}" -- bash \
284+ | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
265285 else
266− warn_msg "no boilerplate for instance '$i'"
286+ err_msg "[${_BBlue}${container_name}${_creset}] no boilerplate for image '${image_name}'"
267287 fi
288+
268289 done
269290}
270291
271292lxc_delete_containers() {
272293 for i in "${LOCAL_IMAGES[@]}"; do
273294 if lxc info "$i" &>/dev/null; then
274− info_msg "stop & delete instance '$i'"
295+ info_msg "stop & delete instance ${_BBlue}${i}${_creset}"
275296 lxc stop "$i" &>/dev/null
276297 lxc delete "$i" | prefix_stdout
277298 else