-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
install.sh
99 lines (83 loc) · 3.63 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
#!/bin/bash
# Install script for Proxmox VM Autoscale project
# Repository: https://github.com/fabriziosalmi/proxmox-vm-autoscale
# Variables
INSTALL_DIR="/usr/local/bin/vm_autoscale"
BACKUP_DIR="/etc/vm_autoscale" # New separate backup directory
REPO_URL="https://github.com/fabriziosalmi/proxmox-vm-autoscale"
SERVICE_FILE="vm_autoscale.service"
CONFIG_FILE="$INSTALL_DIR/config.yaml"
BACKUP_FILE="$BACKUP_DIR/config.yaml.backup" # Updated backup location
REQUIREMENTS_FILE="$INSTALL_DIR/requirements.txt"
PYTHON_CMD="/usr/bin/python3"
# Ensure the script is run as root
if [ "$EUID" -ne 0 ]; then
echo "ERROR: Please run this script as root."
exit 1
fi
# Create backup directory if it doesn't exist
if [ ! -d "$BACKUP_DIR" ]; then
echo "Creating backup directory..."
mkdir -p "$BACKUP_DIR" || { echo "ERROR: Failed to create backup directory"; exit 1; }
fi
# Backup existing config.yaml if it exists
if [ -f "$CONFIG_FILE" ]; then
echo "Backing up existing config.yaml to $BACKUP_FILE..."
cp "$CONFIG_FILE" "$BACKUP_FILE" || { echo "ERROR: Failed to backup config.yaml"; exit 1; }
fi
# Install necessary dependencies
echo "Installing necessary dependencies..."
apt-get update || { echo "ERROR: Failed to update package lists"; exit 1; }
apt-get install -y python3 curl bash git python3-paramiko python3-yaml python3-requests python3-cryptography \
|| { echo "ERROR: Failed to install required packages"; exit 1; }
# Clone the repository
echo "Cloning the repository..."
if [ -d "$INSTALL_DIR" ]; then
echo "Removing existing installation directory..."
rm -rf "$INSTALL_DIR" || { echo "ERROR: Failed to remove existing directory $INSTALL_DIR"; exit 1; }
fi
git clone "$REPO_URL" "$INSTALL_DIR" || { echo "ERROR: Failed to clone the repository from $REPO_URL"; exit 1; }
# Restore backup if it exists
if [ -f "$BACKUP_FILE" ]; then
echo "Restoring config.yaml from backup..."
cp "$BACKUP_FILE" "$CONFIG_FILE" || { echo "ERROR: Failed to restore config.yaml from backup"; exit 1; }
fi
# Install Python dependencies
if [ -f "$REQUIREMENTS_FILE" ]; then
echo "Installing Python dependencies..."
pip3 install -r "$REQUIREMENTS_FILE" || { echo "ERROR: Failed to install Python dependencies"; exit 1; }
else
echo "WARNING: Requirements file not found. Skipping Python dependency installation."
fi
# Set permissions
echo "Setting permissions for installation directory..."
chmod -R 755 "$INSTALL_DIR" || { echo "ERROR: Failed to set permissions on $INSTALL_DIR"; exit 1; }
chmod -R 755 "$BACKUP_DIR" || { echo "ERROR: Failed to set permissions on $BACKUP_DIR"; exit 1; }
# Create the systemd service file
echo "Creating the systemd service file..."
cat <<EOF > /etc/systemd/system/$SERVICE_FILE
[Unit]
Description=Proxmox VM Autoscale Service
After=network.target
[Service]
ExecStart=$PYTHON_CMD $INSTALL_DIR/autoscale.py
WorkingDirectory=$INSTALL_DIR
Restart=always
User=root
Environment=PYTHONUNBUFFERED=1
[Install]
WantedBy=multi-user.target
EOF
if [ $? -ne 0 ]; then
echo "ERROR: Failed to create systemd service file at /etc/systemd/system/$SERVICE_FILE"
exit 1
fi
# Reload systemd, enable the service, and ensure it's not started
echo "Reloading systemd and enabling the service..."
systemctl daemon-reload || { echo "ERROR: Failed to reload systemd"; exit 1; }
systemctl enable "$SERVICE_FILE" || { echo "ERROR: Failed to enable the service"; exit 1; }
# Post-installation instructions
echo "Installation complete. The service is enabled but not started."
echo "To start the service, use: sudo systemctl start $SERVICE_FILE"
echo "Logs can be monitored using: journalctl -u $SERVICE_FILE -f"
echo "Config backup location: $BACKUP_FILE"