Class: Ridley::CookbookResource
- Defined in:
- lib/ridley/resources/cookbook_resource.rb
Instance Method Summary collapse
-
#all ⇒ Hash
List all of the cookbooks and their versions present on the remote.
-
#delete(name, version, options = {}) ⇒ Boolean
Delete a cookbook of the given name and version on the remote Chef server.
-
#delete_all(name, options = {}) ⇒ Object
Delete all of the versions of a given cookbook on the remote Chef server.
-
#download(name, version, destination = Dir.mktmpdir) ⇒ String
Download the entire cookbook.
- #find(object, version) ⇒ nil, CookbookResource
-
#initialize(connection_registry, client_name, client_key, options = {}) ⇒ CookbookResource
constructor
A new instance of CookbookResource.
-
#latest_version(name) ⇒ String?
Return the latest version of the given cookbook found on the remote Chef server.
-
#satisfy(name, constraint) ⇒ CookbookResource?
Return the version of the given cookbook which best stasifies the given constraint.
-
#update(cookbook, options = {}) ⇒ Hash
(also: #create)
Update or create a new Cookbook Version of the given name, version with the given manifest of files and checksums.
-
#upload(path, options = {}) ⇒ Hash
Uploads a cookbook to the remote Chef server from the contents of a filepath.
-
#versions(name) ⇒ Array<String>
Return a list of versions for the given cookbook present on the remote Chef server.
Methods inherited from Resource
#connection, #from_file, #from_json, #new, representation, represented_by, resource_path, set_resource_path
Constructor Details
#initialize(connection_registry, client_name, client_key, options = {}) ⇒ CookbookResource
Returns a new instance of CookbookResource.
10 11 12 13 |
# File 'lib/ridley/resources/cookbook_resource.rb', line 10 def initialize(connection_registry, client_name, client_key, = {}) super(connection_registry) @sandbox_resource = SandboxResource.new_link(connection_registry, client_name, client_key, ) end |
Instance Method Details
#all ⇒ Hash
List all of the cookbooks and their versions present on the remote
30 31 32 33 34 35 36 37 38 |
# File 'lib/ridley/resources/cookbook_resource.rb', line 30 def all response = request(:get, self.class.resource_path, num_versions: "all") {}.tap do |cookbooks| response.each do |name, details| cookbooks[name] = details["versions"].collect { |version| version["version"] } end end end |
#delete(name, version, options = {}) ⇒ Boolean
Delete a cookbook of the given name and version on the remote Chef server
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ridley/resources/cookbook_resource.rb', line 48 def delete(name, version, = {}) = .reverse_merge(purge: false) url = "#{self.class.resource_path}/#{name}/#{version}" url += "?purge=true" if [:purge] request(:delete, url) true rescue AbortError => ex return nil if ex.cause.is_a?(Errors::HTTPNotFound) abort(ex.cause) end |
#delete_all(name, options = {}) ⇒ Object
Delete all of the versions of a given cookbook on the remote Chef server
66 67 68 |
# File 'lib/ridley/resources/cookbook_resource.rb', line 66 def delete_all(name, = {}) versions(name).collect { |version| future(:delete, name, version, ) }.map(&:value) end |
#download(name, version, destination = Dir.mktmpdir) ⇒ String
Download the entire cookbook
82 83 84 85 86 87 88 |
# File 'lib/ridley/resources/cookbook_resource.rb', line 82 def download(name, version, destination = Dir.mktmpdir) if cookbook = find(name, version) cookbook.download(destination) else abort Errors::ResourceNotFound.new("cookbook #{name} (#{version}) was not found") end end |
#find(object, version) ⇒ nil, CookbookResource
94 95 96 97 98 99 100 |
# File 'lib/ridley/resources/cookbook_resource.rb', line 94 def find(object, version) chef_id = object.respond_to?(:chef_id) ? object.chef_id : object new(request(:get, "#{self.class.resource_path}/#{chef_id}/#{version}")) rescue AbortError => ex return nil if ex.cause.is_a?(Errors::HTTPNotFound) abort(ex.cause) end |
#latest_version(name) ⇒ String?
Return the latest version of the given cookbook found on the remote Chef server
109 110 111 112 113 114 115 |
# File 'lib/ridley/resources/cookbook_resource.rb', line 109 def latest_version(name) ver = versions(name).collect do |version| Semverse::Version.new(version) end.sort.last ver.nil? ? nil : ver.to_s end |
#satisfy(name, constraint) ⇒ CookbookResource?
Return the version of the given cookbook which best stasifies the given constraint
128 129 130 131 132 133 |
# File 'lib/ridley/resources/cookbook_resource.rb', line 128 def satisfy(name, constraint) version = Semverse::Constraint.satisfy_best(constraint, versions(name)).to_s find(name, version) rescue Semverse::NoSolutionError nil end |
#update(cookbook, options = {}) ⇒ Hash Also known as: create
Update or create a new Cookbook Version of the given name, version with the given manifest of files and checksums.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/ridley/resources/cookbook_resource.rb', line 154 def update(cookbook, = {}) = .reverse_merge(force: false, freeze: false) cookbook.frozen = [:freeze] url = "cookbooks/#{cookbook.cookbook_name}/#{cookbook.version}" url << "?force=true" if [:force] request(:put, url, cookbook.to_json) rescue AbortError => ex if ex.cause.is_a?(Errors::HTTPConflict) abort Ridley::Errors::FrozenCookbook.new(ex) end abort(ex.cause) end |
#upload(path, options = {}) ⇒ Hash
Uploads a cookbook to the remote Chef server from the contents of a filepath
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/ridley/resources/cookbook_resource.rb', line 186 def upload(path, = {}) = .reverse_merge(validate: true, force: false, freeze: false) cookbook = Ridley::Chef::Cookbook.from_path(path) unless (existing = find(cookbook.cookbook_name, cookbook.version)).nil? if existing.frozen? && [:force] == false msg = "The cookbook #{cookbook.cookbook_name} (#{cookbook.version}) already exists and is" msg << " frozen on the Chef server. Use the 'force' option to override." abort Ridley::Errors::FrozenCookbook.new(msg) end end if [:validate] cookbook.validate end # Compile metadata on upload if it hasn't been compiled already unless cookbook. = cookbook. cookbook.reload end # Skip uploading the raw metadata (metadata.rb). The raw metadata is unecessary for the # client, and this is required until compiled metadata (metadata.json) takes precedence over # raw metadata in the Chef-Client. # # We can change back to including the raw metadata in the future after this has been fixed or # just remove these comments. There is no circumstance that I can currently think of where # raw metadata should ever be read by the client. # # - Jamie # # See the following tickets for more information: # * https://tickets.opscode.com/browse/CHEF-4811 # * https://tickets.opscode.com/browse/CHEF-4810 cookbook.manifest[:root_files].reject! do |file| File.basename(file[:name]).downcase == Ridley::Chef::Cookbook::Metadata::RAW_FILE_NAME end checksums = cookbook.checksums.dup sandbox = sandbox_resource.create(checksums.keys.sort) sandbox.upload(checksums) sandbox.commit update(cookbook, Ridley::Helpers.(, :force, :freeze)) ensure # Destroy the compiled metadata only if it was created if # The garbage collector still has a reference to the file we'd # like to delete. On *nix this isn't a big deal, but on Windows # [ ruby 2.0.0p451 (2014-02-24) [i386-mingw32] ] open files # cannot be deleted, so we're forced to garbage collect to # ensure we can delete the file. This is CRITICAL to ensure that # a stale metadata file isn't left on disk because next time we # would use that file instead of recompiling. GC.start File.delete() end end |
#versions(name) ⇒ Array<String>
Return a list of versions for the given cookbook present on the remote Chef server
256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/ridley/resources/cookbook_resource.rb', line 256 def versions(name) response = request(:get, "#{self.class.resource_path}/#{name}") response[name]["versions"].collect do |cb_ver| cb_ver["version"] end rescue AbortError => ex if ex.cause.is_a?(Errors::HTTPNotFound) abort Errors::ResourceNotFound.new(ex) end abort(ex.cause) end |