Permalink
Browse files

Add logging capabilities

  • Loading branch information...
1 parent 3618917 commit b062fdf0c10c0f95bdc8b1a64579e8742096018b @priteau committed Apr 2, 2010
Showing with 41 additions and 4 deletions.
  1. +12 −1 lib/gosen/deployment.rb
  2. +29 −3 test/gosen/test_deployment.rb
View
@@ -1,6 +1,12 @@
module Gosen
+ class NullLogger
+ def method_missing(method, *args)
+ nil
+ end
+ end
+
class Deployment
- attr_reader :environment, :max_deploy_runs, :min_deployed_nodes, :nodes, :site, :ssh_public_key
+ attr_reader :environment, :logger, :max_deploy_runs, :min_deployed_nodes, :nodes, :site, :ssh_public_key
# Launch a new deployment
# @param [Restfully::Resource] site the deployment site, as a restfully resource
@@ -15,6 +21,7 @@ def initialize(site, environment, nodes, options = {})
@bad_nodes = Array.new(nodes)
@all_runs_done = false
@api_options = {}
+ @logger = options.delete(:logger) || NullLogger.new
@min_deployed_nodes = options[:min_deployed_nodes] || 1
raise Gosen::Error if @min_deployed_nodes > @nodes.length || @min_deployed_nodes < 0
@@ -40,12 +47,16 @@ def bad_nodes
def join
@max_deploy_runs.times do |i|
@deployment_resource = Gosen::DeploymentRun.new(@site, @environment, @bad_nodes)
+ @logger.info("Kadeploy run #{i + 1} with #{@bad_nodes.length} nodes (#{@good_nodes.length} already deployed, need #{@min_deployed_nodes - @good_nodes.length} more)")
@deployment_resource.wait_for_completion
@deployment_resource.update_nodes
@bad_nodes = @deployment_resource.bad_nodes
@good_nodes |= @deployment_resource.good_nodes
+ @logger.info("Nodes deployed: #{@deployment_resource.good_nodes.join(' ')}") unless @deployment_resource.good_nodes.empty?
+ @logger.info("Nodes which failed: #{@deployment_resource.bad_nodes.join(' ')}") unless @deployment_resource.bad_nodes.empty?
if no_more_required?
@all_runs_done = true
+ @logger.info("Had to run #{i + 1} kadeploy runs, deployed #{@good_nodes.length} nodes")
return
end
end
@@ -4,8 +4,12 @@ class TestDeployment < Test::Unit::TestCase
context 'A deployment instance' do
setup do
@site = mock()
+ @site_name = "Rennes"
+ @site.stubs(:name).returns(@site_name)
@environment = 'lenny-x64-base'
@nodes = [ 'paramount-1.rennes.grid5000.fr', 'paramount-2.rennes.grid5000.fr' ]
+ @null_logger = Gosen::NullLogger.new
+ Gosen::NullLogger.stubs(:new).returns(@null_logger)
end
context 'without options' do
@@ -29,6 +33,10 @@ class TestDeployment < Test::Unit::TestCase
assert_equal(@ssh_public_key, @deployment.ssh_public_key)
end
+ should 'have a reader on logger defaulting to NullLogger' do
+ assert_equal(@null_logger, @deployment.logger)
+ end
+
should 'throw an error when accessing good_nodes' do
assert_raise(Gosen::Error) {
@deployment.good_nodes
@@ -63,6 +71,12 @@ class TestDeployment < Test::Unit::TestCase
assert_equal(@min_deployed_nodes, @deployment.min_deployed_nodes)
end
+ should 'have a reader on logger' do
+ logger = mock()
+ @deployment = Gosen::Deployment.new(@site, @environment, @nodes, { :logger => logger })
+ assert_equal(logger, @deployment.logger)
+ end
+
should 'throw an error if not enough nodes are available from the start' do
assert_raise(Gosen::Error) {
Gosen::Deployment.new(@site, @environment, @nodes, { :min_deployed_nodes => @nodes.length + 1 })
@@ -99,6 +113,7 @@ class TestDeployment < Test::Unit::TestCase
Kernel.stubs(:sleep).with(Gosen::DeploymentRun::POLLING_TIME)
@site_deployments = mock()
@site.stubs(:deployments).returns(@site_deployments)
+ @logger = mock()
@deployment_resource = mock()
@deployment_resource.stubs(:reload)
@@ -112,8 +127,12 @@ class TestDeployment < Test::Unit::TestCase
}
@deployment_resource.expects(:[]).with('result').returns(@deployment_result)
@site_deployments.expects(:submit).with({ :environment => @environment, :nodes => @nodes }).returns(@deployment_resource)
+ @min_deployed_nodes = 2
+ @logger.expects(:info).with("Kadeploy run 1 with #{@nodes.length} nodes (0 already deployed, need #{@min_deployed_nodes} more)")
+ @logger.expects(:info).with("Nodes deployed: paramount-1.rennes.grid5000.fr paramount-2.rennes.grid5000.fr")
+ @logger.expects(:info).with("Had to run 1 kadeploy runs, deployed #{@deployment_result.length} nodes")
- @deployment = Gosen::Deployment.new(@site, @environment, @nodes, { :min_deployed_nodes => 2 })
+ @deployment = Gosen::Deployment.new(@site, @environment, @nodes, { :logger => @logger, :min_deployed_nodes => @min_deployed_nodes })
@deployment.join
assert_equal(@nodes, @deployment.good_nodes)
assert_equal([], @deployment.bad_nodes)
@@ -150,10 +169,17 @@ class TestDeployment < Test::Unit::TestCase
@deployment_resource2.stubs(:[]).with('status').returns('processing', 'processing', 'terminated')
@deployment_resource1.expects(:[]).with('result').returns(@deployment_result1)
@deployment_resource2.expects(:[]).with('result').returns(@deployment_result2)
+ @min_deployed_nodes = 2
@site_deployments.expects(:submit).with({ :environment => @environment, :nodes => @nodes }).returns(@deployment_resource1)
@site_deployments.expects(:submit).with({ :environment => @environment, :nodes => [ 'paramount-2.rennes.grid5000.fr'] }).returns(@deployment_resource2)
-
- @deployment = Gosen::Deployment.new(@site, @environment, @nodes, { :min_deployed_nodes => 2, :max_deploy_runs => 2 })
+ @logger.expects(:info).with("Kadeploy run 1 with 2 nodes (0 already deployed, need 2 more)")
+ @logger.expects(:info).with("Nodes deployed: paramount-1.rennes.grid5000.fr")
+ @logger.expects(:info).with("Nodes which failed: paramount-2.rennes.grid5000.fr")
+ @logger.expects(:info).with("Kadeploy run 2 with 1 nodes (1 already deployed, need 1 more)")
+ @logger.expects(:info).with("Nodes deployed: paramount-2.rennes.grid5000.fr")
+ @logger.expects(:info).with("Had to run 2 kadeploy runs, deployed 2 nodes")
+
+ @deployment = Gosen::Deployment.new(@site, @environment, @nodes, { :logger => @logger, :min_deployed_nodes => @min_deployed_nodes, :max_deploy_runs => 2 })
@deployment.join
assert_equal(@nodes, @deployment.good_nodes)
assert_equal([], @deployment.bad_nodes)

0 comments on commit b062fdf

Please sign in to comment.