Skip to content

Commit

Permalink
Merge pull request #26 from glerchundi/master
Browse files Browse the repository at this point in the history
implement how s6 behaves if stage2 fails
  • Loading branch information
glerchundi committed Mar 22, 2015
2 parents ccff187 + 8b16232 commit 959d3f6
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 88 deletions.
5 changes: 4 additions & 1 deletion builder/build-latest
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ for output in "${outputs[@]}"; do
mkdir -p $overlaydstpath/etc/s6/services/s6-fdholderd/supervise
chmod 0700 $overlaydstpath/etc/s6/services/s6-fdholderd/supervise

# fix fix-attrs perms
# fix perms for utilities
chmod 0755 $overlaydstpath/usr/bin/fix-attrs
chmod 0755 $overlaydstpath/usr/bin/import-from-envdir
chmod 0755 $overlaydstpath/usr/bin/with-contenv
chmod 0755 $overlaydstpath/usr/bin/with-notifywhenup

# fix init perms
chmod 0755 $overlaydstpath/init
Expand Down
18 changes: 11 additions & 7 deletions builder/overlay-rootfs/etc/s6/init-catchall/init-stage2
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
# environment.
# Purpose of this file: to perform all the one-time initialization tasks.

if -nt
foreground
{
if
{
redirfd -w 1 /var/run/s6/uncaught-logs-fifo
fdmove -c 2 1

/etc/s6/init/init-stage2-fix-attrs
/etc/s6/init/init-stage2-cont-inits
/etc/s6/init/init-stage2-services
/etc/s6/init/init-stage2-partial
}

/etc/s6/init/init-stage2-cmd $@
}

# if anything in the if -nt { } block fails, control jumps here.
import -u ?
if { s6-test ${?} -ne 0 }
import-from-envdir -u -D 0 /var/run/s6/container_environment S6_BEHAVIOUR_IF_STAGE2_FAILS
if { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -ne 0 }
ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -ne 1 }
{
# Stop supervision tree
s6-svscanctl -t /var/run/s6/services
}
s6-echo -- "\n!!!!!\n init-stage2 failed.\n Logs are in /var/log/s6-uncaught-logs\n!!!!!"
21 changes: 11 additions & 10 deletions builder/overlay-rootfs/etc/s6/init-no-catchall/init-stage2
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
# environment.
# Purpose of this file: to perform all the one-time initialization tasks.

if -nt
foreground
{
if
{
/etc/s6/init/init-stage2-fix-attrs
/etc/s6/init/init-stage2-cont-inits
/etc/s6/init/init-stage2-services
}

if { /etc/s6/init/init-stage2-partial }
/etc/s6/init/init-stage2-cmd $@
}

# if anything in the if -nt { } block fails, control jumps here.
import -u ?
if { s6-test ${?} -ne 0 }
import-from-envdir -u -D 0 /var/run/s6/container_environment S6_BEHAVIOUR_IF_STAGE2_FAILS
if { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -ne 0 }
ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -ne 1 }
{
# Stop supervision tree
s6-svscanctl -t /var/run/s6/services
}
s6-echo -- "\n!!!!!\n init-stage2 failed.\n!!!!!"
9 changes: 2 additions & 7 deletions builder/overlay-rootfs/etc/s6/init/init-stage1
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@
/usr/bin/s6-envdir /etc/s6/init/env
/usr/bin/exec --


##
## route based on what was provided in S6_USE_CATCHALL_LOGGER
##

