Repository navigation
Expand file tree
/
Copy pathsetup_wizard.sh
More file actions
executable file
·274 lines (241 loc) · 9.14 KB
/
Copy pathsetup_wizard.sh
File metadata and controls
executable file
·274 lines (241 loc) · 9.14 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
#!/bin/bash
# Interactive Setup Wizard for pi-gcode-server
# Guides user through configuration and generates config.local
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="$SCRIPT_DIR/config.local"
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${BLUE}╔══════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ pi-gcode-server Configuration Wizard ║${NC}"
echo -e "${BLUE}╚══════════════════════════════════════════════════╝${NC}"
echo
# Check if config.local already exists
if [ -f "$CONFIG_FILE" ]; then
echo -e "${YELLOW}⚠️ config.local already exists!${NC}"
echo
read -p "Do you want to reconfigure? This will backup your existing config. (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${GREEN}✓ Using existing configuration${NC}"
exit 0
fi
# Backup existing config
BACKUP_FILE="$CONFIG_FILE.backup.$(date +%Y%m%d_%H%M%S)"
cp "$CONFIG_FILE" "$BACKUP_FILE"
echo -e "${GREEN}✓ Backed up existing config to: $BACKUP_FILE${NC}"
echo
fi
echo "This wizard will help you configure your pi-gcode-server installation."
echo "Press Ctrl+C at any time to cancel."
echo
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
# Function to prompt for input with default
prompt_with_default() {
local prompt="$1"
local default="$2"
local var_name="$3"
local value
read -rp "$prompt [$default]: " value
value="${value:-$default}"
printf -v "$var_name" '%s' "$value"
}
# Function to validate directory
validate_directory() {
local dir="$1"
# Expand variables safely
dir="${dir/#\~/$HOME}"
dir=$(echo "$dir" | envsubst)
if [ ! -d "$dir" ]; then
echo -e "${YELLOW}⚠️ Directory doesn't exist: $dir${NC}"
read -p "Create it now? (Y/n): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
mkdir -p "$dir"
echo -e "${GREEN}✓ Created directory: $dir${NC}"
return 0
else
return 1
fi
fi
return 0
}
# Function to validate SSH parameters
validate_ssh_params() {
local user="$1"
local host="$2"
local port="$3"
# Validate port is numeric and in range
if ! [[ "$port" =~ ^[0-9]+$ ]]; then
echo -e "${RED}✗ Port must be numeric${NC}"
return 1
fi
if [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then
echo -e "${RED}✗ Port must be between 1-65535${NC}"
return 1
fi
# Validate no dangerous characters in user/host
if [[ "$user" =~ [\$\`\;\|\&\<\>\(\)\{\}] ]]; then
echo -e "${RED}✗ Username contains invalid characters${NC}"
return 1
fi
if [[ "$host" =~ [\$\`\;\|\&\<\>\(\)\{\}] ]]; then
echo -e "${RED}✗ Hostname contains invalid characters${NC}"
return 1
fi
return 0
}
# Function to test SSH connection
test_ssh_connection() {
local user="$1"
local host="$2"
local port="$3"
echo -n "Testing SSH connection to ${user}@${host}:${port}... "
if ssh -p "$port" -o ConnectTimeout=5 -o BatchMode=yes -o StrictHostKeyChecking=yes "${user}@${host}" "echo 'Connected'" &>/dev/null; then
echo -e "${GREEN}✓ Success${NC}"
return 0
else
echo -e "${RED}✗ Failed${NC}"
echo -e "${YELLOW}⚠️ SSH connection failed. Please ensure:${NC}"
echo " - Raspberry Pi is powered on and connected to network"
echo " - SSH is enabled on the Pi"
echo " - SSH keys are set up (run: ssh-copy-id ${user}@${host})"
echo " - Firewall allows SSH connections"
return 1
fi
}
echo -e "${BLUE}📁 Local Machine Configuration${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
# Watch directory
while true; do
prompt_with_default "Directory to monitor for .gcode files" "\$HOME/Desktop" WATCH_DIR
if validate_directory "$WATCH_DIR"; then
break
fi
done
# Log file
prompt_with_default "Log file location" "\$HOME/.gcode_sync.log" LOG_FILE
echo
echo -e "${BLUE}🔌 Raspberry Pi Connection${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
# Remote user
prompt_with_default "Raspberry Pi SSH username" "$USER" REMOTE_USER
# Remote host
echo
echo "Enter your Raspberry Pi's IP address or hostname."
echo "Examples: 192.168.1.6, raspberrypi.local, localhost (if using port forwarding)"
prompt_with_default "Raspberry Pi hostname/IP" "192.168.1.100" REMOTE_HOST
# Remote port
prompt_with_default "SSH port" "22" REMOTE_PORT
# Validate SSH parameters before testing connection
echo
if ! validate_ssh_params "$REMOTE_USER" "$REMOTE_HOST" "$REMOTE_PORT"; then
echo
echo -e "${RED}✗ Invalid SSH parameters. Please run the wizard again.${NC}"
exit 1
fi
# Test SSH connection
if ! test_ssh_connection "$REMOTE_USER" "$REMOTE_HOST" "$REMOTE_PORT"; then
echo
read -p "Continue anyway? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${RED}✗ Setup cancelled${NC}"
exit 1
fi
fi
# Remote path
echo
echo "Path on Raspberry Pi where USB gadget is mounted."
prompt_with_default "USB gadget mount point" "/mnt/usb_share" REMOTE_PATH
echo
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -e "${BLUE}📝 Configuration Summary${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
echo " Watch Directory: $WATCH_DIR"
echo " Log File: $LOG_FILE"
echo " Pi Username: $REMOTE_USER"
echo " Pi Host: $REMOTE_HOST"
echo " Pi SSH Port: $REMOTE_PORT"
echo " Pi Mount Path: $REMOTE_PATH"
echo
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo
read -p "Save this configuration? (Y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Nn]$ ]]; then
echo -e "${RED}✗ Configuration not saved${NC}"
exit 1
fi
# Generate config.local with secure permissions
(
umask 077
cat > "$CONFIG_FILE" << EOF
# Configuration for pi-gcode-server
# Generated by setup wizard on $(date)
#
# To modify this configuration:
# 1. Edit this file directly, OR
# 2. Run: ./setup_wizard.sh
# Local machine settings
WATCH_DIR="$WATCH_DIR"
LOG_FILE="$LOG_FILE"
# Raspberry Pi connection settings
REMOTE_USER="$REMOTE_USER"
REMOTE_HOST="$REMOTE_HOST"
REMOTE_PORT="$REMOTE_PORT"
REMOTE_PATH="$REMOTE_PATH"
EOF
)
# Ensure secure permissions and verify
chmod 600 "$CONFIG_FILE"
if [ "$(stat -c '%a' "$CONFIG_FILE")" != "600" ]; then
echo -e "${RED}✗ ERROR: Failed to set secure permissions on $CONFIG_FILE${NC}"
echo "Current permissions: $(stat -c '%a' "$CONFIG_FILE")"
exit 1
fi
echo
echo -e "${GREEN}✓ Configuration saved to: $CONFIG_FILE${NC}"
echo
echo -e "${BLUE}📋 Next Steps:${NC}"
echo
echo "1. Install dependencies:"
echo " ${YELLOW}sudo apt-get install -y inotify-tools rsync openssh-client${NC}"
echo
echo "2. Set up SSH keys (if not already done):"
echo " ${YELLOW}ssh-keygen -t rsa -b 4096${NC}"
echo " ${YELLOW}ssh-copy-id -p $REMOTE_PORT ${REMOTE_USER}@${REMOTE_HOST}${NC}"
echo
echo "3. Install Pi scripts on Raspberry Pi:"
echo " ${YELLOW}scp -P $REMOTE_PORT pi_scripts/refresh_usb_gadget.sh ${REMOTE_USER}@${REMOTE_HOST}:/tmp/${NC}"
echo " ${YELLOW}ssh -p $REMOTE_PORT ${REMOTE_USER}@${REMOTE_HOST} \"sudo mv /tmp/refresh_usb_gadget.sh /usr/local/bin/ && sudo chmod +x /usr/local/bin/refresh_usb_gadget.sh\"${NC}"
echo
echo "4. Test the monitor script:"
echo " ${YELLOW}./monitor_and_sync.sh${NC}"
echo
echo "5. Install as systemd service (optional):"
echo " ${YELLOW}./install_service.sh${NC}"
echo
echo "6. Set up automated health monitoring (optional):"
echo " ${YELLOW}crontab -e${NC}"
echo " ${YELLOW}*/15 * * * * $(pwd)/check_gcode_monitor.sh >> \$HOME/.gcode_health.log 2>&1${NC}"
echo
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${GREEN}✓ Setup complete! Happy printing! 🖨️${NC}"
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo
echo "To modify configuration in the future:"
echo " - Edit: $CONFIG_FILE"
echo " - Or run: ./setup_wizard.sh"
echo
echo "To restart the service after config changes:"
echo " ${YELLOW}sudo systemctl restart gcode-monitor.service${NC}"
echo