class Minimart::Web::UniverseGenerator

This class is responsible for generating the universe.json file, and the related directory structure of gzipped cookbooks.

Attributes

cookbooks[R]

@return [Minimart::Web::Cookbooks] The cookbooks to build a universe for

endpoint[R]

@return [String] the base URL to use to build paths for cookbook files.

universe[R]
web_directory[R]

@return [String] the directory to put the universe.json file in

Public Class Methods

new(opts = {}) click to toggle source

@param [Hash] opts @option opts [String] web_directory The directory to put the universe.json file in @option opts [String] endpoint The base URL to use to build paths for cookbook files. @option opts [Minimart::Web::Cookbooks] cookbooks The cookbooks to build a universe for @option opts [Boolean] clean_cookbooks Determines if we should clean the cookbook_files folder and recreate cookbook packages

# File lib/minimart/web/universe_generator.rb, line 27
def initialize(opts = {})
  @web_directory = opts[:web_directory]
  @endpoint      = opts[:endpoint]
  @cookbooks     = opts[:cookbooks]
  @clean_cookbooks = opts.fetch(:clean_cookbooks, true)
  @universe      = {}
end

Public Instance Methods

generate() click to toggle source

Generate the universe file!

# File lib/minimart/web/universe_generator.rb, line 36
def generate
  clean_existing_cookbook_files
  make_cookbook_files_directory
  create_universe
  write_universe_file
end

Private Instance Methods

add_cookbook_to_universe(cookbook) click to toggle source
# File lib/minimart/web/universe_generator.rb, line 89
def add_cookbook_to_universe(cookbook)
  universe[cookbook.name] ||= {}
  universe[cookbook.name][cookbook.version.to_s] = {
    location_type:  :uri,
    location_path:  archive_url(cookbook),
    download_url:   archive_url(cookbook),
    dependencies:   cookbook.dependencies
  }
end
archive_directory(cookbook) click to toggle source

/web/cookbook_files/cookbook-name/cookbook-version

# File lib/minimart/web/universe_generator.rb, line 85
def archive_directory(cookbook)
  File.join(cookbook_directory(cookbook.name), cookbook.web_friendly_version)
end
archive_name(cookbook) click to toggle source
# File lib/minimart/web/universe_generator.rb, line 80
def archive_name(cookbook)
  File.join(archive_directory(cookbook), "#{cookbook}.tar.gz")
end
archive_url(cookbook) click to toggle source
# File lib/minimart/web/universe_generator.rb, line 99
def archive_url(cookbook)
  Utils::Http.build_url(endpoint, cookbook_download_path(cookbook))
end
clean_existing_cookbook_files() click to toggle source
# File lib/minimart/web/universe_generator.rb, line 45
def clean_existing_cookbook_files
  if Dir.exists?(cookbook_files_directory) && @clean_cookbooks
    FileUtils.remove_entry(cookbook_files_directory)
  end
end
cookbook_directory(cookbook_name) click to toggle source

/web/cookbook_files/cookbook-name

# File lib/minimart/web/universe_generator.rb, line 70
def cookbook_directory(cookbook_name)
  File.join(cookbook_files_directory, cookbook_name)
end
cookbook_files_directory() click to toggle source
# File lib/minimart/web/universe_generator.rb, line 109
def cookbook_files_directory
  @cookbook_files_directory ||= File.join(web_directory, '/cookbook_files')
end
create_universe() click to toggle source
# File lib/minimart/web/universe_generator.rb, line 55
def create_universe
  cookbooks.individual_cookbooks.each do |cookbook|
    unless Dir.exists?(archive_directory(cookbook))
      make_cookbook_directory(cookbook)
      generate_archive_file(cookbook)
    end
    add_cookbook_to_universe(cookbook)
  end
end
generate_archive_file(cookbook) click to toggle source
# File lib/minimart/web/universe_generator.rb, line 74
def generate_archive_file(cookbook)
  FileUtils.mkdir_p(archive_directory(cookbook))

  Utils::Archive.pack_archive(cookbook, archive_name(cookbook))
end
make_cookbook_directory(cookbook) click to toggle source
# File lib/minimart/web/universe_generator.rb, line 65
def make_cookbook_directory(cookbook)
  FileUtils.mkdir_p(cookbook_directory(cookbook.name))
end
make_cookbook_files_directory() click to toggle source
# File lib/minimart/web/universe_generator.rb, line 51
def make_cookbook_files_directory
  FileUtils.mkdir_p(cookbook_files_directory)
end
write_universe_file() click to toggle source
# File lib/minimart/web/universe_generator.rb, line 103
def write_universe_file
  File.open(File.join(web_directory, 'universe'), 'w+') do |f|
    f.write(universe.to_json)
  end
end