backtick -n -D 0 S6_USE_CATCHALL_LOGGER
{
if { s6-test -f /var/run/s6/container_environment/S6_USE_CATCHALL_LOGGER }
redirfd -r 0 /var/run/s6/container_environment/S6_USE_CATCHALL_LOGGER
s6-cat
}
import -u S6_USE_CATCHALL_LOGGER
import-from-envdir -u -D 0 /var/run/s6/container_environment S6_USE_CATCHALL_LOGGER
ifelse { s6-test ${S6_USE_CATCHALL_LOGGER} -eq 1 }
{
/etc/s6/init-catchall/init-stage1 $@
Expand Down
5 changes: 4 additions & 1 deletion builder/overlay-rootfs/etc/s6/init/init-stage2-cmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

if -t { s6-test $# -ne 0 }
foreground { s6-setsid -gq -- with-contenv $@ }
foreground { import -u ? s6-echo "${1} exited ${?}. Stopping the supervision tree." }
import -u ?
if { s6-echo "${1} exited ${?}" }

# Stop supervision tree
foreground { s6-svscanctl -t /var/run/s6/services }

# Race condition here, but it's ok: the sync in stage 3 takes ages,
Expand Down
19 changes: 0 additions & 19 deletions builder/overlay-rootfs/etc/s6/init/init-stage2-cont-inits

This file was deleted.

19 changes: 0 additions & 19 deletions builder/overlay-rootfs/etc/s6/init/init-stage2-fix-attrs

This file was deleted.

68 changes: 68 additions & 0 deletions builder/overlay-rootfs/etc/s6/init/init-stage2-partial
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/execlineb -S0

# this env decides what to do if stage2 fails
import-from-envdir -u -D 0 /var/run/s6/container_environment S6_BEHAVIOUR_IF_STAGE2_FAILS

foreground
{
##
## fix-attrs.d: ensure owners and permissions are correct
##

if { s6-echo "[fix-attrs.d] applying owners & permissions fixes..." }
if
{
pipeline { s6-ls -0 -- /etc/fix-attrs.d }
pipeline { s6-sort -0 -- }
forstdin -0 -- i
import -u i
if { s6-echo -n -- "[fix-attrs.d] applying ${i}... " }
foreground { redirfd -r 0 /etc/fix-attrs.d/${i} fix-attrs }
import -u ?
if { s6-echo -- " exited ${?}" }
ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -eq 0 } { exit 0 }
exit ${?}
}
if { s6-echo -- "[fix-attrs.d] done." }


##
## cont-init.d: one-time init scripts
##

if { s6-echo "[cont-init.d] executing container initialization scripts..." }
if
{
pipeline { s6-ls -0 -- /etc/cont-init.d }
pipeline { s6-sort -0 -- }
forstdin -o 0 -0 -- i
import -u i
if { s6-echo -n -- "[cont-init.d] executing ${i}... " }
foreground { with-contenv /etc/cont-init.d/${i} }
import -u ?
if { s6-echo -- " exited ${?}" }
ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -eq 0 } { exit 0 }
exit ${?}
}
if { s6-echo -- "[cont-init.d] done." }

##
## services.d: long-lived processes to be supervised
##


if { s6-echo "[services.d] starting services" }
if
{
pipeline { s6-ls -0 -- /etc/services.d }
forstdin -0 -p -- i
import -u i
if { s6-test -d /etc/services.d/${i} }
s6-hiercopy /etc/services.d/${i} /var/run/s6/services/${i}
}
if { s6-svscanctl -a /var/run/s6/services }
if { s6-echo -- "[services.d] done." }
}
import -u ?
ifelse { s6-test ${S6_BEHAVIOUR_IF_STAGE2_FAILS} -eq 0 } { exit 0 }
exit ${?}
18 changes: 0 additions & 18 deletions builder/overlay-rootfs/etc/s6/init/init-stage2-services

This file was deleted.

8 changes: 2 additions & 6 deletions builder/overlay-rootfs/etc/s6/init/init-stage3
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ foreground { s6-sync }

foreground { s6-echo "[s6-finish] sending all processes the TERM signal." }
foreground { s6-nuke -th } # foreground is process 1: it survives
foreground
{
with-contenv
/usr/bin/import -u -D 3000 S6_KILL_GRACETIME
/usr/bin/s6-sleep -m -- ${S6_KILL_GRACETIME}
}
import-from-envdir -u -D 3000 /var/run/s6/container_environment S6_KILL_GRACETIME
foreground { s6-sleep -m -- ${S6_KILL_GRACETIME} }

# Last message, then close our pipes and give the logger some time.
foreground { s6-echo "[s6-finish] sending all processes the KILL signal and exiting." }
Expand Down
29 changes: 29 additions & 0 deletions builder/overlay-rootfs/usr/bin/import-from-envdir
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/execlineb

elgetopt "uD:"
multisubstitute
{
importas -u envdir 1
importas -u envvar 2
importas -u -D "" defval ELGETOPT_D
import -u -D 0 ELGETOPT_u
}
shift -n 2
elgetpositionals -P0
emptyenv -P
backtick -n ${envvar}
{
ifelse { s6-test -f ${envdir}/${envvar} }
{
redirfd -r 0 ${envdir}/${envvar}
s6-cat
}
s6-echo -n "${defval}"
}
ifelse { s6-test ${ELGETOPT_u} -eq 1 }
{
import -u ${envvar}
$@
}
import ${envvar}
$@

0 comments on commit 959d3f6

Please sign in to comment.