daemon/gui/grpc: added support to retrieve rendered config service files, added support for grpc to access this, and update gui to leverage this call to provide a rendered view of files based on the current scenario, also allows editing the rendered output to use as the input when running the scenario

This commit is contained in:
Blake Harnden 2022-04-08 22:24:07 -07:00
parent 443c0e708f
commit bd6f789cef
8 changed files with 171 additions and 46 deletions

View file

@ -14,6 +14,7 @@ import grpc
from core.api.grpc import core_pb2, core_pb2_grpc, emane_pb2, wrappers
from core.api.grpc.configservices_pb2 import (
GetConfigServiceDefaultsRequest,
GetConfigServiceRenderedRequest,
GetNodeConfigServiceRequest,
)
from core.api.grpc.core_pb2 import (
@ -994,6 +995,23 @@ class CoreGrpcClient:
response = self.stub.GetNodeConfigService(request)
return dict(response.config)
def get_config_service_rendered(
self, session_id: int, node_id: int, name: str
) -> Dict[str, str]:
"""
Retrieve the rendered config service files for a node.
:param session_id: id of session
:param node_id: id of node
:param name: name of service
:return: dict mapping names of files to rendered data
"""
request = GetConfigServiceRenderedRequest(
session_id=session_id, node_id=node_id, name=name
)
response = self.stub.GetConfigServiceRendered(request)
return dict(response.rendered)
def get_emane_event_channel(
self, session_id: int, nem_id: int
) -> wrappers.EmaneEventChannel:

View file

@ -23,6 +23,8 @@ from core.api.grpc.configservices_pb2 import (
ConfigService,
GetConfigServiceDefaultsRequest,
GetConfigServiceDefaultsResponse,
GetConfigServiceRenderedRequest,
GetConfigServiceRenderedResponse,
GetNodeConfigServiceRequest,
GetNodeConfigServiceResponse,
)
@ -1226,6 +1228,27 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
config = {x.id: x.default for x in service.default_configs}
return GetNodeConfigServiceResponse(config=config)
def GetConfigServiceRendered(
self, request: GetConfigServiceRenderedRequest, context: ServicerContext
) -> GetConfigServiceRenderedResponse:
"""
Retrieves the rendered file data for a given config service on a node.
:param request: config service render request
:param context: grpc context
:return: rendered config service files
"""
session = self.get_session(request.session_id, context)
node = self.get_node(session, request.node_id, context, CoreNode)
self.validate_service(request.name, context)
service = node.config_services.get(request.name)
if not service:
context.abort(
grpc.StatusCode.NOT_FOUND, f"unknown node service {request.name}"
)
rendered = service.get_rendered_templates()
return GetConfigServiceRenderedResponse(rendered=rendered)
def GetConfigServiceDefaults(
self, request: GetConfigServiceDefaultsRequest, context: ServicerContext
) -> GetConfigServiceDefaultsResponse:

View file

@ -331,6 +331,33 @@ class ConfigService(abc.ABC):
templates[file] = template
return templates
def get_rendered_templates(self) -> Dict[str, str]:
templates = {}
data = self.data()
for file in sorted(self.files):
rendered = self._get_rendered_template(file, data)
templates[file] = rendered
return templates
def _get_rendered_template(self, file: str, data: Dict[str, Any]) -> str:
file_path = Path(file)
template_path = get_template_path(file_path)
if file in self.custom_templates:
text = self.custom_templates[file]
rendered = self.render_text(text, data)
elif self.templates.has_template(template_path):
rendered = self.render_template(template_path, data)
else:
try:
text = self.get_text_template(file)
except Exception as e:
raise ConfigServiceTemplateError(
f"node({self.node.name}) service({self.name}) file({file}) "
f"failure getting template: {e}"
)
rendered = self.render_text(text, data)
return rendered
def create_files(self) -> None:
"""
Creates service files inside associated node.
@ -342,22 +369,24 @@ class ConfigService(abc.ABC):
logger.debug(
"node(%s) service(%s) template(%s)", self.node.name, self.name, file
)
rendered = self._get_rendered_template(file, data)
# file_path = Path(file)
# template_path = get_template_path(file_path)
# if file in self.custom_templates:
# text = self.custom_templates[file]
# rendered = self.render_text(text, data)
# elif self.templates.has_template(template_path):
# rendered = self.render_template(template_path, data)
# else:
# try:
# text = self.get_text_template(file)
# except Exception as e:
# raise ConfigServiceTemplateError(
# f"node({self.node.name}) service({self.name}) file({file}) "
# f"failure getting template: {e}"
# )
# rendered = self.render_text(text, data)
file_path = Path(file)
template_path = get_template_path(file_path)
if file in self.custom_templates:
text = self.custom_templates[file]
rendered = self.render_text(text, data)
elif self.templates.has_template(template_path):
rendered = self.render_template(template_path, data)
else:
try:
text = self.get_text_template(file)
except Exception as e:
raise ConfigServiceTemplateError(
f"node({self.node.name}) service({self.name}) file({file}) "
f"failure getting template: {e}"
)
rendered = self.render_text(text, data)
self.node.create_file(file_path, rendered)
def run_startup(self, wait: bool) -> None:

View file

@ -743,6 +743,9 @@ class CoreClient:
configs.append(config)
return configs
def get_config_service_rendered(self, node_id: int, name: str) -> Dict[str, str]:
return self.client.get_config_service_rendered(self.session.id, node_id, name)
def get_config_service_configs_proto(
self
) -> List[configservices_pb2.ConfigServiceConfig]:

View file

@ -34,10 +34,10 @@ class ConfigServiceConfigDialog(Dialog):
self.core: "CoreClient" = app.core
self.node: Node = node
self.service_name: str = service_name
self.radiovar: tk.IntVar = tk.IntVar()
self.radiovar.set(2)
self.radiovar: tk.IntVar = tk.IntVar(value=2)
self.directories: List[str] = []
self.templates: List[str] = []
self.rendered: Dict[str, str] = {}
self.dependencies: List[str] = []
self.executables: List[str] = []
self.startup_commands: List[str] = []
@ -48,10 +48,9 @@ class ConfigServiceConfigDialog(Dialog):
self.default_shutdown: List[str] = []
self.validation_mode: Optional[ServiceValidationMode] = None
self.validation_time: Optional[int] = None
self.validation_period: tk.StringVar = tk.StringVar()
self.validation_period: tk.DoubleVar = tk.DoubleVar()
self.modes: List[str] = []
self.mode_configs: Dict[str, Dict[str, str]] = {}
self.notebook: Optional[ttk.Notebook] = None
self.templates_combobox: Optional[ttk.Combobox] = None
self.modes_combobox: Optional[ttk.Combobox] = None
@ -61,6 +60,7 @@ class ConfigServiceConfigDialog(Dialog):
self.validation_time_entry: Optional[ttk.Entry] = None
self.validation_mode_entry: Optional[ttk.Entry] = None
self.template_text: Optional[CodeText] = None
self.rendered_text: Optional[CodeText] = None
self.validation_period_entry: Optional[ttk.Entry] = None
self.original_service_files: Dict[str, str] = {}
self.temp_service_files: Dict[str, str] = {}
@ -87,7 +87,6 @@ class ConfigServiceConfigDialog(Dialog):
self.validation_mode = service.validation_mode
self.validation_time = service.validation_timer
self.validation_period.set(service.validation_period)
defaults = self.core.client.get_config_service_defaults(self.service_name)
self.original_service_files = defaults.templates
self.temp_service_files = dict(self.original_service_files)
@ -95,6 +94,9 @@ class ConfigServiceConfigDialog(Dialog):
self.mode_configs = defaults.modes
self.config = ConfigOption.from_dict(defaults.config)
self.default_config = {x.name: x.value for x in self.config.values()}
self.rendered = self.core.get_config_service_rendered(
self.node.id, self.service_name
)
service_config = self.node.config_service_configs.get(self.service_name)
if service_config:
for key, value in service_config.config.items():
@ -110,7 +112,6 @@ class ConfigServiceConfigDialog(Dialog):
def draw(self) -> None:
self.top.columnconfigure(0, weight=1)
self.top.rowconfigure(0, weight=1)
# draw notebook
self.notebook = ttk.Notebook(self.top)
self.notebook.grid(sticky=tk.NSEW, pady=PADY)
@ -137,33 +138,50 @@ class ConfigServiceConfigDialog(Dialog):
frame.columnconfigure(1, weight=1)
label = ttk.Label(frame, text="Directories")
label.grid(row=0, column=0, sticky=tk.W, padx=PADX)
directories_combobox = ttk.Combobox(
frame, values=self.directories, state="readonly"
)
state = "readonly" if self.directories else tk.DISABLED
directories_combobox = ttk.Combobox(frame, values=self.directories, state=state)
directories_combobox.grid(row=0, column=1, sticky=tk.EW, pady=PADY)
if self.directories:
directories_combobox.current(0)
label = ttk.Label(frame, text="Templates")
label = ttk.Label(frame, text="Files")
label.grid(row=1, column=0, sticky=tk.W, padx=PADX)
state = "readonly" if self.templates else tk.DISABLED
self.templates_combobox = ttk.Combobox(
frame, values=self.templates, state="readonly"
frame, values=self.templates, state=state
)
self.templates_combobox.bind(
"<<ComboboxSelected>>", self.handle_template_changed
)
self.templates_combobox.grid(row=1, column=1, sticky=tk.EW, pady=PADY)
self.template_text = CodeText(tab)
# draw file template tab
notebook = ttk.Notebook(tab)
# notebook.columnconfigure(0, weight=1)
notebook.grid(sticky=tk.NSEW, pady=PADY)
template_tab = ttk.Frame(notebook, padding=FRAME_PAD)
template_tab.grid(sticky=tk.NSEW)
template_tab.rowconfigure(0, weight=1)
notebook.add(template_tab, text="Template")
self.template_text = CodeText(template_tab)
self.template_text.grid(sticky=tk.NSEW)
tab.rowconfigure(self.template_text.grid_info()["row"], weight=1)
self.template_text.text.bind("<FocusOut>", self.update_template_file_data)
# draw rendered file tab
rendered_tab = ttk.Frame(notebook, padding=FRAME_PAD)
rendered_tab.grid(sticky=tk.NSEW)
rendered_tab.rowconfigure(0, weight=1)
notebook.add(rendered_tab, text="Rendered")
self.rendered_text = CodeText(rendered_tab)
self.rendered_text.grid(sticky=tk.NSEW)
self.rendered_text.text.bind("<FocusOut>", self.update_template_file_data)
if self.templates:
self.templates_combobox.current(0)
self.template_text.text.delete(1.0, "end")
self.template_text.text.insert(
"end", self.temp_service_files[self.templates[0]]
)
self.template_text.text.bind("<FocusOut>", self.update_template_file_data)
template_name = self.templates[0]
temp_data = self.temp_service_files[template_name]
self.template_text.set_text(temp_data)
rendered_data = self.rendered[template_name]
self.rendered_text.set_text(rendered_data)
else:
self.template_text.text.configure(state=tk.DISABLED)
self.rendered_text.text.configure(state=tk.DISABLED)
def draw_tab_config(self) -> None:
tab = ttk.Frame(self.notebook, padding=FRAME_PAD)
@ -243,7 +261,7 @@ class ConfigServiceConfigDialog(Dialog):
label = ttk.Label(frame, text="Validation Time")
label.grid(row=0, column=0, sticky=tk.W, padx=PADX)
self.validation_time_entry = ttk.Entry(frame)
self.validation_time_entry.insert("end", self.validation_time)
self.validation_time_entry.insert("end", str(self.validation_time))
self.validation_time_entry.config(state=tk.DISABLED)
self.validation_time_entry.grid(row=0, column=1, sticky=tk.EW, pady=PADY)
@ -323,9 +341,11 @@ class ConfigServiceConfigDialog(Dialog):
self.destroy()
def handle_template_changed(self, event: tk.Event) -> None:
template = self.templates_combobox.get()
self.template_text.text.delete(1.0, "end")
self.template_text.text.insert("end", self.temp_service_files[template])
template_name = self.templates_combobox.get()
temp_data = self.temp_service_files[template_name]
self.template_text.set_text(temp_data)
rendered = self.rendered[template_name]
self.rendered_text.set_text(rendered)
def handle_mode_changed(self, event: tk.Event) -> None:
mode = self.modes_combobox.get()
@ -333,10 +353,13 @@ class ConfigServiceConfigDialog(Dialog):
logger.info("mode config: %s", config)
self.config_frame.set_values(config)
def update_template_file_data(self, event: tk.Event) -> None:
scrolledtext = event.widget
def update_template_file_data(self, _event: tk.Event) -> None:
template = self.templates_combobox.get()
self.temp_service_files[template] = scrolledtext.get(1.0, "end")
self.temp_service_files[template] = self.rendered_text.get_text()
if self.rendered[template] != self.temp_service_files[template]:
self.modified_files.add(template)
return
self.temp_service_files[template] = self.template_text.get_text()
if self.temp_service_files[template] != self.original_service_files[template]:
self.modified_files.add(template)
else:
@ -351,14 +374,24 @@ class ConfigServiceConfigDialog(Dialog):
return has_custom_templates or has_custom_config
def click_defaults(self) -> None:
# clear all saved state data
self.modified_files.clear()
self.node.config_service_configs.pop(self.service_name, None)
self.temp_service_files = dict(self.original_service_files)
# reset session definition and retrieve default rendered templates
self.core.start_session(definition=True)
self.rendered = self.core.get_config_service_rendered(
self.node.id, self.service_name
)
logger.info(
"cleared config service config: %s", self.node.config_service_configs
)
self.temp_service_files = dict(self.original_service_files)
filename = self.templates_combobox.get()
self.template_text.text.delete(1.0, "end")
self.template_text.text.insert("end", self.temp_service_files[filename])
# reset current selected file data and config data, if present
template_name = self.templates_combobox.get()
temp_data = self.temp_service_files[template_name]
self.template_text.set_text(temp_data)
rendered_data = self.rendered[template_name]
self.rendered_text.set_text(rendered_data)
if self.config_frame:
logger.info("resetting defaults: %s", self.default_config)
self.config_frame.set_values(self.default_config)

View file

@ -257,6 +257,13 @@ class CodeText(ttk.Frame):
yscrollbar.grid(row=0, column=1, sticky=tk.NS)
self.text.configure(yscrollcommand=yscrollbar.set)
def get_text(self) -> str:
return self.text.get(1.0, tk.END)
def set_text(self, text: str) -> None:
self.text.delete(1.0, tk.END)
self.text.insert(tk.END, text.rstrip())
class Spinbox(ttk.Entry):
def __init__(self, master: tk.BaseWidget = None, **kwargs: Any) -> None:

View file

@ -58,3 +58,13 @@ message GetNodeConfigServiceRequest {
message GetNodeConfigServiceResponse {
map<string, string> config = 1;
}
message GetConfigServiceRenderedRequest {
int32 session_id = 1;
int32 node_id = 2;
string name = 3;
}
message GetConfigServiceRenderedResponse {
map<string, string> rendered = 1;
}

View file

@ -91,6 +91,8 @@ service CoreApi {
}
rpc ConfigServiceAction (services.ServiceActionRequest) returns (services.ServiceActionResponse) {
}
rpc GetConfigServiceRendered (configservices.GetConfigServiceRenderedRequest) returns (configservices.GetConfigServiceRenderedResponse) {
}
// wlan rpc
rpc GetWlanConfig (wlan.GetWlanConfigRequest) returns (wlan.GetWlanConfigResponse) {