nix-processmgmt/tools/systemd/nixproc-systemd-switch.in

199 lines
4.9 KiB
Bash

#!/bin/bash
set -e
shopt -s nullglob
showUsage()
{
cat <<EOF
Usage: $0 [OPTION] PATH
This command repopulates a folder with systemd configuration files and updates
the configuration so that obsolete services will be stoppped and new services
will be started.
Options:
-p, --profile=NAME Name of the Nix profile that stores the sysvinit scripts
(defaults to: processes)
-o, --old-profile=PATH
Path to the previously deployed Nix profile (by default,
it gets auto detected)
--state-dir Changes the directory in which the state of the
processes are stored
--runtime-dir Changes the directory in which the PID files are stored
--log-dir Changes the directory in which the log files are stored
--tmp-dir Changes the directory in which temp files are stored
--force-disable-user-change
Forces to not create users, groups or change user
permissions
--show-trace Shows a trace of the output
-h, --help Shows the usage of this command
Environment:
NIX_STATE_DIR Overrides the location of the Nix state directory
SYSTEMD_TARGET_DIR Directory in which the unit configuration files are
managed (defaults to: /etc/systemd/system)
NIXPROC_STATE_DIR Changes the directory in which the state of the
processes is stored
NIXPROC_RUNTIME_DIR Changes the directory in which the PID files are stored
NIXPROC_LOG_DIR Changes the directory in which log files are stored
NIXPROC_TMP_DIR Changes the directory in which temp files are stored
NIXPROC_FORCE_DISABLE_USER_CHANGE
Forces to not create users, groups or change user
permissions
EOF
}
# Parse valid argument options
PARAMS=`@getopt@ -n $0 -o p:o:h -l profile:,old-profile:,state-dir:,runtime-dir:,log-dir:,tmp-dir:,force-disable-user-change,show-trace,help -- "$@"`
if [ $? != 0 ]
then
showUsage
exit 1
fi
# Evaluate valid options
eval set -- "$PARAMS"
while [ "$1" != "--" ]
do
case "$1" in
-p|--profile)
profile="$2"
;;
-o|--old-profile)
oldProfilePath="$2"
;;
--state-dir)
stateDirArg="--state-dir $2"
;;
--runtime-dir)
runtimeDirArg="--runtime-dir $2"
;;
--log-dir)
logDirArg="--log-dir $2"
;;
--tmp-dir)
tmpDirArg="--tmp-dir $2"
;;
--force-disable-user-change)
forceDisableUserChangeArg="--force-disable-user-change"
;;
--show-trace)
showTraceArg="--show-trace"
;;
-h|--help)
showUsage
exit 0
;;
esac
shift
done
shift
path="$1"
# Validate the given options
SYSTEMD_TARGET_DIR=${SYSTEMD_TARGET_DIR:-/etc/systemd/system}
source @commonchecks@
checkNixStateDir
checkProfile
composeOldProfilePath
# Build the environment with supervisord config files
buildProfile systemd
# Determine paths of old units
oldunits=()
if [ -d "$oldProfilePath" ]
then
for i in $oldProfilePath/etc/systemd/system/*.service
do
currentPath=$(readlink -f "$i")
oldunits+=($currentPath)
done
fi
# Determine paths of new units
newunits=()
for i in $profilePath/etc/systemd/system/*.service
do
currentPath=$(readlink -f "$i")
newunits+=($currentPath)
done
# Create new groups and users
createNewGroups
createNewUsers
if [ "$oldProfilePath" != "" ]
then
# Stop obsolete units
for i in $oldProfilePath/etc/systemd/system/*.service
do
if ! containsElement "$(readlink -f "$i")" "${newunits[@]}"
then
systemctl stop "$(basename "$i")"
fi
done
# Remove obsolete units
for i in $oldProfilePath/etc/systemd/system/*.service
do
if ! containsElement "$(readlink -f "$i")" "${newunits[@]}"
then
unitTargetPath="$SYSTEMD_TARGET_DIR/$(basename "$i")"
rm -f "$unitTargetPath"
if [ -d "$i.wants" ]
then
rm -f "$unitTargetPath.wants"
fi
fi
done
fi
# Add new units
for i in $profilePath/etc/systemd/system/*.service
do
if ! containsElement "$(readlink -f "$i")" "${oldunits[@]}"
then
if [ -d "$i.wants" ]
then
ln -sfn "$(readlink -f "$i.wants")" $SYSTEMD_TARGET_DIR
fi
ln -sfn "$(readlink -f "$i")" $SYSTEMD_TARGET_DIR/$(basename "$i")
fi
done
# Reload the systemd configuration
systemctl daemon-reload
# Start all units in the new configuration
for i in $profilePath/etc/systemd/system/*.service
do
systemctl start "$(basename "$i")"
done
# Delete obsolete users and groups
deleteObsoleteUsers
deleteObsoleteGroups
# Set new profile
setNixProfile