From 893fffaf907b2b1eeb130a4963a7d9604004eafc Mon Sep 17 00:00:00 2001 From: Julian Waller Date: Wed, 2 Feb 2022 22:14:50 +0000 Subject: [PATCH] feat: build pi image --- .github/workflows/satellitepi.yml | 44 +++++++++++++++ pi-image/.gitignore | 1 + pi-image/50-satellite.rules | 15 ++++++ pi-image/motd | 12 +++++ pi-image/satellite-config | 5 ++ pi-image/satellite-edit-config | 16 ++++++ pi-image/satellite-help | 19 +++++++ pi-image/satellite-license | 3 ++ pi-image/satellite-update | 22 ++++++++ pi-image/satellite.service | 17 ++++++ pi-image/satellitepi.pkr.hcl | 90 +++++++++++++++++++++++++++++++ pi-image/update.sh | 40 ++++++++++++++ 12 files changed, 284 insertions(+) create mode 100644 .github/workflows/satellitepi.yml create mode 100644 pi-image/.gitignore create mode 100644 pi-image/50-satellite.rules create mode 100644 pi-image/motd create mode 100644 pi-image/satellite-config create mode 100755 pi-image/satellite-edit-config create mode 100755 pi-image/satellite-help create mode 100755 pi-image/satellite-license create mode 100755 pi-image/satellite-update create mode 100644 pi-image/satellite.service create mode 100644 pi-image/satellitepi.pkr.hcl create mode 100755 pi-image/update.sh diff --git a/.github/workflows/satellitepi.yml b/.github/workflows/satellitepi.yml new file mode 100644 index 0000000..c576014 --- /dev/null +++ b/.github/workflows/satellitepi.yml @@ -0,0 +1,44 @@ +name: SatellitePi Build + +on: + # TODO not on every push + push: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +jobs: + build: + runs-on: ubuntu-latest + name: build image + + steps: + - name: Checkout Repository + uses: actions/checkout@v2 + + - name: install packer + run: | + curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - + sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" + sudo apt-get update && sudo apt-get install -y packer + + - name: build image + run: | + cd pi-image + sudo packer init satellitepi.pkr.hcl + sudo packer build --var branch=${GITHUB_REF_NAME} satellitepi.pkr.hcl + + - name: rename image + run: | + cd pi-image/output-satellitepi + sudo mv image satellitepi-test.img + + # upload artifact + - name: Upload production artifacts + uses: actions/upload-artifact@v2 + with: + name: satellitepi + if-no-files-found: error + path: | + pi-image/output-satellitepi/*.img diff --git a/pi-image/.gitignore b/pi-image/.gitignore new file mode 100644 index 0000000..307f30e --- /dev/null +++ b/pi-image/.gitignore @@ -0,0 +1 @@ +output-* \ No newline at end of file diff --git a/pi-image/50-satellite.rules b/pi-image/50-satellite.rules new file mode 100644 index 0000000..2193a93 --- /dev/null +++ b/pi-image/50-satellite.rules @@ -0,0 +1,15 @@ +SUBSYSTEM=="input", GROUP="input", MODE="0666" +SUBSYSTEM=="usb", ATTRS{idVendor}=="0fd9", ATTRS{idProduct}=="0060", MODE:="666", GROUP="plugdev" +KERNEL=="hidraw*", ATTRS{idVendor}=="0fd9", ATTRS{idProduct}=="0060", MODE:="666", GROUP="plugdev" +SUBSYSTEM=="usb", ATTRS{idVendor}=="ffff", ATTRS{idProduct}=="1f40", MODE:="666", GROUP="plugdev" +KERNEL=="hidraw*", ATTRS{idVendor}=="ffff", ATTRS{idProduct}=="1f40", MODE:="666", GROUP="plugdev" +SUBSYSTEM=="usb", ATTRS{idVendor}=="0fd9", ATTRS{idProduct}=="0063", MODE:="666", GROUP="plugdev" +KERNEL=="hidraw*", ATTRS{idVendor}=="0fd9", ATTRS{idProduct}=="0063", MODE:="666", GROUP="plugdev" +SUBSYSTEM=="usb", ATTRS{idVendor}=="0fd9", ATTRS{idProduct}=="006c", MODE:="666", GROUP="plugdev" +KERNEL=="hidraw*", ATTRS{idVendor}=="0fd9", ATTRS{idProduct}=="006c", MODE:="666", GROUP="plugdev" +SUBSYSTEM=="usb", ATTRS{idVendor}=="0fd9", ATTRS{idProduct}=="006d", MODE:="666", GROUP="plugdev" +KERNEL=="hidraw*", ATTRS{idVendor}=="0fd9", ATTRS{idProduct}=="006d", MODE:="666", GROUP="plugdev" +SUBSYSTEM=="usb", ATTRS{idVendor}=="0fd9", ATTRS{idProduct}=="0080", MODE:="666", GROUP="plugdev" +KERNEL=="hidraw*", ATTRS{idVendor}=="0fd9", ATTRS{idProduct}=="0080", MODE:="666", GROUP="plugdev" +SUBSYSTEM=="usb", ATTRS{idVendor}=="ffff", ATTRS{idProduct}=="1f41", MODE:="666", GROUP="plugdev" +KERNEL=="hidraw*", ATTRS{idVendor}=="ffff", ATTRS{idProduct}=="1f41", MODE:="666", GROUP="plugdev" \ No newline at end of file diff --git a/pi-image/motd b/pi-image/motd new file mode 100644 index 0000000..cc8007b --- /dev/null +++ b/pi-image/motd @@ -0,0 +1,12 @@ +The source code repository for this project can be found here: +https://github.com/bitfocus/companion-satellite + +Full licensing information for Bitfocus Commpanion can be found by running 'satellite-license' +in the terminal or visiting https://github.com/bitfocus/companion-satellite/blob/master/LICENSE in a web browser + +Any bugs, issues, or feature requests for the Companion Satellite software should be reported on the project's GitHub: +https://github.com/bitfocus/companion-satellite/issues + +Companion Satellite should auto-start on this computer as soon as a viable network connection is detected. + +For more information on Companion Satellite specific commands you can run try running `satellite-help` \ No newline at end of file diff --git a/pi-image/satellite-config b/pi-image/satellite-config new file mode 100644 index 0000000..59c5dfa --- /dev/null +++ b/pi-image/satellite-config @@ -0,0 +1,5 @@ +# Set this to the ip address or hostname of your companion installation +# examples: +# - COMPANION_IP=192.168.100.1 +# - COMPANION_IP=companion.example.org +COMPANION_IP=127.0.0.1 \ No newline at end of file diff --git a/pi-image/satellite-edit-config b/pi-image/satellite-edit-config new file mode 100755 index 0000000..fd6a87d --- /dev/null +++ b/pi-image/satellite-edit-config @@ -0,0 +1,16 @@ +#!/bin/bash -e + +if [[ $(/usr/bin/id -u) -ne 0 ]]; then + echo "Must be run as root. Try sudo satellite-edit-config" + exit 1 +fi + +# stop satellite +systemctl stop satellite + +# open config editor +nano /boot/satellite-config + +# restart satellite +# reboot +systemctl start satellite \ No newline at end of file diff --git a/pi-image/satellite-help b/pi-image/satellite-help new file mode 100755 index 0000000..29e2698 --- /dev/null +++ b/pi-image/satellite-help @@ -0,0 +1,19 @@ +#!/bin/bash + +echo "To view the Companion Satellite License information run:" +echo " $ satellite-license" +echo "" + +echo "To stop/start/restart or check the status of Companion Satellite run:" +echo " $ sudo systemctl [start/stop/restart/status] satellite" +echo "" + +echo "To update Companion Satellite run:" +echo " $ sudo satellite-update" +echo "and follow the prompts" +echo "" + +echo "To edit the companion configuration run:" +echo " $ sudo companion-edit-config" +echo "and follow the prompts" +echo "" diff --git a/pi-image/satellite-license b/pi-image/satellite-license new file mode 100755 index 0000000..5697a1f --- /dev/null +++ b/pi-image/satellite-license @@ -0,0 +1,3 @@ +#!/bin/bash + +less /usr/local/src/companion-satellite/LICENSE \ No newline at end of file diff --git a/pi-image/satellite-update b/pi-image/satellite-update new file mode 100755 index 0000000..9a3bc37 --- /dev/null +++ b/pi-image/satellite-update @@ -0,0 +1,22 @@ +#!/bin/bash -e + +if [[ $(/usr/bin/id -u) -ne 0 ]]; then + echo "Must be run as root. Try sudo satellite-update" + exit 1 +fi + +# stop satellite +systemctl stop satellite + +# fetch new code +cd /usr/local/src/companion-satellite +git pull + +# TODO - prompt for which branch to use + +# do the update +./pi-image/update.sh + +# restart satellite +# reboot +systemctl start satellite \ No newline at end of file diff --git a/pi-image/satellite.service b/pi-image/satellite.service new file mode 100644 index 0000000..2f0256f --- /dev/null +++ b/pi-image/satellite.service @@ -0,0 +1,17 @@ +[Unit] +Description=Bitfocus Companion Satellite +After=network-online.target +Wants=network-online.target + +[Service] +Type=simple +User=satellite +WorkingDirectory=/usr/local/src/companion-satellite +EnvironmentFile=/boot/satellite-config +ExecStart=/opt/fnm/aliases/default/bin/node /usr/local/src/companion-satellite/dist/main.js $COMPANION_IP +Restart=on-failure +KillSignal=SIGINT +TimeoutStopSec=60 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/pi-image/satellitepi.pkr.hcl b/pi-image/satellitepi.pkr.hcl new file mode 100644 index 0000000..22a0e10 --- /dev/null +++ b/pi-image/satellitepi.pkr.hcl @@ -0,0 +1,90 @@ +packer { + required_plugins { + arm-image = { + version = "0.2.5" + source = "github.com/solo-io/arm-image" + } + } +} + +variable "branch" { + type = string + default = "master" +} + +source "arm-image" "satellitepi" { + iso_checksum = "sha256:d694d2838018cf0d152fe81031dba83182cee79f785c033844b520d222ac12f5" + iso_url = "https://downloads.raspberrypi.org/raspios_lite_arm64/images/raspios_lite_arm64-2022-01-28/2022-01-28-raspios-bullseye-arm64-lite.zip" + last_partition_extra_size = 4294967296 + qemu_binary = "qemu-aarch64-static" +} + +build { + sources = ["source.arm-image.satellitepi"] + + provisioner "shell" { + #system setup + inline = [ + # # enable ssh + # "touch /boot/ssh", + + # change the hostname + "CURRENT_HOSTNAME=`cat /etc/hostname | tr -d \" \t\n\r\"`", + "echo satellitepi > /etc/hostname", + "sed -i \"s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\tsatellitepi/g\" /etc/hosts", + + # add a system user + "adduser --disabled-password satellite --gecos \"\"", + + # install some dependencies + "apt-get update", + "apt-get install -y git unzip curl libusb-1.0-0-dev libudev-dev cmake", + "apt-get clean" + ] + } + + provisioner "shell" { + # run as root + execute_command = "chmod +x {{ .Path }}; {{ .Vars }} su root -c {{ .Path }}" + inline_shebang = "/bin/bash -e" + inline = [ + # install fnm to manage node version + # we do this to /opt/fnm, so that the satellite user can use the same installation + "export FNM_DIR=/opt/fnm", + "echo \"export FNM_DIR=/opt/fnm\" >> /root/.bashrc", + "curl -fsSL https://fnm.vercel.app/install | bash -s -- --install-dir /opt/fnm", + "export PATH=/opt/fnm:$PATH", + "eval \"`fnm env --shell bash`\"", + + # clone the repository + "git clone https://github.com/bitfocus/companion-satellite.git -b ${var.branch} /usr/local/src/companion-satellite", + "cd /usr/local/src/companion-satellite", + + # configure git for future updates + "git config --global pull.rebase false", + + # run the update script + "./pi-image/update.sh ${var.branch}", + + # enable start on boot + "systemctl enable satellite", + + # copy config file into place + "cp ./pi-image/satellite-config /boot/satellite-config" + ] + } + + provisioner "shell" { + # run as satellite user + execute_command = "chmod +x {{ .Path }}; {{ .Vars }} su satellite -c {{ .Path }}" + inline_shebang = "/bin/bash -e" + inline = [ + "cd /usr/local/src/companion-satellite", + + # add the fnm node to this users path + "echo \"export PATH=/opt/fnm/aliases/default/bin:\\$PATH\" >> ~/.bashrc" + + ] + } + +} \ No newline at end of file diff --git a/pi-image/update.sh b/pi-image/update.sh new file mode 100755 index 0000000..27e7aa4 --- /dev/null +++ b/pi-image/update.sh @@ -0,0 +1,40 @@ +#!/bin/bash -e + +# this is the bulk of the update script +# It is a separate file, so that the freshly cloned copy is invoked, not the old copy + +# imitiate the fnm setup done in .bashrc +export FNM_DIR=/opt/fnm +export PATH=/opt/fnm:$PATH +eval "`fnm env`" + +cd /usr/local/src/companion-satellite + +# update the node version +fnm use --install-if-missing +fnm default $(fnm current) +npm --unsafe-perm install -g yarn + +# install dependencies +yarn config set network-timeout 100000 -g +yarn + +# build typescript +yarn build + +# update some tooling +cp pi-image/50-satellite.rules /etc/udev/rules.d/ + +# update startup script +cp pi-image/satellite.service /etc/systemd/system +systemctl daemon-reload + +# install some scripts +ln -s -f /usr/local/src/companion-satellite/pi-image/satellite-license /usr/local/bin/satellite-license +ln -s -f /usr/local/src/companion-satellite/pi-image/satellite-help /usr/local/bin/satellite-help +# This isn't something we want to allow for now, so skip these +# ln -s -f /usr/local/src/companion-satellite/pi-image/satellite-update /usr/local/sbin/satellite-update +# ln -s -f /usr/local/src/companion-satellite/pi-image/satellite-edit-config /usr/local/sbin/satellite-edit-config + +# install the motd +ln -s -f /usr/local/src/companion-satellite/pi-image/motd /etc/motd \ No newline at end of file