This repository has been archived by the owner on Apr 20, 2022. It is now read-only.
forked from balena-io-experimental/balena-octoprint
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
octoprint: Update API model with Octoprint API key
When running Octoprint, the API key is pulled from the config and a corresponding Device/Service API key is updated to match. This triggers Octodash to restart and use the newly updated key. Change-type: patch Signed-off-by: Rich Bayliss <rich@balena.io>
- Loading branch information
Rich Bayliss
committed
Jun 29, 2020
1 parent
591c4a5
commit 66436c1
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
|
||
if [ -z $BALENA_API_KEY ]; then | ||
echo "BALENA_API_KEY is not set. Exiting." | ||
exit 1 | ||
fi | ||
|
||
SLEEP_FOR=1 | ||
while [ -z $OCTOPRINT_API_KEY ] | ||
do | ||
echo "OctoPrint API Key not set. Sleeping ${SLEEP_FOR}s..." | ||
sleep $SLEEP_FOR | ||
|
||
SLEEP_FOR=5 | ||
OCTOPRINT_API_KEY="$(cat ~/.octoprint/config.yaml | grep key: | awk 'BEGIN {OFS = ":"} { print $2 }')" | ||
done | ||
|
||
|
||
OCTODASH_SI=$(curl -Ss \ | ||
--header "Authorization: Bearer ${BALENA_API_KEY}" \ | ||
--request GET \ | ||
'https://api.balena-cloud.com/v5/service_install?$top=1&$select=id&$filter=device/any(d:d/uuid%20eq%20%27'${BALENA_DEVICE_UUID}'%27)%20and%20installs__service/any(s:s/service_name%20eq%20%27octodash%27)' \ | ||
| jq .d[0].id) | ||
|
||
create_api_key() { | ||
curl -f -Ss \ | ||
--header 'Accept: application/json' \ | ||
--header "Authorization: Bearer ${BALENA_API_KEY}" \ | ||
--header "Content-Type: application/json" \ | ||
--data-raw '{"service_install":'$OCTODASH_SI', "name":"OCTOPRINT_APIKEY", "value":"'$OCTOPRINT_API_KEY'"}' \ | ||
--request POST \ | ||
https://api.balena-cloud.com/v5/device_service_environment_variable | ||
} | ||
|
||
patch_api_key() { | ||
curl -Ss \ | ||
--header 'Accept: application/json' \ | ||
--header "Authorization: Bearer ${BALENA_API_KEY}" \ | ||
--header "Content-Type: application/json" \ | ||
--data-raw '{"value":"'$OCTOPRINT_API_KEY'"}' \ | ||
--request PATCH \ | ||
'https://api.balena-cloud.com/v5/device_service_environment_variable?$top=1&$filter=service_install%20eq%20'$OCTODASH_SI'%20and%20name%20eq%20%27OCTOPRINT_APIKEY%27' | ||
} | ||
|
||
create_api_key || patch_api_key | ||
|
||
while [ true ] | ||
do | ||
sleep 1 | ||
done |