forked from fabriziosalmi/proxmox-lxc-autoscale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
142 lines (123 loc) · 4.21 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
#!/bin/bash
# Variables
SCRIPT_URL="https://raw.githubusercontent.com/fabriziosalmi/proxmox-lxc-autoscale/main/usr/local/bin/lxc_autoscale.py"
SERVICE_URL="https://raw.githubusercontent.com/fabriziosalmi/proxmox-lxc-autoscale/main/etc/systemd/system/lxc_autoscale.service"
CONF_URL="https://raw.githubusercontent.com/fabriziosalmi/proxmox-lxc-autoscale/main/etc/lxc_autoscale/lxc_autoscale.conf"
INSTALL_PATH="/usr/local/bin/lxc_autoscale.py"
SERVICE_PATH="/etc/systemd/system/lxc_autoscale.service"
CONF_DIR="/etc/lxc_autoscale"
CONF_PATH="${CONF_DIR}/lxc_autoscale.conf"
LOG_PATH="/var/log/lxc_autoscale.log"
BACKUP_DIR="/var/lib/lxc_autoscale/backups"
# Function to check and stop the service if running
stop_service_if_running() {
if systemctl is-active --quiet lxc_autoscale.service; then
echo "🛑 Stopping LXC AutoScale service..."
systemctl stop lxc_autoscale.service
if [ $? -ne 0 ]; then
echo "❌ Error: Failed to stop the service."
exit 1
fi
fi
}
# Function to start the service
start_service() {
echo "🚀 Starting the LXC AutoScale service..."
systemctl start lxc_autoscale.service
if [ $? -ne 0 ]; then
echo "❌ Error: Failed to start the service."
exit 1
fi
}
# Function to enable the service
enable_service() {
echo "🔧 Enabling the LXC AutoScale service..."
systemctl enable lxc_autoscale.service
if [ $? -ne 0 ]; then
echo "❌ Error: Failed to enable the service."
exit 1
fi
}
# Function to backup existing configuration file
backup_existing_conf() {
if [ -f "$CONF_PATH" ]; then
timestamp=$(date +"%Y%m%d-%H%M%S")
backup_conf="${CONF_PATH}.${timestamp}.backup"
echo "💾 Backing up existing configuration file to $backup_conf..."
cp "$CONF_PATH" "$backup_conf"
if [ $? -ne 0 ]; then
echo "❌ Error: Failed to backup the existing configuration file."
exit 1
fi
fi
}
# Function to prompt user for overwriting the configuration file
prompt_overwrite_conf() {
if [ -f "$CONF_PATH" ]; then
read -p "⚠️ A configuration file already exists at $CONF_PATH. Do you want to overwrite it? [y/N]: " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "🚫 Keeping the existing configuration file."
return 1
fi
fi
return 0
}
# Stop the service if it's already running
stop_service_if_running
# Download the Python script
echo "📥 Downloading the LXC AutoScale script..."
curl -sSL -o $INSTALL_PATH $SCRIPT_URL
if [ $? -ne 0 ]; then
echo "❌ Error: Failed to download the script."
exit 1
fi
# Make the script executable
chmod +x $INSTALL_PATH
# Download the systemd service file
echo "📥 Downloading the systemd service file..."
curl -sSL -o $SERVICE_PATH $SERVICE_URL
if [ $? -ne 0 ]; then
echo "❌ Error: Failed to download the service file."
exit 1
fi
# Set up the configuration directory and file, with backup if needed
echo "📂 Setting up configuration directory and file..."
mkdir -p $CONF_DIR
if prompt_overwrite_conf; then
backup_existing_conf
curl -sSL -o $CONF_PATH $CONF_URL
if [ $? -ne 0 ]; then
echo "❌ Error: Failed to download the configuration file."
exit 1
fi
fi
# Set up directories for logs and backups
echo "📂 Setting up directories..."
mkdir -p $(dirname $LOG_PATH)
mkdir -p $BACKUP_DIR
# Create the log file if it doesn't exist
touch $LOG_PATH
# Set the correct permissions
echo "🔧 Setting permissions..."
chown root:root $LOG_PATH
chown -R root:root $BACKUP_DIR
chmod 755 $BACKUP_DIR
chmod 644 $LOG_PATH
# Reload systemd to recognize the new service
echo "🔄 Reloading systemd daemon..."
systemctl daemon-reload
# Enable and start the LXC AutoScale service
enable_service
start_service
# Check the status of the service
echo "🔍 Checking service status..."
systemctl status lxc_autoscale.service --no-pager
# Verify that the service is running
if systemctl is-active --quiet lxc_autoscale.service; then
echo "✅ LXC AutoScale service is running successfully."
else
echo "❌ Error: LXC AutoScale service failed to start."
exit 1
fi
echo "🎉 Installation and setup completed successfully."