Class OpenNebula::ImageRepository

  1. OpenNebula/ImageRepository.rb
Parent: Object
Pool VirtualNetworkPool ClusterPool ImagePool VirtualMachinePool HostPool UserPool XMLElement PoolElement XMLPool User VirtualNetwork Cluster Image VirtualMachine Host Enumerable Client ImageRepository Error OpenNebula dot/f_0.png

The ImageRepository class represents and abstraction of the Image Repository, and it provides basic operations to manage and mantain it. This class is used by the OpenNebula daemon (through the image hook) to save and update images, and by the OpenNebula CLI to create and delete them

Methods

public instance

  1. create
  2. delete
  3. update_source

Constants

FS_UTILS = { :dd => "env dd", :mkfs => "env mkfs" }

Public instance methods

create (image, template)
[show source]
     # File OpenNebula/ImageRepository.rb, line 34
 34:         def create(image, template)
 35:             if image.nil?
 36:                 error_msg = "Image could not be found, aborting."
 37:                 return OpenNebula::Error.new(error_msg)
 38:             end
 39: 
 40:             # ------ Allocate the Image ------
 41:             result = image.allocate(template)
 42: 
 43:             if OpenNebula.is_error?(result)
 44:                 return result
 45:             end
 46: 
 47: 
 48:             # ------ Copy the Image file ------
 49:             image.info
 50: 
 51:             if image['SOURCE'] and File.exists?(image['SOURCE'])
 52:                 error_msg =
 53:                         "Destination file for image already exists, aborting."
 54:                 result = OpenNebula::Error.new(error_msg)
 55: 
 56:             elsif image['TEMPLATE/PATH'] and image['TEMPLATE/SOURCE'].nil?
 57:                 # --- CDROM, DATABLOCK or OS based on a PATH ---
 58:                 file_path = image['TEMPLATE/PATH']
 59: 
 60:                 if !File.exists?(file_path)
 61:                     error_msg = "Image file could not be found, aborting."
 62:                     result = OpenNebula::Error.new(error_msg)
 63:                 end
 64: 
 65:                 if !OpenNebula.is_error?(result)
 66:                     result = copy(file_path, image['SOURCE'])
 67:                 end
 68: 
 69:                 # If the copy failed, the file should be removed
 70:                 if OpenNebula.is_error?(result)
 71:                     remove(image['SOURCE'])
 72:                 end
 73: 
 74:             elsif image['TEMPLATE/SIZE'] and image['TEMPLATE/FSTYPE'] and  \
 75:                             image['TEMPLATE/TYPE'] == 'DATABLOCK'
 76:                 # --- Empty DATABLOCK ---
 77:                 result = dd(image['TEMPLATE/SIZE'], image['SOURCE'])
 78: 
 79:                 if !OpenNebula.is_error?(result)
 80:                     result = mkfs(image['TEMPLATE/FSTYPE'], image['SOURCE'])
 81:                 end
 82: 
 83:                 # If the dd or mkfs failed, the file should be removed
 84:                 if OpenNebula.is_error?(result)
 85:                     remove(image['SOURCE'])
 86:                 end
 87: 
 88:             elsif image['TEMPLATE/PATH'].nil? and image['TEMPLATE/SOURCE'].nil?
 89:                 error_msg = "Image path not present, aborting."
 90:                 result = OpenNebula::Error.new(error_msg)
 91: 
 92:             elsif image['TEMPLATE/PATH'] and image['TEMPLATE/SOURCE']
 93:                 error_msg = "Template malformed, PATH and SOURCE are" <<
 94:                             " mutuallly exclusive"
 95:                 result = OpenNebula::Error.new(error_msg)
 96: 
 97:             end
 98: 
 99:             # ------ Enable the Image ------
100:             if !OpenNebula.is_error?(result)
101:                 image.enable
102:             else
103:                 image.delete
104:             end
105: 
106:             return result
107:         end
delete (image)
[show source]
     # File OpenNebula/ImageRepository.rb, line 112
112:         def delete(image)
113:             if image.nil?
114:                 error_msg = "Image could not be found, aborting."
115:                 return OpenNebula::Error.new(error_msg)
116:             end
117: 
118:             result = image.info
119: 
120:             if !OpenNebula.is_error?(result)
121:                 file_path = image['SOURCE']
122: 
123:                 result = image.delete
124: 
125:                 if !OpenNebula.is_error?(result)
126:                     result = remove(file_path)
127:                 end
128:             end
129: 
130:             return result
131:         end
update_source (image, source)
[show source]
     # File OpenNebula/ImageRepository.rb, line 136
136:         def update_source(image, source)
137:             if image.nil?
138:                 error_msg = "Image could not be found, aborting."
139:                 return OpenNebula::Error.new(error_msg)
140:             end
141: 
142:             result = image.info
143: 
144:             if !OpenNebula.is_error?(result)
145:                 result = move(source, image['SOURCE'])
146: 
147:                 image.enable
148:             end
149: 
150:             return result
151:         end