2012年3月25日日曜日

今さらながらvirtualenv

pythonbrewとかの方が流行なんじゃないかとは思いますが、今さらvirtualenv

Pythonの環境を切り替えるためのツールです。site-packagesのパスを切り替える感じ。それに対してpythonbrewの方は/usr/bin/pythonから丸ごと切り替える感じ。

同じpython2.7でも、使用するモジュールのバージョンを変えたいことがあると思います。

  • gaeはDjango1.2
  • herokuはDjango1.4

とか。今日はgae環境で作業、明日はheroku環境で作業、といったことがコマンド一つでできてします。それがvirtualenv。

自分としては仮想環境でOS丸ごと切り替えるのが流行だったのですが(雑でスイマセン)、herokuが推奨してたのでたまには使ってやろうかな、と。
herokuはrubyだけじゃなくてpythonもコミュニティの文化を良く理解してる感じがして、期待できそう。

そんな感じなvirtualenvのメモ。

 インストール方法 


  1. インストールはeasy_installで。これ以降easy_installは使わず、pipでインストールします。
  2. # easy_install virtualenv
    Searching for virtualenv
    Reading http://pypi.python.org/simple/virtualenv/
    Reading http://www.virtualenv.org
    Reading http://virtualenv.openplans.org
    Best match: virtualenv 1.7.1.2
    Downloading http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.7.1.2.tar.gz#md5=3be8a014c27340f48b56465f9109d9fa
    Processing virtualenv-1.7.1.2.tar.gz
    Running virtualenv-1.7.1.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-7Nfz5N/virtualenv-1.7.1.2/egg-dist-tmp-GxwbmV
    warning: no previously-included files matching '*.*' found under directory 'docs/_templates'
    Adding virtualenv 1.7.1.2 to easy-install.pth file
    Installing virtualenv script to /usr/local/bin
    
    Installed /Library/Python/2.7/site-packages/virtualenv-1.7.1.2-py2.7.egg
    Processing dependencies for virtualenv
    Finished processing dependencies for virtualenv
    
  3. 特に書くことが無い・・・


 使い方


  • 環境を作る。venvという名前でディレクトリが作成される。
    % virtualenv  venv
    New python executable in venv/bin/python
    Installing setuptools............done.
    Installing pip...............done.
    % ls
    venv/ 
    
  • 環境を切り替える。プロンプトに環境名が表示される。
    % source venv/bin/activate
    (venv)% 
    
  • パッケージのインストールはpipで行う。uninstallもできるよ!
    (venv)% pip install Django psycopg2
    Downloading/unpacking Django
      Downloading Django-1.4.tar.gz (7.6Mb): 7.6Mb downloaded
      Running setup.py egg_info for package Django
        
    Downloading/unpacking psycopg2
      Downloading psycopg2-2.4.4.tar.gz (648Kb): 648Kb downloaded
      Running setup.py egg_info for package psycopg2
        
    
    (中略)
    
        
        no previously-included directories found matching 'doc/src/_build'
    Successfully installed Django psycopg2
    Cleaning up...
    
    
    venvディレクトリのsite-packagesを見ると、djangoとpsycopg2がインストールされている。Django1.4!!
    (venv)% ls venv/lib/python2.7/site-packages/
    Django-1.4-py2.7.egg-info/     
    django/ 
    psycopg2-2.4.4-py2.7.egg-info/
    pip-1.1-py2.7.egg/        
    psycopg2/
    easy-install.pth               
    setuptools.pth
    
  • 設定について。インタープリターでsys.pathを確認してみてください。
    (venv)% python
    Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
    [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.path
    (秘密。だけど、venvの下のパスが並ぶ)
    >>> 
    
    

こりゃ簡単便利。試してみたいモジュールがあったら一つ環境を作ってインストールするとよさそう。

2012年3月22日木曜日

sshの便利な設定

便利な便利なssh。

  • 結構ハマりどころも多いので、ググって適当に設定しても上手くいかないことがある
  • たまにしか設定しないので忘れがちで、少し調べると「あー、そうだった」なんて思うことが多い

ので一通りの手順をまとめ。


パスワード無しでログイン

  1. 秘密鍵と公開鍵の作成

    ssh-keygenコマンドで秘密鍵と公開鍵を生成する。
    $ ssh-keygen -t rsa -C "myaddress@gmail.com"
    Generating public/private rsa key pair.
    Enter file in which to save the key (~/.ssh/id_rsa): 空欄でよろしい
    Enter passphrase (empty for no passphrase): 任意の文字列を入力!
    Enter same passphrase again: もう一度任意の文字列を入力!
    Your identification has been saved in ~/.ssh/id_rsa.
    Your public key has been saved in ~/.ssh/id_rsa.pub.
    The key fingerprint is:
    3f:dc:4e:ff:ff:ff:db:55:a3:52:ee:ee:ee:7a:64:47 myaddress@gmail.com
    
  2. id_rsa(秘密鍵)とid_rsa.pub(公開鍵)ができている。秘密と公開となっているがどちらも人の目に触れないように管理しなければいけないので勘違いしないよう注意。
    $ ls
    id_rsa     id_rsa.pub
    
  3. パスワード無しでログインしたいサーバーに登録する。クライアント側に秘密鍵(このままでOK)、サーバーのauthorized_keysの中に公開鍵を登録するのが最終目標。

    とりあえず一度ログインして、known_hostsにサーバーのキーを自動登録。
    $ ssh server-01
    The authenticity of host 'server01 (127.0.0.1)' can't be established.
    RSA key fingerprint is 3f:dc:4e:ff:ff:ff:db:55:a3:52:ee:ee:ee:7a:64:47.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'server01' (RSA) to the list of known hosts.
    your_name@server01's password:
    Last login: Thu Mar 22 16:18:31 2012 from 192.168.0.0
    
    $ exit
    
  4. 続いてid_rsa.pubの内容をauthorized_keysに追加。

    scpでコピーしておいてからログインし追記でもいいし、
    $ scp ~/.ssh/id_rsa.pub your_name@server01
    $ ssh your_name@server01
    your_name@server01's password:ここは普通にログインパスワード
    Last login: Thu Mar 22 16:18:31 2012 from 192.168.0.0
    $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
    
    エディタで開いてコピペでもよろしい
    $ vi ~/.ssh/authorized_keys
    
  5. 必ずパーミッションを変更する!クライアント側もサーバー側も、.sshは自分だけが見れる状態にするのを忘れずに!これを怠るとパスワード聞かれちゃうよ!
    $ chmod 700 ~/.ssh
    $ chmod 600 ~/.ssh/*
    


複数の設定を使い分ける


  • 複数のサーバーがあり、別々の秘密鍵でアクセスしたい場合がある。社内や自分で用意したサーバーだけならば全て同じ秘密鍵でもいいんだけど、githubとかは別にしておきたい、とか。

    さらに、ホスト名を短縮した形で登録する機能も併せ持っているので、
    $ ssh 長いユーザー名@長いホスト名
    
    なんて毎回打ちたくない人にもオススメ。

  • .ssh/configファイルに記述します。詳しくはman ssh-configを見てください。
    $ vi ~/.ssh/config
    
    Host myserver
        HostName        long_long_host_name.localdomain.com
        IdentityFile    ~/.ssh/id_rsa
        User            long_long_user_name
    
    Host github
        HostName        github.com
        IdentityFile    ~/.ssh/id_dsa
        User            git
        PreferredAuthentications publickey
    
  • ssh myserverだけでパスワードも聞かれずにスパーンとログインできる!
    $ ssh myserver
    Last login: Thu Mar 22 16:48:27 2012 from 127.0.0.1
    $ 
    
  • githubの接続テスト。id_dsaを使用して接続を行う!
    $ ssh -T github
    Hi sporty! You've successfully authenticated, but GitHub does not provide shell access.
    $ 
    


これだけ押さえておけば特に問題ないはず。