Skip to content

Commit 92e3ba2

Browse files
committed
CLOUDSTACK-8818: Use MySQL native connector with Python
MySQLdb 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/
1 parent 6d0c92b commit 92e3ba2

8 files changed

Lines changed: 38 additions & 39 deletions

File tree

client/bindir/cloud-update-xenserver-licenses.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import glob
2424
from random import choice
2525
import string
2626
from optparse import OptionParser
27-
import MySQLdb
27+
import mysql.connector
2828
import paramiko
2929
from threading import Thread
3030

@@ -59,7 +59,7 @@ parser.add_option("-a", "--all", action="store_true", dest="all", default=False,
5959
def e(msg): parser.error(msg)
6060

6161
def getknownhosts(host,username,password):
62-
conn = MySQLdb.connect(host=host,user=username,passwd=password)
62+
conn = mysql.connector.connect(host=host, user=username, password=password)
6363
cur = conn.cursor()
6464
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")
6565
usernames = dict(cur.fetchall())

debian/control

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Section: libs
33
Priority: extra
44
Maintainer: Wido den Hollander <wido@widodh.nl>
55
Build-Depends: debhelper (>= 9), openjdk-8-jdk | openjdk-7-jdk, genisoimage,
6-
python-mysqldb, maven (>= 3) | maven3, python (>= 2.7)
6+
python-mysql.connector, maven (>= 3) | maven3, python (>= 2.7)
77
Standards-Version: 3.8.1
88
Homepage: http://www.cloudstack.org/
99

@@ -15,7 +15,7 @@ Description: A common package which contains files which are shared by several C
1515

1616
Package: cloudstack-management
1717
Architecture: all
18-
Depends: ${misc:Depends}, ${python:Depends}, cloudstack-common (= ${source:Version}), tomcat6, sudo, jsvc, python-mysqldb, libmysql-java, augeas-tools, mysql-client, adduser, bzip2
18+
Depends: ${misc:Depends}, ${python:Depends}, cloudstack-common (= ${source:Version}), tomcat6, sudo, jsvc, python-mysql.connector, libmysql-java, augeas-tools, mysql-client, adduser, bzip2
1919
Conflicts: cloud-server, cloud-client, cloud-client-ui
2020
Description: CloudStack server library
2121
The CloudStack management server

packaging/centos63/cloud.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ BuildRequires: jpackage-utils
5050
BuildRequires: gcc
5151
BuildRequires: glibc-devel
5252
BuildRequires: /usr/bin/mkisofs
53-
BuildRequires: MySQL-python
53+
BuildRequires: mysql-connector-python
5454
#BuildRequires: maven => 3.0.0
5555

5656
%description
@@ -79,7 +79,7 @@ Requires: /sbin/service
7979
Requires: /sbin/chkconfig
8080
Requires: /usr/bin/ssh-keygen
8181
Requires: mkisofs
82-
Requires: MySQL-python
82+
Requires: mysql-connector-python
8383
Requires: python-paramiko
8484
Requires: ipmitool
8585
Requires: %{name}-common = %{_ver}

packaging/centos7/cloud.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ BuildRequires: jpackage-utils
5050
BuildRequires: gcc
5151
BuildRequires: glibc-devel
5252
BuildRequires: /usr/bin/mkisofs
53-
BuildRequires: MySQL-python
53+
BuildRequires: mysql-connector-python
5454
BuildRequires: maven => 3.0.0
5555

5656
%description
@@ -79,7 +79,7 @@ Requires: /sbin/service
7979
Requires: /sbin/chkconfig
8080
Requires: /usr/bin/ssh-keygen
8181
Requires: mkisofs
82-
Requires: MySQL-python
82+
Requires: mysql-connector-python
8383
Requires: ipmitool
8484
Requires: %{name}-common = %{_ver}
8585
Requires: iptables-services

packaging/fedora20/cloud.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ BuildRequires: jpackage-utils
5050
BuildRequires: gcc
5151
BuildRequires: glibc-devel
5252
BuildRequires: /usr/bin/mkisofs
53-
BuildRequires: MySQL-python
53+
BuildRequires: mysql-connector-python
5454
#BuildRequires: maven => 3.0.0
5555

5656
%description
@@ -79,7 +79,7 @@ Requires: /sbin/service
7979
Requires: /sbin/chkconfig
8080
Requires: /usr/bin/ssh-keygen
8181
Requires: mkisofs
82-
Requires: MySQL-python
82+
Requires: mysql-connector-python
8383
Requires: python-paramiko
8484
Requires: ipmitool
8585
Requires: %{name}-common = %{_ver}

packaging/fedora21/cloud.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ BuildRequires: jpackage-utils
5050
BuildRequires: gcc
5151
BuildRequires: glibc-devel
5252
BuildRequires: /usr/bin/mkisofs
53-
BuildRequires: MySQL-python
53+
BuildRequires: mysql-connector-python
5454
#BuildRequires: maven => 3.0.0
5555

5656
%description
@@ -79,7 +79,7 @@ Requires: /sbin/service
7979
Requires: /sbin/chkconfig
8080
Requires: /usr/bin/ssh-keygen
8181
Requires: mkisofs
82-
Requires: MySQL-python
82+
Requires: mysql-connector-python
8383
Requires: python-paramiko
8484
Requires: ipmitool
8585
Requires: %{name}-common = %{_ver}

python/lib/cloudutils/db.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
# to you under the Apache License, Version 2.0 (the
66
# "License"); you may not use this file except in compliance
77
# with the License. You may obtain a copy of the License at
8-
#
8+
#
99
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
10+
#
1111
# Unless required by applicable law or agreed to in writing,
1212
# software distributed under the License is distributed on an
1313
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
import MySQLdb
1817
import os
1918
from utilities import bash
2019
from cloudException import CloudRuntimeException
21-
import sys
22-
class Database:
20+
import mysql.connector
21+
22+
class Database(object):
2323
"""Database connection"""
2424
def __init__(self, username, password=None, host='localhost', port='3306', db="cloud"):
2525
self.host = host
@@ -28,15 +28,16 @@ def __init__(self, username, password=None, host='localhost', port='3306', db="c
2828
self.port = port
2929
self.db = db
3030

31+
def connect(self):
32+
return mysql.connector.connect(host=self.host,
33+
user=self.username,
34+
password=self.password,
35+
database=self.db)
36+
3137
def execute(self, statement):
3238
txn = None
3339
try:
34-
if self.password is not None:
35-
txn = MySQLdb.Connect(host=self.host, user=self.username,
36-
passwd=self.password, db=self.db)
37-
else:
38-
txn = MySQLdb.Connect(host=self.host, user=self.username,
39-
db=self.db)
40+
txn = self.connect()
4041
cursor = txn.cursor()
4142
cursor.execute(statement)
4243
cursor.close()
@@ -47,35 +48,33 @@ def execute(self, statement):
4748
except:
4849
pass
4950
except:
51+
raise CloudRuntimeException("Failed to execute: %s " % statement)
52+
finally:
5053
if txn is not None:
5154
try:
5255
txn.close()
5356
except:
5457
pass
55-
raise CloudRuntimeException("Failed to execute:%s"%statement)
56-
58+
5759
def testConnection(self):
5860
try:
59-
if self.password is not None:
60-
db = MySQLdb.Connect(host=self.host, user=self.username,
61-
passwd=self.password, db=self.db)
62-
else:
63-
db = MySQLdb.Connect(host=self.host, user=self.username,
64-
db=self.db)
61+
conn = self.connect()
62+
conn.ping()
63+
conn.close()
6564
return True
6665
except:
6766
raise CloudRuntimeException("Failed to Connect to DB")
68-
67+
6968
def executeFromFile(self, file):
7069
if not os.path.exists(file):
7170
return False
72-
71+
7372
cmdLine = "mysql --host=" + self.host + " --port=" + str(self.port) + " --user=" + self.username
7473
if self.password is not None:
7574
cmdLine += " --password=" + self.password
76-
75+
7776
cmdLine += " < " + file
78-
77+
7978
try:
8079
bash(cmdLine)
8180
except:

setup/bindir/cloud-migrate-databases.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import os,logging,sys
2222
from optparse import OptionParser
23-
import MySQLdb
23+
import mysql.connector
2424
import subprocess
2525
import glob
2626

@@ -72,10 +72,10 @@ class CloudContext(cloud_utils.MigrationContext):
7272
self.database = database
7373
self.configdir = configdir
7474
self.resourcedir = resourcedir
75-
self.conn = MySQLdb.connect(host=self.host,
75+
self.conn = mysql.connector.connect(host=self.host,
7676
user=self.username,
77-
passwd=self.password,
78-
db=self.database,
77+
password=self.password,
78+
database=self.database,
7979
port=self.port)
8080
self.conn.autocommit(False)
8181
self.db = self.conn.cursor()

0 commit comments

Comments
 (0)