From 1b6cf320a66c7c43ff3690d6e4a163e8ba48a8ab Mon Sep 17 00:00:00 2001 From: root Date: Fri, 11 Nov 2022 13:20:43 +0800 Subject: [PATCH 1/2] add optional sub-stage to install container by default --- .../.04-pre-install-containers/00-run.sh | 29 ++++++++ .../.04-pre-install-containers/01-run.sh | 26 +++++++ .../basics-station-packet-forwarder.yml | 14 ++++ .../docker-compose-files/the-things-stack.yml | 72 +++++++++++++++++++ .../udp-packet-forwarder.yml | 13 ++++ .../.04-pre-install-containers/files/rc.local | 34 +++++++++ .../docker-compose.sh.sample | 22 ++++++ .../run-once-services/portainer.sh.sample | 16 +++++ .../.04-pre-install-containers/files/runonce | 12 ++++ stage2-rak/README.md | 52 +++++++------- 10 files changed, 266 insertions(+), 24 deletions(-) create mode 100755 stage2-rak/.04-pre-install-containers/00-run.sh create mode 100755 stage2-rak/.04-pre-install-containers/01-run.sh create mode 100644 stage2-rak/.04-pre-install-containers/files/docker-compose-files/basics-station-packet-forwarder.yml create mode 100644 stage2-rak/.04-pre-install-containers/files/docker-compose-files/the-things-stack.yml create mode 100644 stage2-rak/.04-pre-install-containers/files/docker-compose-files/udp-packet-forwarder.yml create mode 100755 stage2-rak/.04-pre-install-containers/files/rc.local create mode 100755 stage2-rak/.04-pre-install-containers/files/run-once-services/docker-compose.sh.sample create mode 100755 stage2-rak/.04-pre-install-containers/files/run-once-services/portainer.sh.sample create mode 100755 stage2-rak/.04-pre-install-containers/files/runonce diff --git a/stage2-rak/.04-pre-install-containers/00-run.sh b/stage2-rak/.04-pre-install-containers/00-run.sh new file mode 100755 index 0000000..ce863ae --- /dev/null +++ b/stage2-rak/.04-pre-install-containers/00-run.sh @@ -0,0 +1,29 @@ +#!/bin/bash -e + + +# Copy docker-compose files +cp -r files/docker-compose-files/ "${ROOTFS_DIR}/usr/local/etc/" + +# You can save the compressed images to the stage2-rak/.04-pre-install-containers/files/images, +# and then just do local copy and paste +# cp -r files/images "${ROOTFS_DIR}/usr/local/etc/" + +# You can choose to just download the example compressed file we provided, here is some examples +# Basicstation packet forwarder +# wget -O ./files/images/image-tar-basicstation.tar https://github.com/Sheng2216/saved-images-for-rakpios/releases/download/v1.0.1/image-tar-basicstation.tar + +# Portainer +wget -O ./files/images/image-tar-portainer.tar https://github.com/Sheng2216/saved-images-for-rakpios/releases/download/v1.0.1/image-tar-portainer.tar + +# Required images for The Things Stack, including postgres, redis, and stack +# wget -O ./files/images/image-tar-tts-postgres.tar https://github.com/Sheng2216/saved-images-for-rakpios/releases/download/v1.0.1/image-tar-tts-postgres.tar +# wget -O ./files/images/image-tar-tts-redis.tar https://github.com/Sheng2216/saved-images-for-rakpios/releases/download/v1.0.1/image-tar-tts-redis.tar +# wget -O ./files/images/image-tar-tts-stack.tar https://github.com/Sheng2216/saved-images-for-rakpios/releases/download/v1.0.1/image-tar-tts-stack.tar + +# UDP packet forwarder +wget -O ./files/images/image-tar-udp.tar https://github.com/Sheng2216/saved-images-for-rakpios/releases/download/v1.0.1/image-tar-udp.tar + +# You can also choose to manually save the compressed images to the stage2-rak/.04-pre-install-containers/files/images, instead of downloading the compressed image + +# Copy the compressed images to the RAKPiOS +cp -r files/images "${ROOTFS_DIR}/usr/local/etc/" diff --git a/stage2-rak/.04-pre-install-containers/01-run.sh b/stage2-rak/.04-pre-install-containers/01-run.sh new file mode 100755 index 0000000..54dd202 --- /dev/null +++ b/stage2-rak/.04-pre-install-containers/01-run.sh @@ -0,0 +1,26 @@ +#!/bin/bash -e + +# Create a directory structure to store run once scripts +on_chroot << EOF +mkdir -p /etc/local/runonce.d/ran/ +EOF + +# Copy portainer.sh.sample to /etc/local/runonce.d/ +# This is where you store the actual services +cp files/run-once-services/portainer.sh.sample "${ROOTFS_DIR}/etc/local/runonce.d/" +cp files/run-once-services/docker-compose.sh.sample "${ROOTFS_DIR}/etc/local/runonce.d/" + +# copy the runonce script +cp files/runonce "${ROOTFS_DIR}/usr/local/bin/" +# copy the rc.local script +cp files/rc.local "${ROOTFS_DIR}/etc/rc.local" + +# configure the premission for the service scripts +on_chroot << EOF +chown ${FIRST_USER_NAME}:${FIRST_USER_NAME} /etc/local/runonce.d/portainer.sh.sample +chmod +x /etc/local/runonce.d/portainer.sh.sample + +chown ${FIRST_USER_NAME}:${FIRST_USER_NAME} /etc/local/runonce.d/docker-compose.sh.sample +chmod +x /etc/local/runonce.d/docker-compose.sh.sample + +EOF diff --git a/stage2-rak/.04-pre-install-containers/files/docker-compose-files/basics-station-packet-forwarder.yml b/stage2-rak/.04-pre-install-containers/files/docker-compose-files/basics-station-packet-forwarder.yml new file mode 100644 index 0000000..818315b --- /dev/null +++ b/stage2-rak/.04-pre-install-containers/files/docker-compose-files/basics-station-packet-forwarder.yml @@ -0,0 +1,14 @@ +version: '2.0' + +services: + + basicstation: + image: xoseperez/basicstation:latest + container_name: basicstation + restart: unless-stopped + privileged: true + network_mode: host # required to read main interface MAC instead of virtual one + environment: + MODEL: "RAK2287" + INTERFACE: "SPI" + TC_KEY: "..." # Copy here your API key from the LNS diff --git a/stage2-rak/.04-pre-install-containers/files/docker-compose-files/the-things-stack.yml b/stage2-rak/.04-pre-install-containers/files/docker-compose-files/the-things-stack.yml new file mode 100644 index 0000000..34da06b --- /dev/null +++ b/stage2-rak/.04-pre-install-containers/files/docker-compose-files/the-things-stack.yml @@ -0,0 +1,72 @@ +version: '3.7' + +volumes: + redis: + postgres: + stack-blob: + stack-data: + +services: + + postgres: + image: postgres:11.12 + container_name: postgres + restart: unless-stopped + environment: + - POSTGRES_PASSWORD=root + - POSTGRES_USER=root + - POSTGRES_DB=ttn_lorawan + volumes: + - 'postgres:/var/lib/postgresql/data' + ports: + - "127.0.0.1:5432:5432" + + redis: + image: redis:6.2.4-alpine3.13 + container_name: redis + command: redis-server --appendonly yes + restart: unless-stopped + volumes: + - 'redis:/data' + ports: + - "127.0.0.1:6379:6379" + + stack: + image: xoseperez/the-things-stack:latest + container_name: stack + restart: unless-stopped + depends_on: + - redis + - postgres + volumes: + - 'stack-blob:/srv/ttn-lorawan/public/blob' + - 'stack-data:/srv/data' + environment: + - TZ=${TZ:-Europe/London} + - PUID=${PUID:-1000} + - PGID=${PGID:-100} + - TTS_DOMAIN=${TTS_DOMAIN:-127.0.0.1} # set this to the IP or domain name of the host you will be using to access the stack + - TTN_LW_BLOB_LOCAL_DIRECTORY=/srv/ttn-lorawan/public/blob + - TTN_LW_REDIS_ADDRESS=redis:6379 + - TTN_LW_IS_DATABASE_URI=postgres://root:root@postgres:5432/ttn_lorawan?sslmode=disable + + ports: + + - "80:1885" + - "443:8885" + + - "1881:1881" + - "1882:1882" + - "1883:1883" + - "1884:1884" + - "1885:1885" + - "1887:1887" + + - "8881:8881" + - "8882:8882" + - "8883:8883" + - "8884:8884" + - "8885:8885" + - "8887:8887" + + - "1700:1700/udp" diff --git a/stage2-rak/.04-pre-install-containers/files/docker-compose-files/udp-packet-forwarder.yml b/stage2-rak/.04-pre-install-containers/files/docker-compose-files/udp-packet-forwarder.yml new file mode 100644 index 0000000..777fecf --- /dev/null +++ b/stage2-rak/.04-pre-install-containers/files/docker-compose-files/udp-packet-forwarder.yml @@ -0,0 +1,13 @@ +version: '2.0' + +services: + + udp-packet-forwarder: + image: rakwireless/udp-packet-forwarder:latest + container_name: udp-packet-forwarder + restart: unless-stopped + privileged: true + network_mode: host + environment: + MODEL: "RAK2287" + INTERFACE: "SPI" diff --git a/stage2-rak/.04-pre-install-containers/files/rc.local b/stage2-rak/.04-pre-install-containers/files/rc.local new file mode 100755 index 0000000..e470aa9 --- /dev/null +++ b/stage2-rak/.04-pre-install-containers/files/rc.local @@ -0,0 +1,34 @@ +#!/bin/sh -e +# +# rc.local +# +# This script is executed at the end of each multiuser runlevel. +# Make sure that the script will "exit 0" on success or any other +# value on error. +# +# In order to enable or disable this script just change the execution +# bits. +# +# By default this script does nothing. + +# Print the IP address +_IP=$(hostname -I) || true +if [ "$_IP" ]; then + printf "My IP address is %s\n" "$_IP" +fi + +# Check for RAKUID +if grep -Fq "RAKUID=" /etc/environment; then + true +else + echo "RAKUID=$(get_RAKUID 2>&1)" >> /etc/environment +fi + + +# Run once after reboot +/usr/local/bin/runonce + +# Dispaly basic info on oled screen +/bin/oled & + +exit 0 diff --git a/stage2-rak/.04-pre-install-containers/files/run-once-services/docker-compose.sh.sample b/stage2-rak/.04-pre-install-containers/files/run-once-services/docker-compose.sh.sample new file mode 100755 index 0000000..bd46c91 --- /dev/null +++ b/stage2-rak/.04-pre-install-containers/files/run-once-services/docker-compose.sh.sample @@ -0,0 +1,22 @@ +#!/bin/bash -e + +# This is an example script, it load the UDP packet forwarder image and also do docker-compose up during first-boot + +# Load compressed image for the UDP packet forwarder +docker load < /usr/local/etc/images/image-tar-udp.tar +# docker-compose up +docker compose -f /usr/local/etc/docker-compose-files/udp-packet-forwarder.yml up -d + +# Load compressed images for the TTS stack +# docker load < /usr/local/etc/images/image-tar-tts-postgres.tar +# docker load < /usr/local/etc/images/image-tar-tts-redis.tar +# docker load < /usr/local/etc/images/image-tar-tts-stack.tar +# docker-compose up +# docker compose -f /usr/local/etc/docker-compose-files/the-things-stack.yml.yml up -d + +# You can add more container services here(e.g. basicstation packet forwarder), but make sure you load the necessary images first, +# and then do the docker compose up +# docker load < /usr/local/etc/images/image-tar-basicstation.tar +# docker compose -f /usr/local/etc/docker-compose-files/basics-station-packet-forwarder.yml up -d + + diff --git a/stage2-rak/.04-pre-install-containers/files/run-once-services/portainer.sh.sample b/stage2-rak/.04-pre-install-containers/files/run-once-services/portainer.sh.sample new file mode 100755 index 0000000..766aab1 --- /dev/null +++ b/stage2-rak/.04-pre-install-containers/files/run-once-services/portainer.sh.sample @@ -0,0 +1,16 @@ +#!/bin/bash -e + +# This script is an example for simple service using only one image + +# Load ssaved images +docker load < /usr/local/etc/images/image-tar-portainer.tar + +# Set variables +portainer_version=2.16.0 +portainer_template_url=https://raw.githubusercontent.com/RAKWireless/portainer-templates/master/portainer_app_template.json +# Bcrypt-encrypted password, plain text is "changeme" +portainer_password=\$2y\$05\$CamDlWaqdhRQvy1AuOZlY.4QYi/GptU6lDruXD0YiYiwQljGJmamC +portainer_name=portainer + +# Use docker run to start the service +docker run -d -p 8000:8000 -p 9443:9443 --name=$portainer_name --restart=unless-stopped -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:$portainer_version --admin-password=$portainer_password --templates $portainer_template_url diff --git a/stage2-rak/.04-pre-install-containers/files/runonce b/stage2-rak/.04-pre-install-containers/files/runonce new file mode 100755 index 0000000..aca6a37 --- /dev/null +++ b/stage2-rak/.04-pre-install-containers/files/runonce @@ -0,0 +1,12 @@ +#!/bin/sh +for file in /etc/local/runonce.d/* +do + if [ ! -f "$file" ] + then + continue + fi + "$file" + new_file=$(echo ${file} | sed "s#runonce.d/#runonce.d/ran/#g") + mv "$file" "$new_file.$(date +%Y%m%dT%H%M%S)" + logger -t runonce -p local3.info "$file" +done diff --git a/stage2-rak/README.md b/stage2-rak/README.md index ecdef99..0aa0891 100644 --- a/stage2-rak/README.md +++ b/stage2-rak/README.md @@ -1,42 +1,37 @@ # Notes about RAKPiOS specific features -Changes to the default Raspberry Pi OS image are defined on the `stage2-rak` stage. This is used to build a customized image for RAK WisGate Developer products. The stage will make some changes to the systems, including pre-installing docker and other tools, adding new kernel modules, updating system information, and adding some new features tailored for RAK WisGate Developer products. - +Changes to the default Raspberry Pi OS image are defined on the `stage2-rak` stage. This is used to build a customized image for RAK WisGate Developer products. The stage will make some changes to the systems, including pre-installing docker and other tools, adding new kernel modules, updating system information, and adding some new features tailored for RAK WisGate Developer products. ## Getting started with building using stage2-rak -As mentioned in the original `README.md`, users can define a variable called `STAGE_LIST` in the configuration file to change the order of building stages. In the `default_config` file, variable `STAGE_LIST` is set to `stage0 stage1 stage2 stage2-rak`, then instead of working through the numeric stages in order, this list will be followed. - -RAKPiOS is a *lite* system, thus the build will skip stage 3, stage 4, and stage 5 which are only for a desktop system with a graphical user interface. - +As mentioned in the original `README.md`, users can define a variable called `STAGE_LIST` in the configuration file to change the order of building stages. In the `default_config` file, variable `STAGE_LIST` is set to `stage0 stage1 stage2 stage2-rak`, then instead of working through the numeric stages in order, this list will be followed. +RAKPiOS is a *lite* system, thus the build will skip stage 3, stage 4, and stage 5 which are only for a desktop system with a graphical user interface. ## A simple example for building RakpiOS image -We defined some original configuration tags and also introduced some custom configuration tags in the `config_rak` file. +We defined some original configuration tags and also introduced some custom configuration tags in the `config_rak` file. ``` # Original configuration tags ARCH=arm64 -VERSION=0.3.4 +VERSION=0.6.0 IMG_NAME="rakpios-${VERSION}-${ARCH}" TARGET_HOSTNAME="rakpios" -FIRST_USER_NAME="rak" -FIRST_USER_PASS="rakpios" +FIRST_USER_NAME=rak +FIRST_USER_PASS=changeme +DISABLE_FIRST_BOOT_USER_RENAME=1 ENABLE_SSH=1 STAGE_LIST="stage0 stage1 stage2 stage2-rak" IMG_DATE=$( date +%Y%m%d ) - # Custom configuration tags PI_GEN_REPO=https://github.com/RAKWireless/rakpios KERNEL_BUILD=0 KERNEL_CACHED=1 -KERNEL_TAG=ac66b3f +KERNEL_TAG=rpi-5.15.y ``` For more details about the original configurations, please check the original `READMD.md` . For custom configuration tags, now you can define whether you want to build the kernel (`KERNEL_BUILD` and `KERNEL_TAG` variables), use the cached image (`KERNEL_CACHED` variable) or just leave it to the official kernel. - If `KERNEL_BUILD` is set to 1 but no `KERNEL_TAG` is defined then it defaults to the HEAD of the `rpi-5.15.y` branch. But please mind that some specific kernel patches (like GPIO Expander support) will not be applied since they are version-dependent. - The final step is to launch the build.sh script: ```bash @@ -51,17 +46,26 @@ or, you can use docker to perform the build: Please check the original README.md to see how to skip stages and also how to continue the build after a failure. - ## How the stage2-rak stage works There are a number of different directories in the `stage2-rak` directory: - - **00-configure-apt** -Update `sources.list.d` and pre-install docker's dependencies. - - - **01-pre-installt.sh** - Pre-install docker, docker-compose, python libraries, and add user **rak** user to docker group. - - - **02-kernel** - A kernel builder allows you to cross-build a kernel that is tailored exactly to your requirements. - - - **03-sys-update** - This stage focused on moving script to the new image, adding new features like rakuid, OLED script, portainer, MOTD, and so on. This stage also update the new image's os-releases file. - - +- **00-configure-apt** -Update `sources.list.d` and pre-install docker's dependencies. + +- **01-pre-installt.sh** - Pre-install docker, docker-compose, python libraries, and add user **rak** user to docker group. + +- **02-kernel** - A kernel builder allows you to cross-build a kernel that is tailored exactly to your requirements. + +- **03-sys-update** - This stage focused on moving script to the new image, adding new features like rakuid, OLED script, portainer, MOTD, and so on. This stage also update the new image's os-releases file. + +- **.04-pre-install-containers** - This stage is hidden by default, you need to use a command like `ls -a` to see it. The reason is that this is an optional stage. The purpose of this stage is to have docker containers installed by default on boot, and also run a set of commands on the first boot of the device to install docker-related services or tools. + + By removing the dot at the beginning of the directory, this stage will be activated, and it will download the compressed images for [UDP packet forwarder](https://github.com/RAKWireless/udp-packet-forwarder), [Basicstation packet forwarder](https://github.com/RAKWireless/basicstation), [Portainer](https://hub.docker.com/r/portainer/portainer-ce), and also [The Things Stack](https://github.com/xoseperez/the-things-stack-docker), and save them to `/usr/local/etc/images` (see `stage2-rak/.04-pre-install-containers/00-run.sh`) . + + We also created a simple script called `runonce` (check `stage2-rak/.04-pre-install-containers/files/runonce`) to start some customized services defined in the directory `stage2-rak/.04-pre-install-containers/files/run-once-services` during the first boot. + + We also provided some example scripts. For example, load the compressed image for Portainer and start the portainer service during the first boot ( check `stage2-rak/.04-pre-install-containers/files/run-once-services/portainer.sh.sample`); load required images defined in the `.YML` file saved in the `stage2-rak/.04-pre-install-containers/files/docker-compose-file`, and then start the services (check `stage2-rak/.04-pre-install-containers/files/run-once-services/docker-compose.sh.sample`). + + Once the script stored in `stage2-rak/.04-pre-install-containers/files/run-once-services` is completed, you'll find it is moved to the `/etc/local/runonce.d/ran` directory on the host, meanwhile the date and time appended to its name. There will also be an entry in your `syslog`. + + To add your customized compressed docker images to the customized RAKPiOS image, please use the `docker save` command first to create the compressed images first. For example, `docker save -o container-tar-portainer.tar portainer/portainer-ce:2.16.0` , this command will save the image `portainer/portainer-ce:2.16.0` as a compressed file called `container-tar-portainer.tar`, and then you need to move the compressed images to the RAKPiOS build directory(or you can choose to download the images we provided directly, please check `stage2-rak/.04-pre-install-containers/00-run.sh` for example). Last but not least, don't forget to `docker load` the compressed images(check `stage2-rak/.04-pre-install-containers/files/run-once-services/portainer.sh.sample` for example). From ad17c6cc23fa799181ed9729d9dc5f4f0fa6b5b6 Mon Sep 17 00:00:00 2001 From: Sheng_L <87348555+Sheng2216@users.noreply.github.com> Date: Mon, 14 Nov 2022 11:10:11 +0800 Subject: [PATCH 2/2] Update README.md update username/password information --- stage2-rak/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stage2-rak/README.md b/stage2-rak/README.md index 0aa0891..a7a7856 100644 --- a/stage2-rak/README.md +++ b/stage2-rak/README.md @@ -1,6 +1,6 @@ # Notes about RAKPiOS specific features -Changes to the default Raspberry Pi OS image are defined on the `stage2-rak` stage. This is used to build a customized image for RAK WisGate Developer products. The stage will make some changes to the systems, including pre-installing docker and other tools, adding new kernel modules, updating system information, and adding some new features tailored for RAK WisGate Developer products. +Changes to the default Raspberry Pi OS image are defined on the `stage2-rak` stage. This is used to build a customized image for RAK WisGate Developer products. The stage will make some changes to the systems, including pre-installing docker and other tools, adding new kernel modules, updating system information, and adding some new features tailored for RAK WisGate Developer products. The new username and password for the modified image are:`rak/changeme` ## Getting started with building using stage2-rak