Changeset 89

Show
Ignore:
Timestamp:
04/23/08 18:38:44 (7 months ago)
Author:
jweiss
Message:

backend code for host exclusion in deployments

Location:
trunk
Files:
1 added
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/app/models/deployment.rb

    r80 r89  
    88  validates_inclusion_of :success, :in => 0..1 
    99   
    10   attr_accessible :task, :prompt_config, :description 
     10  serialize :excluded_host_ids 
     11   
     12  attr_accessible :task, :prompt_config, :description, :excluded_host_ids 
    1113   
    1214  tz_time_attributes :created_at, :updated_at, :completed_at 
     
    112114    end 
    113115  end 
     116   
     117  # returns a list of hosts that this deployment 
     118  # will deploy to. This computed out of the list 
     119  # of given roles and the excluded hosts 
     120  def deploy_to_hosts 
     121    all_hosts = self.roles.map(&:host).uniq 
     122    return all_hosts - self.excluded_hosts 
     123  end 
     124   
     125  # returns a list of roles that this deployment 
     126  # will deploy to. This computed out of the list 
     127  # of given roles and the excluded hosts 
     128  def deploy_to_roles 
     129    self.roles.delete_if{|role| self.excluded_hosts.include?(role.host) } 
     130  end 
     131   
     132  # a list of all excluded hosts for this deployment 
     133  # see excluded_host_ids 
     134  def excluded_hosts 
     135    res = [] 
     136    self.excluded_host_ids.each do |h_id| 
     137      res << (Host.find(h_id) rescue nil) 
     138    end 
     139    res.compact 
     140  end 
     141   
     142  def excluded_host_ids 
     143    self.read_attribute('excluded_host_ids').blank? ? [] : self.read_attribute('excluded_host_ids') 
     144  end 
     145   
     146  def excluded_host_ids=(val) 
     147    val = [val] unless val.is_a?(Array) 
     148    self.write_attribute('excluded_host_ids', val) 
     149  end 
    114150end 
  • trunk/db/schema.rb

    r64 r89  
    1010# It's strongly recommended to check this file into your version control system. 
    1111 
    12 ActiveRecord::Schema.define(:version => 22) do 
     12ActiveRecord::Schema.define(:version => 23) do 
    1313 
    1414  create_table "configuration_parameters", :force => true do |t| 
     
    2626    t.string   "task" 
    2727    t.text     "log" 
    28     t.integer  "success",      :default => 0 
     28    t.integer  "success",           :default => 0 
    2929    t.integer  "stage_id" 
    3030    t.datetime "created_at" 
     
    3333    t.text     "description" 
    3434    t.integer  "user_id" 
     35    t.string   "excluded_host_ids" 
    3536  end 
    3637 
  • trunk/lib/webistrano/deployer.rb

    r34 r89  
    177177    # sets the roles on the Capistrano configuration 
    178178    def set_stage_roles(config) 
    179       deployment.roles.each do |r| 
     179      deployment.deploy_to_roles.each do |r| 
    180180         
    181181        # create role attributes hash 
  • trunk/test/test_helper.rb

    r42 r89  
    174174      :roles => [], 
    175175      :description => random_string, 
    176       :user => create_new_user 
     176      :user => create_new_user, 
     177      :excluded_host_ids => [] 
    177178    }.update(options) 
    178179     
     
    184185    d.prompt_config = options[:prompt_config] 
    185186    d.description = options[:description] 
     187    d.excluded_host_ids = options[:excluded_host_ids] 
    186188    d.user = options[:user] 
    187189 
  • trunk/test/unit/deployment_test.rb

    r80 r89  
    227227  end 
    228228   
     229  def test_excluded_hosts_accessor 
     230    host = create_new_host 
     231    deployment = create_new_deployment(:excluded_host_ids => [host.id], :stage => @stage) 
     232 
     233    assert_equal [host.id], deployment.excluded_host_ids 
     234    assert_equal [host], deployment.excluded_hosts 
     235     
     236    deployment.excluded_host_ids = host.id 
     237    assert_equal [host.id], deployment.excluded_host_ids 
     238  end 
     239   
     240  def test_excluded_hosts 
     241    host_1 = create_new_host 
     242    host_2 = create_new_host 
     243    stage = create_new_stage 
     244    role_app = create_new_role(:name => 'app', :stage => stage, :host => host_1) 
     245    role_www = create_new_role(:name => 'www', :stage => stage, :host => host_2) 
     246    role_db = create_new_role(:name => 'db', :stage => stage, :host => host_2) 
     247     
     248    deployment = create_new_deployment( 
     249                  :stage => stage,  
     250                  :excluded_host_ids => [host_1.id]) 
     251     
     252    assert_equal 3, deployment.roles.count 
     253    assert_equal [host_1], deployment.excluded_hosts 
     254                   
     255    assert_equal [host_2], deployment.deploy_to_hosts 
     256    assert_equal [role_www, role_db].map(&:id).sort, deployment.deploy_to_roles.map(&:id).sort 
     257  end 
     258   
     259   
     260   
    229261end 
  • trunk/test/unit/webistrano_deployer_test.rb

    r82 r89  
    158158  end 
    159159   
     160  def test_excluded_hosts 
     161    # prepare stage + roles 
     162    @stage = create_new_stage 
     163    dead_host = create_new_host 
     164     
     165    www_role = @stage.roles.build(:name => 'www', :host_id => @host.id) 
     166    www_role.save! 
     167     
     168    app_role = @stage.roles.build(:name => 'app', :host_id => @host.id) 
     169    app_role.save! 
     170     
     171    db_role = @stage.roles.build(:name => 'db', :host_id => dead_host.id) 
     172    db_role.save!     
     173 
     174    @stage.reload 
     175 
     176    deployment = create_new_deployment(:stage => @stage, :excluded_host_ids => [dead_host.id]) 
     177    assert_equal [www_role, app_role].map(&:id).sort, deployment.deploy_to_roles.map(&:id).sort 
     178    # prepare Mocks 
     179    # 
     180     
     181    # Logger stubing 
     182    mock_cap_logger = mock 
     183    mock_cap_logger.expects(:level=).with(3) 
     184     
     185    # config stubbing 
     186    mock_cap_config = mock 
     187    mock_cap_config.stubs(:logger).returns(mock_cap_logger) 
     188    mock_cap_config.stubs(:logger=) 
     189    mock_cap_config.stubs(:load) 
     190    mock_cap_config.stubs(:trigger) 
     191    mock_cap_config.stubs(:find_and_execute_task) 
     192    mock_cap_config.stubs(:[]) 
     193    mock_cap_config.stubs(:fetch).with(:scm) 
     194     
     195    # ignore vars 
     196    mock_cap_config.stubs(:set) 
     197       
     198    #   
     199    # now check the roles         
     200    #  
     201     
     202    #mock_cap_config.expects(:role).with('db', @host.name) 
     203    mock_cap_config.expects(:role).with('www', @host.name) 
     204    mock_cap_config.expects(:role).with('app', @host.name) 
     205     
     206     
     207    # main mock install 
     208    Webistrano::Configuration.expects(:new).returns(mock_cap_config) 
     209     
     210    # get things started 
     211    deployer = Webistrano::Deployer.new( deployment ) 
     212    deployer.invoke_task! 
     213  end 
     214   
    160215  def test_invoke_task 
    161216    assert_correct_task_called('deploy:setup')