Class: Chef::RunList

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/chef/run_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunList

Returns a new instance of RunList.



26
27
28
29
30
31
# File 'lib/chef/run_list.rb', line 26

def initialize
  @run_list = Array.new
  @recipes = Array.new
  @roles = Array.new
  @seen_roles = Array.new
end

Instance Attribute Details

#recipesObject (readonly)

Returns the value of attribute recipes.



24
25
26
# File 'lib/chef/run_list.rb', line 24

def recipes
  @recipes
end

#rolesObject (readonly)

Returns the value of attribute roles.



24
25
26
# File 'lib/chef/run_list.rb', line 24

def roles
  @roles
end

#run_listObject (readonly)

Returns the value of attribute run_list.



24
25
26
# File 'lib/chef/run_list.rb', line 24

def run_list
  @run_list
end

Instance Method Details

#<<(item) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chef/run_list.rb', line 33

def <<(item)
  type, entry, fentry = parse_entry(item)
  case type
  when 'recipe'
    @recipes << entry unless @recipes.include?(entry)
  when 'role'
    @roles << entry unless @roles.include?(entry)
  end
  @run_list << fentry unless @run_list.include?(fentry)
  self
end

#==(*isequal) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chef/run_list.rb', line 45

def ==(*isequal)
  check_array = nil
  if isequal[0].kind_of?(Chef::RunList)
    check_array = isequal[0].run_list
  else
    check_array = isequal.flatten
  end
  
  return false if check_array.length != @run_list.length

  check_array.each_index do |i|
    to_check = check_array[i]
    type, name, fname = parse_entry(to_check)
    return false if @run_list[i] != fname
  end

  true
end

#[](pos) ⇒ Object



72
73
74
# File 'lib/chef/run_list.rb', line 72

def [](pos)
  @run_list[pos]
end

#[]=(pos, item) ⇒ Object



76
77
78
79
# File 'lib/chef/run_list.rb', line 76

def []=(pos, item)
  type, entry, fentry = parse_entry(item)
  @run_list[pos] = fentry 
end

#each(&block) ⇒ Object



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

def each(&block)
  @run_list.each { |i| block.call(i) }
end

#each_index(&block) ⇒ Object



85
86
87
# File 'lib/chef/run_list.rb', line 85

def each_index(&block)
  @run_list.each_index { |i| block.call(i) }
end

#empty?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/chef/run_list.rb', line 68

def empty?
  @run_list.length == 0 ? true : false
end

#expand(from = 'server', couchdb = nil, rest = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/chef/run_list.rb', line 119

def expand(from='server', couchdb=nil, rest=nil)
  couchdb = couchdb ? couchdb : Chef::CouchDB.new
  recipes = Array.new
  default_attrs = Mash.new
  override_attrs = Mash.new
  
  @run_list.each do |entry|
    type, name, fname = parse_entry(entry)
    case type
    when 'recipe'
      recipes << name unless recipes.include?(name)
    when 'role'
      role = begin
               next if @seen_roles.include?(name)
               @seen_roles << name
               if from == 'disk' || Chef::Config[:solo]
                 # Load the role from disk
                 Chef::Role.from_disk("#{name}") || raise(Chef::Exceptions::RoleNotFound)
               elsif from == 'server'
                 # Load the role from the server
                 begin
                   (rest || Chef::REST.new(Chef::Config[:role_url])).get_rest("roles/#{name}") 
                 rescue Net::HTTPServerException
                   raise Chef::Exceptions::RoleNotFound if $!.message == '404 "Not Found"'
                   raise
                 end
               elsif from == 'couchdb'
                 # Load the role from couchdb
                 Chef::Role.cdb_load(name, couchdb) rescue Chef::Exceptions::CouchDBNotFound raise(Chef::Exceptions::RoleNotFound)
               end
             rescue Chef::Exceptions::RoleNotFound
               Chef::Log.error("Role #{name} is in the runlist but does not exist. Skipping expand.")
               next
             end
      rec, d, o = role.run_list.expand(from, couchdb, rest)
      rec.each { |r| recipes <<  r unless recipes.include?(r) }
      default_attrs = Chef::Mixin::DeepMerge.merge(default_attrs, Chef::Mixin::DeepMerge.merge(role.default_attributes,d))
      override_attrs = Chef::Mixin::DeepMerge.merge(override_attrs, Chef::Mixin::DeepMerge.merge(role.override_attributes, o))
    end
  end
  return recipes, default_attrs, override_attrs
end

#include?(item) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
# File 'lib/chef/run_list.rb', line 89

def include?(item)
  type, entry, fentry = parse_entry(item)
  @run_list.include?(fentry)
end

#parse_entry(entry) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/chef/run_list.rb', line 162

def parse_entry(entry)
  case entry 
  when /^(.+)\[(.+)\]$/
    [ $1, $2, entry ]
  else
    [ 'recipe', entry, "recipe[#{entry}]" ]
  end
end

#remove(item) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/chef/run_list.rb', line 108

def remove(item)
  type, entry, fentry = parse_entry(item)
  @run_list.delete_if { |i| i == fentry }
  if type == "recipe"
    @recipes.delete_if { |i| i == entry }
  elsif type == "role"
    @roles.delete_if { |i| i == entry }
  end
  self
end

#reset!(*args) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef/run_list.rb', line 94

def reset!(*args)
  @run_list = Array.new
  @recipes = Array.new
  @roles = Array.new
  args.flatten.each do |item|
    if item.kind_of?(Chef::RunList)
      item.each { |r| self << r }
    else
      self << item
    end
  end
  self
end

#to_sObject



64
65
66
# File 'lib/chef/run_list.rb', line 64

def to_s
  @run_list.join(", ")
end