Class: Chef::Role

Inherits:
Object
  • Object
show all
Includes:
Mixin::FromFile, Mixin::ParamsValidate
Defined in:
lib/chef/role.rb

Constant Summary collapse

DESIGN_DOCUMENT =
{
  "version" => 3,
  "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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ParamsValidate

#set_or_return, #validate

Methods included from Mixin::FromFile

#class_from_file, #from_file

Constructor Details

#initializeRole

Create a new Chef::Role object.



60
61
62
63
64
65
66
67
68
# File 'lib/chef/role.rb', line 60

def initialize()
  @name = '' 
  @description = '' 
  @default_attributes = Mash.new
  @override_attributes = Mash.new
  @recipes = Array.new 
  @couchdb_rev = nil
  @couchdb = Chef::CouchDB.new
end

Instance Attribute Details

#couchdb_revObject

Returns the value of attribute couchdb_rev.



57
58
59
# File 'lib/chef/role.rb', line 57

def couchdb_rev
  @couchdb_rev
end

Class Method Details

.create_design_documentObject

Set up our CouchDB design document



188
189
190
# File 'lib/chef/role.rb', line 188

def self.create_design_document
  Chef::CouchDB.new.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.



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/chef/role.rb', line 199

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



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

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

.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.



147
148
149
150
151
152
153
154
# File 'lib/chef/role.rb', line 147

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

.load(name) ⇒ Object

Load a role by name from CouchDB



157
158
159
# File 'lib/chef/role.rb', line 157

def self.load(name)
  Chef::CouchDB.new.load("role", name)
end

.sync_from_disk_to_couchdbObject

Sync all the json roles with couchdb from disk



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/chef/role.rb', line 214

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.load(short_name)
      r.couchdb_rev = couch_role.couchdb_rev
      Chef::Log.debug("Replacing role #{short_name} with data from #{role_file}")
    rescue Net::HTTPServerException
      Chef::Log.debug("Creating role #{short_name} with data from #{role_file}")
    end
    r.save
  end
end

Instance Method Details

#default_attributes(arg = nil) ⇒ Object



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

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

#description(arg = nil) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/chef/role.rb', line 78

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

#destroyObject

Remove this role from the CouchDB



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/chef/role.rb', line 162

def 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.save
    end
  else
   Chef::Node.list.each do |node|
     n = Chef::Node.load(node)
     n.run_list.remove("role[#{@name}]")
     n.save
   end
  end
end

#name(arg = nil) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/chef/role.rb', line 70

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

#override_attributes(arg = nil) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/chef/role.rb', line 106

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

#recipes(*arg) ⇒ Object



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

def recipes(*arg) 
  arg.flatten!
  if arg.length == 0
    @recipes
  else
    arg.each do |entry|
      raise ArgumentError, 'Recipes must be strings!' unless entry.kind_of?(String)
    end
    @recipes = arg
  end
end

#saveObject

Save this role to the CouchDB



182
183
184
185
# File 'lib/chef/role.rb', line 182

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

#to_hashObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chef/role.rb', line 114

def to_hash
  result = {
    "name" => @name,
    "description" => @description,
    'json_class' => self.class.name,
    "default_attributes" => @default_attributes,
    "override_attributes" => @override_attributes,
    "chef_type" => "role",
    "recipes" => @recipes,
  }
  result["_rev"] = @couchdb_rev if @couchdb_rev
  result
end

#to_json(*a) ⇒ Object

Serialize this object as a hash



129
130
131
# File 'lib/chef/role.rb', line 129

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

#to_sObject

As a string



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

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