Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
changes after BG Build re-arch
Browse files Browse the repository at this point in the history
  • Loading branch information
goldmann committed May 5, 2010
1 parent e3b4016 commit e1c02ff
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 99 deletions.
1 change: 1 addition & 0 deletions app/consumers/image-manager-consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def on_object( image )

@image = Image.find( image[:id] )
@image.status = Image::STATUSES[image[:status]]
@image.node = image[:node] unless image[:node].nil?

ActiveRecord::Base.transaction do
@image.save!
Expand Down
52 changes: 30 additions & 22 deletions app/consumers/node-manager-consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,48 @@
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.

require 'torquebox-messaging-client'

module BoxGrinder
module REST
class ImageManagerConsumer
class NodeManagerConsumer

def on_object( payload )
def on_object(payload, message)
@log = Rails.logger
@reply_to = message.jmsreply_to

@log.info "Received new management message for Node."

node = payload[:node]
if @reply_to.nil? or !payload.is_a?(Hash)
@log.error "Invalid data sent to register a node."
return
end

case payload[:action]
when :register then
register_node( node )
when :deregister then
deregister_node( node )
when :register
register_node(payload[:node])
end

@log.info "Message handled."
end

def register_node( node )
@log.info "Registering node #{node[:address]} (arch = #{node[:arch]}, os = #{node[:os_name]} #{node[:os_version]})..."
begin

@log.info "Node #{node[:address]} registered."
rescue
@log.info "An error occurred while registering node #{node[:address]}."
end
end
def register_node(node_config)
@log.info "Registering new node..."
@log.debug "New node name: '#{node_config[:name]}'"

def deregister_node( node )
# TODO save this to database
# node_config[:address]
# node_config[:os_name]
# node_config[:os_version]
# node_config[:arch]

begin
TorqueBox::Messaging::Client.connect do |client|
client.send(@reply_to, :text => 'OK')
end
@log.info "New node registered."
rescue => e
@log.error e
@log.error e.backtrace.join($/)
@log.error "Couldn't register node '#{node_config[:name]}'."
end
end
end
end
end
end
27 changes: 0 additions & 27 deletions app/controllers/base_controller.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
class BaseController < ApplicationController

end

module ActiveSupport
class BufferedLogger
def add(severity, message = nil, progname = nil, &block)
return if @level > severity
message = (message || (block && block.call) || progname).to_s

level = {
0 => "DEBUG",
1 => "INFO",
2 => "WARN",
3 => "ERROR",
4 => "FATAL"
}[severity] || "U"

message = "[%s:\t%s #%d] %s" % [level,
Time.now.strftime("%m%d %H:%M:%S"),
$$,
message]

message = "#{message}\n" unless message[-1] == ?\n
buffer << message
auto_flush
message
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/definitions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def destroy

return unless object_saved?( @definition )

