Class: Itchy::ImageTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/itchy/image_transformer.rb

Overview

Wraps image format conversion methods and helpers.

Constant Summary collapse

KNOWN_IMAGE_ARCHIVES =

Registered image formats and archives

%w(ova tar).freeze
KNOWN_IMAGE_FORMATS =
%w(cow dmg parallels qcow qcow2 raw vdi vmdk vhd).freeze
ARCHIVE_STRING =

Archive format string msg

'POSIX tar archive'
REAL_RAW_STRING =

raw boot sector message

'boot sector'
FORMAT_PATTERN =

REGEX pattern for getting image format

/format:\s(.*?)$/

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ImageTransformer

Creates a class instance.

Parameters:

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

    configuration options



20
21
22
23
24
25
26
27
28
# File 'lib/itchy/image_transformer.rb', line 20

def initialize(options = {})
  @options = options
  @inputs = ([] << KNOWN_IMAGE_FORMATS << KNOWN_IMAGE_ARCHIVES).flatten

  #fail ArgumentError, 'Unsupported input image format enabled in configuration! ' \
  #     "#{@inputs.inspect}" unless (@options.input_image_formats - @inputs).empty?
  # fail "Unsupported output image format enabled in configuration! " \
  #     "#{KNOWN_IMAGE_FORMATS.inspect}" unless (@options.required_format - KNOWN_IMAGE_FORMATS).empty?
end

Instance Method Details

#transform!(metadata, vmcatcher_configuration) ⇒ String

Transforms image(s) associated with the given event to formats preferred by the underlying image datastore. This process includes unpacking of archive & conversion of image files.

Parameters:

Returns:

  • (String)

    directory with converted images for further processing



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/itchy/image_transformer.rb', line 37

def transform!(, vmcatcher_configuration)
  Itchy::Log.info "[#{self.class.name}] Transforming image format " \
                         "for #{.dc_identifier.inspect}"

  image_file = orig_image_file(, vmcatcher_configuration)

  unless File.file?(image_file)
    Itchy::Log.error "[#{self.class.name}] Event image file - #{image_file}] - does not exist!"
    fail Itchy::Errors::ImageTransformationError
  end

  begin
    if archived?(image_file)
      unpacking_dir = process_archive(, vmcatcher_configuration)
      file_format = format("#{unpacking_dir}/#{.dc_identifier}")
    else
      file_format = format(image_file)
      unpacking_dir = copy_unpacked!(, vmcatcher_configuration)
    end
    if file_format == @options.required_format
      new_file_name = copy_same_format(unpacking_dir, )
    else
      converter = Itchy::FormatConverter.new(unpacking_dir, , vmcatcher_configuration)
      new_file_name = converter.convert!(file_format, @options.required_format, @options.output_dir, @options.qemu_img_binary)
    end
    remove_dir(unpacking_dir)
  rescue Itchy::Errors::FileInspectError, Itchy::Errors::FormatConversionError,
         Itchy::Errors::PrepareEnvError => ex
    fail Itchy::Errors::ImageTransformationError, ex
  end
  new_file_name
end