thirose’s blog

openstackやpythonなどなど

Drone CIで Python-MySQLを使おうとしたら少しハマったのでその対応

今までunittestとかなかったpython2で動いてたスクリプトがあって、あまりにも変更するのが怖いからとりあえずテスト書いて、CIで動くと便利だよねーって思い DroneCIの設定を始めました。 まずはこんな感じかなとdrone.ymlを準備しました。

---
kind: pipeline
name: Unit tests

steps:
  - name: execute unit test
    image: python:2
    commands:
    - pip install --upgrade pip
    - pip install MySQL-Python sqlparse pings paramiko cryptography mock
    - python -m unittest discover .
    when:
      event: [push, pull_request]

そのとき、MySQL-Pythonでこけました。以下のようななんかパッケージがたりてないようなエラーにはまりました。

Failed to build MySQL-Python
94  Installing collected packages: MySQL-Python, sqlparse, pings, ipaddress, pycparser, cffi, enum34, cryptography, pynacl, bcrypt, paramiko, funcsigs, mock
95      Running setup.py install for MySQL-Python: started
96      Running setup.py install for MySQL-Python: finished with status 'error'
97      ERROR: Command errored out with exit status 1:
98       command: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-MVeMp_/mysql-python/setup.py'"'"'; __file__='"'"'/tmp/pip-install-MVeMp_/mysql-python/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-avyllU/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python2.7/MySQL-Python
99           cwd: /tmp/pip-install-MVeMp_/mysql-python/
100     Complete output (38 lines):
101     running install
102     running build
103     running build_py
104     creating build
105     creating build/lib.linux-x86_64-2.7
106     copying _mysql_exceptions.py -> build/lib.linux-x86_64-2.7
107     creating build/lib.linux-x86_64-2.7/MySQLdb
108     copying MySQLdb/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb
109     copying MySQLdb/converters.py -> build/lib.linux-x86_64-2.7/MySQLdb
110     copying MySQLdb/connections.py -> build/lib.linux-x86_64-2.7/MySQLdb
111     copying MySQLdb/cursors.py -> build/lib.linux-x86_64-2.7/MySQLdb
112     copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
113     copying MySQLdb/times.py -> build/lib.linux-x86_64-2.7/MySQLdb
114     creating build/lib.linux-x86_64-2.7/MySQLdb/constants
115     copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
116     copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
117     copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
118     copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
119     copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
120     copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
121     copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
122     running build_ext
123     building '_mysql' extension
124     creating build/temp.linux-x86_64-2.7
125     gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mariadb -I/usr/include/mariadb/mysql -I/usr/local/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o
126     In file included from _mysql.c:44:
127     /usr/include/mariadb/my_config.h:3:2: warning: #warning This file should not be included by clients, include only <mysql.h> [-Wcpp]
128      #warning This file should not be included by clients, include only <mysql.h>
129       ^~~~~~~
130     In file included from _mysql.c:46:
131     /usr/include/mariadb/mysql.h:444:3: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
132        MYSQL_CLIENT_PLUGIN_HEADER
133        ^~~~~~~~~~~~~~~~~~~~~~~~~~
134     _mysql.c: In function ‘_mysql_ConnectionObject_ping’:
135     _mysql.c:2005:41: error: ‘MYSQL’ {aka ‘struct st_mysql’} has no member named ‘reconnect’
136       if ( reconnect != -1 ) self->connection.reconnect = reconnect;
137                                              ^
138     error: command 'gcc' failed with exit status 1
139     ----------------------------------------
140 ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-MVeMp_/mysql-python/setup.py'"'"'; __file__='"'"'/tmp/pip-install-MVeMp_/mysql-python/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-avyllU/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python2.7/MySQL-Python Check the logs for full command output.

普通に調べると、CentOSなら yum -y install python-devel mysql-devel gcc をインストールする必要があるというのですが、dockerのpython:2のイメージでは、それは無理。
ということで、centos:centos7に変更して実行することにしました。

そして、centos:centos7のイメージでは python-pipをインストールできないので、epel-releaseを事前にインストールする必要があります。

---
kind: pipeline
name: Unit tests

steps:
  - name: execute unit test
    image: centos:centos7
    commands:
      - yum -y install python-devel mysql-devel gcc 
      - yum -y install epel-release && yum clean all 
      - yum -y install python2-pip
      - pip install --upgrade pip 
      - pip install MySQL-Python sqlparse pings paramiko cryptography mock
      - python -m unittest discover .
    when:
      event: [push, pull_request]

早くpython3で動くように書き換えたいですね。その前にlintチェックも入れてこれもdroneでチェックできるようにする番だ。