config['end'] = '2017-01-01 23:59:59' config['devices'] = ['c098e5700000'] config['locations'] = ['0'] config['sum'] = False config['tag'] = '' # Check device list query_powerblade = False query_blees = False query_ligeiro = False query_blink = False # Set up connection aws_login = mylogin.get_login_info('aws') aws_db = MySQLdb.connect(aws_login['host'], aws_login['user'], aws_login['passwd'], 'powerblade') aws_c = aws_db.cursor() connected = True qu_saveDir = '' def check_tag(): global qu_saveDir global config query_startTime = datetime.utcnow().strftime('%H_%M_%S') qu_saveDir = master_saveDir + str(query_startTime) qu_saveDir += '_l' for loc in config['locations']: qu_saveDir += loc qu_saveDir += '_' + config['type']
aws_db.commit() success += 1 duration = chop_microseconds(datetime.utcnow() - script_start) remaining = chop_microseconds(((datetime.utcnow() - script_start)*total_uploading/success)-(datetime.utcnow() - script_start)) sys.stdout.write('Filling ' + deviceType + ' - ' + str(round(100*float(success)/total_uploading,2)) + ' pct complete in ' + \ str(duration) + ', ' + str(remaining) + ' remaining \r') return success # Set up connection aws_login = mylogin.get_login_info('aws') aws_db = MySQLdb.connect(aws_login['host'], aws_login['user'], aws_login['passwd'], 'powerblade') aws_c = aws_db.cursor() # userInput = pytch.input_loop('\nWarning: This will overwrite the contens of temp_dat_occ_vector. Cancel? (y/n): ') # if userInput == 'y': # exit() # # Erase the target table # print("Erasing...") # aws_c.execute('delete from temp_dat_occ_vector where id>1;') # aws_c.fetchall() # exit()
def get_mysql_conn(config): """Configure the MySQL database connection.""" #mysql_conn_type = config.get('mysql', 'mysql_conn_type', 0) #mysql_user = config.get('mysql', 'mysql_user', 0) #mysql_pwd = config.get('mysql', 'mysql_pwd', 0) mysql_conn_type = config['mysql'].get('mysql_conn_type') mysql_user = config['mysql'].get('mysql_user') mysql_pwd = config['mysql'].get('mysql_pwd') if mysql_user == '': mysql_user = getpass.getuser() if mysql_conn_type == 'pyodbc': mysql_pwd = get_mysql_pwd(config) mysql_dsn = config['mysql'].get('mysql_dsn') # Create database connection. import pyodbc DB_URI = "mysql+pyodbc://{user}:{password}@{dsn}" conn = create_engine( DB_URI.format(user=mysql_user, password=mysql_pwd, dsn=mysql_dsn)) return (conn) else: # Try to read encrypted MySQL password from ~/.mylogin.cnf and mysql_path. mysql_path = config['mysql'].get('mysql_path') if mysql_pwd == '': if mysql_path != '': # Read encrypted password and decrypt it with mylogin module. # While better than clear-text, be careful about securing the pw file. # However, it's probably the best method for unattended use. try: # Get encrypted password. This requires the mylogin module. import mylogin #mysql_host = config.get('mysql', 'mysql_host', 0) mysql_host = config['mysql'].get('mysql_host') login = mylogin.get_login_info(mysql_path, host=mysql_host) mysql_pwd = login['passwd'] except mylogin.exception.UtilError as err: print(("mylogin error: {0}".format(err))) else: mysql_pwd = get_mysql_pwd(config) # Import packages. import mysql.connector from mysql.connector.constants import ClientFlag # Get SSL settings (file paths to SSL keys and certs). #ssl_ca = config.get('mysql-ssl', 'ssl_ca', 0) #ssl_cert = config.get('mysql-ssl', 'ssl_cert', 0) #ssl_key = config.get('mysql-ssl', 'ssl_key', 0) ssl_ca = config['mysql-ssl'].get('ssl_ca') ssl_cert = config['mysql-ssl'].get('ssl_cert') ssl_key = config['mysql-ssl'].get('ssl_key') # Check for existence of SSL files. for file_path in (ssl_ca, ssl_cert, ssl_key): if not os.path.exists(file_path): message = "Error: Can't find: %s! Check config!" % (file_path) logging.critical(message) raise OSError(message) # Create a dict of SSL settings to pass to create_engine(). ssl_args = { 'client_flags': [ClientFlag.SSL], 'ssl_ca': ssl_ca, 'ssl_cert': ssl_cert, 'ssl_key': ssl_key, } # Create database connection. #mysql_host = config.get('mysql', 'mysql_host', 0) #mysql_port = config.get('mysql', 'mysql_port', 0) #mysql_db = config.get('mysql', 'mysql_db', 0) mysql_host = config['mysql'].get('mysql_host') mysql_port = config['mysql'].get('mysql_port') mysql_db = config['mysql'].get('mysql_db') DB_URI = "mysql+mysqlconnector://{user}:{password}@{host}:{port}/{db}" conn = create_engine(DB_URI.format(user=mysql_user, password=mysql_pwd, host=mysql_host, port=mysql_port, db=mysql_db), connect_args=ssl_args) return (conn)