2010年12月24日金曜日

Rackspace Cloud導入記その8(APIを使う)

RackspaceがCloudkickを買収して少し楽しみな感じになってきましたね。
そして私は、Rackspaceを使っていた事をすっかり忘れていました。
Rackspaceへの興味はうすれていますが、APIくらいは使ってみよう!
そんな訳でCloudServersのRubyインターフェースAPIについて書きたいと思います。
ま、そうは言ってもREADMEに書かれている内容をそのまま実行しただけです(汗)

※APIを使用する為にはAPI keyとユーザー名が必要となります。
API keyの確認方法はこちらに記載しています。

まずは環境の準備
ついでにテスト環境のRubyとGemのバージョンをあげています。

Rubyを1.8.7へ
  1. # cd /usr/local/src/  
  2. # cd ruby-1.8.7/i386/  
  3. # rpm -Uvh ruby-libs-1.8.7.249-4.i386.rpm ¥  
  4. ruby-devel-1.8.7.249-4.i386.rpm ¥  
  5. ruby-rdoc-1.8.7.249-4.i386.rpm ¥  
  6. ruby-1.8.7.249-4.i386.rpm ¥  
  7. ruby-irb-1.8.7.249-4.i386.rpm  

Gemを1.3.7へ Gemが入ってない方はインストールして下さい。RubyGems
  1. # gem update --system  
  2. # gem -v  
  3. 1.3.7  

GemにGithubを登録するとAPIをインストールできます
  1. # gem sources -a http://gems.github.comhttp://gems.github.com added to sources  
  2. # gem install cloudservers  
  3. Building native extensions.  This could take a while...  
  4. Successfully installed json-1.4.6  
  5. Successfully installed cloudservers-0.3.0  
  6. 2 gems installed  
  7. Installing ri documentation for json-1.4.6...  
  8. Installing ri documentation for cloudservers-0.3.0...  
  9. Installing RDoc documentation for json-1.4.6...  
  10. Installing RDoc documentation for cloudservers-0.3.0...  

ユーザーoko_changになって試してみます。
  1. # su - oko_chang  

動作確認程度なのでirbで行います
  1. #irbを実行  
  2. $ irb  
  3.   
  4. #rubygemとcloudserversライブラリを読み込みます  
  5. > require 'rubygems'  
  6. => true  
  7. > require 'cloudservers'  
  8. => true  
  9.   
  10. CloudServers::Connectionクラスのオブジェクトを作ります  
  11. #オブジェクトの生成(ログイン)にはusernameとapi_keyを指定する事が必須です  
  12. > cs = CloudServers::Connection.new(:username => 'oko_chang':api_key => '********')  
  13. => #<cloudservers::server:0xb79c2374 .............@flavorid="1"></cloudservers::server:0xb79c2374>  
  14.   
  15. #自分の全サーバーをリスト表示させます  
  16. > cs.servers  
  17. => [{:name=>"develop-server":id=>272817}]  
  18.   
  19. #特定サーバーの名前を表示させます  
  20. > server = cs.server(272817)  
  21. > server.name  
  22. => "develop-server"  
  23.   
  24. #サーバーのタイプを表示させます  
  25. > server.flavor.name  
  26. => "256 server"  
  27. > server.image.name  
  28. => "CentOS 5.5"  
  29.   
  30. #サーバーを再起動させます  
  31. > server.reboot  
  32. => true  
  33.   
  34. 新しいサーバーを起動さるイメージを表示  
  35. > image = cs.get_image(51)  
  36. => #<cloudservers::image:0xb79b3004 ,="" ......@status="ACTIVE" @created="#<DateTime:" @serverid="nil"></cloudservers::image:0xb79b3004>  
  37. > image.name  
  38. => "CentOS 5.5"  
  39.   
  40. #新しいサーバーのスペックを表示  
  41. > flavor = cs.get_flavor(1)  
  42. => #<cloudservers::flavor:0xb79aa0bc @disk="10," @id="1," @name="256 server" @ram="256,"></cloudservers::flavor:0xb79aa0bc>  
  43. > flavor.name  
  44. => "256 server"  
  45.   
  46. #新しいサーバを起動  
  47. > newserver = cs.create_server(:name => "New Server":imageId => image.id, :flavorId => flavor.id)  
  48. => #<cloudservers::server:0xb799ce6c @addresses="{:public=">["184.106.204.113"], :private=>["10.179.101.68"]},........, @flavorId=1></cloudservers::server:0xb799ce6c>  
  49. > newserver.status  
  50. => "BUILD"  
  51. > newserver.progress  
  52. => 0  
  53. > newserver.adminPass  
  54. => "NewServerJ347qTcfL"  
  55. > newserver.refresh  
  56. => true  
  57. > newserver.progress  
  58. => 100  
  59.   
  60. #起動したサーバを削除  
  61. > newserver.delete!  
  62. => true  

