Sha256: 7abfcb62e0e31cdfaac8b06cfff439c086dddff51e01aaebcdbb8443d77535d8

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

module Uricp::Strategy
  class LocalConvert
    include Uricp::Strategy::Common

    def appropriate?
      if conversion_required? && file_source? && supported_source?
        return proposal if new_qemu? || supported_conversion?

        raise Uricp::UnsupportedConversion,
              'qemu-img does not support required conversion'
      end
      debug "#{self.class.name}: not appropriate"
      false
    end

    def command
      qemu_img_command(proposed_path)
    end

    def proposal
      @proposed_options = options.dup
      if to.scheme == 'file'
        @proposed_options['from_uri'] = @proposed_options['to_uri']
      else
        @proposed_options['from_uri'] = temp_uri
        @proposed_options['clean'] ||= []
        @proposed_options['clean'] << proposed_path
      end
      @proposed_options.delete('source-format')
      @proposed_options.delete('target-format')
      self
    end

    def new_qemu?
      @new_qemu ||= !!(`qemu-img convert -O qcow2 -o ? a b` =~ /\bcompat\b/m)
    end

    def supported_conversion?
      ([:qcow3, :qcow2v3] & [options['source-format'], options['target-format']]).empty? # rubocop:disable Style/SymbolArray
    end

    def qemu_img_command(target)
      "qemu-img convert #{conversion_options} #{from.path} #{target};"
    end

    def conversion_options
      case options['target-format']
      when :raw
        '-O raw'
      when :qcow2
        "#{compress_option} -O qcow2 #{compat_option}"
      when :qcow3, :qcow2v3
        "#{compress_option} -O qcow2 -o compat=1.1"
      end
    end

    def compat_option
      '-o compat=0.10' if new_qemu?
    end

    def compress_option
      options['compress'] && '-c'
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
uricp-0.0.24 lib/uricp/strategy/local_convert.rb
uricp-0.0.23 lib/uricp/strategy/local_convert.rb
uricp-0.0.22 lib/uricp/strategy/local_convert.rb
uricp-0.0.21 lib/uricp/strategy/local_convert.rb
uricp-0.0.20 lib/uricp/strategy/local_convert.rb
uricp-0.0.18 lib/uricp/strategy/local_convert.rb