Class: Itchy::FormatConverter

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

Overview

Converting different image formats

Instance Method Summary collapse

Constructor Details

#initialize(unpacking_dir, metadata, vmcatcher_configuration) ⇒ FormatConverter

Creates and converter instance for converting image to requried format

Parameters:

  • unpacking_dir (String)

    path to directory where image is stored

  • metadata (VmcatcherEvent)

    metadata of event corresponding to image

  • vmcatcher_configuration (VmcatcherConfiguration)

    vmcatcher configuration



9
10
11
12
13
14
15
16
17
18
# File 'lib/itchy/format_converter.rb', line 9

def initialize(unpacking_dir, , vmcatcher_configuration)
  unless vmcatcher_configuration.is_a?(Itchy::VmcatcherConfiguration)
    fail ArgumentError, '\'vmcatcher_configuration\' must be an instance of ' \
                        'Itchy::VmcatcherConfiguration!'
  end

  @unpacking_dir = unpacking_dir
   = 
  @vmcatcher_configuration = vmcatcher_configuration
end

Instance Method Details

#convert!(file_format, required_format, output_dir) ⇒ Object

Converts image to required format. It uses Mixlib::ShelOut.

Parameters:

  • file_format (String)

    actual format of the image

  • required_format (String)

    required format

  • output_dir (String)

    path to a directory where converted image should be stored



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/itchy/format_converter.rb', line 25

def convert!(file_format, required_format, output_dir)
  Itchy::Log.info "[#{self.class.name}] Converting image " \
                         "#{@metadata.dc_identifier.inspect} from " \
                         "original format: #{file_format} to " \
                         "required format: #{required_format}."

  new_file_name = "#{::Time.now.to_i}_#{@metadata.dc_identifier}"
  convert_cmd = Mixlib::ShellOut.new("qemu-img convert -f #{file_format} -O #{required_format} #{@unpacking_dir}/#{@metadata.dc_identifier} #{output_dir}/#{new_file_name}")
  convert_cmd.run_command
  begin
    convert_cmd.error!
  rescue Mixlib::ShellOut::ShellCommandFailed, Mixlib::ShellOut::CommandTimeout,
         Mixlib::Shellout::InvalidCommandOption => ex
    Itchy::Log.fatal "[#{self.class.name}] Converting of image failed with " \
      "error messages #{convert_cmd.stderr}."
    fail Itchy::Errors::FormatConversionError, ex
  end
  new_file_name
end