class Chef::Role

Public Class Methods

chef_server_rest() click to toggle source
# File lib/chef/role.rb, line 50
def self.chef_server_rest
  Chef::ServerAPI.new(Chef::Config[:chef_server_url])
end
from_disk(name) click to toggle source

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.

# File lib/chef/role.rb, line 245
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_dir(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 || 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.
      hsh = Chef::JSONCompat.parse(IO.read(js_path))
      return from_hash(hsh)
    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
from_hash(o) click to toggle source
# File lib/chef/role.rb, line 169
def self.from_hash(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) click to toggle source

Get the list of all roles from the API.

# File lib/chef/role.rb, line 191
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("roles")
  end
end
load(name) click to toggle source

Load a role by name from the API

# File lib/chef/role.rb, line 204
def self.load(name)
  from_hash(chef_server_rest.get("roles/#{name}"))
end
new(chef_server_rest: nil) click to toggle source

Create a new Chef::Role object.

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

Public Instance Methods

active_run_list_for(environment) click to toggle source
# File lib/chef/role.rb, line 88
def active_run_list_for(environment)
  @env_run_lists.has_key?(environment) ? environment : "_default"
end
chef_server_rest() click to toggle source
# File lib/chef/role.rb, line 46
def chef_server_rest
  @chef_server_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url])
end
create() click to toggle source

Create the role via the REST API

# File lib/chef/role.rb, line 233
def create
  chef_server_rest.post("roles", self)
  self
end
default_attributes(arg = nil) click to toggle source
# 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) click to toggle source
# File lib/chef/role.rb, line 62
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 217
def destroy
  chef_server_rest.delete("roles/#{@name}")
end
env_run_list(env_run_lists = nil)
Alias for: env_run_lists
env_run_list_add(env_run_lists = nil)
Alias for: env_run_lists_add
env_run_lists(env_run_lists = nil) click to toggle source

Per environment run lists

# File lib/chef/role.rb, line 93
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
env_run_lists_add(env_run_lists = nil) click to toggle source
# File lib/chef/role.rb, line 108
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
Also aliased as: env_run_list_add
environment(env_name) click to toggle source
# File lib/chef/role.rb, line 208
def environment(env_name)
  chef_server_rest.get("roles/#{@name}/environments/#{env_name}")
end
environments() click to toggle source
# File lib/chef/role.rb, line 212
def environments
  chef_server_rest.get("roles/#{@name}/environments")
end
name(arg = nil) click to toggle source
# File lib/chef/role.rb, line 54
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 125
def override_attributes(arg = nil)
  set_or_return(
    :override_attributes,
    arg,
    :kind_of => Hash
  )
end
recipes(*args)
Alias for: run_list
run_list(*args) click to toggle source
# File lib/chef/role.rb, line 70
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 80
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 222
def save
  begin
    chef_server_rest.put("roles/#{@name}", self)
  rescue Net::HTTPServerException => e
    raise e unless e.response.code == "404"
    chef_server_rest.post("roles", self)
  end
  self
end
to_hash() click to toggle source
# File lib/chef/role.rb, line 133
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) click to toggle source

Serialize this object as a hash

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

As a string

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