Skip to content

Commit

Permalink
pushed forward snapshot and display tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
fields committed Jun 4, 2011
1 parent 9774954 commit 150e7e5
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 16 deletions.
14 changes: 12 additions & 2 deletions app/controllers/instances_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
include InstanceUtil

class InstancesController < ApplicationController
layout 'default_layout'

def index
setup_instance_hashes

Expand All @@ -14,7 +16,6 @@ def make_hosts
@instances.each{|x| Resolv::DNS.new.each_address(x[:dns_name]) { |addr| @addrs[x[:aws_instance_id]] = addr.to_s }}
end


def instance_list
@instances = EC2.describe_instances
csv_string = FasterCSV.generate do |csv|
Expand Down Expand Up @@ -69,7 +70,6 @@ def graph_layout
output = redirect { g.output(:output => "png") }
send_data output, :filename => "aws_graph_#{Time.now.strftime("%Y%m%d")}.png", :type => 'image/png', :disposition => 'inline'
end

end


Expand All @@ -84,6 +84,12 @@ def prune_snapshots
redirect_to '/instances' and return
end

def destroy_oldest_snapshots
delete_oldest_snapshots(params[:id])
redirect_to '/instances' and return
end


def destroy_snapshot
result = delete_snapshot(params[:id])
redirect_to '/instances' and return
Expand All @@ -109,6 +115,10 @@ def setup_instance_hashes

end

def label_for(aws_id)
Label.find_by_aws_id(aws_id).label rescue ""
end

def redirect
orig_defout = $defout
$defout = StringIO.new
Expand Down
3 changes: 3 additions & 0 deletions app/helpers/instances_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
module InstancesHelper
def label_for(aws_id)
Label.find_by_aws_id(aws_id).label rescue ""
end
end
8 changes: 8 additions & 0 deletions app/models/label.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
class Label < ActiveRecord::Base
def before_snapshot
eval self.before_snapshot_code unless self.before_snapshot_code.blank?
end

def after_snapshot
eval self.after_snapshot_code unless self.after_snapshot_code.blank?
end

end
76 changes: 65 additions & 11 deletions app/views/layouts/default_layout.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,68 @@
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>AWS Dashboard</title>
<%= javascript_include_tag :defaults %>
<%= active_scaffold_includes %>
</head>
<body>
<%#= render :partial => "toolbar" %>
<%= yield %>
</body>
</html>
<title>AWS Dashboard</title>
<%= javascript_include_tag :defaults %>
<%= active_scaffold_includes %>

<style>
BODY {
font-family: Arial, Helvetica, Geneva, Verdana;
font-size: 13px;
color: #333333;
}

.body {
font-family: Arial, Helvetica, Geneva, Verdana;
font-size: 13px;
color: #333333;
}

.title {
font-family: Times New Roman;
font-size: 16px;
color: #333333;
font-weight: bold;
}

.subtitle {
font-family: Times New Roman;
font-size: 12px;
color: #333333;
font-weight: bold;
}

A {
text-decoration: none;
}

A:link {
color: #3366cc;
text-decoration: none;
}
A:visited {
color: #663399;
text-decoration: none;
}

A:active
{
color: #cccccc;
text-decoration: none;
}

.disclaimer {
font-family: Geneva, Verdana, Arial, Helvetica;
font-size: 9px;
color: #999999;
}
</style>

</head>
<body>
<%#= render :partial => "toolbar" %>
<%= yield %>
</body>
</html>
10 changes: 10 additions & 0 deletions db/migrate/20100604163324_add_snapshot_hooks_to_labels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddSnapshotHooksToLabels < ActiveRecord::Migration
def self.up
add_column :labels, :before_snapshot_code, :text
add_column :labels, :after_snapshot_code, :text
end

def self.down
remove_column :labels, :after_snapshot_code
end
end
13 changes: 11 additions & 2 deletions lib/instance_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def delete_snapshot(snapshot_id)
def delete_old_snapshots(instance_id, num_to_keep)
@all_snapshots = EC2.describe_snapshots
snapshots = []
index_to_keep = -1 - num_to_keep
index_to_keep = 0 - num_to_keep - 1
EC2.describe_volumes.select{|x| x[:aws_attachment_status] == "attached" and x[:aws_instance_id] == instance_id}.each {|volume|
@all_snapshots.select{|x| x[:aws_volume_id] == volume[:aws_id]}.sort_by{|x| x[:aws_started_at]}[0..index_to_keep].collect{|x| x[:aws_id]}.each{|snapshot_id|
snaps = @all_snapshots.select{|x| x[:aws_volume_id] == volume[:aws_id]}.sort_by{|x| x[:aws_started_at]}
next if snaps.length < (num_to_keep.to_i * 2) + 1
snaps.values_at(num_to_keep..index_to_keep).collect{|x| x[:aws_id]}.each{|snapshot_id|
delete_snapshot(snapshot_id)
snapshots << snapshot_id
}
Expand All @@ -67,5 +69,12 @@ def delete_oldest_snapshots(instance_id, num_to_delete = 1)
snapshots
end

def delete_all_ami_instances(ami_id)
EC2.terminate_instances(EC2.describe_instances.select{|x| x[:aws_image_id] == ami_id}.select{|x| x[:aws_state] == "running"}.collect{|x| x[:aws_instance_id]})
end

def reboot_all_ami_instances(ami_id)
EC2.reboot_instances(EC2.describe_instances.select{|x| x[:aws_image_id] == ami_id}.select{|x| x[:aws_state] == "running"}.collect{|x| x[:aws_instance_id]})
end

end
2 changes: 1 addition & 1 deletion lib/tasks/instance.rake
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace :instance do
task :ssh_to_instance_id => :environment do
hosts = EC2.describe_instances.select{|x| x[:aws_state] == "running"}.collect{|x| [x[:aws_instance_id], x[:dns_name]]}
if ENV["USERNAME"].blank?
username = "maviewer"
username = "root"
else
username = ENV["USERNAME"]
end
Expand Down

0 comments on commit 150e7e5

Please sign in to comment.