install.sh raw

   1  #!/bin/sh
   2  
   3  ALBYHUB_URL="https://getalby.com/install/hub/server-linux-x86_64.tar.bz2"
   4  VERIFIER_URL="https://getalby.com/install/hub/verify.sh"
   5  
   6  # Default values
   7  INSTALL_DIR=""
   8  SYSTEMD=""
   9  NON_INTERACTIVE=false
  10  SKIP_VERIFY=false
  11  
  12  # Parse command-line arguments
  13  while [ $# -gt 0 ]; do
  14    case "$1" in
  15      -d|--install-dir)
  16        if [ -z "$2" ] || [ "${2#-}" != "$2" ]; then
  17          echo "Error: --install-dir requires a non-empty directory path"
  18          exit 1
  19        fi
  20        if printf '%s' "$2" | grep -q '[[:space:]]'; then
  21          echo "Error: --install-dir must not contain whitespace"
  22          exit 1
  23        fi
  24        INSTALL_DIR="$2"
  25        shift 2
  26        ;;
  27      -s|--systemd)
  28        SYSTEMD="yes"
  29        shift
  30        ;;
  31      --no-systemd)
  32        SYSTEMD="no"
  33        shift
  34        ;;
  35      -y|--yes)
  36        NON_INTERACTIVE=true
  37        shift
  38        ;;
  39      --skip-verify)
  40        SKIP_VERIFY=true
  41        shift
  42        ;;
  43      -h|--help)
  44        echo "Usage: $0 [OPTIONS]"
  45        echo ""
  46        echo "Options:"
  47        echo "  -d, --install-dir DIR    Set installation directory (default: \$HOME/albyhub)"
  48        echo "  -s, --systemd            Setup systemd service (auto-yes)"
  49        echo "      --no-systemd         Skip systemd service setup (auto-no)"
  50        echo "  -y, --yes                Non-interactive mode (auto-confirm all prompts)"
  51        echo "      --skip-verify        Skip package signature verification"
  52        echo "  -h, --help               Show this help message"
  53        echo ""
  54        echo "Examples:"
  55        echo "  $0                                    # Interactive mode"
  56        echo "  $0 -y -d /opt/albyhub -s              # Non-interactive with systemd"
  57        echo "  $0 --yes --install-dir /app/albyhub   # Non-interactive, no systemd"
  58        exit 0
  59        ;;
  60      *)
  61        echo "Unknown option: $1"
  62        echo "Use -h or --help for usage information"
  63        exit 1
  64        ;;
  65    esac
  66  done
  67  
  68  echo ""
  69  echo ""
  70  echo "⚡️ Welcome to Alby Hub"
  71  echo "-----------------------------------------"
  72  echo "Installing Alby Hub"
  73  echo ""
  74  
  75  # Determine install directory
  76  if [ -z "$INSTALL_DIR" ]; then
  77    if [ "$NON_INTERACTIVE" = true ]; then
  78      INSTALL_DIR="$HOME/albyhub"
  79    else
  80      printf "Absolute install directory path (default: %s/albyhub): " "$HOME"
  81      read USER_INSTALL_DIR
  82      INSTALL_DIR="${USER_INSTALL_DIR:-$HOME/albyhub}"
  83    fi
  84  fi
  85  
  86  echo "Installing to: $INSTALL_DIR"
  87  
  88  # create installation directory
  89  mkdir -p "$INSTALL_DIR"
  90  cd "$INSTALL_DIR" || exit 1
  91  
  92  # check bzip2 is available before downloading
  93  if ! command -v bzip2 > /dev/null 2>&1; then
  94    echo "❌ bzip2 is required but not installed." >&2
  95    exit 1
  96  fi
  97  
  98  # download and extract the Alby Hub executable
  99  echo "Downloading Alby Hub..."
 100  if ! wget -q "$ALBYHUB_URL"; then
 101    echo "❌ Failed to download Alby Hub" >&2
 102    exit 1
 103  fi
 104  
 105  if [ "$SKIP_VERIFY" = false ]; then
 106    if [ ! -f "verify.sh" ]; then
 107      echo "Downloading the verification script..."
 108      if ! wget -q "$VERIFIER_URL"; then
 109        echo "❌ Failed to download the verification script." >&2
 110        exit 1
 111      fi
 112      chmod +x verify.sh
 113    fi
 114  
 115    if ! ./verify.sh server-linux-x86_64.tar.bz2 albyhub-Server-Linux-x86_64.tar.bz2; then
 116      echo "❌ Verification failed, aborting installation"
 117      exit 1
 118    fi
 119  fi
 120  
 121  if ! tar xf server-linux-x86_64.tar.bz2; then
 122    echo "Failed to unpack Alby Hub. Potentially bzip2 is missing"
 123    echo "Install it with sudo apt-get install bzip2"
 124    exit 1
 125  fi
 126  
 127  rm server-linux-x86_64.tar.bz2
 128  
 129  # prepare the data directory. this is pesistent and will hold all important data
 130  mkdir -p "$INSTALL_DIR/data"
 131  
 132  # create a simple start script that sets the default configuration variables
 133  tee "$INSTALL_DIR/start.sh" > /dev/null << EOF
 134  #!/bin/sh
 135  
 136  echo "Starting Alby Hub"
 137  WORK_DIR="$INSTALL_DIR/data" LDK_GOSSIP_SOURCE="" $INSTALL_DIR/bin/albyhub
 138  EOF
 139  chmod +x "$INSTALL_DIR/start.sh"
 140  
 141  # add an update script to keep the Hub up to date
 142  # run this to update the hub
 143  wget -q https://raw.githubusercontent.com/getAlby/hub/master/scripts/linux-x86_64/update.sh
 144  chmod +x "$INSTALL_DIR/update.sh"
 145  
 146  echo ""
 147  echo ""
 148  echo "✅ Installation done."
 149  echo ""
 150  
 151  # optionally create a systemd service to start alby hub
 152  SETUP_SYSTEMD=""
 153  if [ -n "$SYSTEMD" ]; then
 154    if [ "$SYSTEMD" = "yes" ]; then
 155      SETUP_SYSTEMD="y"
 156    else
 157      SETUP_SYSTEMD="n"
 158    fi
 159  elif [ "$NON_INTERACTIVE" = true ]; then
 160    # Default to no systemd in non-interactive mode unless explicitly requested
 161    SETUP_SYSTEMD="n"
 162  else
 163    printf "Do you want to setup a systemd service (requires sudo permission)? (y/n): "
 164    read REPLY
 165    case "$REPLY" in
 166      [Yy]*) SETUP_SYSTEMD="y" ;;
 167      *) SETUP_SYSTEMD="n" ;;
 168    esac
 169  fi
 170  
 171  if [ "$SETUP_SYSTEMD" = "n" ]; then
 172    echo ""
 173    echo ""
 174    echo "Run $INSTALL_DIR/start.sh to start Alby Hub"
 175    echo "✅ DONE"
 176    exit 0
 177  fi
 178  
 179  sudo tee /etc/systemd/system/albyhub.service > /dev/null << EOF
 180  [Unit]
 181  Description=Alby Hub
 182  After=network-online.target
 183  Wants=network-online.target
 184  
 185  [Service]
 186  Type=simple
 187  Restart=always
 188  RestartSec=1
 189  User=$USER
 190  ExecStart=$INSTALL_DIR/start.sh
 191  Environment="PORT=8029"
 192  
 193  [Install]
 194  WantedBy=multi-user.target
 195  EOF
 196  
 197  echo ""
 198  echo ""
 199  
 200  sudo systemctl enable albyhub
 201  sudo systemctl start albyhub
 202  
 203  echo "Run 'sudo systemctl start/stop albyhub' to start/stop AlbyHub"
 204  echo ""
 205  echo ""
 206  echo " ✅ DONE. Open Alby Hub to get started"
 207  echo "Alby Hub runs by default on localhost:8029"
 208