forge
ExploreLog in

Tahinli/forge

public
0 watching
★ 0
825 bytes
1#!/bin/sh
2# Roll back ~/bin/forge to ~/bin/forge.prev after a unit failed.
3# Usage: forge-rollback <unit-name>. Output goes to the journal.
4set -eu
5
6unit="${1:?usage: forge-rollback <unit-name>}"
7bin="$HOME/bin/forge"
8prev="$bin.prev"
9bad="$bin.bad"
10
11if [ ! -e "$prev" ]; then
12 echo "forge-rollback: $unit failed and $prev does not exist, nothing to roll back"
13 exit 0
14fi
15
16sha=$(sha256sum "$bin" | cut -d' ' -f1)
17# Keep one hash per line, even if the file was edited by hand without a final newline.
18if [ -s "$bad" ] && [ -n "$(tail -c 1 "$bad")" ]; then
19 echo >> "$bad"
20fi
21echo "$sha" >> "$bad"
22mv -f "$prev" "$bin"
23echo "forge-rollback: $unit failed, marked $sha bad, restored $prev"
24
25systemctl --user reset-failed "$unit"
26systemctl --user start "$unit"
27echo "forge-rollback: started $unit on the previous binary"