Hi team,
I have been using part of Balena Dash’s Scheduler code since it seems to be a great way to get chron jobs working at a container level. For my code, I would like to stop a container at night and start it again in the morning.
After starting with the Scheduler code, I know I need to interact with the Supervisor API. To do this, I changed the Dockerfile from alpine
to alpine-python
. In addition to that, it looks like the %%BALENA_MACHINE_NAME%%
variable breaks the code for my Raspberry Pi 4, giving the error “invalid reference format: repository name must be lowercase
”. I was able to get around this by using the image located at FROM balenalib/raspberrypi3-alpine-python
After that, I know interacting with the supervisor seems to require curl, so I add it via
RUN install_packages wget unzip curl
From there, I make the bash and python files executable and run start.sh, which I will paste here:
start.sh
#!/bin/bash
if [ "$ENABLE_TIMER" -eq "1" ]
then
(crontab -l; echo "${TURN_ON:-0 8 * * *} /usr/src/turn_on.py") | crontab -
(crontab -l; echo "${TURN_OFF:-0 23 * * *} /usr/src/turn_off.py") | crontab -
fi
crond -f
And here is the turn_off.py script that runs out of the start.sh file. The turn_on.py script is almost the same.
turn_off.py
import sys
import os
def turnOff():
print('Turning off in turn_off.py')
os.system('curl --header "Content-Type:application/json" "$BALENA_SUPERVISOR_ADDRESS/v2/applications/$BALENA_APP_ID/stop-service?apikey=$BALENA_SUPERVISOR_API_KEY" -d \'{"serviceName": "electronpi4"}\'')
os.system('curl --header "Content-Type:application/json" "$BALENA_SUPERVISOR_ADDRESS/v2/applications/$BALENA_APP_ID/stop-service?apikey=$BALENA_SUPERVISOR_API_KEY" -d \'{"serviceName": "wifi-connect"}\'')
os.system('curl --header "Content-Type:application/json" "$BALENA_SUPERVISOR_ADDRESS/v2/applications/$BALENA_APP_ID/stop-service?apikey=$BALENA_SUPERVISOR_API_KEY" -d \'{"serviceName": "keyboard"}\'')
turnOff()
While this compiles, the terminal throws the following errors:
Error Log
/usr/src/turn_off.py: line 1: import: not found
/usr/src/turn_off.py: line 2: import: not found
/usr/src/turn_off.py: line 3: syntax error: unexpected word (expecting “)”)
It seems like I am missing some frameworks to interact with the Supervisor API. Any thoughts on how to solve this?
TL;DR - Trying to interact with the Supervisor within the Scheduler container seems to throw errors.