From ceee0495eb379a2c9890acdb294c562c6156d499 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Fri, 10 Jan 2020 18:50:21 -0700 Subject: [PATCH] Started integrating osTicket functions in HW Diags --- scripts/wk/hw/diags.py | 40 +++++++++++++++++++++++++++++++++++++++- scripts/wk/osticket.py | 13 +++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/scripts/wk/hw/diags.py b/scripts/wk/hw/diags.py index ce6c9c51..2a0a835b 100644 --- a/scripts/wk/hw/diags.py +++ b/scripts/wk/hw/diags.py @@ -13,7 +13,7 @@ import time from collections import OrderedDict from docopt import docopt -from wk import cfg, debug, exe, graph, log, net, std, tmux +from wk import cfg, debug, exe, graph, log, net, osticket, std, tmux from wk.hw import obj as hw_obj from wk.hw import sensors as hw_sensors @@ -72,6 +72,8 @@ MENU_SETS = { 'Disk Diagnostic (Quick)': ('Disk Attributes',), } MENU_TOGGLES = ( + 'osTicket Integration', + 'osTicket Note (optional)', 'Skip USB Benchmarks', ) PLATFORM = std.PLATFORM @@ -96,12 +98,14 @@ class DeviceTooSmallError(RuntimeError): # Classes class State(): + # pylint: disable=too-many-instance-attributes """Object for tracking hardware diagnostic data.""" def __init__(self): self.cpu = None self.disks = [] self.layout = cfg.hw.TMUX_LAYOUT.copy() self.log_dir = None + self.ost = osticket.osTicket() self.panes = {} self.tests = OrderedDict({ 'CPU & Cooling': { @@ -235,6 +239,11 @@ class State(): for test_data in self.tests.values(): test_data['Objects'].clear() + # osTicket + self.top_text = std.color_string('Hardware Diagnostics', 'GREEN') + self.ost.init() + self.ost.disabled = not menu.toggles['osTicket Integration']['Selected'] + # Set log self.log_dir = log.format_log_path() self.log_dir = pathlib.Path( @@ -340,6 +349,10 @@ class State(): _f.write(f'\n{name}:\n') _f.write('\n'.join(debug.generate_object_report(test, indent=1))) + # osTicket + with open(f'{debug_dir}/osTicket.report', 'a') as _f: + _f.write('\n'.join(debug.generate_object_report(self.ost))) + def update_progress_pane(self): """Update progress pane.""" report = [] @@ -411,6 +424,9 @@ def build_menu(cli_mode=False, quick_mode=False): menu.add_set(name, {'Targets': targets}) menu.actions['Start']['Separator'] = True + # osTicket + menu.toggles['osTicket Note (optional)']['Selected'] = False + # Update default selections for quick mode if necessary if quick_mode: for name in menu.options: @@ -1100,6 +1116,7 @@ def main(): # Quick Mode if args['--quick']: + menu.toggles['osTicket Integration']['Selected'] = False run_diags(state, menu, quick_mode=True) return @@ -1216,12 +1233,33 @@ def run_diags(state, menu, quick_mode=False): atexit.register(state.save_debug_reports) state.init_diags(menu) + def _init_osticket(): + """Dumb private function to avoid pylint error.""" + if not state.ost.disabled: + # Select Ticket + state.ost.select_ticket() + + # Update top_text + if state.ost.ticket_id: + state.top_text += std.color_string( + [f' #{state.ost.ticket_id}', state.ost.ticket_name], + [None, 'CYAN'], + ) + + # Add note + if (state.ost.ticket_id + and menu.toggles['osTicket Note (optional)']['Selected']): + state.ost.add_note() + # Just return if no tests were selected if not any([details['Enabled'] for details in state.tests.values()]): std.print_warning('No tests selected?') std.pause() return + # osTicket + _init_osticket() + # Run tests for name, details in state.tests.items(): if not details['Enabled']: diff --git a/scripts/wk/osticket.py b/scripts/wk/osticket.py index 82c38c7e..8e147059 100644 --- a/scripts/wk/osticket.py +++ b/scripts/wk/osticket.py @@ -184,6 +184,15 @@ class osTicket(): # pylint: disable=invalid-name for line in lines: self.note += f'\n...{line}' + def init(self): + """Revert to defaults.""" + self._disconnect() + self.disabled = False + self.errors = False + self.note = None + self.ticket_id = None + self.ticket_name = None + def post_response(self, response, color='Normal'): """Post a reply to a ticket in osTicket.""" lines = [] @@ -232,6 +241,10 @@ class osTicket(): # pylint: disable=invalid-name """Set ticket number and name from osTicket DB.""" std.print_standard('Connecting to osTicket...') + # Bail if disabled + if self.disabled: + return + # Connect while True: try: