RYSEN/mysql_config.py

98 lines
3.6 KiB
Python
Raw Normal View History

import mysql.connector
2020-09-29 08:42:07 -04:00
from mysql.connector import errorcode
2020-10-19 17:46:55 -04:00
#import mysql.connector.pooling
class useMYSQL:
2020-09-29 08:42:07 -04:00
#Init new object
def __init__(self, server,user,password,database,table,logger):
self.server = server
self.user = user
self.password = password
2020-09-29 08:42:07 -04:00
self.database = database
self.table = table
self.logger = logger
2020-09-29 08:42:07 -04:00
#Connect
def con(self):
logger = self.logger
2020-09-29 08:42:07 -04:00
try:
self.db = mysql.connector.connect(
host=self.server,
user=self.user,
password=self.password,
database=self.database,
2020-10-19 17:46:55 -04:00
# pool_name = "hblink_master",
# pool_size = 2
2020-09-29 08:42:07 -04:00
)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
logger.info('(MYSQL) username or password error')
return (False)
2020-09-29 08:42:07 -04:00
elif err.errno == errorcode.ER_BAD_DB_ERROR:
logger.info('(MYSQL) DB Error')
return (False)
2020-09-29 08:42:07 -04:00
else:
logger.info('(MYSQL) error: %s',err)
return(False)
2020-09-29 08:42:07 -04:00
return(True)
#Close DB connection
def close(self):
self.db.close()
#Get config from DB
def getConfig(self):
2020-09-29 08:42:07 -04:00
CONFIG = {}
CONFIG['SYSTEMS'] = {}
2020-09-29 08:42:07 -04:00
_cursor = self.db.cursor()
try:
_cursor.execute("select * from {} where MODE='MASTER'".format(self.table))
2020-09-29 08:42:07 -04:00
except mysql.connector.Error as err:
_cursor.close()
logger.info('(MYSQL) error, problem with cursor execute')
raise Exception('Problem with cursor execute')
for (callsign, mode, enabled, _repeat, max_peers, export_ambe, ip, port, passphrase, group_hangtime, use_acl, reg_acl, sub_acl, tgid_ts1_acl, tgid_ts2_acl, default_ua_timer, single_mode, voice_ident,ts1_static,ts2_static,default_reflector) in _cursor:
try:
CONFIG['SYSTEMS'].update({callsign: {
'MODE': mode,
'ENABLED': bool(enabled),
'REPEAT': bool(_repeat),
'MAX_PEERS': int(max_peers),
'IP': ip,
'PORT': int(port),
'PASSPHRASE': bytes(passphrase, 'utf-8'),
'GROUP_HANGTIME': int(group_hangtime),
'USE_ACL': bool(use_acl),
'REG_ACL': reg_acl,
'SUB_ACL': sub_acl,
'TG1_ACL': tgid_ts1_acl,
'TG2_ACL': tgid_ts2_acl,
'DEFAULT_UA_TIMER': int(default_ua_timer),
'SINGLE_MODE': bool(single_mode),
'VOICE_IDENT': bool(voice_ident),
'TS1_STATIC': ts1_static,
'TS2_STATIC': ts2_static,
2021-02-21 11:06:25 -05:00
'DEFAULT_REFLECTOR': int(default_reflector),
'GENERATOR': int(1),
}})
CONFIG['SYSTEMS'][callsign].update({'PEERS': {}})
except TypeError:
logger.info('(MYSQL) Problem with data from MySQL - TypeError, carrying on to next row')
return(CONFIG['SYSTEMS'])
2020-09-29 08:42:07 -04:00
#For testing
if __name__ == '__main__':
2020-09-29 08:42:07 -04:00
sql = useMYSQL("ip","user","pass","db")
sql.con()
print( sql.getConfig())