Skip to content

Instantly share code, notes, and snippets.

@pdcastro
pdcastro / poll-tado.py
Created April 17, 2023 21:33
Sample Python script to collect temperature and humidity data through the Tado cloud API
import asyncio
from dataclasses import dataclass
from datetime import datetime, timezone
from PyTado.interface import Tado
TADO_USERNAME = "xxxx"
TADO_PASSWORD = "xxxx"
@dataclass
@pdcastro
pdcastro / get_container_name.sh
Last active February 17, 2022 11:41
balenaOS shell script to get a full container name given the service name
#!/bin/bash
# Get a full container name given its short service name.
# The full container name can then be used as argument for
# the `balena-engine exec` command.
#
# Usage instructions:
#
# * Save this file somewhere, e.g.:
# $ vi /tmp/get_container_name.sh
@pdcastro
pdcastro / notes.md
Last active July 12, 2022 13:28
balenaOS installation in a libvirt / qemu / kvm virtual machine

balenaOS in a libvirt / qemu / kvm virtual machine

Successfully tested with Ubuntu 20.04 as the host OS in 2 quite different "environments":

@pdcastro
pdcastro / filter-stdin.py
Created October 4, 2021 22:45
Extract 'REMOTE_EXIT_CODE=' from stdin and pass the rest through
#!/usr/bin/env python3
import fileinput, sys
exitCode = 0
for line in fileinput.input():
if line.startswith('REMOTE_EXIT_CODE='):
exitCode = int(line.split(sep='=', maxsplit=1)[1])
else:
print(line, end='')
#!/usr/bin/env bash
# Provision an Ubuntu or similar system as an openBalena "local machine"
# Tested with Ubuntu 20.04 LTS
set -eo pipefail # quit this script on errors
CERT_PATH="/usr/local/share/ca-certificates/openbalena.crt"
INSTALL_DIR="/opt"
CLI_BIN_PATH="/usr/local/bin/balena"
@pdcastro
pdcastro / start.sh
Created September 7, 2019 01:36
fix_route function for the provisioning container
#!/bin/bash
...
fix_route () {
good_wifi=$(ip route | perl -ne '/default\s+via\s+(\S+).+?metric\s+600/; if ($1) {print $1; exit}')
good_eth0=$(ip route | perl -ne '/default\s+via\s+(\S+).+?metric\s+100/; if ($1) {print $1; exit}')
bad_eth0=$(ip route | perl -ne '/default\s+via\s+(\S+).+?metric\s+20100/; if ($1) {print $1; exit}')
# if there are no good routes with metrics 100 or 600, and there is a "bad"
# route with metric 20100, assume the bad route is actually good and use it
@pdcastro
pdcastro / Dockerfile.template
Created August 31, 2019 02:24
dash-keyboard
FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3
RUN apt-get update && apt-get install kbd git patch
RUN mkdir -p /usr/src/mydash
WORKDIR /usr/src/mydash
RUN git clone https://github.com/boppreh/keyboard
COPY keyboard.patch /usr/src/mydash/keyboard/
RUN cd keyboard && patch -p1 < keyboard.patch
ENV PYTHONPATH="/usr/src/mydash/keyboard:${PYTHONPATH}"
CMD python keyboard/examples/stdin_stdout_events.py
@pdcastro
pdcastro / Dockerfile
Last active August 14, 2019 14:37
Sample 'eu.gcr.io' registry secrets usage with "balena build"
FROM eu.gcr.io/buoyant-idea-226013/arm32v7/busybox
RUN uname -a
CMD while : ; do echo "(Plain Dockerfile 3) $(uname -a)"; sleep ${INTERVAL=5}; done
@pdcastro
pdcastro / Dockerfile.template
Last active July 6, 2019 01:43
Test SIGTERM
FROM balenalib/%%BALENA_ARCH%%-debian-node:10.16-jessie-build as build
WORKDIR /usr/src/app
COPY . .
# CMD ["npm", "start"]
CMD ["/usr/local/bin/node", "index.js"]
@pdcastro
pdcastro / Dockerfile
Last active February 13, 2021 00:12
Dockerfile for "npm install balena-cli" on Ubuntu Bionic
# Sample Dockerfile for installing balena-cli on Ubuntu Bionic
# Usage:
# $ docker build -t cli .
# $ docker run -it --privileged --network host -v /var/run/docker.sock:/var/run/docker.sock cli
FROM ubuntu:18.04
# install dependencies - don't install nodejs or npm via apt-get!
RUN apt-get update && apt-get install -y curl git python g++ make
ENV NODE_VERSION v12.20.2
ENV NVM_DIR /usr/local/nvm
ENV PATH $NVM_DIR/versions/node/$NODE_VERSION/bin:$PATH