-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·265 lines (208 loc) · 6.92 KB
/
setup.sh
File metadata and controls
executable file
·265 lines (208 loc) · 6.92 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
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
print_header() {
echo -e "\n${BLUE}========================================${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}========================================${NC}\n"
}
print_success() {
echo -e "${GREEN}[✓] $1${NC}"
}
print_warning() {
echo -e "${YELLOW}[!] $1${NC}"
}
print_error() {
echo -e "${RED}[✗] $1${NC}"
}
print_info() {
echo -e "${BLUE}[i] $1${NC}"
}
prompt_yes_no() {
local prompt="$1"
local response
while true; do
read -p "$prompt [y/N]: " response
case "$response" in
[Yy]|[Yy][Ee][Ss]) return 0 ;;
[Nn]|[Nn][Oo]|"") return 1 ;;
*) echo "Please answer y or n" ;;
esac
done
}
print_header "Beakon Setup Script"
print_info "Checking prerequisites..."
if ! command -v python3 &> /dev/null; then
print_error "Python 3 is required but not installed."
exit 1
fi
PYTHON_VERSION=$(python3 -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
PYTHON_MAJOR=$(echo "$PYTHON_VERSION" | cut -d. -f1)
PYTHON_MINOR=$(echo "$PYTHON_VERSION" | cut -d. -f2)
if [ "$PYTHON_MAJOR" -lt 3 ] || ([ "$PYTHON_MAJOR" -eq 3 ] && [ "$PYTHON_MINOR" -lt 8 ]); then
print_error "Python 3.8+ is required. You have Python $PYTHON_VERSION"
exit 1
fi
if ! python3 -c "import sqlite3" 2>/dev/null; then
if command -v python3.12 &> /dev/null; then
print_warning "Python $PYTHON_VERSION lacks sqlite3, using python3.12"
export PYTHON=python3.12
else
print_error "sqlite3 module not available. Please install Python with sqlite support."
exit 1
fi
else
export PYTHON=python3
fi
print_success "Python $PYTHON_VERSION found (using $PYTHON)"
if ! command -v pip3 &> /dev/null; then
if ! command -v pip &> /dev/null; then
print_error "pip is required but not installed."
exit 1
fi
export PIP=pip
else
export PIP=pip3
fi
print_success "pip found"
print_header "Installing Dependencies"
if [ -d ".venv" ]; then
print_warning "Virtual environment already exists. Using existing .venv"
else
print_info "Creating virtual environment..."
$PYTHON -m venv .venv
print_success "Virtual environment created at .venv"
fi
print_info "Activating virtual environment..."
source .venv/bin/activate
print_info "Installing Python packages..."
$PIP install --upgrade pip > /dev/null 2>&1
$PIP install "flask<2.3" flask-sqlalchemy flask-login pyOpenSSL > /dev/null 2>&1
print_success "Dependencies installed"
print_header "Configuration"
print_info "Generating SECRET_KEY..."
SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_hex(32))")
print_success "SECRET_KEY generated"
print_info "\nMapbox API Key Setup"
echo -e "${YELLOW}Mapbox provides the map tiles for Beakon.${NC}"
echo -e "You can get a free API key at: ${BLUE}https://www.mapbox.com/${NC}"
echo -e "Without it, the maps won't load but the rest of the app will work."
echo ""
if prompt_yes_no "Do you have a Mapbox API key?"; then
read -p "Enter your Mapbox Access Token: " MAPBOX_API_KEY
else
print_warning "Skipping Mapbox setup. Maps will not work."
MAPBOX_API_KEY=''
fi
print_info "Creating config.cfg..."
mkdir -p instance
cat > config.cfg << EOF
# Beakon Configuration
# Generated by setup script on $(date)
DEBUG=True
SQLALCHEMY_DATABASE_URI='sqlite:///$SCRIPT_DIR/instance/beakon.db'
SQLALCHEMY_TRACK_MODIFICATIONS = False
MAPBOX_API_KEY = '$MAPBOX_API_KEY'
REGISTRATION_ENABLED = True
SECRET_KEY = '$SECRET_KEY'
VERSION = '$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")'
LOCATION_REFRESH_INTERVAL = 10
EOF
print_success "Configuration saved to config.cfg"
print_header "Database Setup"
print_info "Initializing database..."
$PYTHON -c "
import sys
sys.path.insert(0, 'src')
from app import app, db
with app.app_context():
db.create_all()
print('Database initialized successfully')
" 2>/dev/null || $PYTHON -c "
import sys
sys.path.insert(0, 'src')
from models import db
from app import app
with app.app_context():
db.create_all()
print('Database initialized successfully')
"
print_success "Database initialized"
print_header "Test Data (Optional)"
echo -e "Populate the database with test users and locations?"
echo -e "Creates 3 users: ${YELLOW}alice${NC}, ${YELLOW}bob${NC}, ${YELLOW}charlie${NC} (password: test123)"
echo -e "Includes ~15 locations per user and sharing permissions."
echo ""
if prompt_yes_no "Populate test data?"; then
print_info "Seeding test data..."
$PYTHON -c "
import sys
sys.path.insert(0, 'src')
from app import app, seed_test_data
with app.app_context():
seed_test_data()
print('Test data seeded successfully')
"
print_success "Test data populated"
fi
print_header "Systemd Service (Optional)"
echo -e "A systemd service allows Beakon to run automatically on boot."
echo -e "You can run it as a ${YELLOW}user service${NC} (your user only) or ${YELLOW}system service${NC} (everyone)."
echo ""
if prompt_yes_no "Do you want to install a systemd service?"; then
if prompt_yes_no "Install as system service (requires root)? [y] for system, [n] for user"; then
SERVICE_DIR="/etc/systemd/system"
NEEDS_SUDO=true
else
SERVICE_DIR="$HOME/.config/systemd/user"
NEEDS_SUDO=false
fi
mkdir -p "$SERVICE_DIR"
SERVICE_FILE="$SERVICE_DIR/beakon.service"
cat > "$SERVICE_FILE" << EOF
[Unit]
Description=Beakon Location Sharing Server
After=network.target
[Service]
Type=simple
WorkingDirectory=$SCRIPT_DIR
ExecStart=$SCRIPT_DIR/.venv/bin/python -c "import sys; sys.path.insert(0, 'src'); from app import app; app.run(host='0.0.0.0', ssl_context='adhoc')"
Restart=on-failure
RestartSec=5
[Install]
WantedBy=default.target
EOF
if [ "$NEEDS_SUDO" = true ]; then
sudo systemctl daemon-reload
print_success "Systemd service installed to $SERVICE_FILE"
print_info "To enable on boot: sudo systemctl enable beakon"
print_info "To start now: sudo systemctl start beakon"
else
systemctl --user daemon-reload
print_success "Systemd service installed to $SERVICE_FILE"
print_info "To enable on boot: systemctl --user enable beakon"
print_info "To start now: systemctl --user start beakon"
fi
fi
print_header "Setup Complete!"
echo -e "${GREEN}Beakon is ready to run!${NC}"
echo ""
echo "To start the server manually:"
echo -e " ${BLUE}source .venv/bin/activate${NC}"
echo -e " ${BLUE}python -c \"import sys; sys.path.insert(0, 'src'); from app import app; app.run()\"${NC}"
echo ""
echo "Then open in your browser:"
echo -e " ${GREEN}https://localhost:5000${NC}"
echo ""
echo "The first user you register will be the admin."
echo ""
echo "To disable registration after creating your account, edit:"
echo -e " ${YELLOW}config.cfg${NC}"
echo " And set: REGISTRATION_ENABLED = False"
echo ""