Class: Cloudkeeper::Managers::ImageManager

Inherits:
Object
  • Object
show all
Extended by:
Entities::ImageFormats::Ova
Defined in:
lib/cloudkeeper/managers/image_manager.rb

Constant Summary collapse

FORMATS =
{
  qcow2: /qemu qcow image/i,
  ova: /posix tar archive/i,
  vmdk: /vmware4 disk image/i,
  raw: /boot sector/i,
  vdi: /virtual(box)? disk image/i
}.freeze

Constants included from Entities::ImageFormats::Ova

Entities::ImageFormats::Ova::ARCHIVE_MAX_FILES, Entities::ImageFormats::Ova::OVF_REGEX, Entities::ImageFormats::Ova::VMDK_REGEX

Class Method Summary collapse

Methods included from Entities::ImageFormats::Ova

archive_files, check_file_count!, ova?, ova_structure?

Class Method Details

.check_file!(file) ⇒ Object



30
31
32
33
# File 'lib/cloudkeeper/managers/image_manager.rb', line 30

def check_file!(file)
  raise Cloudkeeper::Errors::NoSuchFileError, "No such file #{file.inspect}" unless File.exist?(file)
  raise Cloudkeeper::Errors::PermissionDeniedError, "Cannot read file #{file.inspect}" unless File.readable?(file)
end

.connection_options(uri) ⇒ Object



84
85
86
87
88
# File 'lib/cloudkeeper/managers/image_manager.rb', line 84

def connection_options(uri)
  use_ssl = uri.scheme == 'https'
  ca_path = Cloudkeeper::Settings[:'ca-dir'] if Cloudkeeper::Settings[:'ca-dir']
  { use_ssl: use_ssl, ca_path: ca_path }
end

.download_image(url) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cloudkeeper/managers/image_manager.rb', line 49

def download_image(url)
  logger.debug "Downloading image from #{url.inspect}"
  Cloudkeeper::Utils::URL.check!(url)

  uri = URI.parse url
  filename = generate_filename(uri)
  retrieve_image(uri, filename)

  Cloudkeeper::Entities::ImageFile.new filename, format(filename), Cloudkeeper::Utils::Checksum.compute(filename),
                                       File.size(filename), true
rescue Cloudkeeper::Errors::InvalidURLError, Cloudkeeper::Errors::Image::Format::RecognitionError,
       Cloudkeeper::Errors::ArgumentError, Cloudkeeper::Errors::NetworkConnectionError, ::IOError => ex
  raise Cloudkeeper::Errors::Image::DownloadError, "Image #{url.inspect} download error: #{ex.message}"
end

.file_description(file) ⇒ Object



26
27
28
# File 'lib/cloudkeeper/managers/image_manager.rb', line 26

def file_description(file)
  Cloudkeeper::CommandExecutioner.execute('file', '-b', file)
end

.format(file) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/cloudkeeper/managers/image_manager.rb', line 17

def format(file)
  check_file!(file)
  recognize_format(file)
rescue Cloudkeeper::Errors::CommandExecutionError, Cloudkeeper::Errors::NoSuchFileError,
       Cloudkeeper::Errors::PermissionDeniedError, Cloudkeeper::Errors::Image::Format::NoFormatRecognizedError,
       Cloudkeeper::Errors::Image::Format::Ova::OvaFormatError => ex
  raise Cloudkeeper::Errors::Image::Format::RecognitionError, ex, "Cannot recognize image format for file #{file.inspect}"
end

.generate_filename(uri) ⇒ Object



90
91
92
# File 'lib/cloudkeeper/managers/image_manager.rb', line 90

def generate_filename(uri)
  File.join(Cloudkeeper::Settings[:'image-dir'], Cloudkeeper::Utils::Filename.sanitize(File.basename(uri.path)))
end

.recognize_format(file) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cloudkeeper/managers/image_manager.rb', line 35

def recognize_format(file)
  file_format_string = file_description(file)
  FORMATS.each do |format, regex|
    next unless regex =~ file_format_string

    format_test_method = "#{format}?".to_sym
    additional_test_result = respond_to?(format_test_method) ? send(format_test_method, file) : true

    return format if additional_test_result
  end

  raise Cloudkeeper::Errors::Image::Format::NoFormatRecognizedError, "No image format recognized for file #{file.inspect}"
end

.retrieve_image(uri, filename) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cloudkeeper/managers/image_manager.rb', line 64

def retrieve_image(uri, filename)
  Net::HTTP.start(uri.host, uri.port, connection_options(uri)) do |http|
    request = Net::HTTP::Get.new(uri)

    http.request(request) do |response|
      if response.is_a? Net::HTTPRedirection
        retrieve_image URI.join(uri, response.header['location']), filename
        break
      end

      response.value
      File.open(filename, 'w') { |file| response.read_body { |chunk| file.write(chunk) } }
    end
  end
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, Net::HTTPBadResponse,
       Net::HTTPHeaderSyntaxError, EOFError, Net::HTTPServerException, Net::HTTPRetriableError,
       OpenSSL::SSL::SSLError => ex
  raise Cloudkeeper::Errors::NetworkConnectionError, ex
end