Class: Comfy::Creator

Inherits:
Object
  • Object
show all
Defined in:
lib/comfy/creator.rb

Overview

Creates given virtual machine images.

Constant Summary collapse

TIMESTAMP_FORMAT =
'%Y%m%d%H%M'
NEEDLE_REPLACEMENTS =
{
  :$v => lambda { |instance| instance.send(:version_string) },
  :$t => lambda { |instance| Time.new.strftime(TIMESTAMP_FORMAT) },
  :$n => lambda { |instance| instance.data[:distro]['name'] },
  :$g => lambda { |instance| instance.data[:groups].join(',') }
}
REPLACEMENT_REGEX =
/\$[a-zA-Z]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Creator

Creates a creator instance.

Parameters:

  • options (Hashie::Mash)

    hash-like structure with options



25
26
27
# File 'lib/comfy/creator.rb', line 25

def initialize(options)
  @data = options.clone
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/comfy/creator.rb', line 10

def data
  @data
end

Instance Method Details

#cleanObject

Cleans everything from temporary directory



55
56
57
58
59
60
# File 'lib/comfy/creator.rb', line 55

def clean
  if data[:server_dir]
    logger.debug("Cleaning temporary directory #{data[:server_dir]}.")
    FileUtils.remove_dir(data[:server_dir])
  end
end

#createObject

Method representing the whole creation process. Prepares enviroment, prepare files and starts packer job.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/comfy/creator.rb', line 31

def create
  logger.info('Preparing for image creation...')

  data[:server_dir] = Dir.mktmpdir('comfy')
  logger.debug("Server root directory: #{data[:server_dir]}")

  prepare_data
  logger.debug("Prepared data: #{data}")

  templater = Comfy::Templater.new data
  templater.prepare_files

  packer_file = "#{data[:server_dir]}/#{data[:distribution]}.packer"
  run_packer(packer_file)

  # let's create cloud appliance descriptor files
  if data[:description]
    data[:formats].each do |format|
      File.write(File.join(data[:'output-dir'], "#{data[:identifier]}.json"), description(format))
    end
  end
end