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
}.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_count!, ova?, ova_structure?

Class Method Details

.check_file!(file) ⇒ Object

Raises:



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

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

.download_image(url) ⇒ Object



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

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), true
rescue Cloudkeeper::Errors::InvalidURLError, Cloudkeeper::Errors::Image::Format::RecognitionError,
       Cloudkeeper::Errors::ArgumentError, Cloudkeeper::Errors::NetworkConnectionError, ::IOError => ex
  raise Cloudkeeper::Errors::Image::DownloadError, ex
end

.file_description(file) ⇒ Object



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

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

.format(file) ⇒ Object



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

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



76
77
78
# File 'lib/cloudkeeper/managers/image_manager.rb', line 76

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

.recognize_format(file) ⇒ Object

Raises:



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

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



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cloudkeeper/managers/image_manager.rb', line 62

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

    http.request(request) do |response|
      response.value
      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 => ex
  raise Cloudkeeper::Errors::NetworkConnectionError, ex
end