fog (1)

最近、私は仕事でChefを使いました。Chefの中のKnifeと呼ばれるコマンドラインツールでは、幾つかのクラウドサービスに対応しています。

Launch Cloud Instances with Knife - Chef - Opscode Open Source Wiki

それはRubyのライブラリ「fog」を利用することで実現していました。
そこで第1回目の今回から数回に分けて、このfogについて調べたことを書いてみようと思います。

参考になるサイト

まずはfogの公式サイトとRubyGems.orgです。

また最近、Engine Yardのブログにおいて、fogの作者であるgeemus (Wesley Beary)さんによるfogの紹介記事がありました。

fogが対応しているクラウドサービス

fogは複数のクラウドサービスに対応しているようですが、それはFog::Computeクラスの子クラスから推測できます。(残念ながら、対応リストが探し出せませんでした)私は今までAWSしか利用したことがなく、中には聞いたことがない名前のものがありましたので、全部が実在のクラウドサービスを指しているのかは調べていません。

Rubyスクリプトによるデモ

今回は手始めとして、Amazon EC2インスタンスの作成と終了までを実行してみます。

Region ap-northeast-1 (東京)
Availability Zone ap-northeast-1a
AMI ami-d8b812d9 (ebs/ubuntu-images/ubuntu-natty-11.04-i386-server-20110426)
Instance Type t1.micro

なお、「x」による伏せ字になっているところは適宜置き換えが必要です。

# -*- encoding: utf-8 -*-
require "rubygems"
require "fog"

compute = Fog::Compute.new(:provider => "AWS",
                           :aws_access_key_id => "xxxxxxxxxxxxxxxxxxxx",
                           :aws_secret_access_key => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                           :region => "ap-northeast-1")
    
puts "インスタンスを作成します"
      
server = compute.servers.create(:availability_zone => "ap-northeast-1a",
                                :flavor_id => "t1.micro",
                                :image_id => "ami-d8b812d9",
                                :key_name => "xxxxxxxxxx")
          
puts "インスタンス(#{server.identity})を作成しました"
        
server.wait_for { ready? }
        
puts "インスタンス(#{server.identity})がrunning状態になりました"
        
puts server.inspect
        
puts "OSの起動を待ちます"
        
sleep 60
        
puts "cat /etc/lsb-release コマンドを実行します"
        
puts server.ssh("cat /etc/lsb-release").first.stdout

puts "インスタンス(#{server.identity})を終了します"

server.destroy

このRubyスクリプトを実行してみた結果はこのようになりました。(一部書き換え済み)

$ ruby run_and_terminate_instance.rb
インスタンスを作成します
インスタンス(i-1e50291f)を作成しました
インスタンス(i-1e50291f)がrunning状態になりました
  <Fog::Compute::AWS::Server
    id="i-1e50291f",
    ami_launch_index=0,
    availability_zone="ap-northeast-1a",
    block_device_mapping=[{"deviceName"=>"/dev/sda1", "volumeId"=>"vol-a87b97c2", "status"=>"attached", "attachTime"=>2011-06-30 16:59:56 UTC, "deleteOnTermination"=>true}],
    client_token=nil,
    dns_name="ec2-175-41-199-148.ap-northeast-1.compute.amazonaws.com",
    groups=[nil],
    flavor_id="t1.micro",
    image_id="ami-d8b812d9",
    kernel_id="aki-d209a2d3",
    key_name="xxxxxxxxxx",
    created_at=2011-06-30 16:59:37 UTC,
    monitoring=[{"state"=>false}],
    product_codes=[],
    private_dns_name="ip-10-146-90-204.ap-northeast-1.compute.internal",
    private_ip_address="10.146.90.204",
    public_ip_address="175.41.199.148",
    ramdisk_id=nil,
    reason=nil,
    root_device_name=nil,
    root_device_type="ebs",
    state="running",
    state_reason={},
    subnet_id=nil,
    tags={},
    user_data=nil
  >
OSの起動を待ちます
cat /etc/lsb-release コマンドを実行します
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=11.04
DISTRIB_CODENAME=natty
DISTRIB_DESCRIPTION="Ubuntu 11.04"
インスタンス(i-1e50291f)を終了します

まとめ

簡単なコードでAmazon EC2インスタンスの作成から終了までが実現できます。
次回は、fog内部のコードに踏み込んで調べてみたいと思います。