Laboratorio domestico
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
A Linux machine at home (a small PC, a Raspberry Pi) runs a handful of containers. One Python program on it reads the machine's load and the containers, and brings its own screen: a rack in which every service is a unit with a status light, and a tap pulls a unit out to restart, stop or start it. The Connectivitizer app shows that screen from anywhere. The machine needs no open port and no public address.
| Folder | What it is |
|---|---|
server/homelab_panel.py | The program: an HTTP API on 127.0.0.1, Python standard library only. |
ui/homelab.html | The screen (device UIs): one self-contained page that follows the phone's light or dark setting. |
ui/manifest.json | What the program serves as /.cz/ui.json. |
ui/demo-data.js | A pretend server for the demo on the landing page (demo kit). |
pip.docker or podman on the PATH for the list of services. On Windows or macOS the program runs simulated: an invented machine with eight invented services, so the example can be tried anywhere. On Linux without a container engine the machine is real and the services are simulated. The program prints which it is when it starts, /load says "simulated": true, and the screen shows a Simulated label.cz on the same machine, a device key (czd_…) and a shared key (czs_…) from the web app.cd src/examples/homelab/server
python3 homelab_panel.py # http://127.0.0.1:8765, read-onlyOpen http://127.0.0.1:8765/.cz/ui.html in a browser on the same machine to see the screen without Connectivitizer, or ask the API directly: curl http://127.0.0.1:8765/load.
| Option | Default | |
|---|---|---|
--port 8765 (HOMELAB_PORT) | 8765 | The port on 127.0.0.1. The program never listens on another address. |
--allow-actions (HOMELAB_ALLOW_ACTIONS=1) | off | Allows restart, stop and start. See Read-only. |
--read-only | on | The default, spelled out. It wins over --allow-actions and the variable. |
--engine docker|podman (HOMELAB_ENGINE) | the one that is installed | Which container engine to ask. |
--simulate | off on Linux | The invented machine and services, also on Linux. |
--verbose | off | Logs every request. |
The program stays as it is and keeps speaking plain HTTP on localhost; cz runs next to it and makes that port reachable as the service api:
cz device login --key czd_… --name homelab
cz service add api 127.0.0.1:8765 --protocol http
cz key add default czs_… # the shared key; the app has the same one
cz agent run # keep it running (as a service)The device appears in the workspace as homelab. Installing cz, and running it with the machine: the cz guide.
Instead of cz, the program could start the Python SDK itself (pip install huelake-connectivitizer, then Agent(device_key=…, name="homelab", shared_key=…, services={"api": "127.0.0.1:8765"}), see the Python guide); this example does not, to stay without dependencies.
The Connectivitizer app (Android) shows the device's own screen when the device is opened; the example app in ../react-native-app offers it behind Open. The page asks for /load and /services every 1.5 seconds through the app's session, and the app's bar takes the page's colours.
What needs attention comes first: unhealthy services, then restarting ones, then the running ones, then the stopped ones. While a unit is pulled out the order stays as it is.
Out of the box the three POST addresses answer 403, so that trying the example cannot stop anything by accident. The screen then shows a note, "Read-only mode: this panel is only watching", when a button is pressed.
To control services from the phone, start the program with --allow-actions (or HOMELAB_ALLOW_ACTIONS=1). Whoever can reach the service api of this device in your workspace can then restart and stop its containers.
How an action is carried out:
404, and nothing is executed.docker restart <id>, no shell), and the id in it is the one the engine reported, never text from the request.POST that a browser sends from another site (an Origin that is not the program's own address) is refused with 403."state": "restarting" and runs in the background; the list shows restarting until the engine is done. Stop and start answer when the engine is done.All under the service api. Errors are { "error": "<code>", "message": "…" }.
| Request | Answer |
|---|---|
GET /load | { "hostname", "uptimeSeconds", "load": [1m, 5m, 15m], "cpuPercent", "memory": { "used", "total" }, "disks": [ { "mount", "used", "total" } ], "temperatureC": number | null, "network": { "rxBytesPerSec", "txBytesPerSec" }, "simulated": true | false }. Bytes everywhere. From /proc, os.statvfs and, when there is one, the hottest zone in /sys/class/thermal. |
GET /services | [ { "id", "name", "image", "state": "running" | "stopped" | "restarting" | "unhealthy", "uptimeSeconds", "cpuPercent", "memoryBytes", "ports": ["8080"], "restarts" } ]. id is the first 12 characters of the container's id, cpuPercent is percent of one core, ports are the published host ports. From ps, inspect and stats of the engine, refreshed about every 2 seconds. |
POST /services/{id}/restart | The service after the action, in the shape above. 403 (read-only) unless actions are allowed, 404 (unknown-service) for an id that is not in the list, 502 (action-failed) when the engine refuses. |
POST /services/{id}/stop | The same. |
POST /services/{id}/start | The same. |
GET /.cz/ui.json, GET /.cz/ui.html | The manifest and the page, read from ../ui/ at each request. |
ui/demo-data.js is the pretend device behind the page on the landing page: the same two GET answers and three POST addresses in the same shapes, with invented values (eight services, one of which fails its health check for a quarter of a minute about once a minute and recovers). Actions are allowed there, since nothing real is behind them; a restart takes three seconds. The program's own simulated mode behaves the same way. The page is not changed for the demo.
cd src/connectivitizer
node scripts/build-demos.mjs --only homelab --out <folder> # writes <folder>/homelab/ui.html, which opens from the file systemcurl, read-only (403 on all three actions) and with --allow-actions (restart, the three seconds of restarting, stop, start, 404 for an unknown id, 403 for a foreign Origin). The page in a browser against this program, including the read-only note and the Offline, retrying state when the program is stopped./proc readings once, read-only, in Ubuntu 24.04 under WSL 2: /load gave plausible values, with temperatureC: null (no thermal zone there). Not run on a Raspberry Pi or other real hardware, so the temperature reading from /sys/class/thermal has not been seen working.ps, inspect and stats are read was checked with made-up output only; podman in particular may name fields differently. No real container was restarted, stopped or started.cz agent run with this program nor the page inside the app. The commands above are the ones from the cz guide.