Merge pull request #1054 from wido/CLOUDSTACK-8818

CLOUDSTACK-8818: Use MySQL native connector with PythonMySQLdb has been deprecated and is also not supported in Python 3.

mysql.connector is a connector written in Python which talks the
native MySQL protocol without any external code.

https://dev.mysql.com/doc/connector-python/en/

* pr/1054:
  CLOUDSTACK-8818: Use MySQL native connector with Python

Signed-off-by: Will Stevens <williamstevens@gmail.com>
This commit is contained in:
Will Stevens 2016-05-10 23:54:22 -04:00
commit d2a95b4c69
8 changed files with 38 additions and 39 deletions

View File

@ -24,7 +24,7 @@ import glob
from random import choice from random import choice
import string import string
from optparse import OptionParser from optparse import OptionParser
import MySQLdb import mysql.connector
import paramiko import paramiko
from threading import Thread from threading import Thread
@ -59,7 +59,7 @@ parser.add_option("-a", "--all", action="store_true", dest="all", default=False,
def e(msg): parser.error(msg) def e(msg): parser.error(msg)
def getknownhosts(host,username,password): def getknownhosts(host,username,password):
conn = MySQLdb.connect(host=host,user=username,passwd=password) conn = mysql.connector.connect(host=host, user=username, password=password)
cur = conn.cursor() cur = conn.cursor()
cur.execute("SELECT h.private_ip_address,d.value FROM cloud.host h inner join cloud.host_details d on (h.id = d.host_id) where d.name = 'username' and setup = 1") cur.execute("SELECT h.private_ip_address,d.value FROM cloud.host h inner join cloud.host_details d on (h.id = d.host_id) where d.name = 'username' and setup = 1")
usernames = dict(cur.fetchall()) usernames = dict(cur.fetchall())

4
debian/control vendored
View File

@ -3,7 +3,7 @@ Section: libs
Priority: extra Priority: extra
Maintainer: Wido den Hollander <wido@widodh.nl> Maintainer: Wido den Hollander <wido@widodh.nl>
Build-Depends: debhelper (>= 9), openjdk-8-jdk | openjdk-7-jdk, genisoimage, Build-Depends: debhelper (>= 9), openjdk-8-jdk | openjdk-7-jdk, genisoimage,
python-mysqldb, maven (>= 3) | maven3, python (>= 2.7) python-mysql.connector, maven (>= 3) | maven3, python (>= 2.7)
Standards-Version: 3.8.1 Standards-Version: 3.8.1
Homepage: http://www.cloudstack.org/ Homepage: http://www.cloudstack.org/
@ -15,7 +15,7 @@ Description: A common package which contains files which are shared by several C
Package: cloudstack-management Package: cloudstack-management
Architecture: all Architecture: all
Depends: ${misc:Depends}, ${python:Depends}, cloudstack-common (= ${source:Version}), tomcat6, sudo, jsvc, python-mysqldb, libmysql-java, augeas-tools, mysql-client, adduser, bzip2 Depends: ${misc:Depends}, ${python:Depends}, cloudstack-common (= ${source:Version}), tomcat6, sudo, jsvc, python-mysql.connector, libmysql-java, augeas-tools, mysql-client, adduser, bzip2
Conflicts: cloud-server, cloud-client, cloud-client-ui Conflicts: cloud-server, cloud-client, cloud-client-ui
Description: CloudStack server library Description: CloudStack server library
The CloudStack management server The CloudStack management server

View File

@ -50,7 +50,7 @@ BuildRequires: jpackage-utils
BuildRequires: gcc BuildRequires: gcc
BuildRequires: glibc-devel BuildRequires: glibc-devel
BuildRequires: /usr/bin/mkisofs BuildRequires: /usr/bin/mkisofs
BuildRequires: MySQL-python BuildRequires: mysql-connector-python
#BuildRequires: maven => 3.0.0 #BuildRequires: maven => 3.0.0
%description %description
@ -79,7 +79,7 @@ Requires: /sbin/service
Requires: /sbin/chkconfig Requires: /sbin/chkconfig
Requires: /usr/bin/ssh-keygen Requires: /usr/bin/ssh-keygen
Requires: mkisofs Requires: mkisofs
Requires: MySQL-python Requires: mysql-connector-python
Requires: python-paramiko Requires: python-paramiko
Requires: ipmitool Requires: ipmitool
Requires: %{name}-common = %{_ver} Requires: %{name}-common = %{_ver}

View File

@ -50,7 +50,7 @@ BuildRequires: jpackage-utils
BuildRequires: gcc BuildRequires: gcc
BuildRequires: glibc-devel BuildRequires: glibc-devel
BuildRequires: /usr/bin/mkisofs BuildRequires: /usr/bin/mkisofs
BuildRequires: MySQL-python BuildRequires: mysql-connector-python
BuildRequires: maven => 3.0.0 BuildRequires: maven => 3.0.0
%description %description
@ -79,7 +79,7 @@ Requires: /sbin/service
Requires: /sbin/chkconfig Requires: /sbin/chkconfig
Requires: /usr/bin/ssh-keygen Requires: /usr/bin/ssh-keygen
Requires: mkisofs Requires: mkisofs
Requires: MySQL-python Requires: mysql-connector-python
Requires: ipmitool Requires: ipmitool
Requires: %{name}-common = %{_ver} Requires: %{name}-common = %{_ver}
Requires: iptables-services Requires: iptables-services

View File

@ -50,7 +50,7 @@ BuildRequires: jpackage-utils
BuildRequires: gcc BuildRequires: gcc
BuildRequires: glibc-devel BuildRequires: glibc-devel
BuildRequires: /usr/bin/mkisofs BuildRequires: /usr/bin/mkisofs
BuildRequires: MySQL-python BuildRequires: mysql-connector-python
#BuildRequires: maven => 3.0.0 #BuildRequires: maven => 3.0.0
%description %description
@ -79,7 +79,7 @@ Requires: /sbin/service
Requires: /sbin/chkconfig Requires: /sbin/chkconfig
Requires: /usr/bin/ssh-keygen Requires: /usr/bin/ssh-keygen
Requires: mkisofs Requires: mkisofs
Requires: MySQL-python Requires: mysql-connector-python
Requires: python-paramiko Requires: python-paramiko
Requires: ipmitool Requires: ipmitool
Requires: %{name}-common = %{_ver} Requires: %{name}-common = %{_ver}

View File

@ -50,7 +50,7 @@ BuildRequires: jpackage-utils
BuildRequires: gcc BuildRequires: gcc
BuildRequires: glibc-devel BuildRequires: glibc-devel
BuildRequires: /usr/bin/mkisofs BuildRequires: /usr/bin/mkisofs
BuildRequires: MySQL-python BuildRequires: mysql-connector-python
#BuildRequires: maven => 3.0.0 #BuildRequires: maven => 3.0.0
%description %description
@ -79,7 +79,7 @@ Requires: /sbin/service
Requires: /sbin/chkconfig Requires: /sbin/chkconfig
Requires: /usr/bin/ssh-keygen Requires: /usr/bin/ssh-keygen
Requires: mkisofs Requires: mkisofs
Requires: MySQL-python Requires: mysql-connector-python
Requires: python-paramiko Requires: python-paramiko
Requires: ipmitool Requires: ipmitool
Requires: %{name}-common = %{_ver} Requires: %{name}-common = %{_ver}

View File

@ -5,21 +5,21 @@
# to you under the Apache License, Version 2.0 (the # to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance # "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at # with the License. You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, # Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an # software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the # KIND, either express or implied. See the License for the
# specific language governing permissions and limitations # specific language governing permissions and limitations
# under the License. # under the License.
import MySQLdb
import os import os
from utilities import bash from utilities import bash
from cloudException import CloudRuntimeException from cloudException import CloudRuntimeException
import sys import mysql.connector
class Database:
class Database(object):
"""Database connection""" """Database connection"""
def __init__(self, username, password=None, host='localhost', port='3306', db="cloud"): def __init__(self, username, password=None, host='localhost', port='3306', db="cloud"):
self.host = host self.host = host
@ -28,15 +28,16 @@ class Database:
self.port = port self.port = port
self.db = db self.db = db
def connect(self):
return mysql.connector.connect(host=self.host,
user=self.username,
password=self.password,
database=self.db)
def execute(self, statement): def execute(self, statement):
txn = None txn = None
try: try:
if self.password is not None: txn = self.connect()
txn = MySQLdb.Connect(host=self.host, user=self.username,
passwd=self.password, db=self.db)
else:
txn = MySQLdb.Connect(host=self.host, user=self.username,
db=self.db)
cursor = txn.cursor() cursor = txn.cursor()
cursor.execute(statement) cursor.execute(statement)
cursor.close() cursor.close()
@ -47,35 +48,33 @@ class Database:
except: except:
pass pass
except: except:
raise CloudRuntimeException("Failed to execute: %s " % statement)
finally:
if txn is not None: if txn is not None:
try: try:
txn.close() txn.close()
except: except:
pass pass
raise CloudRuntimeException("Failed to execute:%s"%statement)
def testConnection(self): def testConnection(self):
try: try:
if self.password is not None: conn = self.connect()
db = MySQLdb.Connect(host=self.host, user=self.username, conn.ping()
passwd=self.password, db=self.db) conn.close()
else:
db = MySQLdb.Connect(host=self.host, user=self.username,
db=self.db)
return True return True
except: except:
raise CloudRuntimeException("Failed to Connect to DB") raise CloudRuntimeException("Failed to Connect to DB")
def executeFromFile(self, file): def executeFromFile(self, file):
if not os.path.exists(file): if not os.path.exists(file):
return False return False
cmdLine = "mysql --host=" + self.host + " --port=" + str(self.port) + " --user=" + self.username cmdLine = "mysql --host=" + self.host + " --port=" + str(self.port) + " --user=" + self.username
if self.password is not None: if self.password is not None:
cmdLine += " --password=" + self.password cmdLine += " --password=" + self.password
cmdLine += " < " + file cmdLine += " < " + file
try: try:
bash(cmdLine) bash(cmdLine)
except: except:

View File

@ -20,7 +20,7 @@
import os,logging,sys import os,logging,sys
from optparse import OptionParser from optparse import OptionParser
import MySQLdb import mysql.connector
import subprocess import subprocess
import glob import glob
@ -72,10 +72,10 @@ class CloudContext(cloud_utils.MigrationContext):
self.database = database self.database = database
self.configdir = configdir self.configdir = configdir
self.resourcedir = resourcedir self.resourcedir = resourcedir
self.conn = MySQLdb.connect(host=self.host, self.conn = mysql.connector.connect(host=self.host,
user=self.username, user=self.username,
passwd=self.password, password=self.password,
db=self.database, database=self.database,
port=self.port) port=self.port)
self.conn.autocommit(False) self.conn.autocommit(False)
self.db = self.conn.cursor() self.db = self.conn.cursor()