Class: Chef::Role

Inherits:
Object
  • Object
show all
Includes:
IndexQueue::Indexable, 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

Attributes included from IndexQueue::Indexable

#index_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IndexQueue::Indexable

#add_to_index, #delete_from_index, included, #index_object_type, #with_indexer_metadata

Methods included from Mixin::ParamsValidate

#set_or_return, #validate

Methods included from Mixin::FromFile

#class_from_file, #from_file

Constructor Details

#initialize(couchdb = nil) ⇒ Role

Create a new Chef::Role object.



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

def initialize(couchdb=nil)
  @name = ''
  @description = ''
  @default_attributes = Mash.new
  @override_attributes = Mash.new
  @run_list = Chef::RunList.new
  @couchdb_rev = nil
  @couchdb_id = nil
  @couchdb = couchdb || Chef::CouchDB.new
end

Instance Attribute Details

#couchdbObject

Returns the value of attribute couchdb.



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

def couchdb
  @couchdb
end

#couchdb_idObject

Returns the value of attribute couchdb_id.



63
64
65
# File 'lib/chef/role.rb', line 63

def couchdb_id
  @couchdb_id
end

#couchdb_revObject

Returns the value of attribute couchdb_rev.



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

def couchdb_rev
  @couchdb_rev
end

Class Method Details

.cdb_list(inflate = false, couchdb = nil) ⇒ 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.



172
173
174
175
176
# File 'lib/chef/role.rb', line 172

def self.cdb_list(inflate=false, couchdb=nil)
  rs = (couchdb || Chef::CouchDB.new).list("roles", inflate)
  lookup = (inflate ? "value" : "key")
  rs["rows"].collect { |r| r[lookup] }
end

.cdb_load(name, couchdb = nil) ⇒ Object

Load a role by name from CouchDB



192
193
194
# File 'lib/chef/role.rb', line 192

def self.cdb_load(name, couchdb=nil)
  (couchdb || Chef::CouchDB.new).load("role", name)
end

.chef_server_restObject



86
87
88
# File 'lib/chef/role.rb', line 86

def self.chef_server_rest
  Chef::REST.new(Chef::Config[:chef_server_url])
end

.create_design_document(couchdb = nil) ⇒ Object

Set up our CouchDB design document



242
243
244
# File 'lib/chef/role.rb', line 242

def self.create_design_document(couchdb=nil)
  (couchdb || Chef::CouchDB.new).create_design_document("roles", DESIGN_DOCUMENT)
end

.exists?(rolename, couchdb) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
204
205
206
207
# File 'lib/chef/role.rb', line 201

def self.exists?(rolename, couchdb)
  begin
    self.cdb_load(rolename, couchdb)
  rescue Chef::Exceptions::CouchDBNotFound
    nil
  end
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.



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/chef/role.rb', line 253

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
  else
    raise Chef::Exceptions::RoleNotFound, "Role '#{name}' could not be loaded from disk"
  end
end

.json_create(o) ⇒ Object

Create a Chef::Role from JSON



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

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"])
  role.run_list(if o.has_key?("run_list")
                  o["run_list"]
                else
                  o["recipes"]
                end)
  role.couchdb_rev = o["_rev"] if o.has_key?("_rev")
  role.index_id = role.couchdb_id
  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.



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/chef/role.rb', line 179

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



197
198
199
# File 'lib/chef/role.rb', line 197

def self.load(name)
  chef_server_rest.get_rest("roles/#{name}")
end

.sync_from_disk_to_couchdbObject

Sync all the json roles with couchdb from disk



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/chef/role.rb', line 270

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_destroyObject

Remove this role from the CouchDB



210
211
212
# File 'lib/chef/role.rb', line 210

def cdb_destroy
  couchdb.delete("role", @name, couchdb_rev)
end

#cdb_saveObject

Save this role to the CouchDB



220
221
222
# File 'lib/chef/role.rb', line 220

def cdb_save
  self.couchdb_rev = couchdb.store("role", @name, self)["rev"]
end

#chef_server_restObject



82
83
84
# File 'lib/chef/role.rb', line 82

def chef_server_rest
  Chef::REST.new(Chef::Config[:chef_server_url])
end

#createObject

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

def recipes(*args)

  Chef::Log.warn "Chef::Role#recipes method is deprecated.  Please use Chef::Role#run_list"
  run_list(*args)
end


117
118
119
120
121
122
123
# File 'lib/chef/role.rb', line 117

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

#description(arg = nil) ⇒ Object



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

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

#destroyObject

Remove this role via the REST API



215
216
217
# File 'lib/chef/role.rb', line 215

def destroy
  chef_server_rest.delete_rest("roles/#{@name}")
end

#name(arg = nil) ⇒ Object



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

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

#override_attributes(arg = nil) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/chef/role.rb', line 125

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

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



106
107
108
# File 'lib/chef/role.rb', line 106

def run_list(*args)
  (args.length > 0) ? @run_list.reset!(args) : @run_list
end

#saveObject

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_hashObject



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/chef/role.rb', line 133

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



148
149
150
# File 'lib/chef/role.rb', line 148

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

#to_sObject

As a string



247
248
249
# File 'lib/chef/role.rb', line 247

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