Better exception handling for MariaDB sections
This commit is contained in:
parent
7d7cf21263
commit
ff715b7a10
1 changed files with 43 additions and 20 deletions
|
|
@ -21,6 +21,7 @@ class osTicket():
|
||||||
def __init__(self, tests_cpu, tests_disk):
|
def __init__(self, tests_cpu, tests_disk):
|
||||||
self.db_connection = None
|
self.db_connection = None
|
||||||
self.db_cursor = None
|
self.db_cursor = None
|
||||||
|
self.disabled = False
|
||||||
self.errors = False
|
self.errors = False
|
||||||
self.tests_cpu = tests_cpu
|
self.tests_cpu = tests_cpu
|
||||||
self.tests_disk = tests_disk
|
self.tests_disk = tests_disk
|
||||||
|
|
@ -35,6 +36,10 @@ class osTicket():
|
||||||
'{User}@{Host}'.format(**OSTICKET['SSH']),
|
'{User}@{Host}'.format(**OSTICKET['SSH']),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Bail if disabled
|
||||||
|
if self.disabled:
|
||||||
|
return
|
||||||
|
|
||||||
# Only open tunnel if one doesn't exist
|
# Only open tunnel if one doesn't exist
|
||||||
if self.tunnel_proc is None or self.tunnel_proc.poll() is not None:
|
if self.tunnel_proc is None or self.tunnel_proc.poll() is not None:
|
||||||
self.tunnel_proc = popen_program(cmd)
|
self.tunnel_proc = popen_program(cmd)
|
||||||
|
|
@ -49,13 +54,24 @@ class osTicket():
|
||||||
database=OSTICKET['Database']['Name'],
|
database=OSTICKET['Database']['Name'],
|
||||||
)
|
)
|
||||||
self.db_cursor = self.db_connection.cursor()
|
self.db_cursor = self.db_connection.cursor()
|
||||||
except Exception:
|
except mariadb.errors.InterfaceError:
|
||||||
# TODO: Refine exception handling
|
# SSH issue?, try again
|
||||||
pass
|
pass
|
||||||
|
except mariadb.errors.Error:
|
||||||
|
# Bad creds or other SQL error, bail
|
||||||
|
break
|
||||||
|
except Exception:
|
||||||
|
# Unknown error
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
# Connection established
|
# Connection established
|
||||||
break
|
break
|
||||||
|
|
||||||
|
# Disable if necessary
|
||||||
|
if self.db_cursor is None:
|
||||||
|
self.disabled = True
|
||||||
|
self.tunnel_proc.kill()
|
||||||
|
|
||||||
def convert_report(self, name, test):
|
def convert_report(self, name, test):
|
||||||
"""Convert report into an osTicket friendly format, returns list."""
|
"""Convert report into an osTicket friendly format, returns list."""
|
||||||
out_report = []
|
out_report = []
|
||||||
|
|
@ -129,13 +145,9 @@ class osTicket():
|
||||||
self.db_cursor.close()
|
self.db_cursor.close()
|
||||||
self.db_connection.close()
|
self.db_connection.close()
|
||||||
except Exception:
|
except Exception:
|
||||||
# TODO: Fix exception handling
|
# Ignore errors since vars will be reset below
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Reset errors
|
|
||||||
if reset_errors:
|
|
||||||
self.errors = False
|
|
||||||
|
|
||||||
# Reset vars
|
# Reset vars
|
||||||
self.db_cursor = None
|
self.db_cursor = None
|
||||||
self.db_connection = None
|
self.db_connection = None
|
||||||
|
|
@ -311,17 +323,12 @@ class osTicket():
|
||||||
sql_cmd = "SELECT name FROM `{Ticket}`".format(**OSTICKET['Tables'])
|
sql_cmd = "SELECT name FROM `{Ticket}`".format(**OSTICKET['Tables'])
|
||||||
sql_cmd += " WHERE `ticket_id` = {}".format(ticket_id)
|
sql_cmd += " WHERE `ticket_id` = {}".format(ticket_id)
|
||||||
sql_cmd += ";"
|
sql_cmd += ";"
|
||||||
# TODO: Is the ';' needed above? It wasn't in the prev version??
|
|
||||||
|
|
||||||
# Lookup name
|
# Lookup name
|
||||||
# NOTE: If multiple entries are found it will return the last
|
# NOTE: If multiple entries are found it will return the last
|
||||||
try:
|
self.db_cursor.execute(sql_cmd)
|
||||||
self.db_cursor.execute(sql_cmd)
|
for s in self.db_cursor:
|
||||||
for s in self.db_cursor:
|
name = s[0]
|
||||||
name = s[0]
|
|
||||||
except Exception:
|
|
||||||
# TODO: Fix exception handling
|
|
||||||
self.errors = True
|
|
||||||
|
|
||||||
# Done
|
# Done
|
||||||
return name
|
return name
|
||||||
|
|
@ -331,6 +338,10 @@ class osTicket():
|
||||||
ticket_number = None
|
ticket_number = None
|
||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
|
# Bail if disabled
|
||||||
|
if self.disabled:
|
||||||
|
return None
|
||||||
|
|
||||||
# Main loop
|
# Main loop
|
||||||
while ticket_number is None:
|
while ticket_number is None:
|
||||||
print_standard(' ')
|
print_standard(' ')
|
||||||
|
|
@ -347,7 +358,11 @@ class osTicket():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Valid ID entered, lookup name and verify
|
# Valid ID entered, lookup name and verify
|
||||||
_name = self.get_ticket_name(_input)
|
try:
|
||||||
|
_name = self.get_ticket_name(_input)
|
||||||
|
except Exception:
|
||||||
|
# Ignore and return None below
|
||||||
|
break
|
||||||
if _name:
|
if _name:
|
||||||
print_standard('You have selected ticket #{} {}'.format(
|
print_standard('You have selected ticket #{} {}'.format(
|
||||||
_input, _name))
|
_input, _name))
|
||||||
|
|
@ -367,6 +382,10 @@ class osTicket():
|
||||||
"""Post a reply to a ticket in osTicket."""
|
"""Post a reply to a ticket in osTicket."""
|
||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
|
# Bail if disabled
|
||||||
|
if self.disabled:
|
||||||
|
return
|
||||||
|
|
||||||
# Convert response to string
|
# Convert response to string
|
||||||
response = '\n'.join(response)
|
response = '\n'.join(response)
|
||||||
response = response.replace('`', '')
|
response = response.replace('`', '')
|
||||||
|
|
@ -385,8 +404,8 @@ class osTicket():
|
||||||
# Run SQL cmd
|
# Run SQL cmd
|
||||||
try:
|
try:
|
||||||
self.db_cursor.execute(sql_cmd)
|
self.db_cursor.execute(sql_cmd)
|
||||||
except Exception:
|
except mariadb.errors.Error:
|
||||||
# TODO: Fix exception handling
|
# Set self.errors to enable warning line on results screen
|
||||||
self.errors = True
|
self.errors = True
|
||||||
|
|
||||||
# Done
|
# Done
|
||||||
|
|
@ -410,6 +429,10 @@ class osTicket():
|
||||||
"""Set flag in osTicket."""
|
"""Set flag in osTicket."""
|
||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
|
# Bail if disabled
|
||||||
|
if self.disabled:
|
||||||
|
return
|
||||||
|
|
||||||
# Build SQL cmd
|
# Build SQL cmd
|
||||||
sql_cmd = "UPDATE `{Name}`.`{Ticket}`".format(
|
sql_cmd = "UPDATE `{Name}`.`{Ticket}`".format(
|
||||||
**OSTICKET['Database'], **OSTICKET['Tables'])
|
**OSTICKET['Database'], **OSTICKET['Tables'])
|
||||||
|
|
@ -421,8 +444,8 @@ class osTicket():
|
||||||
# Run SQL cmd
|
# Run SQL cmd
|
||||||
try:
|
try:
|
||||||
self.db_cursor.execute(sql_cmd)
|
self.db_cursor.execute(sql_cmd)
|
||||||
except Exception:
|
except mariadb.errors.Error:
|
||||||
# TODO: Fix exception handling
|
# Set self.errors to enable warning line on results screen
|
||||||
self.errors = True
|
self.errors = True
|
||||||
|
|
||||||
# Done
|
# Done
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue