#!/bin/bash
# =======================================================
# Libvirt VM CPU Exporter Deployment Script (English)
# =======================================================

# Configuration
DOWNLOAD_URL="https://download.cloudclusters.io/libvirt_vm_cpu_exporter"
BINARY_NAME="libvirt_vm_cpu_exporter"
INSTALL_PATH="/usr/bin/$BINARY_NAME"
SERVICE_NAME="libvirt-vm-cpu-exporter.service"
EXPORTER_PORT=9105

# Download binary directly to /usr/bin
echo "Downloading exporter to $INSTALL_PATH ..."
sudo wget -O "$INSTALL_PATH" "$DOWNLOAD_URL" || { echo "Download failed"; exit 1; }

# Set executable permissions
echo "Setting executable permissions..."
sudo chmod +x "$INSTALL_PATH"

# Create systemd service
echo "Creating systemd service..."
sudo tee "/etc/systemd/system/$SERVICE_NAME" > /dev/null <<EOF
[Unit]
Description=Libvirt VM CPU Prometheus Exporter
After=network.target

[Service]
Type=simple
ExecStart=$INSTALL_PATH
Restart=always
User=root
RestartSec=5s

[Install]
WantedBy=multi-user.target
EOF

# Reload systemd, start service and enable at boot
echo "Reloading systemd and starting service..."
sudo systemctl daemon-reload
sudo systemctl enable "$SERVICE_NAME"
sudo systemctl start "$SERVICE_NAME"
sudo systemctl status "$SERVICE_NAME" --no-pager

ufw allow 9105
