ci/deploy: add basic locking

This commit is contained in:
Aaron Bieber 2023-02-02 15:24:22 -07:00
parent af0b144a27
commit c5c1b1284a
No known key found for this signature in database
2 changed files with 28 additions and 0 deletions

View File

@ -1,6 +1,13 @@
. /etc/profile . /etc/profile
. /run/secrets/po_env . /run/secrets/po_env
SCRIPT_NAME="${0##*/}"
SCRIPT_PID=$$
LOCK_PATH="${LOCK:-/tmp/xin}"
LOCK_FILE="${LOCK_PATH}/${SCRIPT_NAME}"
mkdir -p "${LOCK_PATH}"
NIX_SSHOPTS="-i /run/secrets/manager_pubkey -oIdentitiesOnly=yes -oControlPath=/tmp/manager-ssh-%r@%h:%p -F/dev/null" NIX_SSHOPTS="-i /run/secrets/manager_pubkey -oIdentitiesOnly=yes -oControlPath=/tmp/manager-ssh-%r@%h:%p -F/dev/null"
SSH="ssh ${NIX_SSHOPTS}" SSH="ssh ${NIX_SSHOPTS}"
CurrentVersion="$(git rev-parse HEAD)" CurrentVersion="$(git rev-parse HEAD)"
@ -11,6 +18,24 @@ msg() {
echo -e "===> $@" echo -e "===> $@"
} }
unlock() {
rm -f ${LOCK_FILE};
}
_lock() {
echo "${SCRIPT_PID}" > "${LOCK_FILE}"
trap 'unlock' INT EXIT TERM
}
lock() {
if [ -f "${LOCK_FILE}" ]; then
msg "${SCRIPT_NAME} already running..."
exit 0
else
_lock
fi
}
listNixOSHosts() { listNixOSHosts() {
for i in $(nix eval .#nixosConfigurations --apply builtins.attrNames --json | jq -r '.[]'); do for i in $(nix eval .#nixosConfigurations --apply builtins.attrNames --json | jq -r '.[]'); do
if [ -d hosts/${i} ]; then if [ -d hosts/${i} ]; then
@ -73,6 +98,7 @@ start() {
} }
start_ci() { start_ci() {
lock
agentHasKey "$(cat /run/secrets/ci_ed25519_pub | awk '{print $2}')" || agentHasKey "$(cat /run/secrets/ci_ed25519_pub | awk '{print $2}')" ||
ssh-add /run/secrets/ci_ed25519_key ssh-add /run/secrets/ci_ed25519_key
} }
@ -106,3 +132,4 @@ handle_merge_fail() {
handle_push_fail() { handle_push_fail() {
po_error "CI: git push failed!" "Pelase help!" po_error "CI: git push failed!" "Pelase help!"
} }

1
deploy
View File

@ -133,6 +133,7 @@ ret=0
if [ ${#@} = 1 ]; then if [ ${#@} = 1 ]; then
rebuild $1 true || ret=1 rebuild $1 true || ret=1
else else
lock
for host in $(listNixOSHosts); do for host in $(listNixOSHosts); do
rebuild $host false || ret=1 rebuild $host false || ret=1
done done