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

#chgrp, #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



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

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



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

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)


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

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

#error?(image) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 121

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

#expire(image) ⇒ Object



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

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
24
25
# File 'lib/cloudkeeper/one/opennebula/image_handler.rb', line 19

def expired
  xpaths = { "TEMPLATE/#{Tags::EXPIRED}" => 'yes' }
  xpaths['UNAME'] = Cloudkeeper::One::Settings[:'opennebula-users'] \
    if Cloudkeeper::One::Settings[:'opennebula-users'] && !Cloudkeeper::One::Settings[:'opennebula-users'].empty?

  find_all xpaths
end

#expired?(image) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#free?(image) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#ready?(image) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#register(image_template, datastore, group) ⇒ Object



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

def register(image_template, datastore, group)
  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']
  chgrp image, group

  image
end

#used?(image) ⇒ Boolean

Returns:

  • (Boolean)


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

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