Class: Ridley::CookbookResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/ridley/resources/cookbook_resource.rb

Instance Method Summary collapse

Methods inherited from Resource

#connection, #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.



6
7
8
9
# File 'lib/ridley/resources/cookbook_resource.rb', line 6

def initialize(connection_registry, client_name, client_key, options = {})
  super(connection_registry)
  @sandbox_resource = SandboxResource.new_link(connection_registry, client_name, client_key, options)
end

Instance Method Details

#allHash

List all of the cookbooks and their versions present on the remote

Examples:

return value

{
  "ant" => [
    "0.10.1"
  ],
  "apache2" => [
    "1.4.0"
  ]
}

Returns:

  • (Hash)

    a hash containing keys which represent cookbook names and values which contain an array of strings representing the available versions



26
27
28
29
30
31
32
33
34
# File 'lib/ridley/resources/cookbook_resource.rb', line 26

def all
  response = request(:get, self.class.resource_path)

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

Parameters:

  • name (String)
  • version (String)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • purge (Boolean) — default: false

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ridley/resources/cookbook_resource.rb', line 44

def delete(name, version, options = {})
  options = options.reverse_merge(purge: false)
  url = "#{self.class.resource_path}/#{name}/#{version}"
  url += "?purge=true" if options[: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

Parameters:

  • name (String)

    name of the cookbook to delete

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • purge (Boolean) — default: false


62
63
64
# File 'lib/ridley/resources/cookbook_resource.rb', line 62

def delete_all(name, options = {})
  versions(name).collect { |version| future(:delete, name, version, options) }.map(&:value)
end

#download(name, version, destination = Dir.mktmpdir) ⇒ String

Download the entire cookbook

Parameters:

  • name (String)
  • version (String)
  • destination (String) (defaults to: Dir.mktmpdir)

    (Dir.mktmpdir) the place to download the cookbook too. If no value is provided the cookbook will be downloaded to a temporary location

Returns:

  • (String)

    the path to the directory the cookbook was downloaded to

Raises:



78
79
80
81
82
83
84
# File 'lib/ridley/resources/cookbook_resource.rb', line 78

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

Parameters:

  • object (String, #chef_id)
  • version (String)

Returns:



90
91
92
93
94
95
96
# File 'lib/ridley/resources/cookbook_resource.rb', line 90

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

Parameters:

  • name (String)

Returns:

  • (String, nil)

Raises:



105
106
107
108
109
110
111
# File 'lib/ridley/resources/cookbook_resource.rb', line 105

def latest_version(name)
  ver = versions(name).collect do |version|
    Solve::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

Parameters:

  • name (String)

    name of the cookbook

  • constraint (String, Solve::Constraint)

    constraint to solve for

Returns:

  • (CookbookResource, nil)

    returns the cookbook resource for the best solution or nil if no solution exists

Raises:



124
125
126
127
128
129
# File 'lib/ridley/resources/cookbook_resource.rb', line 124

def satisfy(name, constraint)
  version = Solve::Solver.satisfy_best(constraint, versions(name)).to_s
  find(name, version)
rescue Solve::Errors::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.

Parameters:

  • cookbook (Ridley::Chef::Cookbook)

    the cookbook to save

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :force (Boolean)

    Upload the Cookbook even if the version already exists and is frozen on the target Chef Server

  • :freeze (Boolean)

    Freeze the uploaded Cookbook on the Chef Server so that it cannot be overwritten

Returns:

  • (Hash)

Raises:

  • (Ridley::Errors::FrozenCookbook)

    if a cookbook of the same name and version already exists on the remote Chef server and is frozen. If the :force option is provided the given cookbook will be saved regardless.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ridley/resources/cookbook_resource.rb', line 150

def update(cookbook, options = {})
  options.reverse_merge(force: false, freeze: false)

  cookbook.frozen = options[:freeze]

  url = "cookbooks/#{cookbook.cookbook_name}/#{cookbook.version}"
  url << "?force=true" if options[: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

Parameters:

  • path (String)

    path to a cookbook on local disk

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :name (String)

    automatically populated by the metadata of the cookbook at the given path, but in the event that the metadata does not contain a name it can be specified with this option

  • :force (Boolean) — default: false

    Upload the Cookbook even if the version already exists and is frozen on the target Chef Server

  • :freeze (Boolean) — default: false

    Freeze the uploaded Cookbook on the Chef Server so that it cannot be overwritten

  • :validate (Boolean) — default: true

    Validate the contents of the cookbook before uploading

Returns:

  • (Hash)


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/ridley/resources/cookbook_resource.rb', line 186

def upload(path, options = {})
  options   = options.reverse_merge(validate: true, force: false, freeze: false)
  cookbook  = Ridley::Chef::Cookbook.from_path(path, options.slice(:name))

  unless (existing = find(cookbook.cookbook_name, cookbook.version)).nil?
    if existing.frozen? && options[: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 options[:validate]
    cookbook.validate
  end

  checksums = cookbook.checksums.dup
  sandbox   = sandbox_resource.create(checksums.keys)

  sandbox.upload(checksums)
  sandbox.commit
  update(cookbook, options.slice(:force, :freeze))
end

#versions(name) ⇒ Array<String>

Return a list of versions for the given cookbook present on the remote Chef server

Examples:

versions("nginx") => [ "1.0.0", "1.2.0" ]

Parameters:

  • name (String)

Returns:

  • (Array<String>)

Raises:



220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ridley/resources/cookbook_resource.rb', line 220

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