以上で簡単なサーバの起動と停止を行う事が出来ました。
思ったよりも良い感じですね。ちょっと工夫をしたら色々な事に使えそうです。
さて、今年はこれが最後の更新となると思います。
もし、このブログを見た方は良い年末と年始をお過ごし下さい。
あ、そだそだ。メリークリスマス・イヴ。

2010年12月17日金曜日

githubへの初回pushでsshエラー

gitをテスト環境に入れてからgithubにpushしようとしたら、少しつまずいたのでメモ。
githubの始め方はこちらのブログで分かりやすく説明されているので割愛します。
gitのインストールとgithubリポジトリ(project_hoge)の作成は事前に完了しておいて下さい。

  • githubへのssh接続に使う秘密鍵と公開鍵のペアを作成
  • ※名前は自分で指定してますが、それがすべての始まり
    ※公開鍵はgithubに追加してくださいね
    1. $ ssh-keygen -t rsa -C "github-hoge"  
    2. Generating public/private rsa key pair.  
    3. Enter file in which to save the key (/home/hoge/.ssh/id_rsa): /home/hoge/.ssh/hoge_rsa  
    4. Enter passphrase (empty for no passphrase):パスフレーズ  
    5. Enter same passphrase again:パスフレーズ  
    6. Your identification has been saved in /home/hoge/.ssh/hoge_rsa.  
    7. Your public key has been saved in /home/hoge/.ssh/hoge_rsa.pub.  
    8. The key fingerprint is:  
  • gitの初期設定を行います
    1. $ git config --global user.name "hoge"  
    2. $ git config --global user.email "hoge@example.com"  
    3. $ git config --global color.ui auto  
  • git設定の確認
    1. $ cat -n .gitconfig  
    2.      1     [user]  
    3.      2          name = hoge  
    4.      3          email = hoge@example.com  
    5.      4     [color]  
    6.      5          ui = auto  
  • リポジトリの初期化、基本設定確認
    1. $ mkdir project_hoge  
    2. $ cd project_hoge/  
    3. $ git init  
    4. Initialized empty Git repository in /home/hoge/project_hoge/.git/  
    5. $ git var GIT_COMMITTER_IDENT  
    6. hoge <hoge@example.com> 1292429310 +0900  
    7. $ git var GIT_AUTHOR_IDENT  
    8. hoge <hoge@example.com> 1292429366 +0900  
    9. </hoge@example.com></hoge@example.com>  
  • README作成、最初のコミット
    1. $ touch README  
    2. $ git add README  
    3. $ git commit -m 'first commit'  
    4. [master (root-commit) 92e6b51] first commit  
    5.  1 files changed, 256 insertions(+), 0 deletions(-)  
    6.  create mode 100644 README  
  • githubをリモートリポジトリに追加
    1. $ git remote add origin git@github.com:hoge/project_hoge.git  
  • pushしようとするとエラーが発生!
    1. $ git push origin master  
    2. Permission denied (publickey).  
    3. fatal: The remote end hung up unexpectedly  
    SSHの鍵をデフォルトの名前にしなかった事があやしいです。
    ここらへん見るとそれらしき事が書いてます。
  • .ssh/configで使用する鍵を指定します
    1. $ vi /home/hoge/.ssh/config  
    2.       1 Host                 github.com  
    3.       2 User                 git  
    4.       3 Port                 22  
    5.       4 Hostname             github.com  
    6.       5 IdentityFile         ~/.ssh/hoge_rsa  
    7.       6 TCPKeepAlive         yes  
    8.       7 IdentitiesOnly       yes  
    9.   
    10. $ chmod 600 .ssh/config   
  • 再トライをすると無事に接続が出来ます
    1. $ git push origin master  
    2. Enter passphrase for key '/home/hoge/.ssh/hoge_rsa':  
    3. Counting objects: 3, done.  
    4. Compressing objects: 100% (2/2), done.  
    5. Writing objects: 100% (3/3), 4.57 KiB, done.  
    6. Total 3 (delta 0), reused 0 (delta 0)  
    7. To git@github.com:hoge/project_hoge.git  
    8.  * [new branch]      master -> master  


