Class: Chef::Role
- Includes:
- Mixin::FromFile, Mixin::ParamsValidate
- Defined in:
- lib/chef/role.rb
Constant Summary collapse
- DESIGN_DOCUMENT =
{ "version" => 6, "language" => "javascript", "views" => { "all" => { "map" => <<-EOJS function(doc) { if (doc.chef_type == "role") { emit(doc.name, doc); } } EOJS }, "all_id" => { "map" => <<-EOJS function(doc) { if (doc.chef_type == "role") { emit(doc.name, doc.name); } } EOJS } } }
Instance Attribute Summary collapse
-
#couchdb_id ⇒ Object
Returns the value of attribute couchdb_id.
-
#couchdb_rev ⇒ Object
Returns the value of attribute couchdb_rev.
Class Method Summary collapse
-
.cdb_list(inflate = false) ⇒ Object
List all the Chef::Role objects in the CouchDB.
-
.cdb_load(name) ⇒ Object
Load a role by name from CouchDB.
-
.create_design_document ⇒ Object
Set up our CouchDB design document.
-
.from_disk(name, force = nil) ⇒ 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.
-
.sync_from_disk_to_couchdb ⇒ Object
Sync all the json roles with couchdb from disk.
Instance Method Summary collapse
-
#cdb_destroy ⇒ Object
Remove this role from the CouchDB.
-
#cdb_save ⇒ Object
Save this role to the CouchDB.
-
#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.
-
#initialize ⇒ Role
constructor
Create a new Chef::Role object.
- #name(arg = nil) ⇒ Object
- #override_attributes(arg = nil) ⇒ Object
- #recipes(*args) ⇒ Object
- #run_list(*args) ⇒ Object
-
#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.
Methods included from Mixin::ParamsValidate
Methods included from Mixin::FromFile
Constructor Details
#initialize ⇒ Role
Create a new Chef::Role object.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/chef/role.rb', line 62 def initialize @name = '' @description = '' @default_attributes = Mash.new @override_attributes = Mash.new @run_list = Chef::RunList.new @couchdb_rev = nil @couchdb_id = nil @couchdb = Chef::CouchDB.new end |
Instance Attribute Details
#couchdb_id ⇒ Object
Returns the value of attribute couchdb_id.
59 60 61 |
# File 'lib/chef/role.rb', line 59 def couchdb_id @couchdb_id end |
#couchdb_rev ⇒ Object
Returns the value of attribute couchdb_rev.
59 60 61 |
# File 'lib/chef/role.rb', line 59 def couchdb_rev @couchdb_rev end |
Class Method Details
.cdb_list(inflate = false) ⇒ Object
List all the Chef::Role objects in the CouchDB. If inflate is set to true, you will get the full list of all Roles, fully inflated.
159 160 161 162 163 164 165 166 167 |
# File 'lib/chef/role.rb', line 159 def self.cdb_list(inflate=false) couchdb = Chef::CouchDB.new rs = couchdb.list("roles", inflate) if inflate rs["rows"].collect { |r| r["value"] } else rs["rows"].collect { |r| r["key"] } end end |
.cdb_load(name) ⇒ Object
Load a role by name from CouchDB
184 185 186 187 |
# File 'lib/chef/role.rb', line 184 def self.cdb_load(name) couchdb = Chef::CouchDB.new couchdb.load("role", name) end |
.create_design_document ⇒ Object
Set up our CouchDB design document
257 258 259 260 |
# File 'lib/chef/role.rb', line 257 def self.create_design_document couchdb = Chef::CouchDB.new couchdb.create_design_document("roles", DESIGN_DOCUMENT) end |
.from_disk(name, force = nil) ⇒ Object
Load a role from disk - prefers to load the JSON, but will happily load the raw rb files as well.
269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/chef/role.rb', line 269 def self.from_disk(name, force=nil) js_file = File.join(Chef::Config[:role_path], "#{name}.json") rb_file = File.join(Chef::Config[:role_path], "#{name}.rb") if File.exists?(js_file) || force == "json" JSON.parse(IO.read(js_file)) elsif File.exists?(rb_file) || force == "ruby" role = Chef::Role.new role.name(name) role.from_file(rb_file) role end end |
.json_create(o) ⇒ Object
Create a Chef::Role from JSON
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/chef/role.rb', line 141 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"]) if o.has_key?("run_list") role.run_list(o["run_list"]) if o.has_key?("run_list") else role.run_list(o["recipes"]) end role.couchdb_rev = o["_rev"] if o.has_key?("_rev") role.couchdb_id = o["_id"] if o.has_key?("_id") role end |
.list(inflate = false) ⇒ Object
Get the list of all roles from the API.
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/chef/role.rb', line 170 def self.list(inflate=false) r = Chef::REST.new(Chef::Config[:chef_server_url]) if inflate response = Hash.new Chef::Search::Query.new.search(:role) do |n| response[n.name] = n unless n.nil? end response else r.get_rest("roles") end end |
.load(name) ⇒ Object
Load a role by name from the API
190 191 192 193 |
# File 'lib/chef/role.rb', line 190 def self.load(name) r = Chef::REST.new(Chef::Config[:chef_server_url]) r.get_rest("roles/#{name}") end |
.sync_from_disk_to_couchdb ⇒ Object
Sync all the json roles with couchdb from disk
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/chef/role.rb', line 284 def self.sync_from_disk_to_couchdb Dir[File.join(Chef::Config[:role_path], "*.json")].each do |role_file| short_name = File.basename(role_file, ".json") Chef::Log.warn("Loading #{short_name}") r = Chef::Role.from_disk(short_name, "json") begin couch_role = Chef::Role.cdb_load(short_name) r.couchdb_rev = couch_role.couchdb_rev Chef::Log.debug("Replacing role #{short_name} with data from #{role_file}") rescue Chef::Exceptions::CouchDBNotFound Chef::Log.debug("Creating role #{short_name} with data from #{role_file}") end r.cdb_save end end |
Instance Method Details
#cdb_destroy ⇒ Object
Remove this role from the CouchDB
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/chef/role.rb', line 196 def cdb_destroy @couchdb.delete("role", @name, @couchdb_rev) if Chef::Config[:couchdb_version] == 0.9 rs = @couchdb.get_view("nodes", "by_run_list", :startkey => "role[#{@name}]", :endkey => "role[#{@name}]", :include_docs => true) rs["rows"].each do |row| node = row["doc"] node.run_list.remove("role[#{@name}]") node.cdb_save end else Chef::Node.cdb_list.each do |node| n = Chef::Node.cdb_load(node) n.run_list.remove("role[#{@name}]") n.cdb_save end end end |
#cdb_save ⇒ Object
Save this role to the CouchDB
229 230 231 232 |
# File 'lib/chef/role.rb', line 229 def cdb_save results = @couchdb.store("role", @name, self) @couchdb_rev = results["rev"] end |
#create ⇒ Object
Create the role via the REST API
250 251 252 253 254 |
# File 'lib/chef/role.rb', line 250 def create r = Chef::REST.new(Chef::Config[:chef_server_url]) r.post_rest("roles", self) self end |
#default_attributes(arg = nil) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/chef/role.rb', line 105 def default_attributes(arg=nil) set_or_return( :default_attributes, arg, :kind_of => Hash ) end |
#description(arg = nil) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/chef/role.rb', line 81 def description(arg=nil) set_or_return( :description, arg, :kind_of => String ) end |
#destroy ⇒ Object
Remove this role via the REST API
216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/chef/role.rb', line 216 def destroy r = Chef::REST.new(Chef::Config[:chef_server_url]) r.delete_rest("roles/#{@name}") Chef::Node.list.each do |node| n = Chef::Node.load(node[0]) n.run_list.remove("role[#{@name}]") n.save end end |
#name(arg = nil) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/chef/role.rb', line 73 def name(arg=nil) set_or_return( :name, arg, :regex => /^[\-[:alnum:]_]+$/ ) end |
#override_attributes(arg = nil) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/chef/role.rb', line 113 def override_attributes(arg=nil) set_or_return( :override_attributes, arg, :kind_of => Hash ) end |
#recipes(*args) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/chef/role.rb', line 97 def recipes(*args) if args.length > 0 @run_list.reset(args) else @run_list.recipes end end |
#run_list(*args) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/chef/role.rb', line 89 def run_list(*args) if args.length > 0 @run_list.reset(args) else @run_list end end |
#save ⇒ Object
Save this role via the REST API
235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/chef/role.rb', line 235 def save r = Chef::REST.new(Chef::Config[:chef_server_url]) begin r.put_rest("roles/#{@name}", self) rescue Net::HTTPServerException => e if e.response.code == "404" r.post_rest("roles", self) else raise e end end self end |
#to_hash ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/chef/role.rb', line 121 def to_hash 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.run_list } result["_rev"] = @couchdb_rev if @couchdb_rev result end |
#to_json(*a) ⇒ Object
Serialize this object as a hash
136 137 138 |
# File 'lib/chef/role.rb', line 136 def to_json(*a) to_hash.to_json(*a) end |
#to_s ⇒ Object
As a string
263 264 265 |
# File 'lib/chef/role.rb', line 263 def to_s "role[#{@name}]" end |