Skip to content
This repository has been archived by the owner on Apr 20, 2022. It is now read-only.

Commit

Permalink
octoprint: Update API model with Octoprint API key
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
- '5000:5000'
labels:
io.balena.features.supervisor-api: '1'
io.balena.features.balena-api: '1'
traefik.enable: 'true'
traefik.http.services.octoprint.loadbalancer.server.port: '5000'

Expand Down
3 changes: 3 additions & 0 deletions octoprint/Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ RUN PYTHONUSERBASE=/usr/local/ pip install --no-cache-dir \
"https://github.com/vitormhenrique/OctoPrint-Enclosure/archive/master.zip"


RUN apk add --no-cache curl jq
COPY update-balena-app.sh /update-balena-app.sh

# Seed the initial config.yaml
COPY ./config.yaml /data/config.yaml

Expand Down
7 changes: 7 additions & 0 deletions octoprint/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ stderr_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes=0
stdout_logfile=/dev/stdout

[program:update-balena]
command=/update-balena-app.sh
stderr_logfile_maxbytes=1024
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes=1024
stdout_logfile=/dev/stdout
52 changes: 52 additions & 0 deletions octoprint/update-balena-app.sh
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

0 comments on commit 66436c1

Please sign in to comment.