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

168 lines
4.3 KiB
Bash

#!/bin/bash
set -e
shopt -s nullglob
showUsage()
{
cat <<EOF
Usage: $0 [OPTION] PATH
This command updates the Windows services 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
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
source @commonchecks@
checkNixStateDir
checkProfile
composeOldProfilePath
# Build the environment with supervisord config files
buildProfile cygrunsrv
# Determine paths of old param files
oldparams=()
if [ -d "$oldProfilePath" ]
then
for i in $oldProfilePath/*-cygrunsrvparams
do
currentPath=$(readlink -f "$i")
oldparams+=($currentPath)
done
fi
# Determine paths of new param files
newparams=()
for i in $profilePath/*-cygrunsrvparams
do
currentPath=$(readlink -f "$i")
newparams+=($currentPath)
done
if [ -d "$oldProfilePath" ]
then
# Stop and remove obsolete services
for i in $oldProfilePath/*-cygrunsrvparams
do
if ! containsElement "$(readlink -f "$i")" "${newparams[@]}"
then
serviceName="$(basename $i -cygrunsrvparams)"
cygrunsrv --stop $serviceName
cygrunsrv --remove $serviceName
fi
done
fi
# Set new profile
setNixProfile
# Install all services in the new configuration
for i in $profilePath/*-cygrunsrvparams
do
if ! containsElement "$(readlink -f "$i")" "${oldparams[@]}"
then
serviceName="$(basename $i -cygrunsrvparams)"
cat $i | xargs -d '\n' /bin/echo cygrunsrv --install $serviceName
fi
done
# Start all services in the new configuration
for i in $profilePath/*-cygrunsrvparams
do
if ! containsElement "$(readlink -f "$i")" "${oldparams[@]}"
then
serviceName="$(basename $i -cygrunsrvparams)"
cygrunsrv --start $serviceName
fi
done