Class: Marionetta::Group

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Group

Group name is currently optional.

[View source]

20
21
22
23
24
# File 'lib/marionetta/group.rb', line 20

def initialize(name = nil)
  @name = name
  @groups = []
  @servers = []
end

Instance Attribute Details

#nameObject (readonly)

The name of the group.


28
29
30
# File 'lib/marionetta/group.rb', line 28

def name
  @name
end

Instance Method Details

#add_group(group) ⇒ Object

Nest a group using ‘.add_group()`.

[View source]

32
33
34
# File 'lib/marionetta/group.rb', line 32

def add_group(group)
  @groups << group
end

#add_server(server = nil) {|server| ... } ⇒ Object

Add a ‘server` hash or build on the default server in a block.

Example:

staging = Marionetta::Group.new(:staging)
staging.add_server(:hostname => 'ubuntu@example.com')
staging.add_server do |s|
  s[:hostname] = 'ubuntu@example.com'
end

Yields:

  • (server)
[View source]

59
60
61
62
63
# File 'lib/marionetta/group.rb', line 59

def add_server(server = nil)
  server ||= Marionetta.default_server
  yield server if block_given?
  @servers << server
end

#each_serverObject

Iterate over each ‘server` definition (including nested servers) in parallel by passing a block.

each_server do |s|
  cmd = Marionetta::CommandRunner.new(s)
  cmd.ssh('whoami') do |out|
    puts out.read
  end
end
[View source]

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/marionetta/group.rb', line 88

def each_server()
  futures = []

  servers.each do |s|
    server = s.clone.freeze

    futures << Celluloid::Future.new do
      yield server
    end
  end

  return_values = []

  futures.each do |f|
    return_values << f.value
  end

  return return_values
end

#groupsObject

Get all descending groups contained within this group.

[View source]

38
39
40
41
42
43
44
45
46
# File 'lib/marionetta/group.rb', line 38

def groups()
  groups = @groups

  groups.each do |g|
    groups.concat(g.groups)
  end

  return groups
end

#manipulate_each_server(manipulator_class, method_name) ⇒ Object

Manipulate each server by passing a manipulator key as registered with ‘Manipulators` and a method name.

If manipulator cannot be run on a server definition then a warn message will be logged.

If block passed in then the server and return value for each server will be passed in when complete.

[View source]

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/marionetta/group.rb', line 117

def manipulate_each_server(manipulator_class, method_name)
  each_server do |s|
    if manipulator_class.is_a? Symbol
      manipulator_class = Manipulators[manipulator_class]
    end

    manipulator = manipulator_class.new(s)

    if manipulator.can?
      return_val = manipulator.method(method_name).call()
      yield s, return_val if block_given?
    else
      s[:logger].warn(
        "Could not Manipulators[:#{manipulator.class.name}].#{method_name}()")
    end
  end
end

#serversObject

Get servers in this group and all descendant groups.

[View source]

67
68
69
70
71
72
73
74
75
76
# File 'lib/marionetta/group.rb', line 67

def servers()
  servers  = []
  servers.concat(@servers)

  @groups.each do |g|
    servers.concat(g.servers)
  end

  return servers
end