nixos-rebuild related API queries

master
Alexander Tomokhov 2023-12-12 12:04:21 +04:00
parent 79b91a2c57
commit be7e7b086d
2 changed files with 71 additions and 0 deletions

View File

@ -60,6 +60,11 @@
script = ./enable-service.sh;
env-vars = [ ];
}
{
name = "nixos-rebuild";
script = ./nixos-rebuild.sh;
env-vars = [ ];
}
]);
};
}

66
nixos-rebuild.sh Executable file
View File

@ -0,0 +1,66 @@
#! /usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
shopt -s inherit_errexit
: "${DOMAIN:?specify domain name}"
readonly REBUILD_OP="${1:?nixos-rebuild related API operation not given error, e.g. Rebuild, Rollback, Upgrade}"
readonly AUTH_HEADER="Authorization: Bearer ${API_TOKEN:?not set error}"
QUERY=$(cat << EOF
mutation RebuildSwitch {
__typename
system {
runSystem${REBUILD_OP} {
code
success
}
}
}
EOF
)
# shellcheck disable=SC2086,SC2116
REQUEST=$(cat << EOF
{
"query": "$(echo $QUERY)",
"variables": null
}
EOF
)
readonly REQUEST
EXPECTED_RESPONSE=$(cat << EOF
{
"data": {
"__typename": "Mutation",
"system": {
"runSystemRebuild": {
"code": 200,
"success": true
}
}
}
}
EOF
)
received_response="$(curl --show-error -s "https://api.$DOMAIN/graphql" \
--compressed \
-H 'Content-Type: application/json' \
--data-raw "$REQUEST" -k -H "$AUTH_HEADER")"
sorted_response="$(jq --sort-keys <<<"$received_response")" \
|| { echo "error"; echo "$received_response"; echo -e "QUERY:\n$QUERY"; exit 1; }
jq <<<"$sorted_response"
if diff -w -u --color <(printf "%s\n" "$EXPECTED_RESPONSE") <(printf "%s\n" "$sorted_response") 2>/dev/null
then
echo -e "\e[1;32mOK"
else
echo -e "\e[1;31mFAIL: response does not match!"
jd -color <(printf "%s\n" "$EXPECTED_RESPONSE") <(printf "%s" "$sorted_response")
exit 1
fi