From 0af745ff6339cf460ad04d8596bdf5350981ce1c Mon Sep 17 00:00:00 2001 From: Artur Basiak Date: Thu, 7 Apr 2016 12:35:12 +0200 Subject: [PATCH] Migrate from MySQLDB to pymysql Replacing MySQLDB with PyMySQL. PyMySQL is a drop-in replacement for MySQLdb and released under MIT License instead of GPL (OpenStack incompatible http://governance.openstack.org/reference/licensing.html). Change-Id: I76ecac112c9a5a373f2f0362de77f9e32e97511c --- monasca_agent/collector/checks_d/cacti.py | 6 +++--- monasca_agent/collector/checks_d/mysql.py | 20 ++++++++++---------- monasca_setup/detection/plugins/mysql.py | 17 ++++++++--------- tests_to_fix/test_mysql.py | 2 +- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/monasca_agent/collector/checks_d/cacti.py b/monasca_agent/collector/checks_d/cacti.py index d9975a3e..30f3b325 100644 --- a/monasca_agent/collector/checks_d/cacti.py +++ b/monasca_agent/collector/checks_d/cacti.py @@ -63,12 +63,12 @@ class Cacti(AgentCheck): # Try importing MySQL try: - import MySQLdb + import pymysql except ImportError: raise Exception( - "Cannot import MySQLdb module. This module is required for the cacti plugin to work correctly") + "Cannot import PyMySQL module. This module is required for the cacti plugin to work correctly") - connection = MySQLdb.connect(config.host, config.user, config.password, config.db) + connection = pymysql.connect(config.host, config.user, config.password, config.db) self.log.debug("Connected to MySQL to fetch Cacti metadata") diff --git a/monasca_agent/collector/checks_d/mysql.py b/monasca_agent/collector/checks_d/mysql.py index 1c78b428..a83bc62f 100644 --- a/monasca_agent/collector/checks_d/mysql.py +++ b/monasca_agent/collector/checks_d/mysql.py @@ -56,14 +56,14 @@ class MySql(checks.AgentCheck): @staticmethod def get_library_versions(): try: - import MySQLdb - version = MySQLdb.__version__ + import pymysql + version = pymysql.__version__ except ImportError: version = "Not Found" except AttributeError: version = "Unknown" - return {"MySQLdb": version} + return {"PyMySQL": version} def check(self, instance): host, port, user, password, mysql_sock, defaults_file, options = self._get_config( @@ -94,25 +94,25 @@ class MySql(checks.AgentCheck): def _connect(self, host, port, mysql_sock, user, password, defaults_file): try: - import MySQLdb + import pymysql except ImportError: raise Exception( - "Cannot import MySQLdb module. Check the instructions " - "to install this module at https://app.datadoghq.com/account/settings#integrations/mysql") + "Cannot import PyMySQl module. Check the instructions " + "to install this module at https://pypi.python.org/pypi/PyMySQL") if defaults_file != '': - db = MySQLdb.connect(read_default_file=defaults_file) + db = pymysql.connect(read_default_file=defaults_file) elif mysql_sock != '': - db = MySQLdb.connect(unix_socket=mysql_sock, + db = pymysql.connect(unix_socket=mysql_sock, user=user, passwd=password) elif port: - db = MySQLdb.connect(host=host, + db = pymysql.connect(host=host, port=port, user=user, passwd=password) else: - db = MySQLdb.connect(host=host, + db = pymysql.connect(host=host, user=user, passwd=password) self.log.debug("Connected to MySQL") diff --git a/monasca_setup/detection/plugins/mysql.py b/monasca_setup/detection/plugins/mysql.py index 3a1a1d39..52204092 100644 --- a/monasca_setup/detection/plugins/mysql.py +++ b/monasca_setup/detection/plugins/mysql.py @@ -41,11 +41,10 @@ class MySQL(monasca_setup.detection.Plugin): configured_mysql = False # Attempt login, requires either an empty root password from localhost # or relying on a configured /root/.my.cnf - if self.dependencies_installed(): # ensures MySQLdb is available - import _mysql_exceptions - import MySQLdb + if self.dependencies_installed(): # ensures PyMySQL is available + import pymysql try: - MySQLdb.connect(read_default_file=mysql_conf) + pymysql.connect(read_default_file=mysql_conf) log.info( "\tUsing client credentials from {:s}".format(mysql_conf)) # Read the mysql config file to extract the needed variables. @@ -76,24 +75,24 @@ class MySQL(monasca_setup.detection.Plugin): except IOError: log.error("\tI/O error reading {:s}".format(mysql_conf)) pass - except _mysql_exceptions.MySQLError: + except pymysql.MySQLError: log.warn("\tCould not connect to mysql using credentials from {:s}".format(mysql_conf)) pass # Try logging in as 'root' with an empty password if not configured_mysql: try: - MySQLdb.connect(host='localhost', port=3306, user='root') + pymysql.connect(host='localhost', port=3306, user='root') log.info("\tConfiguring plugin to connect with user root.") config['mysql'] = {'init_config': None, 'instances': [{'name': 'localhost', 'server': 'localhost', 'user': 'root', 'port': 3306}]} configured_mysql = True - except _mysql_exceptions.MySQLError: + except pymysql.MySQLError: log.warn("\tCould not connect to mysql using root user") pass else: - exception_msg = 'The mysql dependency MySQLdb is not installed;' \ + exception_msg = 'The mysql dependency PyMySQL is not installed;' \ ' the mysql plugin is not configured' log.error(exception_msg) raise Exception(exception_msg) @@ -108,7 +107,7 @@ class MySQL(monasca_setup.detection.Plugin): def dependencies_installed(self): try: - import MySQLdb + import pymysql except ImportError: return False diff --git a/tests_to_fix/test_mysql.py b/tests_to_fix/test_mysql.py index 79dd2f9b..e6900633 100644 --- a/tests_to_fix/test_mysql.py +++ b/tests_to_fix/test_mysql.py @@ -9,7 +9,7 @@ class TestMySql(unittest.TestCase): # This should run on pre-2.7 python so no skiptest self.skip = False try: - import MySQLdb + import pymysql except ImportError: self.skip = True