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

run script refactor #57

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
71 changes: 40 additions & 31 deletions run
Original file line number Diff line number Diff line change
@@ -1,57 +1,66 @@
#!/bin/bash
# Executed by init script

# load/set general global vars
INITHOOKS_DEFAULT="${INITHOOKS_DEFAULT:-/etc/default/inithooks}"
# shellcheck source=/dev/null
source "$INITHOOKS_DEFAULT"
TERM=${TERM:-linux}
RUN_FIRSTBOOT="${RUN_FIRSTBOOT:-}"
RUN_FIRSTBOOT="${RUN_FIRSTBOOT,,}"
if [[ -f $INITHOOKS_CONF ]]; then
# shellcheck source=/dev/null
source "$INITHOOKS_CONF"
export INITHOOKS_CONF=$INITHOOKS_CONF
fi
TKLINFO="${TKLINFO:-/var/lib/turnkey-info}"
export INITHOOKS_LOGFILE="${INITHOOKS_LOGFILE:-/var/log/inithooks.log}"
PID=

INITHOOKS_DEFAULT=/etc/default/inithooks
. $INITHOOKS_DEFAULT

TKLINFO=/var/lib/turnkey-info

unset PID INITHOOKS_LOGFILE

REDIRECT_OUTPUT=$(echo $REDIRECT_OUTPUT | tr [A-Z] [a-z])
# initalise log file
mkdir -p "$(dirname "$INITHOOKS_LOGFILE")"
touch "$INITHOOKS_LOGFILE"
chmod 640 "$INITHOOKS_LOGFILE"

log() {
# log to journal as well as $INITHOOKS_LOGFILE
LEVEL=$1 # err|warn|info|debug
local level=${1} # err|warn|info|debug
shift
logger -t inithooks -p $LEVEL "$@"
[[ -n "$INITHOOKS_LOGFILE" ]] \
&& echo "${LEVEL^^}: $@" >> "$INITHOOKS_LOGFILE"
logger -t inithooks -p "${level,,}" "$@"
if [[ -f "$INITHOOKS_LOGFILE" ]]; then
echo "${level^^}: $*" >> "$INITHOOKS_LOGFILE"
fi
}

if [[ "$REDIRECT_OUTPUT" == "true" ]]; then
# redirect stdout/stderr (use when preseeding headless deployments)
export INITHOOKS_LOGFILE=/var/log/inithooks.log
touch $INITHOOKS_LOGFILE
chmod 640 $INITHOOKS_LOGFILE

if [[ "${REDIRECT_OUTPUT,,}" == "true" ]]; then
# on xen redirection is performed by the inithooks-xen service
# on lxc and other headless deployments, redirection is handled below
# otherwise redirection is handled by inithooks service and redirected to
# tty8

if [[ ! -f "$TKLINFO/xen" ]]; then
TTY=$(cat /sys/devices/virtual/tty/tty0/active)
[[ -z $TTY ]] && TTY=console
tail -f $INITHOOKS_LOGFILE > /dev/$TTY &
if [[ -z $TTY ]]; then
TTY=console
fi
tail -f "$INITHOOKS_LOGFILE" > "/dev/$TTY" &
PID="$!"
fi
fi

exec_scripts() {
script_dir=$1
local script_dir=$1
local script_executable=
local script=
[[ -d "$script_dir" ]] || return 0
for SCRIPT in $(find $script_dir -type f -or -type l | sort); do
[[ -e $INITHOOKS_CONF ]] && . $INITHOOKS_CONF
script=$(basename $SCRIPT)
if [[ ! -x "$SCRIPT" ]]; then
for script_executable in $(find "$script_dir" -type f -or -type l | sort); do
script=$(basename "$script_executable")
if [[ ! -x "$script_executable" ]]; then
log warn "[$script] skipping"
continue
fi
log info "[$script] running"
"$SCRIPT"
"$script_executable"
exit_code=$?
if [[ "$exit_code" -eq 0 ]]; then
log info "[$script] successfully completed"
Expand All @@ -69,13 +78,13 @@ exec_scripts() {
return 0
}

[[ -e $INITHOOKS_CONF ]] && . $INITHOOKS_CONF
export INITHOOKS_CONF=$INITHOOKS_CONF

if [[ "${RUN_FIRSTBOOT,,}" == "true" ]]; then
exec_scripts $INITHOOKS_PATH/firstboot.d
if [[ "$RUN_FIRSTBOOT" == "true" ]]; then
log info "Running firstboot scripts"
exec_scripts "$INITHOOKS_PATH/firstboot.d"
fi
exec_scripts $INITHOOKS_PATH/everyboot.d
log info "Running firstboot scripts"
exec_scripts "$INITHOOKS_PATH/everyboot.d"

if [[ -n "$PID" ]]; then
kill -9 $PID || true
Expand Down