Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions tools/run-container
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Usage: ${0##*/} [ options ] [images:]image-ref
-p | --package build a binary package (.deb or .rpm)
-s | --source-package build source package (debuild -S or srpm)
-u | --unittest run unit tests
--vm use a VM instead of a container

Example:
* ${0##*/} --package --source-package --unittest centos/6
Expand All @@ -50,7 +51,7 @@ cleanup() {
if [ "$KEEP" = "true" ]; then
error "not deleting container '$CONTAINER' due to --keep"
else
delete_container "$CONTAINER"
delete_instance "$CONTAINER"
fi
fi
}
Expand Down Expand Up @@ -340,6 +341,12 @@ wait_inside() {
wait_for_boot() {
local name="$1"
local out="" ret="" wtime=$DEFAULT_WAIT_MAX
local system_up=false
for i in {0..30}; do
[ "$i" -gt 1 ] && sleep 5
inside "$name" true 2>/dev/null && system_up=true && break
done
[ $system_up == true ] || { errorrc "exec command inside $name failed."; return; }
get_os_info_in "$name"
[ "$OS_NAME" = "debian" ] && wtime=300 &&
debug 1 "on debian we wait for ${wtime}s"
Expand All @@ -360,18 +367,20 @@ wait_for_boot() {
fi
}

start_container() {
local src="$1" name="$2"
start_instance() {
local src="$1" name="$2" use_vm="$3"
debug 1 "starting container $name from '$src'"
lxc launch "$src" "$name" || {
launch_flags=()
Copy link
Copy Markdown
Contributor Author

@paride paride May 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lays foundation for another change I have in mind: use ephemeral containers: when handling multiple parameters arrays as much more convenient.

[ "$use_vm" == true ] && launch_flags+=(--vm)
lxc launch "$src" "$name" "${launch_flags[@]}" || {
errorrc "Failed to start container '$name' from '$src'";
return
}
CONTAINER=$name
wait_for_boot "$name"
}

delete_container() {
delete_instance() {
debug 1 "removing container $1 [--keep to keep]"
lxc delete --force "$1"
}
Expand All @@ -391,7 +400,7 @@ run_self_inside_as_cd() {

main() {
local short_opts="a:hknpsuv"
local long_opts="artifacts:,dirty,help,keep,name:,package,source-package,unittest,verbose"
local long_opts="artifacts:,dirty,help,keep,name:,package,source-package,unittest,verbose,vm"
local getopt_out=""
getopt_out=$(getopt --name "${0##*/}" \
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
Expand All @@ -401,6 +410,7 @@ main() {
local cur="" next=""
local package=false srcpackage=false unittest="" name=""
local dirty=false artifact_d="."
local use_vm=false

while [ $# -ne 0 ]; do
cur="${1:-}"; next="${2:-}";
Expand All @@ -414,6 +424,7 @@ main() {
-s|--source-package) srcpackage=true;;
-u|--unittest) unittest=1;;
-v|--verbose) VERBOSITY=$((VERBOSITY+1));;
--vm) use_vm=true;;
--) shift; break;;
esac
shift;
Expand Down Expand Up @@ -443,7 +454,7 @@ main() {

trap cleanup EXIT

start_container "$img_ref" "$name" ||
start_instance "$img_ref" "$name" "$use_vm" ||
{ errorrc "Failed to start container for $img_ref"; return; }

get_os_info_in "$name" ||
Expand Down