nix-processmgmt/tools/common/nixproc-build.in

219 lines
5.9 KiB
Bash

#!/bin/bash -e
# Shows the usage of this command to the user
showUsage()
{
me="$(basename "$0")"
cat <<EOF
Usage: $me [OPTION] [PATH]
or: $me --undeploy [OPTION]
This command builds a Nix profile containing multiple sysvinit scripts, and
their start and stop symlinks.
Options:
--undeploy Generates a configuration to undeploy all processes
-P, --process-manager=MANAGER
Process manager to build for
--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
--cache-dir Changes the directory in which cache files are stored
--spool-dir Changes the directory in which spool files are stored
--lock-dir Changes the directory in which lock files are stored
--lib-dir Changes the directory in which state files are stored
--force-disable-user-change
Forces to not create users, groups or change user
permissions
--no-out-link Do not create a symlink to the output path
--show-trace Shows a trace of the output
--extra-params=PARAMS
A string with an attribute set in the Nix expression
language propagating extra parameters to the input models
-h, --help Shows the usage of this command
Environment:
NIXPROC_PROCESSES Path to a processes model
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_CACHE_DIR Changes the directory in which cache files are stored
NIXPROC_SPOOL_DIR Changes the directory in which spool files are stored
NIXPROC_LOCK_DIR Changes the directory in which lock files are stored
NIXPROC_LIB_DIR Changes the directory in which state 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:h -l undeploy,process-manager:,state-dir:,runtime-dir:,log-dir:,tmp-dir:,cache-dir:,spool-dir:,lock-dir:,lib-dir:,force-disable-user-change,no-out-link,show-trace,extra-params:,help -- "$@"`
if [ $? != 0 ]
then
showUsage
exit 1
fi
# Evaluate valid options
eval set -- "$PARAMS"
while [ "$1" != "--" ]
do
case "$1" in
--undeploy)
undeploy=1
;;
-P|--process-manager)
processManager="$2"
;;
--state-dir)
stateDirArg="--argstr stateDir $2"
;;
--runtime-dir)
runtimeDirArg="--argstr runtimeDir $2"
;;
--log-dir)
logDirArg="--argstr logDir $2"
;;
--tmp-dir)
tmpDirArg="--argstr tmpDir $2"
;;
--cache-dir)
cacheDirArg="--argstr cacheDir $2"
;;
--spool-dir)
cacheDirArg="--argstr spoolDir $2"
;;
--lock-dir)
lockDirArg="--argstr lockDir $2"
;;
--lib-dir)
libDirArg="--argstr libDir $2"
;;
--force-disable-user-change)
forceDisableUserChangeArg="--arg forceDisableUserChange true"
;;
--no-out-link)
noOutLinkArg="--no-out-link"
;;
--show-trace)
showTraceArg="--show-trace"
;;
--extra-params)
extraParamsArg=("--arg" "extraParams" "$2")
;;
-h|--help)
showUsage
exit 0
;;
esac
shift
done
shift
# Validate the given options
if [ "$processManager" = "" ]
then
echo "No process manager specified!" >&2
exit 1
else
processManagerArg="--argstr processManager $processManager"
fi
if [ "$undeploy" = "1" ]
then
exprFileArg="--arg exprFile null"
else
if [ "$1" != "" ]
then
exprFile="$(@readlink@ -f "$1")"
elif [ "$NIXPROC_PROCESSES" != "" ]
then
exprFile="$(@readlink@ -f "$NIXPROC_PROCESSES")"
else
echo "No processes expression provided!" >&2
exit 1
fi
exprFileArg="--argstr exprFile $exprFile"
fi
if [ "$NIXPROC_STATE_DIR" != "" ]
then
stateDirArg="--argstr stateDir $NIXPROC_STATE_DIR"
fi
if [ "$NIXPROC_RUNTIME_DIR" != "" ]
then
runtimeDirArg="--argstr stateDir $NIXPROC_RUNTIME_DIR"
fi
if [ "$NIXPROC_LOG_DIR" != "" ]
then
logDirArg="--argstr logDir $NIXPROC_LOG_DIR"
fi
if [ "$NIXPROC_TMP_DIR" != "" ]
then
tmpDirArg="--argstr tmpDir $NIXPROC_TMP_DIR"
fi
if [ "$NIXPROC_CACHE_DIR" != "" ]
then
cacheDirArg="--argstr cacheDir $NIXPROC_CACHE_DIR"
fi
if [ "$NIXPROC_SPOOL_DIR" != "" ]
then
cacheDirArg="--argstr spoolDir $NIXPROC_SPOOL_DIR"
fi
if [ "$NIXPROC_LOCK_DIR" != "" ]
then
cacheDirArg="--argstr lockDir $NIXPROC_LOCK_DIR"
fi
if [ "$NIXPROC_LIB_DIR" != "" ]
then
cacheDirArg="--argstr libDir $NIXPROC_LIB_DIR"
fi
if [ "$NIXPROC_FORCE_DISABLE_USER_CHANGE" = "1" ]
then
forceDisableUserChangeArg="--arg forceDisableUserChange true"
fi
NIXPROC=${NIXPROC:-@NIXPROC@}
# Build the profile
nix-build $stateDirArg \
$runtimeDirArg \
$logDirArg \
$tmpDirArg \
$cacheDirArg \
$spoolDirArg \
$lockDirArg \
$libDirArg \
$forceDisableUserChangeArg \
--argstr callingUser "$(id -un)" \
--argstr callingGroup "$(id -gn)" \
$noOutLinkArg \
$showTraceArg \
$processManagerArg \
"${extraParamsArg[@]}" \
$exprFileArg \
$NIXPROC/backends/$processManager/build-$processManager-env.nix