In Files

Parent

Class/Module Index [+]

Quicksearch

Chef::Role

Constants

DESIGN_DOCUMENT

Attributes

couchdb[RW]
couchdb_id[R]
couchdb_rev[RW]

Public Class Methods

cdb_list(inflate=false, couchdb=nil) click to toggle source

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.

# File lib/chef/role.rb, line 225
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) click to toggle source

Load a role by name from CouchDB

# File lib/chef/role.rb, line 245
def self.cdb_load(name, couchdb=nil)
  (couchdb || Chef::CouchDB.new).load("role", name)
end
chef_server_rest() click to toggle source
# File lib/chef/role.rb, line 87
def self.chef_server_rest
  Chef::REST.new(Chef::Config[:chef_server_url])
end
create_design_document(couchdb=nil) click to toggle source

Set up our CouchDB design document

# File lib/chef/role.rb, line 303
def self.create_design_document(couchdb=nil)
  (couchdb || Chef::CouchDB.new).create_design_document("roles", DESIGN_DOCUMENT)
end
exists?(rolename, couchdb) click to toggle source
# File lib/chef/role.rb, line 254
def self.exists?(rolename, couchdb)
  begin
    self.cdb_load(rolename, couchdb)
  rescue Chef::Exceptions::CouchDBNotFound
    nil
  end
end
from_disk(name, force=nil) click to toggle source

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

# File lib/chef/role.rb, line 314
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"
    # from_json returns object.class => json_class in the JSON.
    Chef::JSONCompat.from_json(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) click to toggle source

Create a Chef::Role from JSON

# File lib/chef/role.rb, line 199
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.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) click to toggle source

Get the list of all roles from the API.

# File lib/chef/role.rb, line 232
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) click to toggle source

Load a role by name from the API

# File lib/chef/role.rb, line 250
def self.load(name)
  chef_server_rest.get_rest("roles/#{name}")
end
new(couchdb=nil) click to toggle source

Create a new Chef::Role object.

# File lib/chef/role.rb, line 67
def initialize(couchdb=nil)
  @name = ''
  @description = ''
  @default_attributes = Mash.new
  @override_attributes = Mash.new
  @env_run_lists = {"_default" => Chef::RunList.new}
  @couchdb_rev = nil
  @couchdb_id = nil
  @couchdb = couchdb || Chef::CouchDB.new
end
sync_from_disk_to_couchdb() click to toggle source

Sync all the json roles with couchdb from disk

# File lib/chef/role.rb, line 332
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

Public Instance Methods

active_run_list_for(environment) click to toggle source
# File lib/chef/role.rb, line 125
def active_run_list_for(environment)
  @env_run_lists.has_key?(environment) ? environment : '_default'
end
cdb_destroy() click to toggle source

Remove this role from the CouchDB

# File lib/chef/role.rb, line 271
def cdb_destroy
  couchdb.delete("role", @name, couchdb_rev)
end
cdb_save() click to toggle source

Save this role to the CouchDB

# File lib/chef/role.rb, line 281
def cdb_save
  self.couchdb_rev = couchdb.store("role", @name, self)["rev"]
end
chef_server_rest() click to toggle source
# File lib/chef/role.rb, line 83
def chef_server_rest
  Chef::REST.new(Chef::Config[:chef_server_url])
end
couchdb_id=(value) click to toggle source
# File lib/chef/role.rb, line 78
def couchdb_id=(value)
  @couchdb_id = value
  self.index_id = value
end
create() click to toggle source

Create the role via the REST API

# File lib/chef/role.rb, line 297
def create
  chef_server_rest.post_rest("roles", self)
  self
end
default_attributes(arg=nil) click to toggle source
# File lib/chef/role.rb, line 145
def default_attributes(arg=nil)
  set_or_return(
    :default_attributes,
    arg,
    :kind_of => Hash
  )
end
description(arg=nil) click to toggle source
# File lib/chef/role.rb, line 99
def description(arg=nil)
  set_or_return(
    :description,
    arg,
    :kind_of => String
  )
end
destroy() click to toggle source

Remove this role via the REST API

# File lib/chef/role.rb, line 276
def destroy
  chef_server_rest.delete_rest("roles/#{@name}")
end
env_run_list(env_run_lists=nil) click to toggle source
Alias for: env_run_lists
env_run_lists(env_run_lists=nil) click to toggle source

Per environment run lists

# File lib/chef/role.rb, line 130
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
Also aliased as: env_run_list
environment(env_name) click to toggle source
# File lib/chef/role.rb, line 262
def environment(env_name)
  chef_server_rest.get_rest("roles/#{@name}/environments/#{env_name}")
end
environments() click to toggle source
# File lib/chef/role.rb, line 266
def environments
  chef_server_rest.get_rest("roles/#{@name}/environments")
end
name(arg=nil) click to toggle source
# File lib/chef/role.rb, line 91
def name(arg=nil)
  set_or_return(
    :name,
    arg,
    :regex => /^[\-[:alnum:]_]+$/
  )
end
override_attributes(arg=nil) click to toggle source
# File lib/chef/role.rb, line 153
def override_attributes(arg=nil)
  set_or_return(
    :override_attributes,
    arg,
    :kind_of => Hash
  )
end
recipes(*args) click to toggle source
Alias for: run_list
run_list(*args) click to toggle source
# File lib/chef/role.rb, line 107
def run_list(*args)
  if (args.length > 0)
    @env_run_lists["_default"].reset!(args)
  end
  @env_run_lists["_default"]
end
Also aliased as: recipes
run_list_for(environment) click to toggle source

For run_list expansion

# File lib/chef/role.rb, line 117
def run_list_for(environment)
  if env_run_lists[environment].nil?
    env_run_lists["_default"]
  else
    env_run_lists[environment]
  end
end
save() click to toggle source

Save this role via the REST API

# File lib/chef/role.rb, line 286
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() click to toggle source
# File lib/chef/role.rb, line 161
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["_rev"] = couchdb_rev if couchdb_rev
  result
end
to_json(*a) click to toggle source

Serialize this object as a hash

# File lib/chef/role.rb, line 185
def to_json(*a)
  to_hash.to_json(*a)
end
to_s() click to toggle source

As a string

# File lib/chef/role.rb, line 308
def to_s
  "role[#{@name}]"
end
update_from!(o) click to toggle source
# File lib/chef/role.rb, line 189
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

[Validate]

Generated with the Darkfish Rdoc Generator 2.