#!/bin/bash
### Farbdefinitionen ####
#   TEXT in Weiss       #
#   DATEIEN in Gelb     #
#   ORDNER in HellBlau  #
#########################
# Farbdefinitionen zur Ausgabe 
ROT='\033[0;31m'
HELLROT='\033[1;31m'
GRUEN='\033[0;32m'
HELLGRUEN='\033[1;32m'
GELB='\033[1;33m'
BLAU='\033[0;34m'
HELLBLAU='\033[1;34m'
LILA='\033[0;35m'
HELLLILA='\033[1;35m'
DUNKELESTUERKIS='\033[0;36m'
TUERKIS='\033[1;36m'
HELLGRAU='\033[0;37m'
WEISS='\033[1;37m'
NC='\033[0m' # No Color
#
#
########################################
# Das zu installierende Template holen #
########################################
#
echo
echo -e -n "${WEISS}Download der Datei:${GELB}debian-12-standard_12.2-1_amd64.tar.zst ${WEISS}und Verschieben in den Ordner ${HELLBLAU}/var/lib/vz/template/cache ${NC}"
echo
echo
wget http://download.proxmox.com/images/system/debian-12-standard_12.2-1_amd64.tar.zst && mv debian-12-standard_12.2-1_amd64.tar.zst /var/lib/vz/template/cache
echo
echo -e -n "${WEISS}Dowmload des Templates und Verschieben fertig ${NC}"
echo
sleep 2
#
############################
## LXC-Container erstellen #
############################
#
echo
echo -e -n "${WEISS}Erstellen des Grfana Metrikserver LXC-Containers ${NC}"
echo
sleep 2
##
## [Variablen]
##
#
NODE=""			# Name der Proxmoxnode, auf dem Apache Guacamole betrieben wird
LXC_TEMPLATE=""		# Pfad zum LCX Template auf dem o.g. Proxmox
IPSNM=""		# Die IP des Apache Guacamole und das Subnet Eingabe Beispiel. 10.1.0.x/24
GATEWAY=""		# Gateway in dem Netzwerk
VSWITCH=""		# Linux Bridge des Apache Guacamole
ROOTPWD=""		# Das root Passwort WIRD BEI DER EINGABE ANGEZEIGT !!!!!!!!
FQDN=""			# Der Name des LXC-Containers
PROMPT=""		# PROMPT Schreibeingabe
VMID=""			# Nummer des LXC-Contaner 
PFAD=""			# Pfad zum Template
TEMPLATE=:vztmpl/debian-12-standard_12.2-1_amd64.tar.zst
#
##
## [Argumente auslesen]
##
#
clear
if [ -z "${NODE}" ]; then
    # 1.Frage: Die Proxmox Note angeben
      read -p "Die Notename angeben z.B. pve, stratopve oder h3014859 : " NODE
fi
if [ -z "${PFAD}" ]; then
    # 2.Frage: Die Proxmox Note angeben
      read -p "Speicherort des Templates z.B. local: " PFAD
fi
if [ -z "${VMID}" ]; then
    # 3.Frage: LXC-Container VIMD Nummer
        read -p "Enter LXC-Container VIMD Nummer: " VMID
fi
if [ -z "${FQDN}" ]; then
    # 4.Frage: LXC-Container Name
        read -p "Enter LXC-Container Name: " FQDN
fi
if [ -z "${VSWITCH}" ]; then
    # 5.Frage: LXC-Container IP/Submask
        read -p "Enter LXC-Container Linux Bridge z.B. vmbr0: " VSWITCH
fi
if [ -z "${IPSNM}" ]; then
    # 6.Frage: LXC-Container IP/Submask
        read -p "Enter LXC-Container IP/Submask z.B. 10.1.0.x/24: " IPSNM
fi
if [ -z "${GATEWAY}" ]; then
    # 7.Frage: LXC-Container IP/Submask
        read -p "Enter LXC-Container GATEWAY z.B. 10.1.0.1: " GATEWAY
