280 lines
9.3 KiB
Bash
280 lines
9.3 KiB
Bash
#!/bin/bash
|
|
|
|
# MYP Installer für Linux/Unix-Systeme
|
|
# Dieses Skript wurde in das MYP Installer Control Center integriert
|
|
# und dient als Kompatibilitätslayer für Linux/Unix-Systeme
|
|
|
|
# Farbdefinitionen
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[0;33m'
|
|
BLUE='\033[0;34m'
|
|
CYAN='\033[0;36m'
|
|
NC='\033[0m' # No Color
|
|
|
|
echo -e "${CYAN}=============================================================${NC}"
|
|
echo -e "${CYAN} MYP INSTALLER (UNIX/LINUX) ${NC}"
|
|
echo -e "${CYAN}=============================================================${NC}"
|
|
echo -e "${YELLOW}Dieses Skript ist ein Kompatibilitätslayer für Linux/Unix.${NC}"
|
|
echo -e "${YELLOW}Es wird empfohlen, das PowerShell-Skript myp_installer.ps1${NC}"
|
|
echo -e "${YELLOW}für eine vollständige Funktionalität zu verwenden. ${NC}"
|
|
echo -e "${CYAN}=============================================================${NC}"
|
|
echo ""
|
|
|
|
# Überprüfen, ob das Skript als Root ausgeführt wird
|
|
if [ "$EUID" -ne 0 ]; then
|
|
echo -e "${YELLOW}Hinweis: Dieses Skript läuft ohne Root-Rechte.${NC}"
|
|
echo -e "${YELLOW}Einige Funktionen sind möglicherweise eingeschränkt.${NC}"
|
|
echo ""
|
|
fi
|
|
|
|
# Hauptmenu anzeigen
|
|
echo "Wählen Sie eine Option:"
|
|
echo "1. SSL-Zertifikate erstellen"
|
|
echo "2. Hosts-Konfiguration einrichten"
|
|
echo "3. Abhängigkeiten installieren"
|
|
echo "4. Anwendung starten"
|
|
echo "5. Beenden"
|
|
echo ""
|
|
read -p "Option (1-5): " choice
|
|
|
|
case $choice in
|
|
1)
|
|
echo -e "${BLUE}SSL-Zertifikate werden erstellt...${NC}"
|
|
|
|
# Verzeichnis erstellen
|
|
mkdir -p backend/instance/ssl
|
|
|
|
# Python-Skript zur Zertifikatserstellung erstellen
|
|
cat > temp_cert_script.py << 'EOL'
|
|
#!/usr/bin/env python3
|
|
import os
|
|
import datetime
|
|
import sys
|
|
|
|
try:
|
|
from cryptography import x509
|
|
from cryptography.x509.oid import NameOID
|
|
from cryptography.hazmat.primitives import hashes
|
|
from cryptography.hazmat.primitives.asymmetric import rsa
|
|
from cryptography.hazmat.primitives.serialization import Encoding, PrivateFormat, NoEncryption
|
|
except ImportError:
|
|
print("Fehler: Paket 'cryptography' nicht gefunden.")
|
|
print("Bitte installieren Sie es mit: pip install cryptography")
|
|
sys.exit(1)
|
|
|
|
def create_self_signed_cert(cert_path, key_path, hostname="localhost"):
|
|
# Verzeichnis erstellen, falls es nicht existiert
|
|
cert_dir = os.path.dirname(cert_path)
|
|
if cert_dir and not os.path.exists(cert_dir):
|
|
os.makedirs(cert_dir, exist_ok=True)
|
|
|
|
# Privaten Schlüssel generieren
|
|
private_key = rsa.generate_private_key(
|
|
public_exponent=65537,
|
|
key_size=4096,
|
|
)
|
|
|
|
# Schlüsseldatei schreiben
|
|
with open(key_path, "wb") as key_file:
|
|
key_file.write(private_key.private_bytes(
|
|
encoding=Encoding.PEM,
|
|
format=PrivateFormat.TraditionalOpenSSL,
|
|
encryption_algorithm=NoEncryption()
|
|
))
|
|
|
|
# Aktuelles Datum und Ablaufdatum berechnen
|
|
now = datetime.datetime.now()
|
|
valid_until = now + datetime.timedelta(days=3650) # 10 Jahre gültig
|
|
|
|
# Name für das Zertifikat erstellen
|
|
subject = issuer = x509.Name([
|
|
x509.NameAttribute(NameOID.COMMON_NAME, hostname),
|
|
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Mercedes-Benz AG"),
|
|
x509.NameAttribute(NameOID.ORGANIZATIONAL_UNIT_NAME, "Werk 040 Berlin"),
|
|
x509.NameAttribute(NameOID.COUNTRY_NAME, "DE"),
|
|
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "Berlin"),
|
|
x509.NameAttribute(NameOID.LOCALITY_NAME, "Berlin")
|
|
])
|
|
|
|
# Zertifikat erstellen
|
|
cert = x509.CertificateBuilder().subject_name(
|
|
subject
|
|
).issuer_name(
|
|
issuer
|
|
).public_key(
|
|
private_key.public_key()
|
|
).serial_number(
|
|
x509.random_serial_number()
|
|
).not_valid_before(
|
|
now
|
|
).not_valid_after(
|
|
valid_until
|
|
).add_extension(
|
|
x509.SubjectAlternativeName([
|
|
x509.DNSName(hostname),
|
|
x509.DNSName("localhost")
|
|
]),
|
|
critical=False,
|
|
).add_extension(
|
|
x509.BasicConstraints(ca=True, path_length=None), critical=True
|
|
).add_extension(
|
|
x509.KeyUsage(
|
|
digital_signature=True,
|
|
content_commitment=False,
|
|
key_encipherment=True,
|
|
data_encipherment=False,
|
|
key_agreement=False,
|
|
key_cert_sign=True,
|
|
crl_sign=True,
|
|
encipher_only=False,
|
|
decipher_only=False
|
|
), critical=True
|
|
).add_extension(
|
|
x509.ExtendedKeyUsage([
|
|
x509.oid.ExtendedKeyUsageOID.SERVER_AUTH,
|
|
x509.oid.ExtendedKeyUsageOID.CLIENT_AUTH
|
|
]), critical=False
|
|
).sign(private_key, hashes.SHA256())
|
|
|
|
# Zertifikatsdatei schreiben
|
|
with open(cert_path, "wb") as cert_file:
|
|
cert_file.write(cert.public_bytes(Encoding.PEM))
|
|
|
|
print(f"Selbstsigniertes SSL-Zertifikat für '{hostname}' erstellt:")
|
|
print(f"Zertifikat: {cert_path}")
|
|
print(f"Schlüssel: {key_path}")
|
|
print(f"Gültig für 10 Jahre.")
|
|
|
|
# Hostname wählen
|
|
print("Wählen Sie den Hostnamen für das Zertifikat:")
|
|
print("1. localhost (für lokale Entwicklung)")
|
|
print("2. raspberrypi (für Raspberry Pi)")
|
|
print("3. m040tbaraspi001.de040.corpintra.net (für Unternehmens-Setup)")
|
|
choice = input("Option (1-3, Standard: 1): ")
|
|
|
|
hostname = "localhost"
|
|
if choice == "2":
|
|
hostname = "raspberrypi"
|
|
elif choice == "3":
|
|
hostname = "m040tbaraspi001.de040.corpintra.net"
|
|
|
|
# Zertifikate erstellen
|
|
create_self_signed_cert("backend/instance/ssl/myp.crt", "backend/instance/ssl/myp.key", hostname)
|
|
create_self_signed_cert("backend/instance/ssl/frontend.crt", "backend/instance/ssl/frontend.key", hostname)
|
|
EOL
|
|
|
|
# Python-Skript ausführbar machen und ausführen
|
|
chmod +x temp_cert_script.py
|
|
python3 temp_cert_script.py || python temp_cert_script.py
|
|
|
|
# Temporäres Skript löschen
|
|
rm temp_cert_script.py
|
|
|
|
echo -e "${GREEN}SSL-Zertifikate erstellt.${NC}"
|
|
;;
|
|
|
|
2)
|
|
echo -e "${BLUE}Hosts-Konfiguration wird eingerichtet...${NC}"
|
|
|
|
# Lokale IP-Adresse ermitteln
|
|
LOCAL_IP=$(hostname -I | awk '{print $1}')
|
|
if [ -z "$LOCAL_IP" ]; then
|
|
LOCAL_IP="127.0.0.1"
|
|
fi
|
|
|
|
echo "Lokale IP-Adresse: $LOCAL_IP"
|
|
|
|
# Hosts-Datei bearbeiten
|
|
if [ "$EUID" -eq 0 ]; then
|
|
# Check if entries already exist
|
|
if ! grep -q "m040tbaraspi001.de040.corpintra.net" /etc/hosts; then
|
|
echo "" >> /etc/hosts
|
|
echo "# MYP Frontend Host" >> /etc/hosts
|
|
echo "$LOCAL_IP m040tbaraspi001.de040.corpintra.net m040tbaraspi001" >> /etc/hosts
|
|
echo -e "${GREEN}Frontend-Hostname hinzugefügt${NC}"
|
|
else
|
|
echo -e "${YELLOW}Frontend-Hostname ist bereits konfiguriert${NC}"
|
|
fi
|
|
|
|
if ! grep -q "raspberrypi" /etc/hosts; then
|
|
echo "" >> /etc/hosts
|
|
echo "# MYP Backend Host" >> /etc/hosts
|
|
echo "$LOCAL_IP raspberrypi" >> /etc/hosts
|
|
echo -e "${GREEN}Backend-Hostname hinzugefügt${NC}"
|
|
else
|
|
echo -e "${YELLOW}Backend-Hostname ist bereits konfiguriert${NC}"
|
|
fi
|
|
else
|
|
echo -e "${RED}Root-Rechte erforderlich, um die Hosts-Datei zu bearbeiten.${NC}"
|
|
echo -e "${YELLOW}Bitte führen Sie die folgenden Befehle manuell als Root aus:${NC}"
|
|
echo "echo \"\" >> /etc/hosts"
|
|
echo "echo \"# MYP Frontend Host\" >> /etc/hosts"
|
|
echo "echo \"$LOCAL_IP m040tbaraspi001.de040.corpintra.net m040tbaraspi001\" >> /etc/hosts"
|
|
echo "echo \"\" >> /etc/hosts"
|
|
echo "echo \"# MYP Backend Host\" >> /etc/hosts"
|
|
echo "echo \"$LOCAL_IP raspberrypi\" >> /etc/hosts"
|
|
fi
|
|
;;
|
|
|
|
3)
|
|
echo -e "${BLUE}Abhängigkeiten werden installiert...${NC}"
|
|
|
|
# Python-Abhängigkeiten
|
|
echo "Installiere Python-Abhängigkeiten..."
|
|
pip3 install -r backend/requirements.txt || pip install -r backend/requirements.txt
|
|
|
|
# Frontend-Abhängigkeiten
|
|
if command -v npm &> /dev/null; then
|
|
echo "Installiere Frontend-Abhängigkeiten..."
|
|
(cd frontend && npm install)
|
|
else
|
|
echo -e "${YELLOW}npm nicht gefunden. Frontend-Abhängigkeiten werden übersprungen.${NC}"
|
|
fi
|
|
|
|
echo -e "${GREEN}Abhängigkeiten installiert.${NC}"
|
|
;;
|
|
|
|
4)
|
|
echo -e "${BLUE}Anwendung wird gestartet...${NC}"
|
|
echo "Wie möchten Sie die Anwendung starten?"
|
|
echo "1. Backend-Server starten (Python)"
|
|
echo "2. Frontend-Server starten (npm)"
|
|
echo "3. Mit Docker Compose starten"
|
|
read -p "Option (1-3): " start_choice
|
|
|
|
case $start_choice in
|
|
1)
|
|
echo "Starte Backend-Server..."
|
|
python3 backend/app/app.py || python backend/app/app.py &
|
|
echo -e "${GREEN}Backend-Server läuft im Hintergrund.${NC}"
|
|
;;
|
|
2)
|
|
echo "Starte Frontend-Server..."
|
|
(cd frontend && npm run dev) &
|
|
echo -e "${GREEN}Frontend-Server läuft im Hintergrund.${NC}"
|
|
;;
|
|
3)
|
|
echo "Starte Anwendung mit Docker Compose..."
|
|
docker-compose up -d
|
|
echo -e "${GREEN}Docker Container wurden gestartet.${NC}"
|
|
;;
|
|
*)
|
|
echo -e "${RED}Ungültige Option.${NC}"
|
|
;;
|
|
esac
|
|
;;
|
|
|
|
5)
|
|
echo -e "${GREEN}Installation wird beendet.${NC}"
|
|
exit 0
|
|
;;
|
|
|
|
*)
|
|
echo -e "${RED}Ungültige Option.${NC}"
|
|
;;
|
|
esac
|
|
|
|
echo ""
|
|
echo -e "${CYAN}Installation abgeschlossen. Drücken Sie ENTER, um fortzufahren...${NC}"
|
|
read |