188 lines
4.0 KiB
Bash
188 lines
4.0 KiB
Bash
#!/bin/bash
|
|
set -e
|
|
shopt -s nullglob
|
|
|
|
showUsage()
|
|
{
|
|
cat <<EOF
|
|
Usage: $0 [OPTION] PATH
|
|
|
|
Deploys a prebuilt Docker configuration profile.
|
|
|
|
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)
|
|
-h, --help Shows the usage of this command
|
|
|
|
Environment:
|
|
NIX_STATE_DIR Overrides the location of the Nix state directory
|
|
EOF
|
|
}
|
|
|
|
# Parse valid argument options
|
|
|
|
PARAMS=`@getopt@ -n $0 -o p:o:h -l profile:,old-profile:,state-dir:,force-disable-user-change,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"
|
|
;;
|
|
-h|--help)
|
|
showUsage
|
|
exit 0
|
|
;;
|
|
esac
|
|
|
|
shift
|
|
done
|
|
|
|
shift
|
|
|
|
profilePath="$1"
|
|
|
|
# Validate the given options
|
|
|
|
source @commonchecks@
|
|
|
|
checkNixStateDir
|
|
checkProfile
|
|
composeOldProfilePath
|
|
|
|
# Execute the deployment operation
|
|
|
|
deployContainer()
|
|
{
|
|
local configDir="$1"
|
|
local containerName="$2"
|
|
|
|
(
|
|
source $configDir/$containerName-docker-settings
|
|
dockerContainerName="nixproc-$containerName"
|
|
|
|
# Load the Docker image (this operation is already idempotent)
|
|
docker load -i $dockerImage
|
|
|
|
# Create the container if it does not exists yet
|
|
if [ "$(docker ps -a -f "name=$dockerContainerName\$" | wc -l)" = "1" ]
|
|
then
|
|
(
|
|
cat $configDir/$containerName-docker-createparams
|
|
echo "--name"
|
|
echo "$dockerContainerName"
|
|
|
|
if [ "$forceDisableUserChange" = "1" ]
|
|
then
|
|
echo "--user"
|
|
id -u
|
|
fi
|
|
|
|
echo "$dockerImageTag"
|
|
) | @xargs@ -d '\n' docker create
|
|
fi
|
|
|
|
if [ "$(docker ps -f "name=$dockerContainerName\$" | wc -l)" = "1" ]
|
|
then
|
|
docker start $dockerContainerName
|
|
fi
|
|
)
|
|
}
|
|
|
|
undeployContainer()
|
|
{
|
|
local configDir="$1"
|
|
local containerName="$2"
|
|
|
|
(
|
|
source $configDir/$containerName-docker-settings
|
|
dockerContainerName="nixproc-$containerName"
|
|
dockerStopTimeout=${dockerStopTimeout:-1}
|
|
|
|
if [ "$(docker ps -f "name=$dockerContainerName\$" | wc -l)" = "2" ]
|
|
then
|
|
docker stop -t $dockerStopTimeout $dockerContainerName
|
|
fi
|
|
|
|
if [ "$(docker ps -a -f "name=$dockerContainerName\$" | wc -l)" = "2" ]
|
|
then
|
|
docker rm $dockerContainerName
|
|
fi
|
|
|
|
if [ "$(docker images -f "reference=$dockerImageTag" | wc -l)" = "2" ]
|
|
then
|
|
docker rmi $dockerImageTag
|
|
fi
|
|
)
|
|
}
|
|
|
|
# Determine paths of old containers
|
|
|
|
oldunits=()
|
|
|
|
if [ -d "$oldProfilePath" ]
|
|
then
|
|
for i in $oldProfilePath/*-docker-priority
|
|
do
|
|
currentPath=$(readlink -f "$i")
|
|
oldunits+=($currentPath)
|
|
done
|
|
fi
|
|
|
|
# Determine paths of new containers
|
|
|
|
newunits=()
|
|
|
|
for i in $profilePath/*-docker-priority
|
|
do
|
|
currentPath=$(readlink -f "$i")
|
|
newunits+=($currentPath)
|
|
done
|
|
|
|
if [ "$oldProfilePath" != "" ]
|
|
then
|
|
# Undeploy obsolete containers
|
|
|
|
if [ "$(echo $oldProfilePath/*-docker-priority)" != "" ]
|
|
then
|
|
for i in $(ls $oldProfilePath/*-docker-priority | sort -r)
|
|
do
|
|
if ! containsElement "$(readlink -f "$i")" "${newunits[@]}"
|
|
then
|
|
priorityFile="$(basename "$i" -docker-priority)"
|
|
undeployContainer "$(dirname "$i")" "${priorityFile:3}"
|
|
fi
|
|
done
|
|
fi
|
|
fi
|
|
|
|
# Deploy new containers
|
|
|
|
for i in $profilePath/*-docker-priority
|
|
do
|
|
if ! containsElement "$(readlink -f "$i")" "${oldunits[@]}"
|
|
then
|
|
priorityFile="$(basename "$i" -docker-priority)"
|
|
deployContainer "$(dirname "$i")" "${priorityFile:3}"
|
|
fi
|
|
done
|
|
|
|
# Set new profile
|
|
setNixProfile
|