-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
109 lines (82 loc) · 3.13 KB
/
Copy pathsetup.sh
File metadata and controls
109 lines (82 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# Exit on error
set -e
echo "🚀 Starting Beatnik Hardware API Production Setup..."
REPO="byrdsandbytes/beatnik-hardware-api"
ARTIFACT="beatnik-hardware-api.tar.gz"
INSTALL_DIR="/opt/beatnik-hardware-api"
SERVICE_FILE="beatnik-hardware.service"
if systemctl is-active --quiet "$SERVICE_FILE"; then
echo "🛑 Stopping existing service for update..."
sudo systemctl stop "$SERVICE_FILE"
fi
echo "📂 Creating installation directory at $INSTALL_DIR..."
sudo mkdir -p $INSTALL_DIR
sudo chown -R $USER:$USER $INSTALL_DIR
cd $INSTALL_DIR
echo "🔍 Finding latest release on GitHub..."
DOWNLOAD_URL=$(curl -s https://api.github.com/repos/$REPO/releases/latest | grep "browser_download_url" | grep "$ARTIFACT" | cut -d '"' -f 4)
if [ -z "$DOWNLOAD_URL" ]; then
echo "❌ Could not find the latest release. Check your internet connection or repository name."
exit 1
fi
echo "⬇️ Downloading release..."
wget -q -O $ARTIFACT "$DOWNLOAD_URL"
echo "📦 Extracting release..."
tar -xzvf $ARTIFACT
rm $ARTIFACT
# 1.5 Setup Node.js via NVM
echo "🟢 Setting up Node.js (NVM)..."
# Define NVM_DIR (robust method from README/nvm docs)
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
# Install NVM if not found or incomplete
if [ ! -s "$NVM_DIR/nvm.sh" ]; then
echo " Installing NVM..."
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
fi
# Load NVM
if [ -s "$NVM_DIR/nvm.sh" ]; then
# Use '.' instead of '\.' for script compatibility, and add --no-use
. "$NVM_DIR/nvm.sh" --no-use
else
echo "❌ Failed to locate nvm.sh"
exit 1
fi
# Install and use Node.js 22
echo " Installing/Using Node.js 22..."
nvm install 22
nvm use 22
# 2. Install Dependencies
echo "🟢 Installing Production Dependencies..."
npm install --omit=dev
# 4. Configure Systemd Service
echo "🟢 Configuring Systemd Service..."
# Get absolute path to node executable
NODE_EXEC=$(nvm which 22)
echo " Node executable found at: $NODE_EXEC"
# Create a temporary service file
TEMP_SERVICE_FILE="${SERVICE_FILE}.tmp"
cp "$SERVICE_FILE" "$TEMP_SERVICE_FILE"
# Update WorkingDirectory
# Escape slashes for sed
ESCAPED_INSTALL_DIR=$(echo "$INSTALL_DIR" | sed 's/\//\\\//g')
sed -i "s/^WorkingDirectory=.*/WorkingDirectory=$ESCAPED_INSTALL_DIR/" "$TEMP_SERVICE_FILE"
# Update ExecStart
# We want: ExecStart=/path/to/node dist/server.js
# Escape slashes for sed
ESCAPED_NODE_EXEC=$(echo "$NODE_EXEC" | sed 's/\//\\\//g')
sed -i "s/^ExecStart=.*/ExecStart=$ESCAPED_NODE_EXEC dist\/server.js/" "$TEMP_SERVICE_FILE"
echo " Service file patched with local paths."
# 5. Install Service (requires sudo)
echo "🟢 Installing Service to /etc/systemd/system/ (requires sudo)..."
sudo cp "$TEMP_SERVICE_FILE" "/etc/systemd/system/$SERVICE_FILE"
rm "$TEMP_SERVICE_FILE"
echo " Reloading systemd daemon..."
sudo systemctl daemon-reload
echo " Enabling service..."
sudo systemctl enable "$SERVICE_FILE"
echo " Restarting service..."
sudo systemctl restart "$SERVICE_FILE"
echo "✅ Setup Complete!"
echo " Checking service status..."
sudo systemctl status "$SERVICE_FILE" --no-pager