Class: Rye::Set

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

Overview

Rye::Set

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = 'default', opts = {}) ⇒ Set

  • name The name of the set of machines

  • opts a hash of optional arguments

The opts hash is used as defaults for all for all Rye::Box objects. All args supported by Rye::Box are available here with the addition of:

  • :parallel => run the commands in parallel? true or false (default).



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rye/set.rb', line 23

def initialize(name='default', opts={})
  @name = name
  @boxes = []
  
  # These opts are use by Rye::Box and also passed to Net::SSH
  @opts = {
    :parallel => false,
    :user => Rye.sysinfo.user, 
    :safe => true,
    :port => 22,
    :keys => [],
    :password => nil,
    :proxy => nil,
    :debug => nil,
    :error => STDERR,
  }.merge(opts)
  
  @parallel = @opts.delete(:parallel) # Rye::Box doesn't have :parallel
  
  @safe = @opts.delete(:safe)
  @debug = @opts.delete(:debug)
  @error = @opts.delete(:error)
  
  @opts[:keys] = [@opts[:keys]].flatten.compact
  
  add_keys(@opts[:keys])
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Catches calls to Rye::Box commands. If meth is the name of an instance method defined in Rye::Cmd then we call it against all the boxes in @boxes. Otherwise this method raises a Rye::CommandNotFound exception. It will also raise a Rye::NoBoxes exception if this set has no boxes defined.

Returns a Rye::Rap object containing the responses from each Rye::Box.

Raises:



128
129
130
131
132
133
134
# File 'lib/rye/set.rb', line 128

def method_missing(meth, *args, &block) 
  # Ruby 1.8 populates Module.instance_methods with Strings. 1.9 uses Symbols.
  meth = (Rye.sysinfo.ruby[1] == 8) ? meth.to_s : meth.to_sym
  raise Rye::NoBoxes if @boxes.empty?
  raise Rye::CommandNotFound, meth.to_s unless Rye::Box.instance_methods.member?(meth)
  run_command(meth, *args, &block)
end

Instance Attribute Details

#boxesObject (readonly)

Returns the value of attribute boxes.



8
9
10
# File 'lib/rye/set.rb', line 8

def boxes
  @boxes
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/rye/set.rb', line 7

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



9
10
11
# File 'lib/rye/set.rb', line 9

def opts
  @opts
end

#parallelObject

Run commands in parallel? A Boolean value. Default: false.



12
13
14
# File 'lib/rye/set.rb', line 12

def parallel
  @parallel
end

Instance Method Details

#[](key = nil) ⇒ Object

See Rye::Box.[]



106
107
108
109
# File 'lib/rye/set.rb', line 106

def [](key=nil)
  run_command(:cd, key)
  self
end

#add_box(*boxes) ⇒ Object Also known as: add_boxes

  • boxes one or more boxes. Rye::Box objects will be added directly

to the set. Hostnames will be used to create new instances of Rye::Box and those will be added to the list.



57
58
59
60
61
62
63
64
65
# File 'lib/rye/set.rb', line 57

def add_box(*boxes)
  boxes = boxes.flatten.compact 
  @boxes += boxes.collect do |box|
    box = Rye::Box.new(box, @opts) if box.is_a?(String) 
    box.add_keys(@keys)
    box
  end
  self
end

#add_key(*additional_keys) ⇒ Object Also known as: add_keys

Add one or more private keys to the SSH Agent.

  • additional_keys is a list of file paths to private keys

Returns the instance of Rye::Set



71
72
73
74
75
76
77
78
79
80
# File 'lib/rye/set.rb', line 71

def add_key(*additional_keys)
  if Rye.sysinfo.os == :windows
    @opts[:keys] ||= []
    @opts[:keys] += additional_keys.flatten
    return @opts[:keys]
  end
  additional_keys = [additional_keys].flatten.compact || []
  Rye.add_keys(additional_keys)
  self
end

#cd(key = nil) ⇒ Object

alias :cd :‘[]’ # fix for jruby



111
112
113
114
# File 'lib/rye/set.rb', line 111

def cd(key=nil)
  run_command(:cd, key)
  self
end

#empty?Boolean

Are there any boxes in this set?

Returns:

  • (Boolean)


117
118
119
# File 'lib/rye/set.rb', line 117

def empty?
  @boxes.nil? || @boxes.empty?
end

#inspectObject



100
101
102
103
# File 'lib/rye/set.rb', line 100

def inspect
  a = [self.class.to_s, @name, @parallel, @opts.inspect, @boxes.inspect]
  %q{#<%s:%s parallel=%s opts=%s boxes=%s>} % a
end

#keysObject

See Rye.keys



92
93
94
# File 'lib/rye/set.rb', line 92

def keys
  Rye.keys
end

#setenv(n, v) ⇒ Object Also known as: setenvironment_variable

Add an environment variable. n and v are the name and value. Returns the instance of Rye::Set



85
86
87
88
# File 'lib/rye/set.rb', line 85

def setenv(n, v)
  run_command(:setenv, n, v)
  self
end

#to_sObject



96
97
98
# File 'lib/rye/set.rb', line 96

def to_s
  "%s:%s" % [self.class.to_s, @name]
end

#userObject



52
# File 'lib/rye/set.rb', line 52

def user; (@opts || {})[:user]; end