l'essentiel est invisible pour les yeux

Thursday, December 06, 2007

[EC2] Debian Etch AMIの作成

基本的には、こちらの手順通り実行するだけですが、/usr/lib/site_ruby/aes/amiutil/image.rbを少し編集しないとAMIが起動しません。
HOWTO Building a self-bundling Debian AMI

作成したAMI上でAMIのバンドルを可能にするためec2-ami-toolsをインストールする。


# cd /usr/local/src
# wget http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools.noarch.rpm
# alien --to-deb ec2-ami-tools.noarch.rpm
# dpkg -i ec2-ami-tools_1.3-15587_all.deb


/usr/lib/site_ruby/aes/amiutil/image.rbに対するパッチ

*** image.rb 2007-11-20 11:06:08.000000000 +0000
--- /usr/lib/site_ruby/aes/amiutil/image.rb 2007-12-06 02:59:31.000000000 +0000
***************
*** 172,183 ****
--- 172,190 ----
# Make device nodes. dev_dir = IMG_MNT + '/dev'
Dir.mkdir( dev_dir )
+
+ # Make mount point
+ FileUtils.mkdir_p(IMG_MNT + '/lib/init/rw')
+
# MAKEDEV is incredibly variable across distros, so use mknod directly.
exec("mknod #{dev_dir}/null c 1 3")
exec("mknod #{dev_dir}/zero c 1 5")
exec("mknod #{dev_dir}/tty c 5 0")
exec("mknod #{dev_dir}/console c 5 1")
exec("ln -s null #{dev_dir}/X0R")
+
+ # Make special device
+ exec("cd #{dev_dir} && /sbin/MAKEDEV std && /sbin/MAKEDEV generic")
end

#----------------------------------------------------------------------------#


Railsで使用するなら、Rakefileを作成しておきます。
コマンド例

% rake ec2:images
% rake ec2:run id=i-38f7397
% rake ec2:instances
% rake ec2:terminate id=i-38f7397
% rake ec2:console_output id=i-38f7397
% rake ec2:register manifest=my-images/debian_etch_Dec06.manifest.xml
% rake ec2:deregister id=ami-33js73
% rake ec2:login user=Gabriel


#{RAILS_ROOT}/lib/tasks/ec2.rake

# cavii, Inc
# Tasks for EC2
require 'env'
require 'erb'

namespace :ec2 do
@config ||= YAML::load(ERB.new(IO.read("#{RAILS_ROOT}/config/aws.yml")).result)

desc <<-EOH
Describe the available images to launch. You can specify the owners of images.
e.g.) rake ec2:images [id=ami_id | --all]
EOH
task :images do
id = ENV['id']
system %{cd #{ENV['EC2_HOME']} && ./bin/ec2-describe-images #{id}}
end

desc <<-EOH
Describe the current state of the specified instances(s).
e.g.) rake ec2:instances [id=i-038592]
EOH
task :instances do
system %{cd #{ENV['EC2_HOME']} && ./bin/ec2-describe-instances #{ENV['id']}}
end

desc <<-EOH
Run an instance with ami_id. You can obtain ami_id using 'rake ec2:images'.
e.g.) rake ec2:run id=ami-3683a933
EOH
task :run do
begin
ENV['key'] ||= 'gsg-keypair'
system %{cd #{ENV['EC2_HOME']} && ./bin/ec2-run-instances #{ENV['id']} -k #{ENV['key']}}
rescue
puts %{Usage: rake ec2:run id=<AMI ID>}
end
end

desc <<-EOH
Terminate the specified instance.
e.g.) rake ec2:run id=i-5ab44333
EOH
task :terminate do
begin
system %{cd #{ENV['EC2_HOME']} && ./bin/ec2-terminate-instances #{ENV['id']}}
rescue
puts "Usage: rake ec2:run id=<AMI INSTANCE ID>"
end
end

desc <<-EOH
Registers the Amazon Machine Image, and you should specify the manifest file.
e.g.) rake ec2:register manifest=mybucket/image.manifest.xml
EOH
task :register do
begin
raise unless ENV['manifest']
system %{cd #{ENV['EC2_HOME']} && ./bin/ec2-register #{ENV['manifest']}}
rescue
puts "Usage: rake ec2:register manifest=mybucket/image.manifest.xml"
end
end

desc <<-EOH
Deregister the amazon machine image.
EOH
task :deregister do
system %{cd #{ENV['EC2_HOME']} && ./bin/ec2-deregister #{ENV['id']}}
end

desc <<-EOH
Show the console output, and you should specify instance id.
EOH
task :console_output do
system %{cd #{ENV['EC2_HOME']} && ./bin/ec2-get-console-output #{ENV['id']}}
end

task :login do
user = ENV['user']? ENV['user'] : 'root'
system %{ssh -i #{@config['ec2_id_rsa']} #{user}@#{@config['primary_instance_url']}}
end

end



config/aws.yml

aws_access_key: 'XXXXXXXXXXXXXXXXX'
aws_secret_access_key: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
aws_account: 'XXXX-XXXX-XXXX'
image_bucket: "mybucket"
ec2_id_rsa: '~/ec2/id_rsa-gsg-keypair'
ec2_keypair_name: "gsg-keypair"
primary_instance_url: 'ec2-62-222-21-118.compute-1.amazonaws.com'
options:
use_ssl: true