Permalink
Browse files

Added a find method to pick up AMIs by their AWS ID

  • Loading branch information...
1 parent 04e5e96 commit d9158542a73eeab58edbd84a0ab518b9a4c82279 @johnfink8 committed Mar 22, 2012
View
3 README
@@ -19,12 +19,11 @@ If you've ever used an official Ubuntu EC2 image, you know why.
What it does not do (yet):
* Have any controllers or views.
-* Have a method to find an AMI that isn't already in the database.
* Anything useful, really, outside of irb. Even that is spurious.
What it does do:
- instance=Ec2Ami.find('whatever').run_server
+ instance=Ec2Ami.find('ami-some_aws_id').run_server
# wait for it to come up, otherwise you'll get an exception
instance.fix_username!
#It's got a bang on it because it'll make a change to the model
@@ -1,4 +0,0 @@
-class HomeController < ApplicationController
- def index
- end
-end
@@ -1,83 +0,0 @@
-class PostsController < ApplicationController
- # GET /posts
- # GET /posts.json
- def index
- @posts = Post.all
-
- respond_to do |format|
- format.html # index.html.erb
- format.json { render :json => @posts }
- end
- end
-
- # GET /posts/1
- # GET /posts/1.json
- def show
- @post = Post.find(params[:id])
-
- respond_to do |format|
- format.html # show.html.erb
- format.json { render :json => @post }
- end
- end
-
- # GET /posts/new
- # GET /posts/new.json
- def new
- @post = Post.new
-
- respond_to do |format|
- format.html # new.html.erb
- format.json { render :json => @post }
- end
- end
-
- # GET /posts/1/edit
- def edit
- @post = Post.find(params[:id])
- end
-
- # POST /posts
- # POST /posts.json
- def create
- @post = Post.new(params[:post])
-
- respond_to do |format|
- if @post.save
- format.html { redirect_to @post, :notice => 'Post was successfully created.' }
- format.json { render :json => @post, :status => :created, :location => @post }
- else
- format.html { render :action => "new" }
- format.json { render :json => @post.errors, :status => :unprocessable_entity }
- end
- end
- end
-
- # PUT /posts/1
- # PUT /posts/1.json
- def update
- @post = Post.find(params[:id])
-
- respond_to do |format|
- if @post.update_attributes(params[:post])
- format.html { redirect_to @post, :notice => 'Post was successfully updated.' }
- format.json { head :no_content }
- else
- format.html { render :action => "edit" }
- format.json { render :json => @post.errors, :status => :unprocessable_entity }
- end
- end
- end
-
- # DELETE /posts/1
- # DELETE /posts/1.json
- def destroy
- @post = Post.find(params[:id])
- @post.destroy
-
- respond_to do |format|
- format.html { redirect_to posts_url }
- format.json { head :no_content }
- end
- end
-end
View
@@ -17,4 +17,17 @@ def run_server(key=nil)
:key_pair_id => key.id,
:username => username)
end
+
+ def self.find(target)
+ begin
+ super(target)
+ rescue
+ ami=Ec2Ami.where('amiID=?',target)[0]
+ return ami unless ami.nil?
+ ec2=AWS::EC2.new
+ ami=ec2.images[target]
+ Ec2Ami.create!(:name => ami.location, :amiID => ami.id)
+ end
+ end
+
end
@@ -1,3 +0,0 @@
-<h1>Home#index</h1>
-<p>Find me in app/views/home/index.html.erb</p>
-<%= link_to "My Blog", posts_path %>
@@ -1,29 +0,0 @@
-<%= form_for(@post) do |f| %>
- <% if @post.errors.any? %>
- <div id="error_explanation">
- <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
-
- <ul>
- <% @post.errors.full_messages.each do |msg| %>
- <li><%= msg %></li>
- <% end %>
- </ul>
- </div>
- <% end %>
-
- <div class="field">
- <%= f.label :name %><br />
- <%= f.text_field :name %>
- </div>
- <div class="field">
- <%= f.label :title %><br />
- <%= f.text_field :title %>
- </div>
- <div class="field">
- <%= f.label :content %><br />
- <%= f.text_area :content %>
- </div>
- <div class="actions">
- <%= f.submit %>
- </div>
-<% end %>
@@ -1,6 +0,0 @@
-<h1>Editing post</h1>
-
-<%= render 'form' %>
-
-<%= link_to 'Show', @post %> |
-<%= link_to 'Back', posts_path %>
@@ -1,27 +0,0 @@
-<h1>Listing posts</h1>
-
-<table>
- <tr>
- <th>Name</th>
- <th>Title</th>
- <th>Content</th>
- <th></th>
- <th></th>
- <th></th>
- </tr>
-
-<% @posts.each do |post| %>
- <tr>
- <td><%= post.name %></td>
- <td><%= post.title %></td>
- <td><%= post.content %></td>
- <td><%= link_to 'Show', post %></td>
- <td><%= link_to 'Edit', edit_post_path(post) %></td>
- <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
- </tr>
-<% end %>
-</table>
-
-<br />
-
-<%= link_to 'New Post', new_post_path %>
@@ -1,5 +0,0 @@
-<h1>New post</h1>
-
-<%= render 'form' %>
-
-<%= link_to 'Back', posts_path %>
@@ -1,20 +0,0 @@
-<p id="notice"><%= notice %></p>
-
-<p>
- <b>Name:</b>
- <%= @post.name %>
-</p>
-
-<p>
- <b>Title:</b>
- <%= @post.title %>
-</p>
-
-<p>
- <b>Content:</b>
- <%= @post.content %>
-</p>
-
-
-<%= link_to 'Edit', edit_post_path(@post) %> |
-<%= link_to 'Back', posts_path %>
View
@@ -16,7 +16,7 @@
t.string "name"
t.string "amiID"
end
-
+ add_index "ec2_amis", ["amiID"], :name => "index_ec2_amis_on_amiID"
create_table "ec2_instances", :force => true do |t|
t.string "name"

0 comments on commit d915854

Please sign in to comment.