Class: Chef::Role

Inherits:
Object show all
Includes:
Mixin::FromFile, Mixin::ParamsValidate
Defined in:
lib/chef/role.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ParamsValidate

#set_or_return, #validate

Methods included from Mixin::FromFile

#class_from_file, #from_file

Constructor Details

#initializeRole

Create a new Chef::Role object.



35
36
37
38
39
40
41
# File 'lib/chef/role.rb', line 35

def initialize()
  @name = ''
  @description = ''
  @default_attributes = Mash.new
  @override_attributes = Mash.new
  @env_run_lists = {"_default" => Chef::RunList.new}
end

Class Method Details

.from_disk(name) ⇒ Object

Load a role from disk - prefers to load the JSON, but will happily load the raw rb files as well.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/chef/role.rb', line 150

def self.from_disk(name)
  role = nil
  Chef::Config[:role_path].each do |rp|
    rb_file = File.join(rp, "#{name}.rb")

    if File.exists?(rb_file) && role.nil?
      role = Chef::Role.new
      role.name(name)
      role.from_file(rb_file)
    end
  end

  if role
    role
  else
    raise Chef::Exceptions::RoleNotFound, "Role '#{name}' could not be loaded from disk"
  end
end

Instance Method Details

#active_run_list_for(environment) ⇒ Object



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

def active_run_list_for(environment)
  @env_run_lists.has_key?(environment) ? environment : '_default'
end

#default_attributes(arg = nil) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/chef/role.rb', line 97

def default_attributes(arg=nil)
  set_or_return(
    :default_attributes,
    arg,
    :kind_of => Hash
  )
end

#description(arg = nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/chef/role.rb', line 51

def description(arg=nil)
  set_or_return(
    :description,
    arg,
    :kind_of => String
  )
end

#env_run_lists(env_run_lists = nil) ⇒ Object Also known as: env_run_list

Per environment run lists



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chef/role.rb', line 82

def env_run_lists(env_run_lists=nil)
  if (!env_run_lists.nil?)
    unless env_run_lists.key?("_default")
      msg = "_default key is required in env_run_lists.\n"
      msg << "(env_run_lists: #{env_run_lists.inspect})"
      raise Chef::Exceptions::InvalidEnvironmentRunListSpecification, msg
    end
    @env_run_lists.clear
    env_run_lists.each { |k,v| @env_run_lists[k] = Chef::RunList.new(*Array(v))}
  end
  @env_run_lists
end

#name(arg = nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/chef/role.rb', line 43

def name(arg=nil)
  set_or_return(
    :name,
    arg,
    :regex => /^[\-[:alnum:]_]+$/
  )
end

#override_attributes(arg = nil) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/chef/role.rb', line 105

def override_attributes(arg=nil)
  set_or_return(
    :override_attributes,
    arg,
    :kind_of => Hash
  )
end

#run_list(*args) ⇒ Object Also known as: recipes



59
60
61
62
63
64
# File 'lib/chef/role.rb', line 59

def run_list(*args)
  if (args.length > 0)
    @env_run_lists["_default"].reset!(args)
  end
  @env_run_lists["_default"]
end

#run_list_for(environment) ⇒ Object

For run_list expansion



69
70
71
72
73
74
75
# File 'lib/chef/role.rb', line 69

def run_list_for(environment)
  if env_run_lists[environment].nil?
    env_run_lists["_default"]
  else
    env_run_lists[environment]
  end
end

#to_hashObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/chef/role.rb', line 113

def to_hash
  env_run_lists_without_default = @env_run_lists.dup
  env_run_lists_without_default.delete("_default")
  result = {
    "name" => @name,
    "description" => @description,
    'json_class' => self.class.name,
    "default_attributes" => @default_attributes,
    "override_attributes" => @override_attributes,
    "chef_type" => "role",
    "run_list" => run_list,
    "env_run_lists" => env_run_lists_without_default
  }
  result
end

#to_json(*a) ⇒ Object

Serialize this object as a hash



130
131
132
# File 'lib/chef/role.rb', line 130

def to_json(*a)
  to_hash.to_json(*a)
end

#to_sObject

As a string



144
145
146
# File 'lib/chef/role.rb', line 144

def to_s
  "role[#{@name}]"
end

#update_from!(o) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/chef/role.rb', line 134

def update_from!(o)
  description(o.description)
  recipes(o.recipes) if defined?(o.recipes)
  default_attributes(o.default_attributes)
  override_attributes(o.override_attributes)
  env_run_lists(o.env_run_lists) unless o.env_run_lists.nil?
  self
end