2019-11-20 18:59:30 +00:00
|
|
|
import logging
|
2019-11-03 06:47:43 +00:00
|
|
|
import tkinter as tk
|
2019-11-20 18:59:30 +00:00
|
|
|
from functools import partial
|
2019-11-03 06:47:43 +00:00
|
|
|
from tkinter import ttk
|
|
|
|
|
|
|
|
from coretk.dialogs.dialog import Dialog
|
2019-11-07 19:33:40 +00:00
|
|
|
from coretk.dialogs.icondialog import IconDialog
|
2019-11-11 18:57:26 +00:00
|
|
|
from coretk.dialogs.nodeservice import NodeService
|
2019-11-20 19:20:08 +00:00
|
|
|
from coretk.nodeutils import NodeUtils
|
2019-11-20 18:59:30 +00:00
|
|
|
from coretk.widgets import FrameScroll
|
2019-11-03 06:47:43 +00:00
|
|
|
|
2019-11-16 16:54:15 +00:00
|
|
|
PAD = 5
|
2019-11-16 07:45:01 +00:00
|
|
|
|
2019-11-03 06:47:43 +00:00
|
|
|
|
2019-11-20 18:59:30 +00:00
|
|
|
def mac_auto(is_auto, entry):
|
|
|
|
logging.info("mac auto clicked")
|
|
|
|
if is_auto.get():
|
|
|
|
logging.info("disabling mac")
|
|
|
|
entry.var.set("")
|
|
|
|
entry.config(state=tk.DISABLED)
|
|
|
|
else:
|
|
|
|
entry.var.set("00:00:00:00:00:00")
|
|
|
|
entry.config(state=tk.NORMAL)
|
|
|
|
|
|
|
|
|
|
|
|
class InterfaceData:
|
|
|
|
def __init__(self, is_auto, mac, ip4, ip6):
|
|
|
|
self.is_auto = is_auto
|
|
|
|
self.mac = mac
|
|
|
|
self.ip4 = ip4
|
|
|
|
self.ip6 = ip6
|
|
|
|
|
|
|
|
|
2019-11-03 06:47:43 +00:00
|
|
|
class NodeConfigDialog(Dialog):
|
|
|
|
def __init__(self, master, app, canvas_node):
|
|
|
|
"""
|
|
|
|
create an instance of node configuration
|
|
|
|
|
|
|
|
:param master: dialog master
|
|
|
|
:param coretk.app.Application: main app
|
|
|
|
:param coretk.graph.CanvasNode canvas_node: canvas node object
|
|
|
|
"""
|
2019-11-20 18:59:30 +00:00
|
|
|
super().__init__(
|
|
|
|
master, app, f"{canvas_node.core_node.name} Configuration", modal=True
|
|
|
|
)
|
2019-11-03 06:47:43 +00:00
|
|
|
self.canvas_node = canvas_node
|
2019-11-20 18:59:30 +00:00
|
|
|
self.node = canvas_node.core_node
|
2019-11-03 06:47:43 +00:00
|
|
|
self.image = canvas_node.image
|
|
|
|
self.image_button = None
|
2019-11-20 18:59:30 +00:00
|
|
|
self.name = tk.StringVar(value=self.node.name)
|
|
|
|
self.type = tk.StringVar(value=self.node.model)
|
2019-11-20 19:20:08 +00:00
|
|
|
self.container_image = tk.StringVar(value=self.node.image)
|
2019-11-20 18:59:30 +00:00
|
|
|
server = "localhost"
|
|
|
|
if self.node.server:
|
|
|
|
server = self.node.server
|
|
|
|
self.server = tk.StringVar(value=server)
|
|
|
|
self.interfaces = {}
|
2019-11-03 06:47:43 +00:00
|
|
|
self.draw()
|
|
|
|
|
|
|
|
def draw(self):
|
2019-11-13 18:45:43 +00:00
|
|
|
self.top.columnconfigure(0, weight=1)
|
2019-11-16 16:54:15 +00:00
|
|
|
row = 0
|
2019-11-03 06:47:43 +00:00
|
|
|
|
2019-11-16 16:54:15 +00:00
|
|
|
# field frame
|
2019-11-13 18:45:43 +00:00
|
|
|
frame = ttk.Frame(self.top)
|
2019-11-16 16:54:15 +00:00
|
|
|
frame.grid(sticky="ew")
|
2019-11-03 06:47:43 +00:00
|
|
|
frame.columnconfigure(1, weight=1)
|
|
|
|
|
2019-11-16 16:54:15 +00:00
|
|
|
# name field
|
|
|
|
label = ttk.Label(frame, text="Name")
|
|
|
|
label.grid(row=row, column=0, sticky="ew", padx=PAD, pady=PAD)
|
2019-11-12 20:13:53 +00:00
|
|
|
entry = ttk.Entry(frame, textvariable=self.name)
|
2019-11-16 16:54:15 +00:00
|
|
|
entry.grid(row=row, column=1, sticky="ew")
|
|
|
|
row += 1
|
|
|
|
|
|
|
|
# icon field
|
|
|
|
label = ttk.Label(frame, text="Icon")
|
|
|
|
label.grid(row=row, column=0, sticky="ew", padx=PAD, pady=PAD)
|
|
|
|
self.image_button = ttk.Button(
|
|
|
|
frame,
|
|
|
|
text="Icon",
|
|
|
|
image=self.image,
|
|
|
|
compound=tk.NONE,
|
|
|
|
command=self.click_icon,
|
|
|
|
)
|
|
|
|
self.image_button.grid(row=row, column=1, sticky="ew")
|
|
|
|
row += 1
|
2019-11-03 06:47:43 +00:00
|
|
|
|
2019-11-16 16:54:15 +00:00
|
|
|
# node type field
|
2019-11-20 19:20:08 +00:00
|
|
|
if NodeUtils.is_model_node(self.node.type):
|
|
|
|
label = ttk.Label(frame, text="Type")
|
|
|
|
label.grid(row=row, column=0, sticky="ew", padx=PAD, pady=PAD)
|
|
|
|
combobox = ttk.Combobox(
|
|
|
|
frame,
|
|
|
|
textvariable=self.type,
|
|
|
|
values=list(NodeUtils.NODE_MODELS),
|
|
|
|
state="readonly",
|
|
|
|
)
|
|
|
|
combobox.grid(row=row, column=1, sticky="ew")
|
|
|
|
row += 1
|
|
|
|
|
|
|
|
# container image field
|
|
|
|
if NodeUtils.is_image_node(self.node.type):
|
|
|
|
label = ttk.Label(frame, text="Image")
|
|
|
|
label.grid(row=row, column=0, sticky="ew", padx=PAD, pady=PAD)
|
|
|
|
entry = ttk.Entry(frame, textvariable=self.container_image)
|
|
|
|
entry.grid(row=row, column=1, sticky="ew")
|
|
|
|
row += 1
|
2019-11-03 06:47:43 +00:00
|
|
|
|
2019-11-20 19:20:08 +00:00
|
|
|
if NodeUtils.is_container_node(self.node.type):
|
2019-11-21 07:16:04 +00:00
|
|
|
# server
|
2019-11-20 19:20:08 +00:00
|
|
|
frame.grid(sticky="ew")
|
|
|
|
frame.columnconfigure(1, weight=1)
|
|
|
|
label = ttk.Label(frame, text="Server")
|
|
|
|
label.grid(row=row, column=0, sticky="ew", padx=PAD, pady=PAD)
|
|
|
|
servers = ["localhost"]
|
|
|
|
servers.extend(list(sorted(self.app.core.servers.keys())))
|
|
|
|
combobox = ttk.Combobox(
|
|
|
|
frame, textvariable=self.server, values=servers, state="readonly"
|
|
|
|
)
|
|
|
|
combobox.grid(row=row, column=1, sticky="ew")
|
|
|
|
row += 1
|
2019-11-03 06:47:43 +00:00
|
|
|
|
2019-11-21 07:16:04 +00:00
|
|
|
# services
|
|
|
|
button = ttk.Button(self.top, text="Services", command=self.click_services)
|
|
|
|
button.grid(sticky="ew", pady=PAD)
|
2019-11-03 06:47:43 +00:00
|
|
|
|
2019-11-20 18:59:30 +00:00
|
|
|
# interfaces
|
|
|
|
if self.canvas_node.interfaces:
|
|
|
|
self.draw_interfaces()
|
|
|
|
|
2019-11-16 16:54:15 +00:00
|
|
|
self.draw_buttons()
|
2019-11-03 06:47:43 +00:00
|
|
|
|
2019-11-20 18:59:30 +00:00
|
|
|
def draw_interfaces(self):
|
|
|
|
scroll = FrameScroll(self.top, self.app, text="Interfaces")
|
|
|
|
scroll.grid(sticky="nsew")
|
|
|
|
scroll.frame.columnconfigure(0, weight=1)
|
|
|
|
scroll.frame.rowconfigure(0, weight=1)
|
|
|
|
for interface in self.canvas_node.interfaces:
|
|
|
|
logging.info("interface: %s", interface)
|
|
|
|
frame = ttk.LabelFrame(scroll.frame, text=interface.name, padding=PAD)
|
|
|
|
frame.grid(sticky="ew", pady=PAD)
|
|
|
|
frame.columnconfigure(1, weight=1)
|
|
|
|
frame.columnconfigure(2, weight=1)
|
|
|
|
|
|
|
|
label = ttk.Label(frame, text="MAC")
|
|
|
|
label.grid(row=0, column=0, padx=PAD, pady=PAD)
|
|
|
|
is_auto = tk.BooleanVar(value=True)
|
|
|
|
checkbutton = ttk.Checkbutton(frame, text="Auto?", variable=is_auto)
|
|
|
|
checkbutton.var = is_auto
|
|
|
|
checkbutton.grid(row=0, column=1, padx=PAD)
|
|
|
|
mac = tk.StringVar(value=interface.mac)
|
|
|
|
entry = ttk.Entry(frame, textvariable=mac, state=tk.DISABLED)
|
|
|
|
entry.grid(row=0, column=2, sticky="ew")
|
|
|
|
func = partial(mac_auto, is_auto, entry)
|
|
|
|
checkbutton.config(command=func)
|
|
|
|
|
|
|
|
label = ttk.Label(frame, text="IPv4")
|
|
|
|
label.grid(row=1, column=0, padx=PAD, pady=PAD)
|
|
|
|
ip4 = tk.StringVar(value=f"{interface.ip4}/{interface.ip4mask}")
|
|
|
|
entry = ttk.Entry(frame, textvariable=ip4)
|
|
|
|
entry.grid(row=1, column=1, columnspan=2, sticky="ew")
|
|
|
|
|
|
|
|
label = ttk.Label(frame, text="IPv6")
|
|
|
|
label.grid(row=2, column=0, padx=PAD, pady=PAD)
|
|
|
|
ip6 = tk.StringVar(value=f"{interface.ip6}/{interface.ip6mask}")
|
|
|
|
entry = ttk.Entry(frame, textvariable=ip6)
|
|
|
|
entry.grid(row=2, column=1, columnspan=2, sticky="ew")
|
|
|
|
|
|
|
|
self.interfaces[interface.id] = InterfaceData(is_auto, mac, ip4, ip6)
|
|
|
|
|
2019-11-16 16:54:15 +00:00
|
|
|
def draw_buttons(self):
|
2019-11-13 18:45:43 +00:00
|
|
|
frame = ttk.Frame(self.top)
|
2019-11-16 16:54:15 +00:00
|
|
|
frame.grid(sticky="ew")
|
2019-11-03 06:47:43 +00:00
|
|
|
frame.columnconfigure(0, weight=1)
|
|
|
|
frame.columnconfigure(1, weight=1)
|
|
|
|
|
2019-11-12 20:13:53 +00:00
|
|
|
button = ttk.Button(frame, text="Apply", command=self.config_apply)
|
2019-11-03 06:47:43 +00:00
|
|
|
button.grid(row=0, column=0, padx=2, sticky="ew")
|
|
|
|
|
2019-11-12 20:13:53 +00:00
|
|
|
button = ttk.Button(frame, text="Cancel", command=self.destroy)
|
2019-11-03 06:47:43 +00:00
|
|
|
button.grid(row=0, column=1, sticky="ew")
|
|
|
|
|
2019-11-04 19:34:28 +00:00
|
|
|
def click_services(self):
|
2019-11-11 18:57:26 +00:00
|
|
|
dialog = NodeService(self, self.app, self.canvas_node)
|
2019-11-04 19:34:28 +00:00
|
|
|
dialog.show()
|
|
|
|
|
2019-11-03 06:47:43 +00:00
|
|
|
def click_icon(self):
|
2019-11-20 18:59:30 +00:00
|
|
|
dialog = IconDialog(self, self.app, self.node.name, self.canvas_node.image)
|
2019-11-03 06:47:43 +00:00
|
|
|
dialog.show()
|
|
|
|
if dialog.image:
|
|
|
|
self.image = dialog.image
|
|
|
|
self.image_button.config(image=self.image)
|
|
|
|
|
|
|
|
def config_apply(self):
|
2019-11-20 18:59:30 +00:00
|
|
|
# update core node
|
|
|
|
self.node.name = self.name.get()
|
2019-11-20 19:20:08 +00:00
|
|
|
if NodeUtils.is_image_node(self.node.type):
|
|
|
|
self.node.image = self.container_image.get()
|
|
|
|
if NodeUtils.is_container_node(self.node.type):
|
|
|
|
self.node.server = self.server.get()
|
2019-11-20 18:59:30 +00:00
|
|
|
|
|
|
|
# update canvas node
|
2019-11-03 06:47:43 +00:00
|
|
|
self.canvas_node.image = self.image
|
2019-11-20 18:59:30 +00:00
|
|
|
|
|
|
|
# redraw
|
|
|
|
self.canvas_node.redraw()
|
|
|
|
|
2019-11-03 06:47:43 +00:00
|
|
|
self.destroy()
|