Seminar report (Building a Linux server and AWS SDK for Ruby)
Amazon EC2 を利用したサーバ構築セミナーの実践報告.それと... AWS SDK for Ruby を使用したEC2インスタンス管理.
Seminar report of building a Linux server using Amazon EC2 and management of EC2 instances using AWS SDK for Ruby.
クラウドサービスについて確認
• クラウドの「構成要素」
- SaaS (Software as a Service)
- PaaS (Platform as a Service)
- IaaS (Infrastructure as a Service)
アプリケーション
ミドルウェア
OS SaaS
PaaS
CPU/メモリ/HDD IaaS
ネットワーク
K-Ruby, 15th. September 27, 2012 7
12年10月1日月曜日
main_controller.rb
class MainController < ApplicationController
INSTANCES_YAML_PATH = File.join(Rails.root, "config", "ec2.yml")
def index
instances = []
@infos = Hash.new{ |h, k| h[k] = {}}
all = YAML.load_file(INSTANCES_YAML_PATH)
@instance_ids = all["instance_id"]
ec2 = AWS::EC2.new
@instance_ids.each do |instance_id|
instances << ec2.instances[instance_id]
end
instances.each do |ins|
@infos[ins.id]["tag"] = ins.tags["ccsemi"]
@infos[ins.id]["status"] = ins.status
@infos[ins.id]["public_dns_name"] = ins.public_dns_name
@infos[ins.id]["public_ip_address"] = ins.public_ip_address
end
end
K-Ruby, 15th. September 27, 2012 48
12年10月1日月曜日
49.
main_controller.rb(続き)
def stop_instance
id = params[:id]
ec2 = AWS::EC2.new
instance = ec2.instances[id]
if instance.status.to_s == "running"
instance.stop
end
redirect_to main_path
end
def start_instance
id = params[:id]
ec2 = AWS::EC2.new
instance = ec2.instances[id]
if instance.status.to_s == "stopped"
instance.start
end
redirect_to main_path
end
end
K-Ruby, 15th. September 27, 2012 49
12年10月1日月曜日
50.
<table border="1">
index.html.erb
<tr>
<th>Instance ID</th>
<th>Tag name</th>
<th>Status</th>
<th>Action</th>
<th>Public DNS name</th>
<th>Public IP address</th>
</tr>
<% @instance_ids.each do |id| %>
<tr>
<td><%= id %></td>
<td><%= @infos[id]["tag"] %></td>
<td><%= @infos[id]["status"] %></td>
<td>
<%=
if @infos[id]["status"].to_s == "running" then
button_to "Stop", stop_instance_path(:id => id)
elsif @infos[id]["status"].to_s == "stopped" then
button_to "Start", start_instance_path(:id => id)
end
%>
</td>
<td><%= @infos[id]["public_dns_name"] %></td>
<td><%= @infos[id]["public_ip_address"] %></td>
</tr>
<% end %>
</table>
K-Ruby, 15th. September 27, 2012 50
12年10月1日月曜日
51.
routes.rb
Ccaws::Application.routes.draw do
root :to => 'main#index'
match 'main' => 'main#index'
match 'stop_instance/:id' => 'main#stop_instance', :as => 'stop_instance'
match 'start_instance/:id' => 'main#start_instance', :as => 'start_instance'
end
ルーティング設定.URLパターンは3つ
Railsサーバの起動
$ bundle exec rails s
Rails.root
config app
routes.rb
controllers
ec2.yml main_controller.rb
views
initializers
aws.rb main
index.html.erb
K-Ruby, 15th. September 27, 2012 51
12年10月1日月曜日