update to hooks dialog to leverage grpc, allows for getting hooks and creation

This commit is contained in:
bharnden 2019-11-02 10:29:16 -07:00
parent 09e18889b0
commit c947d8c6c2

View file

@ -1,3 +1,4 @@
import logging
import tkinter as tk
from tkinter import ttk
@ -11,10 +12,12 @@ class HookDialog(Dialog):
self.name = tk.StringVar()
self.data = None
self.hook = None
self.state = tk.StringVar()
self.draw()
def draw(self):
self.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
# name and states
frame = tk.Frame(self)
@ -26,12 +29,33 @@ class HookDialog(Dialog):
label.grid(row=0, column=0, sticky="ew")
entry = tk.Entry(frame, textvariable=self.name)
entry.grid(row=0, column=1, sticky="ew")
combobox = ttk.Combobox(frame, values=("DEFINITION", "CONFIGURATION"))
values = tuple(x for x in core_pb2.SessionState.Enum.keys() if x != "NONE")
initial_state = core_pb2.SessionState.Enum.Name(core_pb2.SessionState.RUNTIME)
self.state.set(initial_state)
self.name.set(f"{initial_state.lower()}_hook.sh")
combobox = ttk.Combobox(frame, textvariable=self.state, values=values)
combobox.grid(row=0, column=2, sticky="ew")
combobox.bind("<<ComboboxSelected>>", self.state_change)
# data
self.data = tk.Text(self)
self.data.grid(row=1, sticky="nsew", pady=2)
frame = tk.Frame(self)
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
frame.grid(row=1, sticky="nsew", pady=2)
self.data = tk.Text(frame)
self.data.insert(
1.0,
(
"#!/bin/sh\n"
"# session hook script; write commands here to execute on the host at the\n"
"# specified state\n"
),
)
self.data.grid(row=0, column=0, sticky="nsew")
scrollbar = tk.Scrollbar(frame)
scrollbar.grid(row=0, column=1, sticky="ns")
self.data.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=self.data.yview)
# button row
frame = tk.Frame(self)
@ -43,15 +67,26 @@ class HookDialog(Dialog):
button = tk.Button(frame, text="Cancel", command=lambda: self.destroy())
button.grid(row=0, column=1, sticky="ew")
def state_change(self, event):
state_name = self.state.get()
self.name.set(f"{state_name.lower()}_hook.sh")
def set(self, hook):
self.hook = hook
self.name.set(hook.file)
self.data.delete(1.0, tk.END)
self.data.insert(tk.END, hook.data)
state_name = core_pb2.SessionState.Enum.Name(hook.state)
self.state.set(state_name)
def save(self):
data = self.data.get("1.0", tk.END).strip()
self.hook = core_pb2.Hook(file=self.name.get(), data=data)
state_value = core_pb2.SessionState.Enum.Value(self.state.get())
self.hook = core_pb2.Hook(state=state_value, file=self.name.get(), data=data)
response = self.app.core.client.add_hook(
self.app.core.session_id, self.hook.state, self.hook.file, self.hook.data
)
logging.info("add hook: %s", response)
self.destroy()
@ -59,29 +94,35 @@ class HooksDialog(Dialog):
def __init__(self, master, app):
super().__init__(master, app, "Hooks", modal=True)
self.listbox = None
self.edit = None
self.delete = None
self.edit_button = None
self.delete_button = None
self.selected = None
self.hooks = {}
self.draw()
def draw(self):
response = self.app.core.client.get_hooks(self.app.core.session_id)
logging.info("get hooks: %s", response)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.listbox = tk.Listbox(self)
self.listbox.grid(row=0, sticky="ew")
self.listbox.grid(row=0, sticky="nsew")
self.listbox.bind("<<ListboxSelect>>", self.select)
for hook in response.hooks:
self.hooks[hook.file] = hook
self.listbox.insert(tk.END, hook.file)
frame = tk.Frame(self)
frame.grid(row=1, sticky="ew")
for i in range(4):
frame.columnconfigure(i, weight=1)
button = tk.Button(frame, text="Create", command=self.click_create)
button.grid(row=0, column=0, sticky="ew")
self.edit = tk.Button(
self.edit_button = tk.Button(
frame, text="Edit", state=tk.DISABLED, command=self.click_edit
)
self.edit.grid(row=0, column=1, sticky="ew")
self.delete = tk.Button(frame, text="Delete", state=tk.DISABLED)
self.delete.grid(row=0, column=2, sticky="ew")
self.edit_button.grid(row=0, column=1, sticky="ew")
self.delete_button = tk.Button(frame, text="Delete", state=tk.DISABLED)
self.delete_button.grid(row=0, column=2, sticky="ew")
button = tk.Button(frame, text="Cancel", command=lambda: self.destroy())
button.grid(row=0, column=3, sticky="ew")
@ -100,7 +141,7 @@ class HooksDialog(Dialog):
dialog.show()
def select(self, event):
self.edit.config(state=tk.NORMAL)
self.delete.config(state=tk.NORMAL)
self.edit_button.config(state=tk.NORMAL)
self.delete_button.config(state=tk.NORMAL)
index = self.listbox.curselection()[0]
self.selected = self.listbox.get(index)