nix-processmgmt/tools/commonchecks

209 lines
4.5 KiB
Bash

#!/bin/bash
set -e
shopt -s nullglob
containsElement()
{
local element match="$1"
shift
for element
do
[[ "$element" == "$match" ]] && return 0
done
return 1
}
checkNixStateDir()
{
NIX_STATE_DIR=${NIX_STATE_DIR:-/nix/var/nix}
}
checkProfile()
{
profile=${profile:-processes}
}
checkProcessesFile()
{
if [ "$1" != "" ]
then
processesFile="$1"
elif [ "$NIXPROC_PROCESSES" != "" ]
then
processesFile="$NIXPROC_PROCESSES"
fi
}
buildProfile()
{
local processManager="$1"
if [ "$undeploy" = "1" ]
then
local deployArg="--undeploy"
else
local deployArg="$processesFile"
fi
profilePath=$(nixproc-build --process-manager $processManager $stateDirArg $runtimeDirArg $logDirArg $tmpDirArg $cacheDirArg $forceDisableUserChangeArg $showTraceArg "${extraParamsArg[@]}" --no-out-link $deployArg)
}
composeProfileBaseDir()
{
if [ "$NIX_USER_PROFILE_DIR" = "" ]
then
profileDir="$NIX_STATE_DIR/profiles"
else
profileDir="$NIX_USER_PROFILE_DIR"
fi
}
composeProfileDir()
{
if [ "$NIX_USER_PROFILE_DIR" = "" ]
then
echo "$NIX_STATE_DIR/profiles/$profile"
else
echo "$NIX_USER_PROFILE_DIR/$profile"
fi
}
composeOldProfilePath()
{
if [ "$oldProfilePath" = "" ]
then
oldProfilePath=$(composeProfileDir)
fi
}
setNixProfile()
{
local profileDir=$(composeProfileDir)
nix-env -p "$profileDir" --set "$profilePath"
}
createNewGroups()
{
for groupfile in $profilePath/dysnomia-support/groups/*
do
local groupname="$(basename $groupfile)"
if [ ! -f "$oldProfilePath/dysnomia-support/groups/$groupname" ]
then
dysnomia-addgroups "$profilePath/dysnomia-support/groups/$groupname"
fi
done
}
createNewUsers()
{
for userfile in $profilePath/dysnomia-support/users/*
do
local username="$(basename $userfile)"
if [ ! -f "$oldProfilePath/dysnomia-support/users/$username" ]
then
dysnomia-addusers "$profilePath/dysnomia-support/users/$username"
fi
done
}
deleteObsoleteUsers()
{
for userfile in $oldProfilePath/dysnomia-support/users/*
do
local username="$(basename $userfile)"
if [ ! -f "$profilePath/dysnomia-support/users/$username" ]
then
dysnomia-delusers "$oldProfilePath/dysnomia-support/users/$username"
fi
done
}
deleteObsoleteGroups()
{
for groupfile in $oldProfilePath/dysnomia-support/groups/*
do
local groupname="$(basename $groupfile)"
if [ ! -f "$profilePath/dysnomia-support/groups/$groupname" ]
then
dysnomia-delgroups "$oldProfilePath/dysnomia-support/groups/$groupname"
fi
done
}
determineLastProfileGenerationId()
{
if [ -d "$profileDir" ] && [ "$(echo $profileDir/$profile-*-link)" != "" ] && [ "$(ls -d $profileDir/$profile-*-link | wc -l)" -gt 1 ]
then
generation="$(basename $(ls -d $profileDir/$profile-*-link | sort -V | tail -2 | head -1))"
else
echo "Unable to roll back as there are no known previous deployments!" >&2
exit 1
fi
}
switchGenerations()
{
# If no generation link has been requested, figure out what the previous one is
if [ "$generationId" = "" ]
then
determineLastProfileGenerationId
else
generation="$profile-$generationId-link"
fi
if [ -e "$profileDir/$generation" ]
then
# Retrieve the manifest of the requested profile generation
profilePath=`readlink -f $profileDir/$generation`
else
echo "Cannot switch profile generations, because the requested profile: $profileDir/$generation does not exist!" >&2
exit 1
fi
}
listGenerations()
{
nix-env -p $profileDir/$profile --list-generations
}
deleteGenerations()
{
nix-env -p $profileDir/$profile --delete-generations "$generations"
}
deleteAllGenerations()
{
rm -f $profileDir/$profile
rm -f $profileDir/$profile-*-link
}
executeDeploymentOperation()
{
local processManager="$1"
case "$operation" in
switchGenerations)
switchGenerations
deploy
;;
listGenerations)
listGenerations
;;
deleteGenerations)
deleteGenerations
;;
deleteAllGenerations)
deleteAllGenerations
;;
*)
buildProfile "$processManager"
deploy
;;
esac
}