diff --git a/Dockerfile.template b/Dockerfile.template index ce543e0..a708c6f 100644 --- a/Dockerfile.template +++ b/Dockerfile.template @@ -1,9 +1,42 @@ # see more about dockerfile templates here: https://www.balena.io/docs/learn/develop/dockerfile/ FROM python:3.13-slim-bookworm -# Set our working directory +RUN apt-get update && apt-get install -y --no-install-recommends \ + curl \ + dnsmasq \ + wireless-tools + +# use latest version. If specific version is required, it should be provided as vX.Y.Z, e.g v4.11.37 +ARG VERSION="latest" + +# create /usr/src/app/ui directory where we will extract pre-built UI +WORKDIR /usr/src/app/ui + WORKDIR /usr/src/app +RUN \ + export BASE_URL="https://github.com/balena-os/wifi-connect/releases" &&\ + DETECTED_ARCH=$(uname -m) &&\ + case $DETECTED_ARCH in \ + "aarch64") \ + BINARY_ARCH_NAME="aarch64-unknown-linux-gnu" ;; \ + "x86_64") \ + BINARY_ARCH_NAME="x86_64-unknown-linux-gnu" ;;\ + "armv7l") \ + BINARY_ARCH_NAME="armv7-unknown-linux-gnueabihf" ;;\ + *) \ + echo >&2 "error: unsupported architecture ($DETECTED_ARCH)"; exit 1 ;; \ + esac;\ + if [ ${VERSION} = "latest" ]; then \ + export URL_PARTIAL="latest/download" ; \ + else \ + export URL_PARTIAL="download/${VERSION}" ; \ + fi; \ + curl -Ls "$BASE_URL/$URL_PARTIAL/wifi-connect-$BINARY_ARCH_NAME.tar.gz" \ + | tar -xvz -C /usr/src/app/ && \ + curl -Ls "$BASE_URL/$URL_PARTIAL/wifi-connect-ui.tar.gz" \ + | tar -xvz -C /usr/src/app/ui + # Copy requirements.txt first for better cache on later pushes COPY requirements.txt requirements.txt @@ -13,5 +46,9 @@ RUN pip install -r requirements.txt # This will copy all files in our root to the working directory in the container COPY . ./ +COPY scripts/start.sh . + +ENV DBUS_SYSTEM_BUS_ADDRESS unix:path=/host/run/dbus/system_bus_socket + # main.py will run when container starts up on the device -CMD ["python","-u","src/app.py"] +CMD ["sh", "./start.sh"] diff --git a/docker-compose.yml b/docker-compose.yml index 00511b2..83a8f5a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,4 +4,8 @@ services: balena-hello-world: build: . ports: - - "80:80" \ No newline at end of file + - "80:80" + volumes: + - /run/dbus:/host/run/dbus:ro + labels: + io.balena.features.dbus: "1" diff --git a/requirements.txt b/requirements.txt index e0c1d21..a81e1a7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ -click==8.1.8 -Flask==2.3.3 -importlib-metadata==4.13.0 -itsdangerous==2.2.0 -Jinja2==3.1.6 -MarkupSafe==2.1.5 -Werkzeug==2.0.3 -zipp==3.21.0 +click +Flask +importlib-metadata +itsdangerous +Jinja2 +MarkupSafe +Werkzeug +zipp diff --git a/scripts/start.sh b/scripts/start.sh new file mode 100644 index 0000000..72a34ea --- /dev/null +++ b/scripts/start.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +sleep 1 # give Supervisor time to attach log stream +set -x + +export DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket + +# Optional step - it takes couple of seconds (or longer) to establish a WiFi connection +# sometimes. In this case, following checks will fail and wifi-connect +# will be launched even if the device will be able to connect to a WiFi network. +# If this is your case, you can wait for a while and then check for the connection. +# sleep 15 + +# Choose a condition for running WiFi Connect according to your use case: + +# 1. Is there a default gateway? +# ip route | grep default + +# 2. Is there Internet connectivity? +# nmcli -t g | grep full + +# 3. Is there Internet connectivity via a google ping? +# wget --spider http://google.com 2>&1 + +# 4. Is there an active WiFi connection? +iwgetid -r + +if [ $? -eq 0 ]; then + printf 'Skipping WiFi Connect\n' +else + printf 'Starting WiFi Connect\n' + ./wifi-connect +fi + +python -u src/app.py \ No newline at end of file diff --git a/src/app.py b/src/app.py index 160d40e..1f323a1 100644 --- a/src/app.py +++ b/src/app.py @@ -6,4 +6,5 @@ def hello_world(): return render_template('index.html') if __name__ == '__main__': + print('Starting Hello World') app.run(host='0.0.0.0', port=80)