以上、メモでしたー。
今日はクラウド勉強会に参加していましたが、とても勉強になりました。
おそらく年内は最後かもですが、勉強会はとても刺激になるので来年も積極的に参加したいですね。

2010年12月15日水曜日

CentOS5.5でgit1.7.3のrpmを作成する

そうだ!ブログやってたんだ!
相変わらずの気まぐれ更新になってしまった。
勉強会とかに参加した感想をもっとキチンと書きたいなー。
ブログがある事を思い出す為にもCentOS5.5でgit1.7.3のrpmを作る手順をまとめとく。

こちらのブログが非常に手順としては参考になった。
というか手順はまんまです。

必要になりそうなパッケージをとりあえず一気にインストール

  1. # yum -y groupinstall "Development Tools"  
  2. # yum -y groupinstall "Development Libraries"  

その他以下のパッケージは必須なのでインストール

  1. # yum -y install zlib-devel openssl-devel curl-devel expat-devel gettext xmlto gettext-devel  

EPELからダウンロードしてインストールするパッケージもあります

  1. # cd /usr/local/src/  
  2. # wget http://ftp.iij.ad.jp/pub/linux/fedora/epel/5Server/i386/asciidoc-8.1.0-1.el5.noarch.rpm  
  3. # wget ftp://ftp.iij.ad.jp/pub/linux/fedora/epel/5Server/i386/perl-Error-0.17010-1.el5.noarch.rpm  
  4. # rpm -ivh ./asciidoc-8.1.0-1.el5.noarch.rpm  
  5. # rpm -ivh ./perl-Error-0.17010-1.el5.noarch.rpm  

パッケージの作成ディレクトリにソースをダウンロード

  1. # cd /usr/src/redhat/SOURCES/  
  2. # wget http://www.kernel.org/pub/software/scm/git/git-1.7.3.tar.gz  

specファイルを取り出してSPECSディレクトリに移動

  1. # tar zxvf ./git-1.7.3.tar.gz git-1.7.3/git.spec  
  2. git-1.7.3/git.spec  
  3. # mv ./git-1.7.3/git.spec /usr/src/redhat/SPECS/  

不要なディレクトリは削除します

  1. # rm -rf ./git-1.7.3  

SPECSディレクトリに移動してビルド開始

  1. # cd /usr/src/redhat/SPECS/  
  2. # rpmbuild -bb --clean git.spec  

こんな感じで作成されます

  1. # tree /usr/src/redhat/RPMS/  
  2. /usr/src/redhat/RPMS/  
  3. |-- athlon  
  4. |-- geode  
  5. |-- i386  
  6. |   |-- git-1.7.3-1.i386.rpm  
  7. |   |-- git-all-1.7.3-1.i386.rpm  
  8. |   |-- git-arch-1.7.3-1.i386.rpm  
  9. |   |-- git-cvs-1.7.3-1.i386.rpm  
  10. |   |-- git-debuginfo-1.7.3-1.i386.rpm  
  11. |   |-- git-email-1.7.3-1.i386.rpm  
  12. |   |-- git-gui-1.7.3-1.i386.rpm  
  13. |   |-- git-svn-1.7.3-1.i386.rpm  
  14. |   |-- gitk-1.7.3-1.i386.rpm  
  15. |   |-- gitweb-1.7.3-1.i386.rpm  
  16. |   `-- perl-Git-1.7.3-1.i386.rpm  
  17. |-- i486  
  18. |-- i586  
  19. |-- i686  
  20. `-- noarch  
  21. 7 directories, 11 files  

あとはrpmコマンドでインストールすればOKです


すぐに消すテスト環境なのでrootユーザーで作業をしています。
各自の環境でビルドする時は一般ユーザーでビルドして下さい。