Files
lxc-manage/create.py
2023-11-16 09:50:25 +01:00

129 lines
5.8 KiB
Python

import tkinter as tk
from tkinter import ttk, messagebox
from subprocess import Popen, PIPE
class LxcLauncherGUI:
def __init__(self, root):
self.root = root
self.root.title("LXC Launcher")
# Neue Variable für den Hostnamen
self.host_name_var = tk.StringVar()
# Neue Variable für die Anzahl der zu erstellenden Hosts
self.num_hosts_var = tk.StringVar()
self.num_hosts_var.set("1") # Standardwert auf 1 setzen
self.create_widgets()
def create_widgets(self):
# Label
label = ttk.Label(self.root, text="Wähle ein LXC-Image:")
label.grid(row=0, column=0, padx=10, pady=10, sticky="w")
# Auswahlmenü
images = self.get_lxc_images()
self.selected_image = tk.StringVar()
image_menu = ttk.Combobox(self.root, textvariable=self.selected_image, values=images)
image_menu.grid(row=0, column=1, padx=10, pady=10, sticky="w")
# Eingabefeld für den Hostnamen
host_label = ttk.Label(self.root, text="Host-Name:")
host_label.grid(row=1, column=0, padx=10, pady=10, sticky="w")
host_entry = ttk.Entry(self.root, textvariable=self.host_name_var)
host_entry.grid(row=1, column=1, padx=10, pady=10, sticky="w")
# Eingabefeld für die Anzahl der zu erstellenden Hosts
num_hosts_label = ttk.Label(self.root, text="Anzahl der Hosts:")
num_hosts_label.grid(row=2, column=0, padx=10, pady=10, sticky="w")
num_hosts_entry = ttk.Entry(self.root, textvariable=self.num_hosts_var)
num_hosts_entry.grid(row=2, column=1, padx=10, pady=10, sticky="w")
# Checkbox für Nesting
self.nesting_checkbox_var = tk.BooleanVar()
nesting_checkbox = ttk.Checkbutton(self.root, text="Nesting", variable=self.nesting_checkbox_var)
nesting_checkbox.grid(row=3, column=0, padx=10, pady=10, sticky="w")
# Checkbox für Protection
self.protection_checkbox_var = tk.BooleanVar()
protection_checkbox = ttk.Checkbutton(self.root, text="Protection", variable=self.protection_checkbox_var)
protection_checkbox.grid(row=3, column=1, padx=10, pady=10, sticky="w")
# Button zum Starten
launch_button = ttk.Button(self.root, text="Starten", command=self.launch_lxc)
launch_button.grid(row=4, column=0, padx=10, pady=10, sticky="w")
# Button für Weitere Hosts
hosts_button = ttk.Button(self.root, text="Weitere Hosts", command=self.show_hosts)
hosts_button.grid(row=4, column=1, padx=10, pady=10, sticky="w")
# Button zum Beenden
exit_button = ttk.Button(self.root, text="Beenden", command=self.root.destroy)
exit_button.grid(row=5, column=0, columnspan=4, padx=10, pady=10, sticky="w")
# Label für das Ergebnis
self.result_label = ttk.Label(self.root, text="")
self.result_label.grid(row=6, column=0, columnspan=4, padx=10, pady=10, sticky="w")
def show_error_message(self, message):
messagebox.showerror("Fehler", message)
def show_info_message(self, message):
messagebox.showinfo("OK", message)
def get_lxc_images(self):
command = "lxc image list images: | awk -F'|' '{ print $2}' | sed '/^[[:space:]]*$/d' | awk -F'/' '{ print $1\"/\"$2 }' | sort | uniq | grep -vE 'more|ALIAS' | grep -iE 'Debian|CentOS|Ubuntu'"
process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
output, error = process.communicate()
images = [line.strip() for line in output.decode("utf-8").split('\n') if line.strip()]
return images
def launch_lxc(self):
selected_image = self.selected_image.get()
host_name_prefix = self.host_name_var.get()
if selected_image and host_name_prefix:
num_hosts = self.num_hosts_var.get()
if not num_hosts.isdigit() or int(num_hosts) <= 0:
self.show_error_message("Bitte geben Sie eine gültige positive Ganzzahl für die Anzahl der Hosts ein.")
return
created_hosts = [] # Eine Liste, um die erstellten Hostnamen zu speichern
for i in range(1, int(num_hosts) + 1):
# Erstellen des Hostnamens mit Ordnungszahl
host_name = f"{host_name_prefix}{i}"
nesting_option = "true" if self.nesting_checkbox_var.get() else "false"
protection_option = "true" if self.protection_checkbox_var.get() else "false"
launch_command = f"lxc launch images:{selected_image} {host_name} -c security.nesting={nesting_option} -c security.protection.delete={protection_option}"
process = Popen(launch_command, shell=True, stdout=PIPE, stderr=PIPE)
output, error = process.communicate()
if error:
self.show_error_message(f"Fehler beim Starten des Containers {host_name}:\n{error.decode('utf-8')}")
else:
created_hosts.append(host_name)
if created_hosts:
self.show_info_message(f"Container erfolgreich eingerichtet:\n{', '.join(created_hosts)}")
def show_hosts(self):
# Hier kannst du die Logik für die Anzeige weiterer Hosts implementieren
# Zum Beispiel ein Popup-Fenster oder eine separate Seite im GUI
# Reset der Auswahl
self.selected_image.set("") # Zurücksetzen des Auswahlmenüs
self.host_name_var.set("") # Zurücksetzen des Hostnamen-Eingabefelds
self.num_hosts_var.set("") # Zurücksetzen des Felds für die Anzahl der Hosts
self.nesting_checkbox_var.set(False) # Zurücksetzen der Nesting-Checkbox
self.protection_checkbox_var.set(False) # Zurücksetzen der Protection-Checkbox
if __name__ == "__main__":
root = tk.Tk()
app = LxcLauncherGUI(root)
root.mainloop()