Class: Chef::Role
- Inherits:
-
Object
- Object
- Chef::Role
- Includes:
- Mixin::FromFile, Mixin::ParamsValidate
- Defined in:
- lib/chef/role.rb
Instance Attribute Summary collapse
-
#chef_server_rest ⇒ Object
Returns the value of attribute chef_server_rest.
Class Method Summary collapse
- .chef_server_rest ⇒ Object
-
.from_disk(name) ⇒ Object
Load a role from disk - prefers to load the JSON, but will happily load the raw rb files as well.
-
.json_create(o) ⇒ Object
Create a Chef::Role from JSON.
-
.list(inflate = false) ⇒ Object
Get the list of all roles from the API.
-
.load(name) ⇒ Object
Load a role by name from the API.
Instance Method Summary collapse
- #active_run_list_for(environment) ⇒ Object
-
#create ⇒ Object
Create the role via the REST API.
- #default_attributes(arg = nil) ⇒ Object
- #description(arg = nil) ⇒ Object
-
#destroy ⇒ Object
Remove this role via the REST API.
-
#env_run_lists(env_run_lists = nil) ⇒ Object
(also: #env_run_list)
Per environment run lists.
- #env_run_lists_add(env_run_lists = nil) ⇒ Object (also: #env_run_list_add)
- #environment(env_name) ⇒ Object
- #environments ⇒ Object
-
#initialize(chef_server_rest: nil) ⇒ Role
constructor
Create a new Chef::Role object.
- #name(arg = nil) ⇒ Object
- #override_attributes(arg = nil) ⇒ Object
- #run_list(*args) ⇒ Object (also: #recipes)
-
#run_list_for(environment) ⇒ Object
For run_list expansion.
-
#save ⇒ Object
Save this role via the REST API.
- #to_hash ⇒ Object
-
#to_json(*a) ⇒ Object
Serialize this object as a hash.
-
#to_s ⇒ Object
As a string.
- #update_from!(o) ⇒ Object
Methods included from Mixin::ParamsValidate
#lazy, #set_or_return, #validate
Methods included from Mixin::FromFile
Constructor Details
#initialize(chef_server_rest: nil) ⇒ Role
Create a new Chef::Role object.
38 39 40 41 42 43 44 45 |
# File 'lib/chef/role.rb', line 38 def initialize(chef_server_rest: nil) @name = '' @description = '' @default_attributes = Mash.new @override_attributes = Mash.new @env_run_lists = {"_default" => Chef::RunList.new} @chef_server_rest = chef_server_rest end |
Instance Attribute Details
#chef_server_rest ⇒ Object
Returns the value of attribute chef_server_rest.
35 36 37 |
# File 'lib/chef/role.rb', line 35 def chef_server_rest @chef_server_rest end |
Class Method Details
.chef_server_rest ⇒ Object
51 52 53 |
# File 'lib/chef/role.rb', line 51 def self.chef_server_rest Chef::REST.new(Chef::Config[:chef_server_url]) end |
.from_disk(name) ⇒ Object
Load a role from disk - prefers to load the JSON, but will happily load the raw rb files as well. Can search within directories in the role_path.
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/chef/role.rb', line 248 def self.from_disk(name) paths = Array(Chef::Config[:role_path]) paths.each do |path| roles_files = Dir.glob(File.join(Chef::Util::PathHelper.escape_glob(path), "**", "**")) js_files = roles_files.select { |file| file.match(/\/#{name}\.json$/) } rb_files = roles_files.select { |file| file.match(/\/#{name}\.rb$/) } if js_files.count > 1 or rb_files.count > 1 raise Chef::Exceptions::DuplicateRole, "Multiple roles of same type found named #{name}" end js_path, rb_path = js_files.first, rb_files.first if js_path && File.exists?(js_path) # from_json returns object.class => json_class in the JSON. return Chef::JSONCompat.from_json(IO.read(js_path)) elsif rb_path && File.exists?(rb_path) role = Chef::Role.new role.name(name) role.from_file(rb_path) return role end end raise Chef::Exceptions::RoleNotFound, "Role '#{name}' could not be loaded from disk" end |
.json_create(o) ⇒ Object
Create a Chef::Role from JSON
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/chef/role.rb', line 172 def self.json_create(o) role = new role.name(o["name"]) role.description(o["description"]) role.default_attributes(o["default_attributes"]) role.override_attributes(o["override_attributes"]) # _default run_list is in 'run_list' for newer clients, and # 'recipes' for older clients. env_run_list_hash = {"_default" => (o.has_key?("run_list") ? o["run_list"] : o["recipes"])} # Clients before 0.10 do not include env_run_lists, so only # merge if it's there. if o["env_run_lists"] env_run_list_hash.merge!(o["env_run_lists"]) end role.env_run_lists(env_run_list_hash) role end |
.list(inflate = false) ⇒ Object
Get the list of all roles from the API.
194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/chef/role.rb', line 194 def self.list(inflate=false) if inflate response = Hash.new Chef::Search::Query.new.search(:role) do |n| response[n.name] = n unless n.nil? end response else chef_server_rest.get_rest("roles") end end |
.load(name) ⇒ Object
Load a role by name from the API
207 208 209 |
# File 'lib/chef/role.rb', line 207 def self.load(name) chef_server_rest.get_rest("roles/#{name}") end |
Instance Method Details
#active_run_list_for(environment) ⇒ Object
89 90 91 |
# File 'lib/chef/role.rb', line 89 def active_run_list_for(environment) @env_run_lists.has_key?(environment) ? environment : '_default' end |
#create ⇒ Object
Create the role via the REST API
236 237 238 239 |
# File 'lib/chef/role.rb', line 236 def create chef_server_rest.post_rest("roles", self) self end |
#default_attributes(arg = nil) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/chef/role.rb', line 119 def default_attributes(arg=nil) set_or_return( :default_attributes, arg, :kind_of => Hash ) end |
#description(arg = nil) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/chef/role.rb', line 63 def description(arg=nil) set_or_return( :description, arg, :kind_of => String ) end |
#destroy ⇒ Object
Remove this role via the REST API
220 221 222 |
# File 'lib/chef/role.rb', line 220 def destroy chef_server_rest.delete_rest("roles/#{@name}") end |
#env_run_lists(env_run_lists = nil) ⇒ Object Also known as: env_run_list
Per environment run lists
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/chef/role.rb', line 94 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 |
#env_run_lists_add(env_run_lists = nil) ⇒ Object Also known as: env_run_list_add
109 110 111 112 113 114 |
# File 'lib/chef/role.rb', line 109 def env_run_lists_add(env_run_lists=nil) if (!env_run_lists.nil?) env_run_lists.each { |k,v| @env_run_lists[k] = Chef::RunList.new(*Array(v))} end @env_run_lists end |
#environment(env_name) ⇒ Object
211 212 213 |
# File 'lib/chef/role.rb', line 211 def environment(env_name) chef_server_rest.get_rest("roles/#{@name}/environments/#{env_name}") end |
#environments ⇒ Object
215 216 217 |
# File 'lib/chef/role.rb', line 215 def environments chef_server_rest.get_rest("roles/#{@name}/environments") end |
#name(arg = nil) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/chef/role.rb', line 55 def name(arg=nil) set_or_return( :name, arg, :regex => /^[\-[:alnum:]_]+$/ ) end |
#override_attributes(arg = nil) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/chef/role.rb', line 127 def override_attributes(arg=nil) set_or_return( :override_attributes, arg, :kind_of => Hash ) end |
#run_list(*args) ⇒ Object Also known as: recipes
71 72 73 74 75 76 |
# File 'lib/chef/role.rb', line 71 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
81 82 83 84 85 86 87 |
# File 'lib/chef/role.rb', line 81 def run_list_for(environment) if env_run_lists[environment].nil? env_run_lists["_default"] else env_run_lists[environment] end end |
#save ⇒ Object
Save this role via the REST API
225 226 227 228 229 230 231 232 233 |
# File 'lib/chef/role.rb', line 225 def save begin chef_server_rest.put_rest("roles/#{@name}", self) rescue Net::HTTPServerException => e raise e unless e.response.code == "404" chef_server_rest.post_rest("roles", self) end self end |
#to_hash ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/chef/role.rb', line 135 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", #Render to_json correctly for run_list items (both run_list and evn_run_lists) #so malformed json does not result "run_list" => run_list.run_list.map { |item| item.to_s }, "env_run_lists" => env_run_lists_without_default.inject({}) do |accumulator, (k, v)| accumulator[k] = v.map { |x| x.to_s } accumulator end } result end |
#to_json(*a) ⇒ Object
Serialize this object as a hash
158 159 160 |
# File 'lib/chef/role.rb', line 158 def to_json(*a) Chef::JSONCompat.to_json(to_hash, *a) end |
#to_s ⇒ Object
As a string
242 243 244 |
# File 'lib/chef/role.rb', line 242 def to_s "role[#{@name}]" end |
#update_from!(o) ⇒ Object
162 163 164 165 166 167 168 169 |
# File 'lib/chef/role.rb', line 162 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 |