diff --git a/balenaos-in-container.ps1 b/balenaos-in-container.ps1 index 694d156..99cc7c3 100644 --- a/balenaos-in-container.ps1 +++ b/balenaos-in-container.ps1 @@ -24,7 +24,7 @@ The config.json path. This you can download from balena.io dashboard. Mandatory argument. Alias: -c -.PARAMETER detach +.PARAMETER detached Run the container in the background and print container ID (just like "docker run -d") Default: no. Alias: -d @@ -42,11 +42,11 @@ Default: no. .EXAMPLE -PS> .\balenaos-in-container.ps1 -image resin/resinos:2.46.0_rev1.dev-intel-nuc -id test -c "$PWD\config.json" -detach +PS> .\balenaos-in-container.ps1 -image resin/resinos:2.46.0_rev1.dev-intel-nuc -id test -c "$PWD\config.json" -detached #> param ([Parameter(Mandatory,HelpMessage="Docker image to be used as balenaOS. Mandatory Argument")][string]$image,[switch]$no_tty, [Parameter(Mandatory,HelpMessage="The config.json path. This you can download from balena.io dashboard. -Mandatory argument.")][Alias('c')][string]$config_path,[string]$prefix,[string]$id, [switch]$clean_volumes,[switch][Alias('d')]$detach, [string]$extra_args) +Mandatory argument.")][Alias('c')][string]$config_path,[string]$prefix,[string]$id, [switch]$clean_volumes,[switch][Alias('d')]$detached, [string]$extra_args) #check if config file exists if(![System.IO.File]::Exists($config_path)){ @@ -66,12 +66,6 @@ if($prefix){ $docker_prefix = "balena-" } -if($detach){ - $detachVal = "--detach" -}else{ - $detachVal = "" -} - if($no_tty){ $no_ttyVal = "" }else{ @@ -109,7 +103,7 @@ foreach ( $volume in $balena_boot_volume,$balena_state_volume, $balena_data_volu $container_name="${docker_prefix}container-${docker_postfix}" echo "INFO: Running balenaOS as container ${container_name} ..." -docker run $no_ttyVal --rm --privileged ` +docker run -d -t --rm --privileged ` --stop-timeout=30 ` -e "container=docker" ` --name ${container_name} ` @@ -121,14 +115,14 @@ docker run $no_ttyVal --rm --privileged ` -v "${balena_state_volume}:/mnt/state" ` -v "${balena_data_volume}:/mnt/data" ` $extra_args ` - ${detachVal} ` ${image} ` sh -c '/aufs2overlay;exec /sbin/init' if ($LastExitCode -eq 0){ - if ($detach -ne ""){ - echo "INFO: balenaOS container running as ${container_name}" + echo "INFO: balenaOS container running as ${container_name}" + if ($detached -ne ""){ + docker attach ${container_name} } } elseif ($LastExitCode -eq 130 -or $LastExitCode -eq 137) { @@ -138,7 +132,7 @@ else{ echo "ERROR: Running docker container." } -if (!$detach -and $clean_volumes){ +if ($clean_volumes){ echo "Cleaning volumes..." docker volume rm "${balena_boot_volume}" "${balena_state_volume}" "${balena_data_volume}" 2>&1>$null } diff --git a/balenaos-in-container.sh b/balenaos-in-container.sh index afdbdf4..c3829e8 100755 --- a/balenaos-in-container.sh +++ b/balenaos-in-container.sh @@ -7,8 +7,7 @@ docker_prefix="balena-" docker_postfix="$RANDOM" clean_volumes=no docker_extra_args="" -detach="" -no_tty="-ti" +detached="" function help { cat << EOF @@ -32,7 +31,7 @@ ARGUMENTS: -c, --config The config.json path. This you can download from balena.io dashboard. Mandatory argument. - -d, --detach + -d, --detached Run the container in the background and print container ID (just like "docker run -d") Default: no. --extra-args @@ -41,9 +40,6 @@ ARGUMENTS: If volumes are not planned to be reused, you can take advantage of this argument to clean up the system. Cannot be used together with -d. Default: no. - --no-tty - Don't allocate a pseudo-TTY and don't keep STDIN open (docker run without "-it"). - Default: no. EOF } @@ -91,8 +87,8 @@ while [[ $# -ge 1 ]]; do fi shift ;; - -d|--detach) - detach="--detach" + -d|--detached) + detached="true" ;; --extra-args) docker_extra_args="$2" @@ -156,7 +152,7 @@ done container_name="${docker_prefix}container-${docker_postfix}" echo "INFO: Running balenaOS as container ${container_name} ..." #shellcheck disable=SC2086 -if docker run $no_tty --rm --privileged \ +if docker run -d -t --rm --privileged \ -e "container=docker" \ --stop-timeout=30 \ --dns 127.0.0.2 \ @@ -169,23 +165,18 @@ if docker run $no_tty --rm --privileged \ -v "${balena_state_volume}:/mnt/state" \ -v "${balena_data_volume}:/mnt/data" \ $docker_extra_args \ - $detach \ "$image" \ sh -c '/aufs2overlay;exec /sbin/init'; then - if [ "$detach" != "" ]; then - echo "INFO: balenaOS container running as ${container_name}" + + echo "INFO: balenaOS container running as ${container_name}" + if [ "$detached" = "" ]; then + docker attach "${container_name}" else echo "ERROR: Running docker container." fi -else - if [ "$detach" != "" ]; then - echo "ERROR: Running docker container." - else - echo "INFO: balenaOS container stopped." - fi fi -if [ "$detach" = "" ] && [ "$clean_volumes" = "yes" ]; then +if [ "$clean_volumes" = "yes" ]; then echo "Cleaning volumes..." docker volume rm "${balena_boot_volume}" "${balena_state_volume}" "${balena_data_volume}" &> /dev/null fi