Created
March 5, 2009 03:50
-
-
Save danmayer/74180 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
EC2_HOME = '~/.ec2' | |
use Rack::Auth::Basic do |username, password| | |
[username, password] == ['some_user', 'some_pass'] | |
end | |
get "/" do | |
@links = %w{describe_ec2s restart_all_ec2s shutdown_all_ec2s}.map { |cmd| | |
cmd_link(cmd) | |
}.join | |
erb :index | |
end | |
get "/describe_ec2s" do | |
results = `cd #{EC2_HOME}; ec2-describe-instances` | |
instances = results.scan(/INSTANCE\ti-\w*/).each{|i| i.sub!("INSTANCE\t",'')} | |
@links = instances.map { |i| | |
instance_link(i) | |
}.join | |
erb :index | |
end | |
get "/restart_all_ec2s" do | |
@results = `cd #{EC2_HOME}; ec2-describe-instances` | |
instances = @results.scan(/INSTANCE\ti-\w*/).each{|i| i.sub!("INSTANCE\t",'')} | |
cmd="cd #{EC2_HOME}; ec2-reboot-instances #{instances.join(' ')}" | |
@results = `cmd` | |
erb :index | |
end | |
get "/shutdown_all_ec2s" do | |
@results = `cd #{EC2_HOME}; ec2-describe-instances` | |
instances = @results.scan(/INSTANCE\ti-\w*/).each{|i| i.sub!("INSTANCE\t",'')} | |
cmd="cd #{EC2_HOME}; ec2-terminate-instances #{instances.join(' ')}" | |
@results = `cmd` | |
erb :index | |
end | |
get "/instance/:id" do | |
id = params[:id] if params[:id] | |
verify_id(id) | |
@results = `cd #{EC2_HOME}; ec2-describe-instances #{id}` | |
@links = "<li><a href='/shutdown/#{id}' target='_self'>shutdown #{id}</a></li>" | |
@links += " <li><a href='/reboot/#{id}' target='_self'>reboot #{id}</a></li>" | |
erb :index | |
end | |
get "/reboot/:id" do | |
id = params[:id] if params[:id] | |
verify_id(id) | |
@results = `cd #{EC2_HOME}; ec2-reboot-instances #{id}` | |
erb :index | |
end | |
get "/shutdown/:id" do | |
id = params[:id] if params[:id] | |
verify_id(id) | |
@results = `cd #{EC2_HOME}; ec2-terminate-instances #{id}` | |
erb :index | |
end | |
helpers do | |
def cmd_link(cmd) | |
"<li><a href='#{cmd}' target='_self'>#{cmd}</a></li>" | |
end | |
def instance_link(instance) | |
"<li><a href='/instance/#{instance}' target='_self'>#{instance}</a></li>" | |
end | |
def verify_id(id) | |
raise Sinatra::ServerError, 'bad-id, What you doin?' unless id.match(/i-\w*/) | |
end | |
end | |
use_in_file_templates! | |
__END__ | |
@@ index | |
<html> | |
<head> | |
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/> | |
<style type="text/css" media="screen">@import "/stylesheets/iui.css";</style> | |
<script type="application/x-javascript" src="/javascripts/iui.js"></script> | |
</head> | |
<body> | |
<div class="toolbar"> | |
<h1 id="pageTitle"></h1> | |
</div> | |
<% if(@links) %> | |
<ul id="home" title="ec2 cmds" selected="true"> | |
<li><a href='/' target='_self'>home</a></li> | |
<%= @links %> | |
<% unless(@results) %> | |
</ul> | |
<% end %> | |
<% end %> | |
<% if(@results) %> | |
<% if(@links) %> | |
<li><strong>results</strong></li> | |
<% else %> | |
<ul id="home" title="results" selected="true"> | |
<li><a href='/' target='_self'>home</a></li> | |
<% end %> | |
<%= @results.gsub("\n","<br/>") %> | |
</ul> | |
<% end %> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment