Skip to content

Instantly share code, notes, and snippets.

@bioshazard
Created July 27, 2021 16:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bioshazard/6c7ef1538e6f5deb2b2ede6301109e5c to your computer and use it in GitHub Desktop.
Save bioshazard/6c7ef1538e6f5deb2b2ede6301109e5c to your computer and use it in GitHub Desktop.
Automation for installing GunDB as a service (tested on Pi)
#!/bin/bash
## Usage: GUN_USER=gun GUN_ROOT=~gun/gun-pi-custom GUN_BRACH=master gun-service.sh
# Should be run as root (with sudo or directly)
[[ "$(whoami)" == "root" ]] || { echo "Must be run as root"; exit 1; }
# Setup default environment
[ -z "${GUN_USER}" ] && GUN_USER="pi"
GUN_DETECTED_USER_HOME=$(getent passwd ${GUN_USER} | cut -d: -f6)
[ -z "${GUN_ROOT}" ] && GUN_ROOT="${GUN_DETECTED_USER_HOME}/gun-service"
[ -z "${GUN_BRANCH}" ] && GUN_BRANCH="#manhattan"
# Exit on failure
set -e
# Create gun root folder
test -d "${GUN_ROOT}" || sudo -H -u ${GUN_USER} mkdir -vp "${GUN_ROOT}"
# Install NPM (only support `apt` for now, easily converted to `yum`, etc)
which npm || { apt update && apt install -y npm; }
# Install Gun
(cd "${GUN_ROOT}" && sudo -H -u ${GUN_USER} npm install "https://github.com/amark/gun.git${GUN_BRANCH}" --save)
# Install Service
cat << EOF > /etc/systemd/system/gun.service
[Unit]
Description=GunDB Relay Peer
[Service]
User=${GUN_USER}
Group=${GUN_USER}
WorkingDirectory=${GUN_ROOT}/node_modules/gun
ExecStart=/usr/bin/npm start
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable gun
systemctl start gun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment