Repository navigation
Expand file tree
/
Copy pathsetup_daemon.sh
More file actions
executable file
·359 lines (304 loc) · 10.3 KB
/
Copy pathsetup_daemon.sh
File metadata and controls
executable file
·359 lines (304 loc) · 10.3 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
#!/bin/bash
# CardBrick Sync Daemon Setup Script
# Creates virtual environment and installs dependencies
set -e # Exit on any error
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VENV_DIR="$SCRIPT_DIR/venv-syncd"
DAEMON_DIR="$SCRIPT_DIR/cardbrick-syncd"
CLI_DIR="$SCRIPT_DIR/cardbrick-cli"
DATA_DIR="${CARDBRICK_DATA_DIR:-/var/lib/cardbrick}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if we're in the right directory
check_directory() {
if [[ ! -d "$DAEMON_DIR" ]]; then
log_error "cardbrick-syncd directory not found!"
log_error "Please run this script from the CardBrick root directory"
exit 1
fi
if [[ ! -f "$DAEMON_DIR/main.py" ]]; then
log_error "main.py not found in cardbrick-syncd directory!"
exit 1
fi
log_success "Found CardBrick daemon directory"
}
# Check system dependencies
check_system_deps() {
log_info "Checking system dependencies..."
# Check Python
if ! command -v python3 &> /dev/null; then
log_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])))')
log_info "Found Python $PYTHON_VERSION"
# Check if python3-venv is available
if ! python3 -c "import venv" 2>/dev/null; then
log_warning "python3-venv module not found"
log_info "Installing python3-venv..."
if command -v apt-get &> /dev/null; then
sudo apt-get update && sudo apt-get install -y python3-venv python3-dev
elif command -v yum &> /dev/null; then
sudo yum install -y python3-venv python3-devel
else
log_error "Please install python3-venv package for your system"
exit 1
fi
fi
# Check for Avahi development packages (optional but recommended)
if command -v apt-get &> /dev/null; then
log_info "Checking for Avahi packages for mDNS support..."
if ! dpkg -l | grep -q python3-dbus; then
log_info "Installing python3-dbus for Avahi support..."
sudo apt-get install -y python3-dbus libdbus-1-dev || log_warning "Could not install dbus packages"
fi
if ! dpkg -l | grep -q libavahi-compat-libdnssd-dev; then
log_info "Installing Avahi development packages..."
sudo apt-get install -y avahi-daemon libavahi-compat-libdnssd-dev || log_warning "Could not install Avahi packages"
fi
elif command -v yum &> /dev/null; then
log_info "Installing Avahi packages for RHEL/CentOS..."
sudo yum install -y avahi avahi-devel dbus-python || log_warning "Could not install Avahi packages"
fi
log_info "Note: mDNS will work without Avahi, using simplified discovery"
}
# Create virtual environment
create_venv() {
if [[ -d "$VENV_DIR" ]]; then
log_warning "Virtual environment already exists at $VENV_DIR"
read -p "Recreate it? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
log_info "Removing existing virtual environment..."
rm -rf "$VENV_DIR"
else
log_info "Using existing virtual environment"
return 0
fi
fi
log_info "Creating virtual environment at $VENV_DIR..."
python3 -m venv --system-site-packages "$VENV_DIR"
# Update pip in virtual environment
log_info "Upgrading pip in virtual environment..."
"$VENV_DIR/bin/python" -m pip install --upgrade pip
log_success "Virtual environment created successfully"
}
# Install Python dependencies
install_dependencies() {
log_info "Installing Python dependencies..."
# Install daemon dependencies
log_info "Installing cardbrick-syncd dependencies..."
"$VENV_DIR/bin/pip" install -r "$DAEMON_DIR/requirements.txt"
# Install CLI dependencies (for wormhole functionality)
if [[ -f "$CLI_DIR/requirements.txt" ]]; then
log_info "Installing cardbrick-cli dependencies..."
"$VENV_DIR/bin/pip" install -r "$CLI_DIR/requirements.txt"
fi
log_success "Dependencies installed successfully"
}
# Create data directories
setup_data_directories() {
log_info "Setting up data directories..."
# Create main data directory
if [[ ! -d "$DATA_DIR" ]]; then
log_info "Creating data directory: $DATA_DIR"
sudo mkdir -p "$DATA_DIR"
sudo chown "$USER:$USER" "$DATA_DIR"
fi
# Create subdirectories
mkdir -p "$DATA_DIR"/{keys,keys/devices,inbox,learners}
log_success "Data directories created at $DATA_DIR"
}
# Generate SSH keys if they don't exist
setup_ssh_keys() {
log_info "Setting up SSH keys..."
DAEMON_KEY="$DATA_DIR/keys/daemon_private.pem"
if [[ ! -f "$DAEMON_KEY" ]]; then
log_info "Generating daemon SSH key..."
ssh-keygen -t ed25519 -f "$DAEMON_KEY" -N "" -C "cardbrick-daemon@$(hostname)"
chmod 600 "$DAEMON_KEY"
chmod 644 "${DAEMON_KEY}.pub"
log_success "Daemon SSH key generated"
else
log_info "Daemon SSH key already exists"
fi
}
# Create startup script
create_startup_script() {
log_info "Creating startup script..."
cat > "$SCRIPT_DIR/start_daemon.sh" << EOF
#!/bin/bash
# CardBrick Sync Daemon Startup Script
# Generated by setup_daemon.sh
SCRIPT_DIR="\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" && pwd)"
VENV_DIR="\$SCRIPT_DIR/venv-syncd"
DAEMON_DIR="\$SCRIPT_DIR/cardbrick-syncd"
# Check if virtual environment exists
if [[ ! -d "\$VENV_DIR" ]]; then
echo "Virtual environment not found! Please run setup_daemon.sh first."
exit 1
fi
# Set environment variables
export CARDBRICK_DATA_DIR="${DATA_DIR}"
export PYTHONPATH="\$DAEMON_DIR:\$SCRIPT_DIR/cardbrick-cli:\$PYTHONPATH"
# Activate virtual environment and run daemon
echo "Starting CardBrick Sync Daemon..."
echo "Data directory: \$CARDBRICK_DATA_DIR"
echo "HTTP port: \${CARDBRICK_HTTP_PORT:-6429}"
echo "Press Ctrl+C to stop"
echo
cd "\$DAEMON_DIR"
"\$VENV_DIR/bin/python" -m main "\$@"
EOF
chmod +x "$SCRIPT_DIR/start_daemon.sh"
log_success "Startup script created: $SCRIPT_DIR/start_daemon.sh"
}
# Create systemd service file
create_systemd_service() {
log_info "Creating systemd service file..."
cat > "$SCRIPT_DIR/cardbrick-syncd-venv.service" << EOF
[Unit]
Description=CardBrick Sync Daemon (Virtual Environment)
After=network.target
Wants=network.target
[Service]
Type=simple
User=$USER
Group=$USER
WorkingDirectory=$DAEMON_DIR
Environment=CARDBRICK_DATA_DIR=$DATA_DIR
Environment=PYTHONPATH=$DAEMON_DIR:$CLI_DIR
ExecStart=$VENV_DIR/bin/python -m main
Restart=always
RestartSec=10
# Security settings
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=$DATA_DIR
[Install]
WantedBy=multi-user.target
EOF
log_success "Systemd service file created: $SCRIPT_DIR/cardbrick-syncd-venv.service"
log_info "To install as system service:"
log_info " sudo cp cardbrick-syncd-venv.service /etc/systemd/system/"
log_info " sudo systemctl daemon-reload"
log_info " sudo systemctl enable cardbrick-syncd-venv"
log_info " sudo systemctl start cardbrick-syncd-venv"
}
# Test the installation
test_installation() {
log_info "Testing installation..."
# Test that we can import the main modules
if "$VENV_DIR/bin/python" -c "
import sys
sys.path.insert(0, '$DAEMON_DIR')
sys.path.insert(0, '$CLI_DIR')
try:
from daemon import SyncDaemon
from config import Config
print('✓ Daemon modules import successfully')
except ImportError as e:
print(f'✗ Import error: {e}')
sys.exit(1)
try:
from wormhole_backup import WormholeBackup
print('✓ CLI modules import successfully')
except ImportError as e:
print(f'✗ CLI import error: {e}')
sys.exit(1)
"; then
log_success "Installation test passed"
else
log_error "Installation test failed"
return 1
fi
}
# Main setup function
main() {
echo "=================================="
echo "CardBrick Sync Daemon Setup"
echo "=================================="
echo
check_directory
check_system_deps
create_venv
install_dependencies
setup_data_directories
setup_ssh_keys
create_startup_script
create_systemd_service
echo
log_info "Testing installation..."
if test_installation; then
echo
echo "=================================="
log_success "Setup completed successfully!"
echo "=================================="
echo
echo "To start the daemon:"
echo " ./start_daemon.sh"
echo
echo "To run in background:"
echo " nohup ./start_daemon.sh > daemon.log 2>&1 &"
echo
echo "To install as system service:"
echo " sudo cp cardbrick-syncd-venv.service /etc/systemd/system/"
echo " sudo systemctl enable cardbrick-syncd-venv"
echo " sudo systemctl start cardbrick-syncd-venv"
echo
echo "Data directory: $DATA_DIR"
echo "Virtual environment: $VENV_DIR"
echo
else
log_error "Setup completed with errors. Please check the output above."
exit 1
fi
}
# Handle command line arguments
case "${1:-setup}" in
"setup"|"install")
main
;;
"clean")
log_info "Cleaning up virtual environment..."
rm -rf "$VENV_DIR"
rm -f "$SCRIPT_DIR/start_daemon.sh"
rm -f "$SCRIPT_DIR/cardbrick-syncd-venv.service"
log_success "Cleanup completed"
;;
"test")
if [[ -d "$VENV_DIR" ]]; then
test_installation
else
log_error "Virtual environment not found. Run setup first."
exit 1
fi
;;
*)
echo "Usage: $0 [setup|clean|test]"
echo " setup (default): Create virtual environment and install dependencies"
echo " clean: Remove virtual environment and generated files"
echo " test: Test the installation"
exit 1
;;
esac