Establish SSH tunnel before connecting to SQL DB
* Also added disconnect_from_db() function.
This commit is contained in:
parent
462a87b1ce
commit
d46ae18045
2 changed files with 36 additions and 5 deletions
|
|
@ -10,7 +10,8 @@ from functions.common import *
|
|||
ost_db = {
|
||||
'Connection': None,
|
||||
'Cursor': None,
|
||||
'Errors': False
|
||||
'Errors': False,
|
||||
'Tunnel': None,
|
||||
}
|
||||
|
||||
# STATIC VARIABLES
|
||||
|
|
@ -92,13 +93,39 @@ TESTS = {
|
|||
}
|
||||
|
||||
def connect_to_db():
|
||||
"""Connect to osTicket database."""
|
||||
#TODO# Open SSH tunnel to DB server
|
||||
ost_db['Connection'] = mariadb.connect(
|
||||
user=DB_USER, password=DB_PASS, database=DB_NAME, host=DB_HOST)
|
||||
"""Connect to osTicket database via SSH tunnel."""
|
||||
cmd = [
|
||||
'ssh', '-N', '-p', '2222', '-L3306:127.0.0.1:3306',
|
||||
'{user}@{host}'.format(user=SSH_USER, host=DB_HOST),
|
||||
]
|
||||
|
||||
# Establish SSH tunnel unless one already exists
|
||||
if not ost_db['Tunnel']:
|
||||
ost_db['Tunnel'] = popen_program(cmd)
|
||||
|
||||
# Establish SQL connection (try a few times in case SSH is slow)
|
||||
for x in range(5):
|
||||
sleep(3)
|
||||
try:
|
||||
ost_db['Connection'] = mariadb.connect(
|
||||
user=DB_USER, password=DB_PASS, database=DB_NAME)
|
||||
except:
|
||||
# Just try again
|
||||
pass
|
||||
else:
|
||||
break
|
||||
ost_db['Cursor'] = ost_db['Connection'].cursor()
|
||||
ost_db['Errors'] = False
|
||||
|
||||
def disconnect_from_db():
|
||||
"""Disconnect from SQL DB and close SSH tunnel."""
|
||||
if ost_db['Cursor']:
|
||||
ost_db['Cursor'].close()
|
||||
if ost_db['Connection']:
|
||||
ost_db['Connection'].close()
|
||||
if ost_db['Tunnel']:
|
||||
ost_db['Tunnel'].kill()
|
||||
|
||||
def generate_horizontal_graph(rates):
|
||||
"""Generate two-line horizontal graph from rates, returns str."""
|
||||
line_top = ''
|
||||
|
|
@ -287,6 +314,9 @@ def menu_diags(*args):
|
|||
elif selection == 'Q':
|
||||
break
|
||||
|
||||
# Done
|
||||
disconnect_from_db()
|
||||
|
||||
def osticket_get_ticket_name(ticket_id):
|
||||
"""Lookup ticket and return name as str."""
|
||||
ticket_name = 'Unknown'
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ DB_HOST='127.0.0.1'
|
|||
DB_NAME='osticket'
|
||||
DB_USER='wizardkit'
|
||||
DB_PASS='Abracadabra'
|
||||
SSH_USER='sql_tunnel'
|
||||
# Live Linux
|
||||
MPRIME_LIMIT='7' # of minutes to run Prime95 during hw-diags
|
||||
ROOT_PASSWORD='Abracadabra'
|
||||
|
|
|
|||
Loading…
Reference in a new issue