Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ux: Change the way docker runs the container #29

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
22 changes: 8 additions & 14 deletions balenaos-in-container.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)){
Expand All @@ -66,12 +66,6 @@ if($prefix){
$docker_prefix = "balena-"
}

if($detach){
$detachVal = "--detach"
}else{
$detachVal = ""
}

if($no_tty){
$no_ttyVal = ""
}else{
Expand Down Expand Up @@ -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} `
Expand All @@ -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) {
Expand All @@ -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
}
29 changes: 10 additions & 19 deletions balenaos-in-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,7 +31,7 @@ ARGUMENTS:
-c, --config <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 <arguments>
Expand All @@ -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
}

Expand Down Expand Up @@ -91,8 +87,8 @@ while [[ $# -ge 1 ]]; do
fi
shift
;;
-d|--detach)
detach="--detach"
-d|--detached)
detached="true"
;;
--extra-args)
docker_extra_args="$2"
Expand Down Expand Up @@ -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 \
Expand All @@ -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