Module: Cloudkeeper::Utils::Appliance

Included in:
Managers::ApplianceManager
Defined in:
lib/cloudkeeper/utils/appliance.rb

Constant Summary collapse

IMAGE_UPDATE_ATTRIBUTES =
['hv:version', 'sl:checksum:sha512', 'hv:size'].freeze

Instance Method Summary collapse

Instance Method Details

#clean_image_file(filename) ⇒ Object



19
20
21
# File 'lib/cloudkeeper/utils/appliance.rb', line 19

def clean_image_file(filename)
  File.delete(filename) if File.exist?(filename)
end

#clean_image_files(appliance) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/cloudkeeper/utils/appliance.rb', line 10

def clean_image_files(appliance)
  return unless appliance && appliance.image

  logger.debug "Cleaning downloaded image files for appliance #{appliance.identifier.inspect}"
  appliance.image.image_files.each { |image_file| clean_image_file image_file.file }
rescue ::IOError => ex
  logger.warn "Appliance cleanup error: #{ex.message}"
end

#convert_image!(appliance, image_file) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cloudkeeper/utils/appliance.rb', line 46

def convert_image!(appliance, image_file)
  format = acceptable_formats.find { |acceptable_format| image_file.respond_to? "to_#{acceptable_format}".to_sym }
  unless format
    raise Cloudkeeper::Errors::Image::Format::NoRequiredFormatAvailableError,
          "image #{image.inspect} cannot be converted to any acceptable format"
  end

  appliance.image.add_image_file image_file.send("to_#{format}".to_sym)
rescue Cloudkeeper::Errors::Image::Format::NoRequiredFormatAvailableError, Cloudkeeper::Errors::CommandExecutionError,
       Cloudkeeper::Errors::ArgumentError, ::IOError, ::SystemCallError => ex
  raise Cloudkeeper::Errors::Image::ConversionError, "Image #{appliance.image.uri.inspect} conversion error: #{ex.message}"
end

#log_expired(appliance, message) ⇒ Object



6
7
8
# File 'lib/cloudkeeper/utils/appliance.rb', line 6

def log_expired(appliance, message)
  logger.info "#{message} #{appliance.identifier.inspect}"
end

#prepare_image!(appliance) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/cloudkeeper/utils/appliance.rb', line 38

def prepare_image!(appliance)
  image_file = Cloudkeeper::Managers::ImageManager.download_image(appliance.image.uri)
  appliance.image.add_image_file image_file
  return if acceptable_formats.include? image_file.format

  convert_image! appliance, image_file
end

#update_appliance(appliance) ⇒ Object



34
35
36
# File 'lib/cloudkeeper/utils/appliance.rb', line 34

def update_appliance(appliance)
  modify_appliance :update_appliance, appliance
end

#update_image?(image_list_appliance, backend_appliance) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/cloudkeeper/utils/appliance.rb', line 23

def update_image?(image_list_appliance, backend_appliance)
  image_list_attributes = image_list_appliance.attributes
  backend_attributes = backend_appliance.attributes

  IMAGE_UPDATE_ATTRIBUTES.reduce(false) { |red, elem| red || (image_list_attributes[elem] != backend_attributes[elem]) }
end

#update_metadata?(image_list_appliance, backend_appliance) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/cloudkeeper/utils/appliance.rb', line 30

def update_metadata?(image_list_appliance, backend_appliance)
  image_list_appliance.attributes != backend_appliance.attributes
end