Skip to content

Commit

Permalink
feat: isolated install script
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian authored May 31, 2023
1 parent 7de82f9 commit 4b060a1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 68 deletions.
30 changes: 8 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,22 @@ To manually build the latest version for your machine:
- `yarn dist`
- Locate the output under `electron-output/`

### Manual Headless / Raspbrry pi
### Manual Headless / Raspberry pi

It can be built and run as a systemd service on a pi or other linux machine

To prepare the application, after cloning:

- `yarn install`
- `yarn build`

An example systemd unit (make sure to update the paths and companion ip as appropriate):
No images are provided for this, but the process has been written to be a single script.

As root, run the following:
```
[Unit]
Description=Bitfocus Companion Satellite
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/companion-satellite
ExecStart=node /home/pi/companion-satellite/dist/main.js 192.168.0.1
Restart=on-failure
KillSignal=SIGINT
TimeoutStopSec=60
[Install]
WantedBy=multi-user.target
curl https://raw.githubusercontent.com/bitfocus/companion-satellite/master/pi-image/install.sh | sh
```

After this, you can use `sudo satellite-update` to change the version it has installed. Note: this is currently not fully implemented.

Note: This script will create a new user called `satellite`, which Satellite will be run as and will own the configuration.

## Development

NodeJS 16 is required
Expand Down
48 changes: 48 additions & 0 deletions pi-image/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
set -e

echo "This will attempt to install Companion Satellite as a system service on this device."
echo "It is designed to be run on headless servers, but can be used on desktop machines if you are happy to not have the tray icon."
echo "A user called 'satellite' will be created to run the service, and various scripts will be installed to manage the service"

if [ $(/usr/bin/id -u) -ne 0 ]; then
echo "Must be run as root"
exit 1
fi

SATELLITE_BRANCH="${SATELLITE_BRANCH:-master}"

# 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

# 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`"

# configure git for future updates
git config --global pull.rebase false

# run the update script
./pi-image/update.sh $SATELLITE_BRANCH

# enable start on boot
systemctl enable satellite

# copy config file into place
cp ./pi-image/satellite-config /boot/satellite-config

# add the fnm node to this users path
# TODO - verify permissions
echo "export PATH=/opt/fnm/aliases/default/bin:\$PATH" >> /home/satellite/.bashrc

echo "Companion Satellite is installed!"
echo "You can start it with \"sudo systemctl start satellite\" or \"sudo satellite-update\""
53 changes: 9 additions & 44 deletions pi-image/satellitepi.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ source "arm-image" "satellitepi" {
build {
sources = ["source.arm-image.satellitepi"]

provisioner "file" {
source = "install.sh"
destination = "/tmp/install.sh"
}

provisioner "shell" {
#system setup
inline = [
Expand All @@ -32,15 +37,6 @@ build {
"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 upgrade -y",
"apt-get install -y git unzip curl libusb-1.0-0-dev libudev-dev cmake",
"apt-get clean"
]
}

Expand All @@ -49,42 +45,11 @@ build {
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"

# run the script
"export SATELLITE_BRANCH=${var.branch}",
"chmod +x /tmp/install.sh",
"/tmp/install.sh"
]
}

Expand Down
6 changes: 4 additions & 2 deletions pi-image/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ 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
ln -s -f /usr/local/src/companion-satellite/pi-image/satellite-update /usr/local/sbin/satellite-update
# 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
ln -s -f /usr/local/src/companion-satellite/pi-image/motd /etc/motd

echo "Update is complete"

0 comments on commit 4b060a1

Please sign in to comment.