-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstaller.sh
310 lines (260 loc) · 9.4 KB
/
installer.sh
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/bash
#
# Installer for OpenPod
# More information: https://github.com/blokbot-io/OpenBlok/blob/master/install.sh
# Steps:
# 1. Parse command-line arguments
# 2. Validate environment and dependencies
# 3. Install OpenPod
# 4. Configure systemd service
# 5. Final checks
# ---------------------------------------------------------------------------- #
# Strict Mode & Setup #
# ---------------------------------------------------------------------------- #
set -Eeuo pipefail # Catch unbound variables, errors, and exit the script in case of failure
shopt -s nullglob # If a glob does not match, return empty string
# Ensure the script is run as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (e.g., sudo ./install.sh)."
exit 1
fi
# Ensure the script runs in a Debian/Ubuntu environment
if ! command -v apt-get &>/dev/null; then
echo "This script requires a Debian-based system with apt-get."
exit 1
fi
# Disable interactive prompts
if [ -f /etc/needrestart/needrestart.conf ]; then
sed -i "/#\$nrconf{restart} = 'i';/s/.*/\$nrconf{restart} = 'a';/" /etc/needrestart/needrestart.conf
fi
export DEBIAN_FRONTEND=noninteractive
# ---------------------------------------------------------------------------- #
# Help #
# ---------------------------------------------------------------------------- #
usage() {
echo "Usage: $0 [options]"
echo "Options:"
echo " -h Display this help and exit"
echo " -b <branch> Set custom branch for OpenPod"
echo " -u <url> Set custom URL for OpenPod"
echo " -d Enable debug mode"
echo
echo "Example:"
echo " $0 -b release -u recursion.space"
exit 1
}
# ---------------------------------------------------------------------------- #
# Defaults #
# ---------------------------------------------------------------------------- #
DEBUG=false # -d
BRANCH="" # -b
URL="" # -u
REPO='https://github.com/RecursionSpace/OpenPod'
PYTHON_VERSION='3.11'
VENV_DIR='/opt/OpenPod/venv'
PYTHON_PATH="$VENV_DIR/bin/python"
# ---------------------------------------------------------------------------- #
# Options #
# ---------------------------------------------------------------------------- #
while getopts ":hb:u:d" opt; do
case "${opt}" in
h) # display Help
usage
exit;;
b) # Custom branch
BRANCH="${OPTARG}";;
u) # Custom URL endpoint
URL="${OPTARG}";;
d) # Enable debug mode
DEBUG=true ;;
\?) # Invalid option
echo "Invalid option: -${OPTARG}" >&2;
usage ;;
:) # Missing argument
echo "Option -${OPTARG} requires an argument." >&2;
usage
exit 1 ;;
esac
done
# If no branch set, default based on debug or release
if [ -z "$BRANCH" ]; then
if [ "$DEBUG" = true ]; then
BRANCH='dev-release'
else
BRANCH='release'
fi
fi
# If no URL set, default based on debug or release
if [ -z "$URL" ]; then
if [ "$DEBUG" = true ]; then
URL='dev.recursion.space'
else
URL='recursion.space'
fi
fi
# API_URL logic
if [ "$DEBUG" = true ]; then
API_URL='dev.api.recursion.space'
else
API_URL='api.recursion.space'
fi
echo "==> Installing OpenPod"
echo "Debug | $DEBUG"
echo "Repo | $REPO"
echo "Branch | $BRANCH"
echo "URL | $URL"
echo "API_URL | $API_URL"
echo "-----------------------------------------------"
# ---------------------------------------------------------------------------- #
# Helper Functions #
# ---------------------------------------------------------------------------- #
install_if_needed() {
local pkg="$1"
local status
status=$(dpkg-query -W --showformat='${Status}\n' "$pkg" 2>/dev/null | grep "install ok installed" || true)
if [ -z "$status" ]; then
echo "Installing $pkg..."
apt-get install -y "$pkg"
else
echo "$pkg is already installed, skipping..."
fi
}
# ---------------------------------------------------------------------------- #
# System Checks #
# ---------------------------------------------------------------------------- #
# --------------------------------- SSH User --------------------------------- #
if ! id -u "openpod" >/dev/null 2>&1; then
echo "==> Creating user 'openpod'"
useradd -m -s /bin/bash openpod
usermod -aG sudo openpod
mkdir -p ~openpod/.ssh/
touch ~openpod/.ssh/authorized_keys
echo "openpod ALL=(ALL) NOPASSWD:ALL" | tee -a /etc/sudoers >/dev/null
else
echo "==> User 'openpod' already exists, skipping..."
mkdir -p ~openpod/.ssh/
touch ~openpod/.ssh/authorized_keys
fi
# ---------------------------- Update System Time ---------------------------- #
timedatectl set-timezone UTC
# ---------------------------------------------------------------------------- #
# Dependencies #
# ---------------------------------------------------------------------------- #
install_if_needed build-essential
install_if_needed chrony
install_if_needed unzip
install_if_needed git
install_if_needed libjpeg-dev
install_if_needed zlib1g-dev
install_if_needed libfreetype6-dev
install_if_needed software-properties-common
# -------------------------------- Python 3.11 ------------------------------- #
if ! python3.11 --version &>/dev/null; then
echo "Installing Python 3.11..."
add-apt-repository ppa:deadsnakes/ppa -y
apt-get update -y
apt-get install -y python${PYTHON_VERSION} python${PYTHON_VERSION}-dev python${PYTHON_VERSION}-venv
else
echo "Python 3.11 is already installed."
# still ensure dev and venv
install_if_needed python3.11-dev
install_if_needed python3.11-venv
fi
# ---------------------------------------------------------------------------- #
# OpenPod #
# ---------------------------------------------------------------------------- #
# -------------------------- Clear Previous Install -------------------------- #
if systemctl is-active --quiet openpod.service; then
systemctl stop openpod.service
fi
rm -rf /opt/OpenPod
# ------------------------------- Clone OpenPod ------------------------------ #
mkdir -p /opt
cd /opt
git clone --single-branch --branch $BRANCH "${REPO}".git
cd OpenPod
# ----------------------------- Setup Environment ---------------------------- #
python${PYTHON_VERSION} -m venv "$VENV_DIR"
$PYTHON_PATH -m pip install --upgrade pip
$PYTHON_PATH -m pip install --no-input -U -r /opt/OpenPod/requirements.txt
rm -rf /opt/OpenPod/requirements.txt
# ---------------------------- Create Directories ---------------------------- #
mkdir -p /opt/OpenPod/logs
mkdir -p /opt/OpenPod/data
# ------------------------------- Create Files ------------------------------- #
# Log Location
touch /opt/OpenPod/logs/RecursionLog.log
touch /opt/OpenPod/logs/System.Snapshot
# Data Location
touch /opt/OpenPod/data/dump.json
touch /opt/OpenPod/data/nodes.json
touch /opt/OpenPod/data/owners.json
touch /opt/OpenPod/data/permissions.json
# ------------------------------- Hardware Info ------------------------------ #
hw_controller=$(grep Hardware /proc/cpuinfo | awk '{print $3}' || echo "unknown")
hw_revision=$(grep Revision /proc/cpuinfo | awk '{print $3}' || echo "unknown")
hw_model=$(grep Model /proc/cpuinfo | cut -d':' -f2 | sed 's/^\s*//' || echo "unknown")
hw_serial=$(grep Serial /proc/cpuinfo | awk '{print $3}' || echo "unknown")
echo "==> Hardware Info:"
echo " Controller: $hw_controller"
echo " Revision: $hw_revision"
echo " Model: $hw_model"
echo " Serial: $hw_serial"
# ------------------------------- openpod.toml ------------------------------- #
config_file="/opt/OpenPod/openpod.toml"
touch "$config_file"
uuid=$(cat /proc/sys/kernel/random/uuid)
xbee_uuid=$(cat /proc/sys/kernel/random/uuid)
openpod_version=$(git rev-parse HEAD)
cat <<EOF > "$config_file"
uuid = "$uuid"
debug = $DEBUG
timezone = "UTC"
url = "$URL"
api_url = "$API_URL"
[openpod]
repo = "$REPO"
branch = "$BRANCH"
commit = "$openpod_version"
version = "$openpod_version"
[hardware]
controller = "$hw_controller"
revision = "$hw_revision"
model = "$hw_model"
serial = "$hw_serial"
[xbee]
ky = "$xbee_uuid"
op = false
[gpio]
led_io = 23
led_stat = 17
EOF
# ------------------------- Create Release Directory ------------------------- #
mkdir -p /opt/OpenPod/releases/"$openpod_version"
cp -a /opt/OpenPod/openpod/. /opt/OpenPod/releases/"$openpod_version"/
rm -rf /opt/OpenPod/openpod
# ------------------------------ Symlink Release ----------------------------- #
ln -s /opt/OpenPod/releases/"$openpod_version" /opt/OpenPod/current
# --------------------------- Setup OpenPod Service -------------------------- #
service_file="/etc/systemd/system/openpod.service"
cat <<EOF > "$service_file"
[Unit]
Description = OpenPod | Recursion.Space
After = network.target
StartLimitIntervalSec = 0
[Service]
Type = simple
User = root
WorkingDirectory = /opt/OpenPod
ExecStart=/opt/OpenPod/venv/bin/python /opt/OpenPod/current/pod.py
Restart = always
RestartSec = 10
[Install]
WantedBy = multi-user.target
EOF
systemctl daemon-reload
systemctl enable openpod.service
systemctl start openpod.service
echo "==> OpenPod is now installed."
echo " UUID: $uuid"
exit 0