fi
if [ -z "${ROOTPWD}" ]; then
    # 8.Frage Das "Root" Password
      read -s -p "ACHTUNG root Passwort wird nicht ANGEZEIGT!!: " ROOTPWD
fi
echo -e ""
#
LXC_TEMPLATE=$PFAD$TEMPLATE
#
echo -e "${HELLGRUEN}Das LXC_TEMPLATE ist ${WEISS}$PFAD$TEMPLATE "
echo -e "${HELLGRUEN}Der Notename ist ${WEISS}$NODE "
echo -e "${HELLGRUEN}Der Pfad ist ${WEISS}$PFAD "
echo -e "${HELLGRUEN}Die VMID ist ${WEISS}$VMID "
echo -e "${HELLGRUEN}Der Namen ist ${WEISS}$FQDN "
echo -e "${HELLGRUEN}Die Linux Bridge ist ${WEISS}$VSWITCH "
echo -e "${HELLGRUEN}Die IP4/Submask ist ${WEISS}$IPSNM "
echo -e "${HELLGRUEN}Der GATEWAY ist ${WEISS}$GATEWAY "
echo -e -n "${NC}"
echo
sleep 2
clear
#
##
## [LCX-Container]
##
#
# Ggf. Koennen hier noch Anpassungen zum verwendeten Storage, Speicher, CPU etc. gemacht werden.
#
pct create $VMID \
$LXC_TEMPLATE \
--hostname $FQDN \
--memory 512 \
--cores 1 \
--net0 name=eth0,bridge=$VSWITCH,firewall=1,gw=$GATEWAY,ip=$IPSNM \
--storage local \
--rootfs local:2 \
--unprivileged 1 \
--ignore-unpack-errors  \
--ostype debian \
--password=$ROOTPWD \
--start 1
sleep 20
clear
##
## [SoftwareDownload]
##
pct exec $VMID -- tee -a /etc/locale.gen <<EOF
de_DE.UTF-8 UTF-8
EOF
pct exec $VMID -- locale-gen
# Zur Sicherheit die Variablen der Locales nochmal temporoer in das Proxmoxterminal setzen.
export LANGUAGE=de_DE.UTF-8
export LANG=de_DE.UTF-8
export LC_ALL=de_DE.UTF-8
pct exec $VMID -- locale
pct exec $VMID -- apt update && apt upgrade -y
pct exec $VMID -- apt autoremove -y
sleep 2
clear
echo
echo -e "${WEISS}Instaliere folgende Pakete ${GELB}net-tools ssh mc gnupg2 wget curl ${NC}"
echo
sleep 2
pct exec $VMID -- apt install net-tools ssh mc gnupg2 wget curl -y
pct exec $VMID -- wget http://web.webolch.de/bauanleitung/downloads/linux/metrikserver.webolch.de/influxdb.sh
pct exec $VMID -- bash influxdb.sh
sleep 2
clear
pct exec $VMID -- wget http://web.webolch.de/bauanleitung/downloads/linux/metrikserver.webolch.de/grafana.sh
pct exec $VMID -- bash grafana.sh
pct exec $VMID -- wget http://web.webolch.de/bauanleitung/downloads/linux/zip/metrikserver.zip
pct exec $VMID -- unzip metrikserver.zip 
pct exec $VMID -- mv influxdb.conf /etc/influxdb
pct exec $VMID -- mv grafana.ini /etc/grafana
pct exec $VMID -- mv privkey.pem /etc/grafana
pct exec $VMID -- mv cert.pem /etc/grafana
sleep 2
clear
pct exec $VMID -- mv sshd_config /etc/ssh/
pct exec $VMID -- systemctl restart ssh
clear
pct exec $VMID -- systemctl status influxdb --no-pager
pct exec $VMID -- systemctl daemon-reload
pct exec $VMID -- systemctl enable grafana-server
pct exec $VMID -- systemctl start grafana-server
clear
systemctl status grafana-server --no-pager
sleep 2
pct exec $VMID -- systemctl status ssh --no-pager
echo -e "${TUERKIS} !!!!! Die Installation ist jetzt abgeschlossen.!!!!!"
