-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
358 lines (298 loc) · 11.8 KB
/
install.sh
File metadata and controls
358 lines (298 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
#!/bin/bash
# Default values for command-line arguments
install_dir="/home/$USER/XBeeServerAPI"
no_root_check=0 # Flag for bypassing root check
silent_mode=0 # Flag for silent mode
skip_ufw_ssh=0 # Flag for skipping UFW SSH rule
service_name="xbeeserver.service" # Service name
service_file="/etc/systemd/system/$service_name"
# Determine the destination path for app.py based on install_dir
app_destination="$install_dir/Software/app.py" # Adjust this path as needed
# Define colors for pretty output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Enhanced logging function
log_message() {
if [ "$silent_mode" -eq 0 ]; then
local type="$1"
local message="$2"
echo -e "[$(date)] $type: $message"
fi
}
# Function to print error messages in red
error_message() {
log_message "${RED}ERROR${NC}" "$1"
}
# Function to print success messages in green
success_message() {
log_message "${GREEN}SUCCESS${NC}" "$1"
}
# Function to print info messages in yellow
info_message() {
log_message "${YELLOW}INFO${NC}" "$1"
}
# Function to update app.py and restart the service
update_app_and_restart_service() {
info_message "Stopping the service: $service_name..."
sudo systemctl stop "$service_name" || { error_message "Failed to stop $service_name"; exit 1; }
info_message "Updating app.py..."
if [ -f "$install_dir/Software/app.py" ]; then
sudo cp "$install_dir/Software/app.py" "$app_destination" || { error_message "Failed to update app.py."; exit 1; }
else
error_message "Updated app.py not found."
exit 1
fi
info_message "Restarting the service: $service_name..."
sudo systemctl start "$service_name" || { error_message "Failed to start $service_name"; exit 1; }
success_message "app.py updated and service restarted successfully."
exit 0
}
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -d <dir> Set the installation directory"
echo " -s Silent mode"
echo " -f Skip UFW SSH rule"
echo " -uninstall Uninstall the software"
echo "Example:"
echo " $0 -d /custom/dir -n -s"
}
# Function to check for required commands and install UFW if not found
check_dependencies() {
local dependencies=("python3" "pip" "systemctl" "cp" "mkdir")
for cmd in "${dependencies[@]}"; do
if ! command -v "$cmd" &> /dev/null; then
error_message "Required command '$cmd' not found. Please install it."
exit 1
fi
done
# Improved package checking
local packages=("libusb-1.0-0" "python3-venv")
for pkg in "${packages[@]}"; do
if ! dpkg-query -W -f='${Status}' $pkg 2>/dev/null | grep -q "install ok installed"; then
error_message "Required package '$pkg' not installed. Please install it."
exit 1
fi
done
# Special handling for UFW
if ! command -v ufw &> /dev/null; then
info_message "UFW is not installed. Installing UFW..."
sudo apt-get install ufw -y || { error_message "Failed to install UFW, exiting."; exit 1; }
fi
}
# Function to configure udev rules
configure_udev_rules() {
local UDEV_RULES_PATH="/etc/udev/rules.d/11-ftdi.rules"
sudo bash -c "cat <<EOF > $UDEV_RULES_PATH
# FTDI devices
SUBSYSTEM=='usb', ATTR{idVendor}=='0403', ATTR{idProduct}=='6001', GROUP='plugdev', MODE='0664'
SUBSYSTEM=='usb', ATTR{idVendor}=='0403', ATTR{idProduct}=='6010', GROUP='plugdev', MODE='0664'
SUBSYSTEM=='usb', ATTR{idVendor}=='0403', ATTR{idProduct}=='6011', GROUP='plugdev', MODE='0664'
SUBSYSTEM=='usb', ATTR{idVendor}=='0403', ATTR{idProduct}=='6014', GROUP='plugdev', MODE='0664'
SUBSYSTEM=='usb', ATTR{idVendor}=='0403', ATTR{idProduct}=='6015', GROUP='plugdev', MODE='0664'
SUBSYSTEM=='usb', ATTR{idVendor}=='0403', ATTR{idProduct}=='6048', GROUP='plugdev', MODE='0664'
EOF" || { error_message "Failed to create udev rules, exiting."; exit 1; }
}
uninstall() {
# Confirmation before uninstalling
echo "You are about to uninstall the software. This action cannot be undone."
read -p "Are you sure you want to proceed? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
error_message "Uninstallation aborted by the user."
exit 1
fi
echo "Starting uninstallation process..."
# Stop the service
echo "Stopping the service: $service_name..."
sudo systemctl stop "$service_name" 2>/dev/null
# Disable the service
echo "Disabling the service: $service_name..."
sudo systemctl disable "$service_name" 2>/dev/null
# Remove the systemd service file
local SERVICE_FILE="/etc/systemd/system/$service_name"
if [ -f "$SERVICE_FILE" ]; then
echo "Removing the systemd service file..."
sudo rm "$SERVICE_FILE"
fi
# Remove the udev rules
local UDEV_RULES_PATH="/etc/udev/rules.d/11-ftdi.rules"
if [ -f "$UDEV_RULES_PATH" ]; then
echo "Removing the udev rules file..."
sudo rm "$UDEV_RULES_PATH"
fi
# Reload systemd daemon
sudo systemctl daemon-reload
# Remove the specific UFW rule for port 5001 added by this script
echo "Reverting UFW rule for port 5001 added by the script..."
sudo ufw delete allow 5001/tcp || { error_message "Failed to remove UFW rule for port 5001, please check manually."; }
# Check if UFW is active and reload it
if sudo ufw status | grep -qw "active"; then
sudo ufw reload || { error_message "Failed to reload UFW, please check manually."; }
fi
# Remove log files
if [ -f "$LOGFILE" ]; then
echo "Removing installation log file..."
sudo rm "$LOGFILE"
fi
if [ -f "/var/log/XBeeServerAPI.log" ]; then
echo "Removing application log file..."
sudo rm "/var/log/XBeeServerAPI.log"
fi
# Add here any other cleanup tasks (e.g., UFW rule removal, udev rules cleanup)
echo "Uninstallation complete."
}
# Function to validate the installation path
validate_install_path() {
if [[ ! -d "$install_dir" || ! -w "$install_dir" ]]; then
error_message "Invalid installation path: '$install_dir' is not a directory or not writable."
exit 1
fi
}
# Function for signal handling and cleanup
cleanup() {
echo -e "${RED}Caught signal, script interrupted. Cleanup initiated.${NC}"
# Notify about partial installation
echo "The script was interrupted. The system may be in a partially installed state."
echo "Please check the following:"
echo "1. The 'Software' directory in '$install_dir' may need to be manually removed if partially copied."
echo "2. Systemd service might not be fully configured. Check '/etc/systemd/system/xbeeserver.service'."
echo "3. Firewall settings and udev rules might have been partially applied."
exit 1
}
# Function for user confirmation prompt
confirm_action() {
if [ "$silent_mode" -eq 0 ]; then
read -p "Are you sure you want to proceed? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
error_message "Operation aborted by the user."
exit 1
fi
fi
}
# Main execution starts here
# Trap signals
trap cleanup SIGINT SIGTERM
# Check for required dependencies
check_dependencies
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
show_usage
exit 0
;;
-d)
install_dir="$2"
if [ -z "$install_dir" ]; then
echo "Error: -d requires an argument."
show_usage
exit 1
fi
shift 2
;;
-n)
no_root_check=1
shift
;;
-s)
silent_mode=1
shift
;;
-f)
skip_ufw_ssh=1
shift
;;
-uninstall)
uninstall # Call the uninstall function
exit 0
;;
*)
echo "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Validate installation path
validate_install_path
# User confirmation
confirm_action
# Check if the Software directory exists in the current directory
if [ ! -d "Software" ]; then
error_message "Error: Software directory not found in the current directory."
exit 1
fi
# Installation directory check
sudo mkdir -p "$install_dir"
# Copy the Software directory
if [ -d "$install_dir/Software" ]; then
info_message "Software directory already exists in $install_dir. Skipping copy."
else
info_message "Copying Software directory to $install_dir..."
sudo cp -r Software "$install_dir/"
fi
# System update
info_message "Updating the system..."
sudo apt-get update -y || { error_message "Updating failed, exiting."; exit 1; }
# Udev rules configuration
info_message "Configuring udev rules for FTDI devices..."
configure_udev_rules
sudo udevadm control --reload-rules
sudo udevadm trigger
# Adding user to the plugdev group
info_message "Adding the current user to the plugdev group..."
sudo usermod -a -G plugdev $USER || { error_message "Failed to add user to plugdev group, exiting."; exit 1; }
# Virtual environment creation
info_message "Creating a virtual environment for the XBee Server API..."
python3 -m venv "$install_dir/Software/env" || { error_message "Virtual environment creation failed, exiting."; exit 1; }
source "$install_dir/Software/env/bin/activate"
# Python packages installation
info_message "Installing required Python packages including Flask..."
pip install requests pyftdi flask || { error_message "Package installation failed, exiting."; exit 1; }
# Modify the systemd service file creation section
cat <<EOF | sudo tee $service_file
[Unit]
Description=XBee Server API Service
After=network.target
[Service]
ExecStart=$install_dir/Software/env/bin/python $install_dir/Software/app.py
Restart=on-failure
RestartSec=2
StartLimitIntervalSec=0
User=$USER
WorkingDirectory=$install_dir/Software/
Environment=PATH=$install_dir/Software/env/bin
[Install]
WantedBy=multi-user.target
EOF
# Systemd manager configuration reload
info_message "Reloading the systemd manager configuration..."
sudo systemctl daemon-reload || { error_message "Failed to reload systemd, exiting."; exit 1; }
# Enabling the service to start on boot
info_message "Enabling the XBee Server API service to start on boot..."
sudo systemctl enable xbeeserver.service || { error_message "Failed to enable service, exiting."; exit 1; }
# Configure UFW
if [ "$skip_ufw_ssh" -eq 0 ]; then
info_message "Configuring UFW - allowing SSH..."
sudo ufw allow ssh || { error_message "Failed to configure UFW for SSH, exiting."; exit 1; }
fi
info_message "Adding firewall rule for port 5001..."
sudo ufw allow 5001/tcp || { error_message "Failed to update firewall, exiting."; exit 1; }
info_message "Enabling UFW..."
sudo ufw enable || { error_message "Failed to enable UFW, exiting."; exit 1; }
# Starting the XBee Server API service
info_message "Starting the XBee Server API service..."
sudo systemctl start xbeeserver.service || { error_message "Failed to start service, check status."; sudo systemctl status xbeeserver.service; exit 1; }
# Displaying configuration URL
IP_ADDRESS=$(hostname -I | awk '{print $1}')
success_message "The XBee Server API service is now running."
info_message "You can configure the application by visiting the following URL: http://${IP_ADDRESS}:5001"
# Control instructions
info_message "To stop the XBee Server API service, run: sudo systemctl stop xbeeserver.service"
info_message "To start the XBee Server API service, run: sudo systemctl start xbeeserver.service"
info_message "To restart the XBee Server API service, run: sudo systemctl restart xbeeserver.service"
info_message "If you need to check the service status, run: sudo systemctl status xbeeserver.service"
success_message "Installation and service creation for the XBee Server API is complete!"