#!/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
#
#
############################
## LXC-Container erstellen #
############################
#
echo
echo -e -n "${WEISS}Erstellen des Grfana Metrikserver LXC-Containers ${NC}"
echo
sleep 2
##
## [Variablen]
##
#
NODE="stratopve"			# Name der Proxmoxnode, auf dem Apache Guacamole betrieben wird
IPSNM="10.1.0.5/24"		# Die IP des Apache Guacamole und das Subnet Eingabe Beispiel. 10.1.0.x/24
GATEWAY="10.1.0.1"		# Gateway in dem Netzwerk
VSWITCH="vmbr0"		# Linux Bridge des Apache Guacamole
ROOTPWD="05#Richter#61"		# Das root Passwort WIRD BEI DER EINGABE ANGEZEIGT !!!!!!!!
FQDN="stratometrik.webolchi.de"			# Der Name des LXC-Containers
PROMPT=""		# PROMPT Schreibeingabe
VMID="1005"			# Nummer des LXC-Contaner 
PFAD=""			# Pfad zum Template
LXC_TEMPLATE=local:vztmpl/debian-12-standard_12.2-1_amd64.tar.zst
#
##
## [Argumente auslesen]
##
#
#
echo -e "${HELLGRUEN}Das LXC_TEMPLATE ist ${WEISS}$LXC_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.!!!!!"
