2006-12-10 23:29:44 +01:00
|
|
|
|
#! @shell@
|
|
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
export PATH=/empty
|
2006-12-18 17:27:56 +01:00
|
|
|
|
for i in @path@; do PATH=$PATH:$i/bin:$i/sbin; done
|
2006-12-10 23:29:44 +01:00
|
|
|
|
action="$1"
|
|
|
|
|
|
2006-12-16 22:48:12 +01:00
|
|
|
|
if ! test -e /etc/NIXOS; then
|
|
|
|
|
echo "This is not a NixOS installation (/etc/NIXOS) is missing!"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2006-12-10 23:29:44 +01:00
|
|
|
|
if test -z "$action"; then
|
|
|
|
|
cat <<EOF
|
|
|
|
|
Usage: $0 [switch|boot|test]
|
|
|
|
|
|
|
|
|
|
switch: make the configuration the boot default and activate now
|
|
|
|
|
boot: make the configuration the boot default
|
|
|
|
|
test: activate the configuration, but don't make it the boot default
|
|
|
|
|
EOF
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2009-12-16 19:11:26 +01:00
|
|
|
|
# Install or update the bootloader.
|
|
|
|
|
if [ "$action" = "switch" -o "$action" = "boot" ]; then
|
2012-07-25 16:47:32 +02:00
|
|
|
|
@installBootLoader@ @out@
|
2006-12-10 23:29:44 +01:00
|
|
|
|
fi
|
|
|
|
|
|
2009-12-16 19:11:26 +01:00
|
|
|
|
# Activate the new configuration.
|
2012-03-18 02:53:35 +01:00
|
|
|
|
if [ "$action" != switch -a "$action" != test ]; then exit 0; fi
|
2006-12-18 17:27:56 +01:00
|
|
|
|
|
2012-07-16 17:27:59 +02:00
|
|
|
|
oldVersion=$(cat /run/current-system/upstart-interface-version 2> /dev/null || echo 0)
|
2012-03-18 02:53:35 +01:00
|
|
|
|
newVersion=$(cat @out@/upstart-interface-version 2> /dev/null || echo 0)
|
2009-04-28 15:38:22 +02:00
|
|
|
|
|
2012-03-18 02:53:35 +01:00
|
|
|
|
if test "$oldVersion" -ne "$newVersion"; then
|
2009-04-28 15:38:22 +02:00
|
|
|
|
cat <<EOF
|
|
|
|
|
Warning: the new NixOS configuration has an Upstart version that is
|
|
|
|
|
incompatible with the current version. The new configuration won't
|
|
|
|
|
take effect until you reboot the system.
|
|
|
|
|
EOF
|
|
|
|
|
exit 1
|
2012-03-18 02:53:35 +01:00
|
|
|
|
fi
|
2009-04-28 15:38:22 +02:00
|
|
|
|
|
2012-03-23 14:37:22 +01:00
|
|
|
|
# Ignore SIGHUP so that we're not killed if we're running on (say)
|
|
|
|
|
# virtual console 1 and we restart the "tty1" job.
|
|
|
|
|
trap "" SIGHUP
|
|
|
|
|
|
2012-03-18 19:53:50 +01:00
|
|
|
|
jobsDir=$(readlink -f @out@/etc/init)
|
2006-12-18 17:27:56 +01:00
|
|
|
|
|
2012-03-18 02:53:35 +01:00
|
|
|
|
# Stop all currently running jobs that are not in the new Upstart
|
|
|
|
|
# configuration. (Here "running" means all jobs that are not in the
|
|
|
|
|
# stop/waiting state.)
|
|
|
|
|
for job in $(initctl list | sed -e '/ stop\/waiting/ d; /^[^a-z]/ d; s/^\([^ ]\+\).*/\1/' | sort); do
|
2012-03-18 19:53:50 +01:00
|
|
|
|
if ! [ -e "$jobsDir/$job.conf" ] ; then
|
2012-03-18 02:53:35 +01:00
|
|
|
|
echo "stopping obsolete job ‘$job’..."
|
2012-03-18 03:36:44 +01:00
|
|
|
|
stop --quiet "$job" || true
|
2012-03-18 02:53:35 +01:00
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Activate the new configuration (i.e., update /etc, make accounts,
|
|
|
|
|
# and so on).
|
|
|
|
|
echo "activating the configuration..."
|
|
|
|
|
@out@/activate @out@
|
|
|
|
|
|
|
|
|
|
# Make Upstart reload its jobs.
|
|
|
|
|
initctl reload-configuration
|
|
|
|
|
|
|
|
|
|
# Allow Upstart jobs to react intelligently to a config change.
|
|
|
|
|
initctl emit config-changed
|
|
|
|
|
|
2012-03-18 19:53:50 +01:00
|
|
|
|
declare -A tasks=(@tasks@)
|
|
|
|
|
declare -A noRestartIfChanged=(@noRestartIfChanged@)
|
|
|
|
|
|
2012-03-19 19:07:05 +01:00
|
|
|
|
start_() {
|
|
|
|
|
local job="$1"
|
|
|
|
|
if start --quiet "$job"; then
|
|
|
|
|
# Handle services that cancel themselves.
|
|
|
|
|
if ! [ -n "${tasks[$job]}" ]; then
|
|
|
|
|
local status=$(status "$job")
|
|
|
|
|
[[ "$status" =~ start/running ]] || echo "job ‘$job’ failed to start!"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-23 14:37:22 +01:00
|
|
|
|
log() {
|
|
|
|
|
echo "$@" >&2 || true
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-18 02:53:35 +01:00
|
|
|
|
# Restart all running jobs that have changed. (Here "running" means
|
|
|
|
|
# all jobs that don't have a "stop" goal.) We use the symlinks in
|
|
|
|
|
# /var/run/upstart-jobs (created by each job's pre-start script) to
|
|
|
|
|
# determine if a job has changed.
|
2012-03-18 20:05:44 +01:00
|
|
|
|
for job in @jobs@; do
|
2012-03-18 02:53:35 +01:00
|
|
|
|
status=$(status "$job")
|
|
|
|
|
if ! [[ "$status" =~ start/ ]]; then continue; fi
|
2012-03-18 19:53:50 +01:00
|
|
|
|
if [ "$(readlink -f "$jobsDir/$job.conf")" = "$(readlink -f "/var/run/upstart-jobs/$job")" ]; then continue; fi
|
|
|
|
|
if [ -n "${noRestartIfChanged[$job]}" ]; then
|
2012-03-23 14:37:22 +01:00
|
|
|
|
log "not restarting changed service ‘$job’"
|
2012-03-18 02:53:35 +01:00
|
|
|
|
continue
|
|
|
|
|
fi
|
2012-03-23 14:37:22 +01:00
|
|
|
|
log "restarting changed service ‘$job’..."
|
2012-03-18 02:53:35 +01:00
|
|
|
|
# Note: can't use "restart" here, since that only restarts the
|
|
|
|
|
# job's main process.
|
2012-03-18 03:36:44 +01:00
|
|
|
|
stop --quiet "$job" || true
|
2012-03-19 19:07:05 +01:00
|
|
|
|
start_ "$job" || true
|
2012-03-18 02:53:35 +01:00
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Start all jobs that are not running but should be. The "should be"
|
|
|
|
|
# criterion is tricky: the intended semantics is that we end up with
|
2012-03-18 19:04:49 +01:00
|
|
|
|
# the same jobs as after a reboot. If it's a task, start it if it
|
2012-03-18 02:53:35 +01:00
|
|
|
|
# differs from the previous instance of the same task; if it wasn't
|
|
|
|
|
# previously run, don't run it. If it's a service, only start it if
|
|
|
|
|
# it has a "start on" condition.
|
2012-03-18 20:05:44 +01:00
|
|
|
|
for job in @jobs@; do
|
2012-03-18 02:53:35 +01:00
|
|
|
|
status=$(status "$job")
|
|
|
|
|
if ! [[ "$status" =~ stop/ ]]; then continue; fi
|
|
|
|
|
|
2012-03-18 19:53:50 +01:00
|
|
|
|
if [ -n "${tasks[$job]}" ]; then
|
2012-03-18 02:53:35 +01:00
|
|
|
|
if [ ! -e "/var/run/upstart-jobs/$job" -o \
|
2012-03-18 19:53:50 +01:00
|
|
|
|
"$(readlink -f "$jobsDir/$job.conf")" = "$(readlink -f "/var/run/upstart-jobs/$job")" ];
|
2012-03-18 02:53:35 +01:00
|
|
|
|
then continue; fi
|
2012-03-18 19:53:50 +01:00
|
|
|
|
if [ -n "${noRestartIfChanged[$job]}" ]; then continue; fi
|
2012-03-23 14:37:22 +01:00
|
|
|
|
log "starting task ‘$job’..."
|
2012-03-18 03:36:44 +01:00
|
|
|
|
start --quiet "$job" || true
|
2012-03-18 02:53:35 +01:00
|
|
|
|
else
|
2012-03-18 19:53:50 +01:00
|
|
|
|
if ! grep -q "^start on" "$jobsDir/$job.conf"; then continue; fi
|
2012-03-23 14:37:22 +01:00
|
|
|
|
log "starting service ‘$job’..."
|
2012-03-19 19:07:05 +01:00
|
|
|
|
start_ "$job" || true
|
2012-03-18 02:53:35 +01:00
|
|
|
|
fi
|
2010-07-23 12:52:51 +02:00
|
|
|
|
|
2012-03-18 02:53:35 +01:00
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Signal dbus to reload its configuration.
|
|
|
|
|
dbusPid=$(initctl status dbus 2> /dev/null | sed -e 's/.*process \([0-9]\+\)/\1/;t;d')
|
|
|
|
|
[ -n "$dbusPid" ] && kill -HUP "$dbusPid"
|