Skip to content

Instantly share code, notes, and snippets.

@buzztaiki
Last active August 29, 2015 13:57
Show Gist options
  • Save buzztaiki/9460078 to your computer and use it in GitHub Desktop.
Save buzztaiki/9460078 to your computer and use it in GitHub Desktop.
CentOS6.5でfabricを動かす

結論: pycryptoを2.5に落とせ。

やったこと。

fab deploy とかしたらこんなことを言われた

Traceback (most recent call last):
  File "/usr/bin/fab", line 9, in <module>
    load_entry_point('Fabric==1.8.2', 'console_scripts', 'fab')()
  File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 299, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 2229, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 1948, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/usr/lib/python2.6/site-packages/fabric/main.py", line 19, in <module>
    from fabric import api, state, colors
  File "/usr/lib/python2.6/site-packages/fabric/api.py", line 9, in <module>
    from fabric.context_managers import (cd, hide, settings, show, path, prefix,
  File "/usr/lib/python2.6/site-packages/fabric/context_managers.py", line 42, in <module>
    from fabric.state import output, win32, connections, env
  File "/usr/lib/python2.6/site-packages/fabric/state.py", line 9, in <module>
    from fabric.network import HostConnectionCache, ssh
  File "/usr/lib/python2.6/site-packages/fabric/network.py", line 24, in <module>
    import paramiko as ssh
  File "/usr/lib/python2.6/site-packages/paramiko/__init__.py", line 65, in <module>
    from transport import SecurityOptions, Transport
  File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 33, in <module>
    from paramiko import util
  File "/usr/lib/python2.6/site-packages/paramiko/util.py", line 33, in <module>
    from paramiko.common import *
  File "/usr/lib/python2.6/site-packages/paramiko/common.py", line 98, in <module>
    from Crypto import Random
  File "/usr/lib64/python2.6/site-packages/Crypto/Random/__init__.py", line 29, in <module>
    from Crypto.Random import _UserFriendlyRNG
  File "/usr/lib64/python2.6/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 38, in <module>
    from Crypto.Random.Fortuna import FortunaAccumulator
  File "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaAccumulator.py", line 39, in <module>
    import FortunaGenerator
  File "/usr/lib64/python2.6/site-packages/Crypto/Random/Fortuna/FortunaGenerator.py", line 34, in <module>
    from Crypto.Util.number import ceil_shift, exact_log2, exact_div
  File "/usr/lib64/python2.6/site-packages/Crypto/Util/number.py", line 56, in <module>
    if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:
AttributeError: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'

pycrypto が 文句を言ってるらしい。 http://stackoverflow.com/questions/17319033/fixing-warning-gmp-or-mpir-library-not-found-not-building-crypto-publickkey とか。

mpz-devel, gmp-devel を入れたり pycrypto をビルドしなおしたりとかしたけど、いまいち状況変わらず。

しかたないので pycrypto のバージョンを下げてみる。

sudo easy_install 'pycrypto==2.5'

fabric が動いた。

@t2y
Copy link

t2y commented Mar 10, 2014

基本的には上記の stackoverflow のページの内容をやったら CentOS 6.5 で Pycrypto 2.6 が動きました。
関係ないかもですが、以下のページに Python のリビルドも必要とあったのでそこだけ追加でやりました。

http://wiki.ducca.org/wiki/paramikoのインストール

$ lsb_release -a
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: CentOS
Description:    CentOS release 6.5 (Final)
Release:    6.5
Codename:   Final

$ pip freeze 
Fabric==1.8.2
ecdsa==0.10
paramiko==1.12.2
pycrypto==2.6.1

$ fab deploy
No hosts found. Please specify (single) host string for connection: localhost
[localhost] Login password for 'root': 
[localhost] put: test.py -> /tmp/test.py
Done.

@t2y
Copy link

t2y commented Mar 10, 2014

その他調べた細かいことを書いとく。

gmp5 をインストールする

CentOS 6.x に標準のリポジトリにある gmp4 をインストールして pycrypto をビルドしたらこんなエラーが出た。

$ yum -y install gmp gmp-devel mpz
$ rpm -q gmp gmp-devel
gmp-4.3.1-7.el6_2.2.x86_64
gmp-devel-4.3.1-7.el6_2.2.x86_64

$ python
>>> from Crypto.PublicKey import _fastmath
>>> import Crypto.Random
/root/.virtualenvs/fabric/lib/python2.6/site-packages/Crypto/Util/number.py:57: PowmInsecureWarning: Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.
  _warn("Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.", PowmInsecureWarning)
>>> _fastmath.HAVE_DECL_MPZ_POWM_SEC
0  # 有効じゃない?

$ fab deploy
/root/.virtualenvs/fabric/lib/python2.6/site-packages/Crypto/Util/number.py:57: PowmInsecureWarning: Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.
  _warn("Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.", PowmInsecureWarning)

Fatal error: Couldn't find any fabfiles!
Remember that -f can be used to specify fabfile path, and use -h for help.

gmp5 にしろと怒られた。
標準/epel のリポジトリには gmp5 がなかったので fedora20 の srpm をもってきてビルドすることにした。

http://ftp.iij.ad.jp/pub/linux/fedora/releases/20/Fedora/source/SRPMS/g/

$ rpm -ivh gmp-5.1.2-2.fc20.src.rpm 
$ cd rpmbuild/
$ vi SPECS/gmp.spec
--- SPECS/gmp_orig.spec 2014-03-11 04:32:00.932935145 +0900
+++ SPECS/gmp.spec  2014-03-11 04:26:39.129035380 +0900
@@ -19,7 +19,7 @@
 BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
 BuildRequires: autoconf automake libtool
 #autoreconf on arm needs:
-BuildRequires: perl-Carp
+BuildRequires: perl  # perl-Carp は CentOS6.x には perl パッケージに含まれている

$ rpmbuild -ba SPECS/gmp.spec  # ビルドはできたけど、、、
$ rpm -Uvh RPMS/x86_64/gmp-*.rpm  # この依存関係を変えるのはちょっと危険、、、
エラー: 依存性の欠如:
    libgmp.so.3()(64bit) は (インストール済み)coreutils-8.4-31.el6.x86_64 に必要とされています
    libgmp.so.3()(64bit) は (インストール済み)ppl-0.10.2-11.el6.x86_64 に必要とされています
    libgmp.so.3()(64bit) は (インストール済み)cloog-ppl-0.15.7-1.2.el6.x86_64 に必要とされています
    libgmp.so.3()(64bit) は (インストール済み)mpfr-2.4.1-6.el6.x86_64 に必要とされています
    libgmp.so.3()(64bit) は (インストール済み)cpp-4.4.7-4.el6.x86_64 に必要とされています
    libgmp.so.3()(64bit) は (インストール済み)gcc-c++-4.4.7-4.el6.x86_64 に必要とされています

$ rpm -qpl RPMS/x86_64/gmp-5.1.2-2.el6.x86_64.rpm  # バージョンもかなり違う、、、
/usr/lib64/libgmp.so.10
/usr/lib64/libgmp.so.10.1.2
/usr/lib64/libgmpxx.so.4
/usr/lib64/libgmpxx.so.4.3.2
/usr/share/doc/gmp-5.1.2
/usr/share/doc/gmp-5.1.2/COPYING
/usr/share/doc/gmp-5.1.2/COPYING.LIB
/usr/share/doc/gmp-5.1.2/NEWS
/usr/share/doc/gmp-5.1.2/README

rpm で gmp5 を入れるのは諦めた。もしパッケージで管理するとしたら、既存の gmp4 に影響を与えない gmp5 パッケージを作って pycrypto のビルドのために追加しとくだけにする。

ソースをダウンロードしてきて普通にビルドしてインストールした。

$ tar jxvf gmp-5.1.3.tar.bz2 
$ cd gmp-5.1.3
$ ./configure
$ make && make check && make install
$ /sbin/ldconfig

@t2y
Copy link

t2y commented Mar 10, 2014

これは関係ないかもだけど、、、

Python をリビルドする

$ vi /etc/yum.repos.d/CentOS-Base.repo  # ソースリポジトリを追加する
# sources
[base-source]
name=CentOS-$releasever - Base Source
baseurl=http://vault.centos.org/$releasever/os/Source/
gpgcheck=1
enabled=0
gpgkey=http://vault.centos.org/RPM-GPG-KEY-CentOS-6

[updates-source]
name=CentOS-$releasever - Updates Source
baseurl=http://vault.centos.org/$releasever/updates/Source/
gpgcheck=1
enabled=0
gpgkey=http://vault.centos.org/RPM-GPG-KEY-CentOS-6

[extras-source]
name=CentOS-$releasever - Extras Source
baseurl=http://vault.centos.org/$releasever/extras/Source/
gpgcheck=1
enabled=0
gpgkey=http://vault.centos.org/RPM-GPG-KEY-CentOS-6

$ yum -y install yum-utils
$ yumdownloader --releasever=6.5 --source python
$ rpm -ivh python-2.6.6-52.el6.src.rpm && cd rpmbuild
$ rpmbuild -ba SPECS/python.spec
...
伸張ファイルの検査中: /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/python-2.6.6-52.el6.x86_64
エラー: インストール済み(ただし未伸張)ファイルが見つかりました:
   /usr/lib/debug/usr/lib64/libpython2.6.so.1.0.debug-gdb.py
   /usr/lib/debug/usr/lib64/libpython2.6.so.1.0.debug-gdb.pyc
   /usr/lib/debug/usr/lib64/libpython2.6.so.1.0.debug-gdb.pyo

RPM ビルドエラー:
    インストール済み(ただし未伸張)ファイルが見つかりました:
   /usr/lib/debug/usr/lib64/libpython2.6.so.1.0.debug-gdb.py
   /usr/lib/debug/usr/lib64/libpython2.6.so.1.0.debug-gdb.pyc
   /usr/lib/debug/usr/lib64/libpython2.6.so.1.0.debug-gdb.pyo

$ # spec ファイルの内容と実際のパッケージングされたファイルに差分があるとこのエラーが出るらしい
$ rpmbuild -ba --define="__check_files %{nil}" SPECS/python.spec  # このオプションで無視できるらしい
$ rpm -Uvh --force RPMS/x86_64/python-*.rpm RPMS/x86_64/tkinter-2.6.6-52.el6.x86_64.rpm

@t2y
Copy link

t2y commented Mar 10, 2014

pycrypto のビルド

gmp5 が /usr/local にインストールされてるからそっちを参照するように setup.py を修正する。

$ pip install --download ./ pycrypto
$ tar zxvf pycrypto-2.6.1.tar.gz 
$ cd pycrypto-2.6.1
$ vi setup.py 
--- setup_orig.py   2014-03-11 06:01:42.535911507 +0900
+++ setup.py    2014-03-11 06:01:48.258638836 +0900
@@ -370,7 +370,8 @@
       'ext_modules': plat_ext + [
             # _fastmath (uses GNU mp library)
             Extension("Crypto.PublicKey._fastmath",
-                      include_dirs=['src/','/usr/include/'],
+                      include_dirs=['src/','/usr/local/include','/usr/include/'],
+                      library_dirs=['/usr/local/lib'],
                       libraries=['gmp'],
                       sources=["src/_fastmath.c"]),

$ python setup.py build
$ python setup.py install

有効になった?

$ python
Python 2.6.6 (r266:84292, Mar 11 2014, 05:37:58) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Crypto.PublicKey import _fastmath
>>> import Crypto.Random
>>> _fastmath.HAVE_DECL_MPZ_POWM_SEC
1

@buzztaiki
Copy link
Author

お。ありがとうございます。
gmp5 をビルドしないといけないのですねぇ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment