Skip to content
Browse files

added working node model and routes, job.rb needs refactoring and cle…

…anup, but this Closes #2
  • Loading branch information...
1 parent 1a71e9f commit f8168aef33aa37cc5345a163702b5646c900fb54 Pete Skomoroch committed
View
99 app/controllers/nodes_controller.rb
@@ -0,0 +1,99 @@
+class NodesController < ApplicationController
+ layout 'green'
+
+
+ before_filter :find_job
+
+ # GET /nodes
+ # GET /nodes.xml
+ def index
+ @nodes = Node.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @nodes }
+ end
+ end
+
+ # GET /nodes/1
+ # GET /nodes/1.xml
+ def show
+ @node = Node.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @node }
+ end
+ end
+
+ # GET /nodes/new
+ # GET /nodes/new.xml
+ def new
+ @node = @job.nodes.build
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @node }
+ end
+ end
+
+ # GET /nodes/1/edit
+ def edit
+ @node = @job.nodes.find(params[:id])
+ end
+
+ # POST /nodes
+ # POST /nodes.xml
+ def create
+ @node = Node.new(params[:node])
+
+ respond_to do |format|
+ if (@job.nodes << @node)
+ flash[:notice] = 'Node was successfully created.'
+ format.html { redirect_to job_url(@job) }
+ format.xml { render :xml => @node, :status => :created, :location => @node }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @node.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /nodes/1
+ # PUT /nodes/1.xml
+ def update
+ @node = @job.nodes.find(params[:id])
+
+ respond_to do |format|
+ if @node.update_attributes(params[:node])
+ flash[:notice] = 'Node was successfully updated.'
+ format.html { redirect_to job_url(@job) }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @node.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /nodes/1
+ # DELETE /nodes/1.xml
+ def destroy
+ node = @job.nodes.find(params[:id])
+ @job.nodes.delete(node)
+
+ respond_to do |format|
+ format.html { redirect_to job_url(@job) }
+ format.xml { head :ok }
+ end
+ end
+
+private
+
+ def find_job
+ @job_id = params[:job_id]
+ return(redirect_to(jobs_url)) unless @job_id
+ @job = Job.find(@job_id)
+ end
+
+end
View
2 app/helpers/nodes_helper.rb
@@ -0,0 +1,2 @@
+module NodesHelper
+end
View
48 app/models/job.rb
@@ -1,4 +1,6 @@
class Job < ActiveRecord::Base
+ has_many :nodes
+
include AASM
# Set defaults
@@ -196,19 +198,37 @@ def launch_cluster
puts bootscript_content
- @master_node = @ec2.run_instances(image_id=self.master_ami_id, min_count=1, max_count=1, group_ids=[APP_CONFIG['web_security_group'], self.master_security_group], key_name=self.keypair, user_data=bootscript_content, addressing_type = 'public', instance_type = self.instance_type, kernel_id = nil, ramdisk_id = nil, availability_zone = self.availability_zone, block_device_mappings = nil)
- @master_instance_id = @master_node[0][:aws_instance_id]
- @master_instance_state = @master_node[0][:aws_state]
+ node_description = @ec2.run_instances(image_id=self.master_ami_id, min_count=1, max_count=1, group_ids=[APP_CONFIG['web_security_group'], self.master_security_group], key_name=self.keypair, user_data=bootscript_content, addressing_type = 'public', instance_type = self.instance_type, kernel_id = nil, ramdisk_id = nil, availability_zone = self.availability_zone, block_device_mappings = nil)
+
+ @node = Node.new(:job_id => self.id)
+ @node.save
+ update_node(@node, node_description)
+
+
+ @master_instance_id = node_description[0][:aws_instance_id]
+ @master_instance_state = node_description[0][:aws_state]
# wait until instance has launched, then we can set hostname etc...
while @master_instance_state != 'running'
- @master_node = @ec2.describe_instances(@master_instance_id)
- @master_instance_state = @master_node[0][:aws_state]
+ node_description = @ec2.describe_instances(@master_instance_id)
+ update_node(@node, node_description)
+ # need to set Node properties based on node_description response...
+ # self.nodes.build(node_description[0])
+ # self.nodes.build({:aws_image_id => "ami-e444444d"})
+ # acme = Company.new({:name => "Acme, Inc"})
+ # acme.employees.build({:first_name => "John"})
+ # acme.employees.build({:first_name => "Mary"})
+ # acme.employees.build({:first_name => "Sue"})
+ # acme.save
+
+
+
+ @master_instance_state = node_description[0][:aws_state]
self.save
sleep 10
end
- self.set_master_instance_metadata(@master_node)
+ self.set_master_instance_metadata(node_description)
######## Launch Worker Nodes ##########
@@ -250,6 +270,22 @@ def launch_cluster
end
end
+
+ def update_node(node, node_description)
+ node.aws_image_id = node_description[0][:aws_image_id]
+ node.aws_instance_id = node_description[0][:aws_instance_id]
+ node.aws_state = node_description[0][:aws_state]
+ node.dns_name = node_description[0][:dns_name]
+ node.ssh_key_name = node_description[0][:ssh_key_name]
+ node.aws_groups = node_description[0][:aws_groups].join(" ")
+ node.private_dns_name = node_description[0][:private_dns_name]
+ node.aws_instance_type = node_description[0][:aws_instance_type]
+ node.aws_launch_time = node_description[0][:aws_launch_time]
+ node.aws_availability_zone = node_description[0][:aws_availability_zone]
+ node.is_configured = false
+ node.save
+ end
+
def terminate_cluster_later
# push cluster termination off to background using delayed_job
View
3 app/models/node.rb
@@ -0,0 +1,3 @@
+class Node < ActiveRecord::Base
+ belongs_to :job
+end
View
34 app/views/jobs/show.html.erb
@@ -2,6 +2,7 @@
<%= link_to 'Back', jobs_path %> |
<%= link_to 'Cancel Job', cancel_job_path(@job), :method => :put if @job.is_cancellable? %>
+<h2>Job Details:</h2>
<table border="0" width="100%" cellpadding="10">
<tr>
@@ -169,3 +170,36 @@
</tr>
</table>
+<h2>Cluster Node Information:</h2>
+<table>
+<tr>
+ <th>Aws image</th>
+ <th>Aws instance</th>
+ <th>Aws state</th>
+ <th>Dns name</th>
+ <th>Ssh key name</th>
+ <th>Aws groups</th>
+ <th>Private dns name</th>
+ <th>Aws instance type</th>
+ <th>Aws launch time</th>
+ <th>availability zone</th>
+ <th>Is configured</th>
+</tr>
+
+<% @job.nodes.each do |n| %>
+ <tr>
+ <td><%=h n.aws_image_id %></td>
+ <td><%=h n.aws_instance_id %></td>
+ <td><%=h n.aws_state %></td>
+ <td><%=h n.dns_name %></td>
+ <td><%=h n.ssh_key_name %></td>
+ <td><%=h n.aws_groups %></td>
+ <td><%=h n.private_dns_name %></td>
+ <td><%=h n.aws_instance_type %></td>
+ <td><%=h n.aws_launch_time %></td>
+ <td><%=h n.aws_availability_zone %></td>
+ <td><%=h n.is_configured %></td>
+ </tr>
+<% end %>
+
+</table>
View
17 app/views/layouts/nodes.html.erb
@@ -0,0 +1,17 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+ <title>Nodes: <%= controller.action_name %></title>
+ <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield %>
+
+</body>
+</html>
View
60 app/views/nodes/edit.html.erb
@@ -0,0 +1,60 @@
+<h1>Editing node</h1>
+
+<% form_for(@node) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :job %><br />
+ <%= f.text_field :job %>
+ </p>
+ <p>
+ <%= f.label :aws_image_id %><br />
+ <%= f.text_area :aws_image_id %>
+ </p>
+ <p>
+ <%= f.label :aws_instance_id %><br />
+ <%= f.text_area :aws_instance_id %>
+ </p>
+ <p>
+ <%= f.label :aws_state %><br />
+ <%= f.text_area :aws_state %>
+ </p>
+ <p>
+ <%= f.label :dns_name %><br />
+ <%= f.text_area :dns_name %>
+ </p>
+ <p>
+ <%= f.label :ssh_key_name %><br />
+ <%= f.text_area :ssh_key_name %>
+ </p>
+ <p>
+ <%= f.label :aws_groups %><br />
+ <%= f.text_area :aws_groups %>
+ </p>
+ <p>
+ <%= f.label :private_dns_name %><br />
+ <%= f.text_area :private_dns_name %>
+ </p>
+ <p>
+ <%= f.label :aws_instance_type %><br />
+ <%= f.text_area :aws_instance_type %>
+ </p>
+ <p>
+ <%= f.label :aws_launch_time %><br />
+ <%= f.text_area :aws_launch_time %>
+ </p>
+ <p>
+ <%= f.label :aws_availability_zone %><br />
+ <%= f.text_area :aws_availability_zone %>
+ </p>
+ <p>
+ <%= f.label :is_configured %><br />
+ <%= f.check_box :is_configured %>
+ </p>
+ <p>
+ <%= f.submit 'Update' %>
+ </p>
+<% end %>
+
+<%= link_to 'Show', @node %> |
+<%= link_to 'Back', nodes_path %>
View
37 app/views/nodes/index.html.erb
@@ -0,0 +1,37 @@
+<h1>Listing nodes for Job <%= @job.id %></h1>
+
+<table>
+ <tr>
+ <th>Job</th>
+ <th>Aws image</th>
+ <th>Aws instance</th>
+ <th>Aws state</th>
+ <th>Dns name</th>
+ <th>Ssh key name</th>
+ <th>Aws groups</th>
+ <th>Private dns name</th>
+ <th>Aws instance type</th>
+ <th>Aws launch time</th>
+ <th>Aws availability zone</th>
+ <th>Is configured</th>
+ </tr>
+
+<% @job.nodes.each do |node| %>
+ <tr>
+ <td><%=h node.job_id %></td>
+ <td><%=h node.aws_image_id %></td>
+ <td><%=h node.aws_instance_id %></td>
+ <td><%=h node.aws_state %></td>
+ <td><%=h node.dns_name %></td>
+ <td><%=h node.ssh_key_name %></td>
+ <td><%=h node.aws_groups %></td>
+ <td><%=h node.private_dns_name %></td>
+ <td><%=h node.aws_instance_type %></td>
+ <td><%=h node.aws_launch_time %></td>
+ <td><%=h node.aws_availability_zone %></td>
+ <td><%=h node.is_configured %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
View
59 app/views/nodes/new.html.erb
@@ -0,0 +1,59 @@
+<h1>New node</h1>
+
+<% form_for(@node) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :job %><br />
+ <%= f.text_field :job %>
+ </p>
+ <p>
+ <%= f.label :aws_image_id %><br />
+ <%= f.text_area :aws_image_id %>
+ </p>
+ <p>
+ <%= f.label :aws_instance_id %><br />
+ <%= f.text_area :aws_instance_id %>
+ </p>
+ <p>
+ <%= f.label :aws_state %><br />
+ <%= f.text_area :aws_state %>
+ </p>
+ <p>
+ <%= f.label :dns_name %><br />
+ <%= f.text_area :dns_name %>
+ </p>
+ <p>
+ <%= f.label :ssh_key_name %><br />
+ <%= f.text_area :ssh_key_name %>
+ </p>
+ <p>
+ <%= f.label :aws_groups %><br />
+ <%= f.text_area :aws_groups %>
+ </p>
+ <p>
+ <%= f.label :private_dns_name %><br />
+ <%= f.text_area :private_dns_name %>
+ </p>
+ <p>
+ <%= f.label :aws_instance_type %><br />
+ <%= f.text_area :aws_instance_type %>
+ </p>
+ <p>
+ <%= f.label :aws_launch_time %><br />
+ <%= f.text_area :aws_launch_time %>
+ </p>
+ <p>
+ <%= f.label :aws_availability_zone %><br />
+ <%= f.text_area :aws_availability_zone %>
+ </p>
+ <p>
+ <%= f.label :is_configured %><br />
+ <%= f.check_box :is_configured %>
+ </p>
+ <p>
+ <%= f.submit 'Create' %>
+ </p>
+<% end %>
+
+<%= link_to 'Back', nodes_path %>
View
60 app/views/nodes/show.html.erb
@@ -0,0 +1,60 @@
+<p>
+ <b>Job:</b>
+ <%=h @node.job %>
+</p>
+
+<p>
+ <b>Aws image:</b>
+ <%=h @node.aws_image_id %>
+</p>
+
+<p>
+ <b>Aws instance:</b>
+ <%=h @node.aws_instance_id %>
+</p>
+
+<p>
+ <b>Aws state:</b>
+ <%=h @node.aws_state %>
+</p>
+
+<p>
+ <b>Dns name:</b>
+ <%=h @node.dns_name %>
+</p>
+
+<p>
+ <b>Ssh key name:</b>
+ <%=h @node.ssh_key_name %>
+</p>
+
+<p>
+ <b>Aws groups:</b>
+ <%=h @node.aws_groups %>
+</p>
+
+<p>
+ <b>Private dns name:</b>
+ <%=h @node.private_dns_name %>
+</p>
+
+<p>
+ <b>Aws instance type:</b>
+ <%=h @node.aws_instance_type %>
+</p>
+
+<p>
+ <b>Aws launch time:</b>
+ <%=h @node.aws_launch_time %>
+</p>
+
+<p>
+ <b>Aws availability zone:</b>
+ <%=h @node.aws_availability_zone %>
+</p>
+
+<p>
+ <b>Is configured:</b>
+ <%=h @node.is_configured %>
+</p>
+
View
3 config/routes.rb
@@ -1,6 +1,7 @@
ActionController::Routing::Routes.draw do |map|
+
map.root :controller => "jobs"
- map.resources :jobs, :member => { :cancel => :put , :nextstep => :put, :updateprogress => :put, :error => :put} , :collection => { :refresh => :get }
+ map.resources :jobs, :member => { :cancel => :put , :nextstep => :put, :updateprogress => :put, :error => :put} , :collection => { :refresh => :get }, :has_many => :nodes
# The priority is based upon order of creation: first created -> highest priority.
View
24 db/migrate/20090524202142_create_nodes.rb
@@ -0,0 +1,24 @@
+class CreateNodes < ActiveRecord::Migration
+ def self.up
+ create_table :nodes do |t|
+ t.references :job
+ t.text :aws_image_id
+ t.text :aws_instance_id
+ t.text :aws_state
+ t.text :dns_name
+ t.text :ssh_key_name
+ t.text :aws_groups
+ t.text :private_dns_name
+ t.text :aws_instance_type
+ t.text :aws_launch_time
+ t.text :aws_availability_zone
+ t.boolean :is_configured
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :nodes
+ end
+end
View
19 db/schema.rb
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20090426230408) do
+ActiveRecord::Schema.define(:version => 20090524202142) do
create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0
@@ -59,4 +59,21 @@
t.datetime "failed_at"
end
+ create_table "nodes", :force => true do |t|
+ t.integer "job_id"
+ t.text "aws_image_id"
+ t.text "aws_instance_id"
+ t.text "aws_state"
+ t.text "dns_name"
+ t.text "ssh_key_name"
+ t.text "aws_groups"
+ t.text "private_dns_name"
+ t.text "aws_instance_type"
+ t.text "aws_launch_time"
+ t.text "aws_availability_zone"
+ t.boolean "is_configured"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
end
View
34 lib/command_runner.rb
@@ -73,9 +73,11 @@ class Job < ActiveResource::Base
# node_type()
# fetch_job_attributes()
#
-# # if it is a slave, we do configure_slave_node() then exit...
-# configure_slave_node()
-#
+# # if it is a slave, we do configure_worker_node() then exit...
+# configure_worker_node()
+# hit the REST url for this job_id, use GET on custom action to find node id which matches my instance id.
+# with that node id in hand, when I'm done configuring myself, I ping the REST service custom action to update
+# is_configured to true.
# TODO , need node model in order to check cluster status in job model (node belongs to a job)
@@ -102,6 +104,12 @@ class Job < ActiveResource::Base
#
+# ./script/generate scaffold node job:references aws_image_id:text \
+# aws_instance_id:text aws_state:text dns_name:text \
+# ssh_key_name:text aws_groups:text private_dns_name:text \
+# aws_instance_type:text aws_launch_time:text aws_availability_zone:text \
+# is_configured:boolean
+
# all strings except is_configured
# job_id
@@ -116,16 +124,16 @@ class Job < ActiveResource::Base
# aws_launch_time
# aws_availability_zone
# is_configured (boolean)
-
-
-# def clusterstatus
-# @job = Job.find(params[:id])
-# configured_count = @job.nodes.count(:all, :conditions => {:is_configured => true })
-# if configured_count == @job.number_of_instances:
-# return 'ready'
-# else
-# return '#{configured_count} of #{@job.number_of_instances} nodes configured'
-# end
+# boolean
+
+def clusterstatus
+ @job = Job.find(params[:id])
+ configured_count = @job.nodes.count(:all, :conditions => {:is_configured => true })
+ if configured_count == @job.number_of_instances:
+ return 'ready'
+ else
+ return '#{configured_count} of #{@job.number_of_instances} nodes configured'
+end
View
29 test/fixtures/nodes.yml
@@ -0,0 +1,29 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ job:
+ aws_image_id: MyText
+ aws_instance_id: MyText
+ aws_state: MyText
+ dns_name: MyText
+ ssh_key_name: MyText
+ aws_groups: MyText
+ private_dns_name: MyText
+ aws_instance_type: MyText
+ aws_launch_time: MyText
+ aws_availability_zone: MyText
+ is_configured: false
+
+two:
+ job:
+ aws_image_id: MyText
+ aws_instance_id: MyText
+ aws_state: MyText
+ dns_name: MyText
+ ssh_key_name: MyText
+ aws_groups: MyText
+ private_dns_name: MyText
+ aws_instance_type: MyText
+ aws_launch_time: MyText
+ aws_availability_zone: MyText
+ is_configured: false
View
45 test/functional/nodes_controller_test.rb
@@ -0,0 +1,45 @@
+require 'test_helper'
+
+class NodesControllerTest < ActionController::TestCase
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:nodes)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create node" do
+ assert_difference('Node.count') do
+ post :create, :node => { }
+ end
+
+ assert_redirected_to node_path(assigns(:node))
+ end
+
+ test "should show node" do
+ get :show, :id => nodes(:one).to_param
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, :id => nodes(:one).to_param
+ assert_response :success
+ end
+
+ test "should update node" do
+ put :update, :id => nodes(:one).to_param, :node => { }
+ assert_redirected_to node_path(assigns(:node))
+ end
+
+ test "should destroy node" do
+ assert_difference('Node.count', -1) do
+ delete :destroy, :id => nodes(:one).to_param
+ end
+
+ assert_redirected_to nodes_path
+ end
+end
View
4 test/unit/helpers/nodes_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class NodesHelperTest < ActionView::TestCase
+end
View
8 test/unit/node_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class NodeTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end

0 comments on commit f8168ae

Please sign in to comment.
Something went wrong with that request. Please try again.