Class: Drbman

Inherits:
Object
  • Object
show all
Defined in:
lib/drbman/drbman.rb

Overview

Drbman is the drb manager

Synopsis

Drbman will create a project directory on a host machine, then copy a set of files to the host machine, make sure a given set of gems is installed on the host machine, then run the drb server on the host machine. Drbman also supports issuing a termination command to the drb server on the remote machine and cleaning up the project by removing the files installed onto the host machine.

Notes

Uses the Command design pattern

Instance Method Summary collapse

Constructor Details

#initialize(logger, choices) {|Drbman| ... } ⇒ Drbman

Returns a new instance of Drbman.

Examples:

Usage

Drbman.new(logger, choices) do |drbman|
  drbman.get_object do |obj|
    obj.do_something
  end
end

Parameters:

  • logger (Logger)

    the logger

  • choices (UserChoices, Hash)

Options Hash (choices):

  • :dirs (Hash<String,String>)

    hash of local directories to copy to the host machines where key is local source and value is directory on host machine - REQUIRED.

  • :run (String)

    the name of the file to run on the host machine (REQUIRED). This file should start the drb server. Note, this file will be daemonized before running.

  • :hosts (Array<String>) — default: ['localhost']

    array of host machine descriptions “user{:password@}machine:port”.

  • :port (Integer) — default: 9000

    default port number used to assign to hosts without a port number, the port number is incremented for each host.

  • :gems (Array<String>) — default: []

    array of gem names to verify are installed on the host machine, note, ‘daemons’ is always added to this array.

  • :keys (Array<String>) — default: ['~/.ssh/id_dsa', '~/.ssh/id_rsa']

    array of ssh key file names.

Yields:

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/drbman/drbman.rb', line 35

def initialize(logger, choices, &block)
  @logger = logger
  @user_choices = choices
  
  # @hosts[machine_description] = HostMachine instance
  @hosts = {}

  @user_choices[:port] ||= 9000
  @user_choices[:hosts] ||= ['localhost']
  @user_choices[:gems] ||= []
  @user_choices[:gems] = (@user_choices[:gems] + ['daemons']).uniq.compact
  
  raise ArgumentError.new('Missing choices[:run]')   if @user_choices[:run].blank?
  raise ArgumentError.new('Missing choices[:hosts]') if @user_choices[:hosts].blank?
  raise ArgumentError.new('Missing choices[:dirs]')  if @user_choices[:dirs].blank?
  
  # populate the @hosts hash.  key => host machine description, value => HostMachine instance
  port = @user_choices[:port]
  @user_choices[:hosts].each do |host|
    host = "#{host}:#{port}" unless host =~ /\:\d+\s*$/
    host_machine = HostMachine.new(host, @logger, @user_choices)
    @hosts[host] = host_machine if host_machine.alive?  # don't use off-line host machines
    port += 1
  end
  
  unless block.nil?
    begin
      setup
      @pool = DrbPool.new(@hosts, @logger)
      block.call(self)
    rescue EmptyDrbPoolError, EnvironmentError => e
      @logger.error { e }
    rescue Exception => e
      @logger.error { e }
      @logger.debug { e.backtrace.join("\n") }
    ensure
      @pool.shutdown unless @pool.nil?
      shutdown
    end
  end
end

Instance Method Details

#get_object {|DRbObject| ... } ⇒ Object

Use an object from the pool

Examples:

Usage

drbman.get_object {|obj| obj.do_something}

Yields:

  • (DRbObject)


81
82
83
# File 'lib/drbman/drbman.rb', line 81

def get_object(&block)
  @pool.get_object(&block)
end