-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstall.sh
380 lines (335 loc) · 10.5 KB
/
install.sh
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/bin/bash
# Strict mode
set -euo pipefail
IFS=$'\n\t'
# Global constants
readonly SCRIPT_NAME="handler"
readonly INSTALL_DIR="/opt/erfjab"
readonly BRANCH="master"
readonly LOG_FILE_ADDRESS="${INSTALL_DIR}/${SCRIPT_NAME}/${SCRIPT_NAME}.log"
readonly SERVICE_NAME="${SCRIPT_NAME}"
readonly SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
readonly REPO_URL="https://github.com/erfjab/${SCRIPT_NAME}.git"
readonly SCRIPT_URL="https://raw.githubusercontent.com/erfjab/${SCRIPT_NAME}/${BRANCH}/install.sh"
# ANSI color codes
declare -r -A COLORS=(
[RED]='\033[0;31m'
[GREEN]='\033[0;32m'
[YELLOW]='\033[0;33m'
[BLUE]='\033[0;34m'
[RESET]='\033[0m'
)
# Dependency check
declare -a DEPENDENCIES=(
"git"
"curl"
"python3"
"python3-pip"
"python3-venv"
"lsb-release"
"systemctl"
)
# Logging functions
log() { printf "${COLORS[BLUE]}[INFO]${COLORS[RESET]} %s\n" "$*"; }
warn() { printf "${COLORS[YELLOW]}[WARN]${COLORS[RESET]} %s\n" "$*" >&2; }
error() { printf "${COLORS[RED]}[ERROR]${COLORS[RESET]} %s\n" "$*" >&2; exit 1; }
success() { printf "${COLORS[GREEN]}[SUCCESS]${COLORS[RESET]} %s\n" "$*"; }
# Error handling
trap 'error "An error occurred. Exiting..."' ERR
# Utility functions
check_root() {
[[ $EUID -eq 0 ]] || error "This script must be run as root"
}
# Check for missing dependencies and install them
check_dependencies() {
local missing_deps=()
for dep in "${DEPENDENCIES[@]}"; do
if ! command -v "$dep" &>/dev/null; then
missing_deps+=("$dep")
fi
done
# Ensure script runs on Ubuntu
if [[ "$(lsb_release -si)" != "Ubuntu" ]]; then
error "This script is designed for Ubuntu only."
fi
# Update package lists and install missing dependencies
if [[ ${#missing_deps[@]} -gt 0 ]]; then
log "Installing missing dependencies: ${missing_deps[*]}"
apt update && apt install -y "${missing_deps[@]}" || error "Failed to install dependencies."
else
success "All dependencies are installed."
fi
}
check_and_setup_env() {
local env_file="$INSTALL_DIR/$SCRIPT_NAME/.env"
if [[ ! -f "$env_file" ]]; then
cp $INSTALL_DIR/$SCRIPT_NAME/.env.example $env_file
nano $env_file
else
log ".env file already exists."
fi
}
is_installed() {
[[ -d "$INSTALL_DIR/$SCRIPT_NAME" && -f "$SERVICE_FILE" ]]
}
is_running() {
systemctl is-active --quiet "$SERVICE_NAME"
}
create_directories() {
local target_dir="$INSTALL_DIR/$SCRIPT_NAME"
if [[ -d "$target_dir" ]]; then
log "Existing installation directory found at $target_dir"
log "Removing old installation..."
rm -rf "$target_dir"
if [[ $? -ne 0 ]]; then
error "Failed to remove existing directory. Please check permissions and try again."
fi
success "Old installation removed successfully."
fi
log "Creating new installation directory..."
mkdir -p "$target_dir"
if [[ $? -ne 0 ]]; then
error "Failed to create directory $target_dir. Please check permissions and try again."
fi
chmod 750 "$target_dir"
if [[ $? -ne 0 ]]; then
error "Failed to set permissions on $target_dir. Please check your user permissions."
fi
success "Installation directory created successfully at $target_dir"
}
setup_venv() {
log "Setting up virtual environment..."
python3 -m venv "$INSTALL_DIR/$SCRIPT_NAME/venv"
source "$INSTALL_DIR/$SCRIPT_NAME/venv/bin/activate"
pip install --upgrade pip
[[ -f "$INSTALL_DIR/$SCRIPT_NAME/requirements.txt" ]] && pip install -r "$INSTALL_DIR/$SCRIPT_NAME/requirements.txt" || error "Requirements file not found"
success "Virtual environment set up successfully"
}
create_service() {
log "Creating systemd service..."
cat > "$SERVICE_FILE" <<EOF
[Unit]
Description=$SCRIPT_NAME Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=$INSTALL_DIR/$SCRIPT_NAME
ExecStart=$INSTALL_DIR/$SCRIPT_NAME/venv/bin/python $INSTALL_DIR/$SCRIPT_NAME/main.py
Restart=always
RestartSec=3
StandardOutput=append:$LOG_FILE_ADDRESS
StandardError=append:$LOG_FILE_ADDRESS
[Install]
WantedBy=multi-user.target
EOF
chmod 644 "$SERVICE_FILE"
systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
success "Systemd service created and enabled"
}
clone_repo() {
log "Cloning repository..."
git clone "$INSTALL_DIR/$SCRIPT_NAME" || error "Failed to clone repository"
success "Repository cloned successfully"
}
update_repo() {
log "Updating repository..."
cd "$INSTALL_DIR/$SCRIPT_NAME"
git fetch origin
git reset --hard origin/$BRANCH
success "Repository updated successfully"
}
manage_service() {
local action=$1
if ! is_installed; then
error "$SCRIPT_NAME is not installed. Please install it first."
fi
case "$action" in
start)
if is_running; then
warn "$SCRIPT_NAME is already running."
else
log "Starting $SCRIPT_NAME service..."
systemctl start "$SERVICE_NAME" || error "Failed to start $SCRIPT_NAME service"
success "$SCRIPT_NAME service started"
fi
;;
stop)
if is_running; then
log "Stopping $SCRIPT_NAME service..."
systemctl stop "$SERVICE_NAME" || error "Failed to stop $SCRIPT_NAME service"
success "$SCRIPT_NAME service stopped"
else
warn "$SCRIPT_NAME is not running."
fi
;;
restart)
log "Restarting $SCRIPT_NAME service..."
systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
systemctl restart "$SERVICE_NAME" || error "Failed to restart $SCRIPT_NAME service"
success "$SCRIPT_NAME service restarted"
;;
*)
error "Unknown action: $action"
;;
esac
}
show_status() {
if ! is_installed; then
error "$SCRIPT_NAME is not installed."
fi
systemctl status "$SERVICE_NAME"
}
show_logs() {
if ! is_installed; then
error "$SCRIPT_NAME is not installed."
return
fi
if [[ -f "$LOG_FILE_ADDRESS" ]]; then
tail -f "$LOG_FILE_ADDRESS"
else
error "Log file not found: $LOG_FILE_ADDRESS"
fi
}
uninstall_bot() {
if ! is_installed; then
error "$SCRIPT_NAME is not installed."
fi
log "Uninstalling $SCRIPT_NAME..."
manage_service "stop"
systemctl disable "$SERVICE_NAME"
rm -f "$SERVICE_FILE"
rm -rf "$INSTALL_DIR/$SCRIPT_NAME"
systemctl daemon-reload
success "$SCRIPT_NAME uninstalled successfully"
}
update_bot() {
if ! is_installed; then
error "$SCRIPT_NAME is not installed."
fi
log "Updating $SCRIPT_NAME..."
manage_service "stop"
update_repo
setup_venv
check_and_setup_env
manage_service "start"
success "$SCRIPT_NAME updated successfully"
}
install_script() {
local install_dir="/usr/local/bin"
local script_path="$install_dir/$SCRIPT_NAME"
if [[ -f "$script_path" ]]; then
warn "$SCRIPT_NAME script is already installed in $install_dir. Updating..."
sudo rm -rf $script_path
else
log "Installing $SCRIPT_NAME script in $install_dir..."
fi
curl -H "Authorization: token $GITHUB_TOKEN" -o "$script_path" "$SCRIPT_URL" || error "Failed to download the script"
chmod +x "$script_path" || error "Failed to set execute permissions on the script"
success "$SCRIPT_NAME script installed/updated successfully in $install_dir."
}
update_script() {
log "Updating $SCRIPT_NAME script..."
install_script
success "$SCRIPT_NAME script updated successfully"
}
uninstall_script() {
local install_dir="/usr/local/bin"
local script_path="$install_dir/$SCRIPT_NAME"
if [[ -f "$script_path" ]]; then
log "Uninstalling $SCRIPT_NAME script from $install_dir..."
rm -f "$script_path" || error "Failed to remove the script"
success "$SCRIPT_NAME script uninstalled successfully from $install_dir."
else
warn "$SCRIPT_NAME script is not installed in $install_dir."
fi
}
print_help() {
cat <<EOF
Usage: $SCRIPT_NAME <command>
Commands:
--install-script Install/Update the script in /usr/local/bin
--install Install and set up the handler
--start Start the handler service
--stop Stop the handler service
--restart Restart the handler service
--status Show the status of the handler service
--logs Show the handler logs
--update Update the handler from the repository
--update-script Update the script itself
--uninstall-script Uninstall the script from /usr/local/bin
--uninstall Uninstall the handler and remove all files
--help Show this help message
Examples:
$SCRIPT_NAME --install-script
$SCRIPT_NAME --install
$SCRIPT_NAME --start
$SCRIPT_NAME --update
EOF
}
main() {
check_root
if [ $# -eq 0 ]; then
print_help
exit 0
fi
while [ $# -gt 0 ]; do
case "$1" in
--install-script)
check_dependencies
install_script
;;
--uninstall-script)
uninstall_script
;;
--install)
if is_installed; then
error "$SCRIPT_NAME is installed. Please uninstall it first."
fi
check_dependencies
create_directories
clone_repo
setup_venv
check_and_setup_env
;;
--install-service)
create_service
;;
--start)
manage_service "start"
;;
--stop)
manage_service "stop"
;;
--restart)
manage_service "restart"
;;
--status)
show_status
;;
--logs)
show_logs
;;
--update)
update_bot
;;
--update-script)
update_script
;;
--uninstall)
uninstall_bot
;;
--help)
print_help
;;
*)
error "Unknown command: $1"
;;
esac
shift
done
}
# Execute main function
main "$@"