Skip to content

Commit

Permalink
fixup! [OPS-1467] Introduce mkActivateScript for user-level systemd s…
Browse files Browse the repository at this point in the history
…ervices
  • Loading branch information
karandit committed Oct 17, 2023
1 parent 45f01d7 commit cf7f92f
Showing 1 changed file with 39 additions and 48 deletions.
87 changes: 39 additions & 48 deletions lib/systemd/user-level-services.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,14 @@ in {
set -euo pipefail
export XDG_RUNTIME_DIR="/run/user/$UID"
mkdir -v -p "$HOME/.config/systemd/user"
${if backupAction != ""
then
''
# Deploy may change DB schema via migration and we should be able
# to restore its state to a point before an update to successfully rollback
# failed deployment
echo Backing up the existing state
${backupAction}
''
else
""
${pkgs.lib.optionalString (backupAction != "")
''
# Deploy may change DB schema via migration and we should be able
# to restore its state to a point before an update to successfully rollback
# failed deployment
echo Backing up the existing state
${backupAction}
''
}
${pkgs.lib.concatStringsSep "\n" (builtins.map (service:
Expand All @@ -60,48 +57,42 @@ in {
) allServices)}
systemctl --user daemon-reload
${if restoreAction != ""
then
''
rollback() {
exit_code="$?"
if [[ $exit_code -ne 0 ]]; then
echo Activation failed, restoring the previous working state
${restoreAction}
fi
exit "$exit_code"
}
trap rollback EXIT
''
else ""
${pkgs.lib.optionalString (restoreAction != "")
''
rollback() {
exit_code="$?"
if [[ $exit_code -ne 0 ]]; then
echo Activation failed, restoring the previous working state
${restoreAction}
fi
exit "$exit_code"
}
trap rollback EXIT
''
}
${pkgs.lib.concatStringsSep "\n" (builtins.map (service:
if builtins.elem "default.target" vm.config.systemd.units.${service}.wantedBy
then
''
echo Restarting ${service}
systemctl --user restart ${service}
''
else ""
pkgs.lib.optionalString (builtins.elem "default.target" vm.config.systemd.units.${service}.wantedBy)
''
echo Restarting ${service}
systemctl --user restart ${service}
''
) (auxiliaryServices ++ [primaryService]))}
${if checkAction != null
then
''
retry_count=0
while [[ "$retry_count" -lt 10 ]]; do
echo "Checking if the server is up, round $((retry_count+1))"
set +e
${checkAction vm.config.services.${moduleConfig.nixOsServiceName}}
set -e
retry_count=$((retry_count+1))
sleep 5
done
# If we didn't manage to get 200, fail and cause a rollback
exit 1
''
else ""
${pkgs.lib.optionalString (checkAction != null)
''
retry_count=0
while [[ "$retry_count" -lt 10 ]]; do
echo "Checking if the server is up, round $((retry_count+1))"
set +e
${checkAction vm.config.services.${moduleConfig.nixOsServiceName}}
set -e
retry_count=$((retry_count+1))
sleep 5
done
# If we didn't manage to get 200, fail and cause a rollback
exit 1
''
}
'';
}

0 comments on commit cf7f92f

Please sign in to comment.