Class: Ridley::CookbookObject

Inherits:
ChefObject show all
Includes:
Logging
Defined in:
lib/ridley/chef_objects/cookbook_object.rb

Constant Summary collapse

FILE_TYPES =
[
  :resources,
  :providers,
  :recipes,
  :definitions,
  :libraries,
  :attributes,
  :files,
  :templates,
  :root_files
].freeze

Instance Method Summary collapse

Methods included from Logging

logger, #logger, set_logger

Methods inherited from ChefObject

#<=>, #==, #chef_id, chef_id, chef_json_class, chef_type, #eql?, #hash, #initialize, #inspect, #save, set_chef_id, set_chef_json_class, set_chef_type, #update

Constructor Details

This class inherits a constructor from Ridley::ChefObject

Instance Method Details

#download(destination = Dir.mktmpdir) ⇒ String

Download the entire cookbook

Parameters:

  • 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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ridley/chef_objects/cookbook_object.rb', line 80

def download(destination = Dir.mktmpdir)
  destination = File.expand_path(destination)
  log.debug { "downloading cookbook: '#{name}'" }

  FILE_TYPES.each do |filetype|
    next unless manifest.has_key?(filetype)

    manifest[filetype].each do |file|
      file_destination = File.join(destination, file[:path].gsub('/', File::SEPARATOR))
      FileUtils.mkdir_p(File.dirname(file_destination))
      download_file(filetype, file[:path], file_destination)
    end
  end

  destination
end

#download_file(filetype, path, destination) ⇒ nil

Download a single file from a cookbook

Parameters:

  • filetype (#to_sym)

    the type of file to download. These are broken up into the following types in Chef:

    - attribute
    - definition
    - file
    - library
    - provider
    - recipe
    - resource
    - root_file
    - template
    

    these types are where the files are stored in your cookbook’s structure. For example, a recipe would be stored in the recipes directory while a root_file is stored at the root of your cookbook

  • path (String)

    path of the file to download

  • destination (String)

    where to download the file to

Returns:

  • (nil)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ridley/chef_objects/cookbook_object.rb', line 119

def download_file(filetype, path, destination)
  file_list = case filetype.to_sym
  when :attribute, :attributes; attributes
  when :definition, :definitions; definitions
  when :file, :files; files
  when :library, :libraries; libraries
  when :provider, :providers; providers
  when :recipe, :recipes; recipes
  when :resource, :resources; resources
  when :root_file, :root_files; root_files
  when :template, :templates; templates
  else
    raise Errors::UnknownCookbookFileType.new(filetype)
  end

  file  = file_list.find { |f| f[:path] == path }
  return nil if file.nil?

  destination = File.expand_path(destination)
  log.debug { "downloading '#{filetype}' file: #{file} to: '#{destination}'" }

  resource.connection.stream(file[:url], destination)
end

#manifestHash

A hash containing keys for all of the different cookbook filetypes with values representing each file of that type this cookbook contains

Examples:

{
  root_files: [
    {
      :name => "afile.rb",
      :path => "files/ubuntu-9.10/afile.rb",
      :checksum => "2222",
      :specificity => "ubuntu-9.10"
    },
  ],
  templates: [ manifest_record1, ... ],
  ...
}

Returns:

  • (Hash)


161
162
163
164
165
166
167
# File 'lib/ridley/chef_objects/cookbook_object.rb', line 161

def manifest
  {}.tap do |manifest|
    FILE_TYPES.each do |filetype|
      manifest[filetype] = get_attribute(filetype)
    end
  end
end

#reloadRidley::CookbookObject

Reload the attributes of the instantiated resource



172
173
174
175
# File 'lib/ridley/chef_objects/cookbook_object.rb', line 172

def reload
  mass_assign(resource.find(self, self.version)._attributes_)
  self
end

#to_sObject



177
178
179
# File 'lib/ridley/chef_objects/cookbook_object.rb', line 177

def to_s
  "#{name}: #{manifest}"
end