Class: Salticid::Role

Inherits:
Object show all
Defined in:
lib/salticid/role.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Role

Returns a new instance of Role.



5
6
7
8
9
# File 'lib/salticid/role.rb', line 5

def initialize(name, opts = {})
  @name = name.to_s
  @tasks = []
  @salticid = opts[:salticid]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

When a task name is called on a role, it is automatically run on every host associated with that role.

role(:myapp).deploy => role(:myapp).each_host { |h| h.myapp.deploy }



90
91
92
93
94
95
96
97
# File 'lib/salticid/role.rb', line 90

def method_missing(meth, *args, &block)
  name = meth.to_s
  if task = @tasks.find { |t| t.name == name }
    hosts.each do |host|
      task.run(host, *args)
    end
  end
end

Instance Attribute Details

#name(name = nil) ⇒ Object (readonly)

Sets or gets the name of this role.



3
4
5
# File 'lib/salticid/role.rb', line 3

def name
  @name
end

#salticidObject (readonly)

A role is a list of tasks.



3
4
5
# File 'lib/salticid/role.rb', line 3

def salticid
  @salticid
end

#tasksObject (readonly)

A role is a list of tasks.



3
4
5
# File 'lib/salticid/role.rb', line 3

def tasks
  @tasks
end

Instance Method Details

#each_host(&block) ⇒ Object

Runs the block in the context of each.



12
13
14
15
16
# File 'lib/salticid/role.rb', line 12

def each_host(&block)
  hosts.each do |host|
    host.instance_exec &block
  end
end

#hostsObject

Returns an array of all hosts in this salticid which include this role.



19
20
21
22
23
# File 'lib/salticid/role.rb', line 19

def hosts
  @salticid.hosts.select do |host|
    host.roles.include? self
  end
end

#inspectObject



25
26
27
# File 'lib/salticid/role.rb', line 25

def inspect
  "#<Role #{name} tasks=#{@tasks.inspect}>"
end

#task(name, &block) ⇒ Object

Finds (and optionally defines) a task.

Tasks are first resolved in the role’s task list, then in the Salticid’s task list. Finally, tasks are created from scratch. Any invocation of task adds that task to this role.

If a block is given, the block is assigned to the local (role) task. The task is dup’ed to prevent modifying a possible global task.

The task is returned at the end of the method.



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
# File 'lib/salticid/role.rb', line 48

def task(name, &block)
  name = name.to_s

  if task = @tasks.find{|t| t.name == name}
    # Found in self
  elsif (task = @salticid.tasks.find{|t| t.name == name}) and not block_given?
    # Found in salticid
    @tasks << task
  else
    # Create new task in self
    task = Salticid::Task.new(name, :salticid => @salticid)
    @tasks << task
  end

  if block_given?
    # Remove the task from our list, and replace it with a copy.
    # This is to prevent local declarations from clobbering global tasks.
    i = @tasks.index(task) || @task.size
    task = task.dup
    task.block = block
    @tasks[i] = task
  end

  task
end

#to_sObject



74
75
76
# File 'lib/salticid/role.rb', line 74

def to_s
  @name.to_s
end

#to_stringObject



78
79
80
81
82
83
84
# File 'lib/salticid/role.rb', line 78

def to_string
  r = "Role #{@name}:\n"
  r << "  Tasks:\n"
  r << tasks.map { |t| "    #{t}" }.sort!.join("\n")
  r << "\n  Hosts:\n"
  r << hosts.map { |h| "    #{h}" }.join("\n")
end