TorqueBox::Queues.enqueue( 'BoxGrinder::ActionQueue', :execute,
TorqueBox::Queues.send( 'BoxGrinder::ActionQueue', :execute,
Base64.encode64(
{ :task => Task.new(
:artifact => ARTIFACTS[:definition],
Expand Down
103 changes: 81 additions & 22 deletions app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'boxgrinder-core/models/task'
require 'boxgrinder-core/helpers/queue-helper'

class ImagesController < BaseController
include ImagesHelper
Expand All @@ -25,10 +26,61 @@ def deliver
render_general(@image, 'images/show')
end

def convert
param_platform = params[:platform] || nil

platform = nil

# image_format is optional, if no image_format parameter is specified; RAW format will be used
unless param_platform.nil?
unless BoxGrinder::RESTConfig.instance.plugins[:platform].include?(param_platform.downcase)
render_error(Error.new("Invalid format specified. Available formats: #{BoxGrinder::RESTConfig.instance.plugins[:platform].sort.join(", ")}."))
return
end
platform = param_platform.downcase
end

image = Image.last(
:conditions => {
:appliance_id => @image.appliance.id,
:platform => platform
}
)

if image.nil?
image = Image.new(
:appliance => @image.appliance,
:parent => @image,
:arch => @image.arch,
:platform => platform,
:description => "Image for #{@image.appliance.name} appliance, #{platform} platform and #{@image.arch} architecture."
)

return unless object_saved?(image)

BoxGrinder::QueueHelper.new( :log => logger ).client do |client|
client.send("/queues/boxgrinder/image/convert",
:object => BoxGrinder::Task.new(
:convert,
image.description, {
:appliance_config => YAML.load(@image.appliance.config),
:platform => platform,
:image_id => image.id
}),
:properties => { :node => @image.node }
)
end
end

@image = image

render_general(@image, 'images/show')
end

def create
param_appliance_id = params[:appliance_id] || nil
param_platform = params[:platform] || nil
param_arch = params[:arch] || nil
#param_platform = params[:platform] || nil
param_arch = params[:arch] || nil

if param_appliance_id.nil? or !param_appliance_id.match(/\d+/)
render_error(Error.new("No or invalid 'appliance_id' parameter specified."))
Expand All @@ -40,22 +92,22 @@ def create
return
end

platform = nil

# image_format is optional, if no image_format parameter is specified; RAW format will be used
unless param_platform.nil?
unless BoxGrinder::RESTConfig.instance.plugins[:platform].include?(param_platform.downcase)
render_error(Error.new("Invalid format specified. Available formats: #{BoxGrinder::RESTConfig.instance.plugins[:platform].sort.join(", ")}."))
return
end
platform = param_platform.downcase
end
# platform = nil
#
# # image_format is optional, if no image_format parameter is specified; RAW format will be used
# unless param_platform.nil?
# unless BoxGrinder::RESTConfig.instance.plugins[:platform].include?(param_platform.downcase)
# render_error(Error.new("Invalid format specified. Available formats: #{BoxGrinder::RESTConfig.instance.plugins[:platform].sort.join(", ")}."))
# return
# end
# platform = param_platform.downcase
# end

# 1.check if there is already a built image
@image = Image.last(
:conditions => {
:appliance_id => param_appliance_id,
:platform => platform
:platform => nil
}
)

Expand All @@ -75,20 +127,27 @@ def create

@image = Image.new(
:appliance_id => appliance.id,
:platform => platform,
:arch => param_arch,
:description => "Image for #{appliance.name} appliance, #{param_arch} architecture#{platform.nil? ? '' : ", #{platform} platform"}."
:description => "Base image for #{appliance.name} appliance and #{param_arch} architecture."
)

return unless object_saved?(@image)

enqueue_task("/queues/boxgrinder/image",
BoxGrinder::Task.new(:build, @image.description, {
:appliance_config => appliance_config,
:platform => @image.platform,
:image_id => @image.id
}), :os_name => appliance_config.os.name, :os_version => appliance_config.os.version, :arch => param_arch
)
BoxGrinder::QueueHelper.new( :log => logger ).client do |client|
client.send("/queues/boxgrinder/image/create",
:object => BoxGrinder::Task.new(
:create,
@image.description, {
:appliance_config => appliance_config,
:image_id => @image.id
}),
:properties => {
:os_name => appliance_config.os.name,
:os_version => appliance_config.os.version,
:arch => param_arch
}
)
end
end

render_general(@image, 'images/show')
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module BaseHelper
def render_error( error )
@error = error

logger.error( @error.message, @error.exception )
logger.error( "#{@error.message}#$/#{@error.exception.backtrace.join($/)}" )

respond_to do |format|
format.html { render 'root/error' }
Expand Down
1 change: 1 addition & 0 deletions app/models/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Image < ActiveRecord::Base
validates_presence_of :status, :description
belongs_to :appliance
has_one :package
belongs_to :parent, :class_name => "Image"

def after_initialize
if self.status.nil?
Expand Down
4 changes: 0 additions & 4 deletions app/views/appliances/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@
<li><strong><%= arch %> architecture</strong>
<ul>
<li><%= link_to "Build base image", {:action => :create, :controller => 'images', :appliance_id => @appliance.id, :arch => arch}, :method => :post %></li>

<% BoxGrinder::RESTConfig.instance.plugins[:platform].each do |platform| %>
<li><%= link_to "Build #{platform} image", {:action => :create, :controller => 'images', :appliance_id => @appliance.id, :platform => platform, :arch => arch}, :method => :post %></li>
<% end %>
</ul>
</li>
<% end %>
Expand Down
22 changes: 21 additions & 1 deletion app/views/images/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,25 @@
<td><%= link_to "Image #{@image.id}", :id => @image.id, :action => :show, :controller => 'images' %>
</td>
</tr>
<% unless @image.parent.nil? %>
<tr class="row">
<td>Node</td>
<td><%= link_to "Image #{@image.parent.id}", :id => @image.parent.id, :action => :show, :controller => 'images' %>
</td>
</tr>
<% end %>
<tr class="row">
<td>Status</td>
<td><%= @image.status %>
</td>
</tr>
<% unless @image.node.nil? %>
<tr class="row">
<td>Node</td>
<td><%= @image.node %>
</td>
</tr>
<% end %>
<tr class="row">
<td>Appliance</td>
<td><%= link_to @image.appliance.name, :id => @image.appliance_id, :action => :show, :controller => 'appliances' %>
Expand Down Expand Up @@ -43,8 +57,14 @@
<ul>
<% unless is_image_status?(:error) %>
<% if is_image_status?(:built) %>
<% if @image.platform.nil? %>
<% BoxGrinder::RESTConfig.instance.plugins[:platform].each do |platform| %>
<li><%= link_to "Convert to #{platform}", {:action => :convert, :controller => 'images', :id => @image.id, :platform => platform}, :method => :post %></li>
<% end %>
<% end %>

<% BoxGrinder::RESTConfig.instance.plugins[:delivery].each do |delivery| %>
<li><%= link_to "Deliver using #{delivery}", {:action => :deliver, :controller => 'images', :id => @image.id, :platform => @image.platform, :type => delivery}, :method => :post %></li>
<li><%= link_to "Deliver using #{delivery}", {:action => :deliver, :controller => 'images', :id => @image.id, :type => delivery}, :method => :post %></li>
<% end %>
<% end %>
<% end %>
Expand Down
3 changes: 2 additions & 1 deletion config/consumers.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require 'consumers/image-manager-consumer'
require 'consumers/package-manager-consumer'
require 'consumers/node-manager-consumer'

TorqueBox::Messaging::Container::Config.create {
consumers {
map BoxGrinder::REST::NodeManagerConsumer, '/queues/boxgrinder/manage/node'
map BoxGrinder::REST::ImageManagerConsumer, '/queues/boxgrinder/manage/image'
}
}
4 changes: 4 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@
# config.i18n.default_locale = :de

#config.active_record.schema_format = :sql

require 'boxgrinder-core/helpers/log-helper'

config.logger = BoxGrinder::LogHelper.new( :type => :file, :location => "#{RAILS_ROOT}/log/boxgrinder.log" )
end
Loading

0 comments on commit e1c02ff

Please sign in to comment.