Class: Cloudkeeper::One::Opennebula::ImageHandler

Inherits:
ApplianceHandler show all
Defined in:
lib/cloudkeeper/one/opennebula/image_handler.rb

Constant Summary collapse

IMAGE_STATES =
{
  ready: 'READY',
  used: 'USED',
  disabled: 'DISABLED',
  error: 'ERROR'
}.freeze
EXPIRED_PERMISSIONS =
'600'.freeze

Constants inherited from ApplianceHandler

ApplianceHandler::LEAVE_ID_AS_IS, ApplianceHandler::ONEADMIN_ID

Constants inherited from Handler

Handler::API_POLLING_WAIT

Constants included from Helper

Helper::ERRORS

Instance Attribute Summary

Attributes inherited from ApplianceHandler

#identifier

Attributes inherited from Handler

#client, #pool

Instance Method Summary collapse

Methods inherited from ApplianceHandler

#chmod, #find_by_appliance_id, #find_by_image_list_id, #update

Methods inherited from Handler

#exist?, #find_all, #find_by_id, #find_by_name, #find_one

Methods included from Helper

#decode_error, #handle_opennebula_error

Constructor Details

#initializeImageHandler

Returns a new instance of ImageHandler.



14
15
16
17
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 14

def initialize
  super
  @pool = OpenNebula::ImagePool.new client
end

Instance Method Details

#delete(image) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 25

def delete(image)
  raise Cloudkeeper::One::Errors::ArgumentError, 'image cannot be nil' unless image

  id = image.id

  if used? image
    logger.warn "Image with id #{id.inspect} cannot be removed, still in use"
    return
  end

  super image
end

#disable(image) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 38

def disable(image)
  raise Cloudkeeper::One::Errors::ArgumentError, 'image cannot be nil' unless image

  id = image.id

  if disabled? image
    logger.info "Image with id #{id.inspect} is already disabled, skipping"
    return
  end

  unless free? image
    logger.warn "Image with id #{id.inspect} cannot be disabled"
    return
  end

  handle_opennebula_error { image.disable }

  timeout { sleep(Cloudkeeper::One::Opennebula::Handler::API_POLLING_WAIT) until disabled? image }
end

#disabled?(image) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 102

def disabled?(image)
  is?(image) { image.state_str == IMAGE_STATES[:disabled] }
end

#error?(image) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 118

def error?(image)
  is?(image) { image.state_str == IMAGE_STATES[:error] }
end

#expire(image) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 58

def expire(image)
  raise Cloudkeeper::One::Errors::ArgumentError, 'image cannot be nil' unless image

  id = image.id

  if expired? image
    logger.debug("Image with id #{id.inspect} is already expired, skipping")
    return
  end

  chmod image, EXPIRED_PERMISSIONS
  disable image

  expiration_attribute = "#{Tags::EXPIRED} = \"yes\""

  handle_opennebula_error { image.rename("EXPIRED_#{Time.now.to_i}_#{image.name}") }
  handle_opennebula_error { image.update(expiration_attribute, true) }
end

#expiredObject



19
20
21
22
23
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 19

def expired
  xpaths = { "TEMPLATE/#{Tags::EXPIRED}" => 'yes' }

  find_all xpaths
end

#expired?(image) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 98

def expired?(image)
  is?(image) { image["TEMPLATE/#{Tags::EXPIRED}"] == 'yes' }
end

#free?(image) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 114

def free?(image)
  is?(image) { image.state_str == IMAGE_STATES[:ready] || image.state_str == IMAGE_STATES[:error] }
end

#ready?(image) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 106

def ready?(image)
  is?(image) { image.state_str == IMAGE_STATES[:ready] }
end

#register(image_template, datastore) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 77

def register(image_template, datastore)
  image_alloc = OpenNebula::Image.build_xml
  image = OpenNebula::Image.new(image_alloc, client)

  handle_opennebula_error { image.allocate(image_template, datastore.id) }

  timeout do
    until ready? image
      if error? image
        delete image
        raise Cloudkeeper::One::Errors::Opennebula::ResourceStateError, image['TEMPLATE/ERROR']
      end
      sleep(Cloudkeeper::One::Opennebula::Handler::API_POLLING_WAIT)
    end
  end

  chmod image, Cloudkeeper::One::Settings[:'appliances-permissions']

  image
end

#used?(image) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 110

def used?(image)
  is?(image) { image.state_str == IMAGE_STATES[:used] }
end