Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature optional containers installed by default #2

Open
wants to merge 2 commits into
base: arm64
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions stage2-rak/.04-pre-install-containers/00-run.sh
Original file line number Diff line number Diff line change
@@ -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/"
26 changes: 26 additions & 0 deletions stage2-rak/.04-pre-install-containers/01-run.sh
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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"
34 changes: 34 additions & 0 deletions stage2-rak/.04-pre-install-containers/files/rc.local
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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


Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions stage2-rak/.04-pre-install-containers/files/runonce
Original file line number Diff line number Diff line change
@@ -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
52 changes: 28 additions & 24 deletions stage2-rak/README.md
Original file line number Diff line number Diff line change
@@ -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. The new username and password for the modified image are:`rak/changeme`

## 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
Expand All @@ -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).