Repository navigation
Expand file tree
/
Copy pathstart.sh
More file actions
360 lines (313 loc) · 11.8 KB
/
Copy pathstart.sh
File metadata and controls
360 lines (313 loc) · 11.8 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash
##############################################################################
# infra-TAK - Launcher
# Team Awareness Kit Infrastructure Platform
#
# This is the ONLY script users need to run.
# Everything else happens in the browser.
##############################################################################
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
INSTALL_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_DIR="$INSTALL_DIR/.config"
AUTH_FILE="$CONFIG_DIR/auth.json"
SETTINGS_FILE="$CONFIG_DIR/settings.json"
clear
echo ""
echo -e "${CYAN}${BOLD} ╔══════════════════════════════════════════════════════╗${NC}"
echo -e "${CYAN}${BOLD} ║ infra-TAK ║${NC}"
echo -e "${CYAN}${BOLD} ║ Team Awareness Kit Infrastructure Platform ║${NC}"
echo -e "${CYAN}${BOLD} ╚══════════════════════════════════════════════════════╝${NC}"
echo ""
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}ERROR: This script must be run as root${NC}"
echo "Please run: sudo $0"
exit 1
fi
# ==========================================
# Detect Operating System
# ==========================================
detect_os() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_ID="$ID"
OS_VERSION="$VERSION_ID"
OS_NAME="$PRETTY_NAME"
else
echo -e "${RED}ERROR: Cannot detect operating system${NC}"
exit 1
fi
case "$OS_ID" in
ubuntu)
if [[ "$OS_VERSION" == "22.04"* ]]; then
OS_TYPE="ubuntu-22.04"
PKG_MGR="apt"
elif [[ "$OS_VERSION" == "24.04"* ]]; then
OS_TYPE="ubuntu-24.04"
PKG_MGR="apt"
else
echo -e "${YELLOW}WARNING: Ubuntu $OS_VERSION not tested. Ubuntu 22.04 recommended.${NC}"
OS_TYPE="ubuntu-$OS_VERSION"
PKG_MGR="apt"
fi
;;
debian)
if [[ "$OS_VERSION" == "12"* ]]; then
OS_TYPE="debian-12"
PKG_MGR="apt"
else
echo -e "${YELLOW}WARNING: Debian $OS_VERSION not tested. Debian 12 recommended.${NC}"
OS_TYPE="debian-$OS_VERSION"
PKG_MGR="apt"
fi
;;
rocky|rhel)
if [[ "$OS_VERSION" == 9* ]]; then
OS_TYPE="rocky-9"
PKG_MGR="dnf"
else
echo -e "${YELLOW}WARNING: $OS_NAME not tested. Rocky 9 recommended.${NC}"
OS_TYPE="rocky-$OS_VERSION"
PKG_MGR="dnf"
fi
;;
*)
echo -e "${YELLOW}WARNING: $OS_NAME is not officially supported.${NC}"
echo -e "${YELLOW}Supported: Ubuntu 22.04/24.04, Debian 12, Rocky Linux 9${NC}"
OS_TYPE="$OS_ID-$OS_VERSION"
PKG_MGR="unknown"
;;
esac
echo -e " Detected: ${GREEN}$OS_NAME${NC}"
echo -e " Type: ${GREEN}$OS_TYPE${NC}"
echo ""
}
# ==========================================
# Wait for Unattended Upgrades
# ==========================================
wait_for_upgrades() {
if pgrep -f "/usr/bin/unattended-upgrade$" > /dev/null 2>&1; then
echo -e "${YELLOW} System upgrades in progress, waiting...${NC}"
SECONDS=0
while pgrep -f "/usr/bin/unattended-upgrade$" > /dev/null 2>&1; do
printf "\r Waiting... %02d:%02d elapsed" $((SECONDS/60)) $((SECONDS%60))
sleep 2
done
echo ""
echo -e " ${GREEN}✓ System updates complete${NC}"
echo ""
fi
}
# ==========================================
# Install Python Dependencies
# ==========================================
install_dependencies() {
echo -e " Installing dependencies..."
case "$PKG_MGR" in
apt)
export DEBIAN_FRONTEND=noninteractive
export NEEDRESTART_MODE=a
apt-get update -qq > /dev/null 2>&1
# Dpkg options avoid config prompts; NEEDRESTART_MODE=a avoids "Which services should be restarted?" dialog
NEEDRESTART_MODE=a DEBIAN_FRONTEND=noninteractive apt-get install -y \
-o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" \
python3 python3-pip python3-venv openssl sshpass > /dev/null 2>&1
;;
dnf)
dnf install -y python3 python3-pip openssl sshpass > /dev/null 2>&1
;;
*)
echo -e "${RED} Cannot auto-install dependencies for $PKG_MGR${NC}"
echo " Please install: python3, python3-pip, python3-venv, openssl, sshpass"
exit 1
;;
esac
# Create virtual environment if it doesn't exist
if [ ! -d "$INSTALL_DIR/.venv" ]; then
python3 -m venv "$INSTALL_DIR/.venv" 2>/dev/null || python3 -m venv "$INSTALL_DIR/.venv" --without-pip
fi
# Install Flask and dependencies in venv
"$INSTALL_DIR/.venv/bin/pip" install --quiet flask psutil werkzeug gunicorn 2>/dev/null || \
"$INSTALL_DIR/.venv/bin/pip" install flask psutil werkzeug gunicorn
echo -e " ${GREEN}✓ Dependencies installed${NC}"
echo ""
}
# ==========================================
# First-Time Setup (password only)
# ==========================================
first_time_setup() {
mkdir -p "$CONFIG_DIR"
chmod 700 "$CONFIG_DIR"
echo -e "${BOLD} First-time setup${NC}"
echo ""
# Set admin password
while true; do
read -s -p " Set admin password: " ADMIN_PASS
echo ""
read -s -p " Confirm password: " ADMIN_PASS_CONFIRM
echo ""
if [ -z "$ADMIN_PASS" ]; then
echo -e " ${RED}Password cannot be empty${NC}"
continue
fi
if [ "$ADMIN_PASS" != "$ADMIN_PASS_CONFIRM" ]; then
echo -e " ${RED}Passwords do not match${NC}"
continue
fi
break
done
# Hash the password using Python
PASS_HASH=$("$INSTALL_DIR/.venv/bin/python3" -c "
from werkzeug.security import generate_password_hash
import sys
print(generate_password_hash(sys.argv[1]))
" "$ADMIN_PASS")
# Save auth config
cat > "$AUTH_FILE" << EOF
{
"password_hash": "$PASS_HASH",
"created": "$(date -u '+%Y-%m-%dT%H:%M:%SZ')"
}
EOF
chmod 600 "$AUTH_FILE"
# Detect server IP
SERVER_IP=$(hostname -I | awk '{print $1}')
# Save settings — always start in IP/self-signed mode
# FQDN setup happens in the browser through the Caddy module
cat > "$SETTINGS_FILE" << EOF
{
"ssl_mode": "self-signed",
"fqdn": "",
"server_ip": "$SERVER_IP",
"os_type": "$OS_TYPE",
"os_name": "$OS_NAME",
"pkg_mgr": "$PKG_MGR",
"console_port": 5001,
"install_dir": "$INSTALL_DIR",
"created": "$(date -u '+%Y-%m-%dT%H:%M:%SZ')"
}
EOF
chmod 600 "$SETTINGS_FILE"
echo ""
echo -e " ${GREEN}✓ Configuration saved${NC}"
}
# ==========================================
# Generate Self-Signed Certificate
# ==========================================
generate_self_signed_cert() {
CERT_DIR="$CONFIG_DIR/ssl"
mkdir -p "$CERT_DIR"
if [ ! -f "$CERT_DIR/console.key" ]; then
echo -e " Generating self-signed certificate..."
SERVER_IP=$(hostname -I | awk '{print $1}')
openssl req -x509 -newkey rsa:4096 \
-keyout "$CERT_DIR/console.key" \
-out "$CERT_DIR/console.crt" \
-sha256 -days 3650 -nodes \
-subj "/C=US/ST=TAK/L=TAK/O=TAKWERX/CN=$SERVER_IP" \
-addext "subjectAltName=IP:$SERVER_IP,IP:127.0.0.1,DNS:localhost" \
2>/dev/null
chmod 600 "$CERT_DIR/console.key"
chmod 644 "$CERT_DIR/console.crt"
echo -e " ${GREEN}✓ Self-signed certificate generated${NC}"
fi
}
# ==========================================
# Create systemd Service
# ==========================================
# If the service already exists and points to a directory that has .config/auth.json,
# keep using that directory so "git pull" or running start.sh from another clone
# doesn't switch to a path that has no auth (password would stop working).
create_service() {
SERVICE_FILE="/etc/systemd/system/takwerx-console.service"
USE_DIR="$INSTALL_DIR"
if [ -f "$SERVICE_FILE" ]; then
EXISTING_DIR=$(grep -E '^WorkingDirectory=' "$SERVICE_FILE" 2>/dev/null | cut -d= -f2- | tr -d ' ')
if [ -n "$EXISTING_DIR" ] && [ -d "$EXISTING_DIR" ] && [ -f "$EXISTING_DIR/.config/auth.json" ]; then
USE_DIR="$EXISTING_DIR"
fi
fi
# Build gunicorn command with SSL if certs exist
CERT_DIR="$USE_DIR/.config/ssl"
GUNICORN_BIN="$USE_DIR/.venv/bin/gunicorn"
PORT=$("$USE_DIR/.venv/bin/python3" -c "
import json, os
try:
with open(os.path.join('$USE_DIR', '.config', 'settings.json')) as f:
print(json.load(f).get('console_port', 5001))
except Exception:
print(5001)
" 2>/dev/null || echo 5001)
GUNICORN_ARGS="--bind 0.0.0.0:$PORT --workers 1 --threads 4 --timeout 300 --graceful-timeout 30"
if [ -f "$CERT_DIR/console.crt" ] && [ -f "$CERT_DIR/console.key" ]; then
GUNICORN_ARGS="$GUNICORN_ARGS --certfile=$CERT_DIR/console.crt --keyfile=$CERT_DIR/console.key"
fi
cat > "$SERVICE_FILE" << EOF
[Unit]
Description=infra-TAK - Team Awareness Kit Infrastructure Platform
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=$GUNICORN_BIN $GUNICORN_ARGS app:app
WorkingDirectory=$USE_DIR
Restart=always
RestartSec=5
User=root
Environment=PYTHONUNBUFFERED=1
Environment=CONFIG_DIR=$USE_DIR/.config
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable takwerx-console > /dev/null 2>&1
}
# ==========================================
# Main
# ==========================================
detect_os
wait_for_upgrades
install_dependencies
# First-time setup if no auth file exists
if [ ! -f "$AUTH_FILE" ]; then
first_time_setup
fi
# Always use self-signed cert for console (Caddy handles FQDN SSL)
generate_self_signed_cert
# Create and start systemd service
create_service
# Stop existing instance if running
systemctl stop takwerx-console 2>/dev/null || true
sleep 1
# Start the console
systemctl start takwerx-console
# Ensure backdoor port 5001 is allowed (so when UFW is enabled later, e.g. by TAK Server deploy, we don't lock ourselves out)
if command -v ufw >/dev/null 2>&1; then
ufw allow 5001/tcp >/dev/null 2>&1 || true
fi
if command -v firewall-cmd >/dev/null 2>&1; then
firewall-cmd --permanent --add-port=5001/tcp >/dev/null 2>&1 || true
firewall-cmd --reload >/dev/null 2>&1 || true
fi
# Get access URL
SERVER_IP=$(hostname -I | awk '{print $1}')
echo ""
echo -e "${GREEN}${BOLD} ╔══════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}${BOLD} ║ infra-TAK is running! ║${NC}"
echo -e "${GREEN}${BOLD} ╚══════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e " ${BOLD}Access:${NC} https://$SERVER_IP:5001"
echo -e " ${YELLOW}(Accept the self-signed certificate warning in your browser)${NC}"
echo ""
echo -e " ${BOLD}Service:${NC} systemctl status takwerx-console"
echo -e " ${BOLD}Logs:${NC} journalctl -u takwerx-console -f"
echo ""
echo -e " ${CYAN}Tip: Set up a domain name through the Caddy module${NC}"
echo -e " ${CYAN}in the console for proper SSL and full functionality.${NC}"
echo ""