Class: OpenNebula::Image

Inherits:
PoolElement show all
Defined in:
lib/opennebula/image.rb

Constant Summary collapse

IMAGE_METHODS =
{
    :info        => "image.info",
    :allocate    => "image.allocate",
    :update      => "image.update",
    :enable      => "image.enable",
    :persistent  => "image.persistent",
    :delete      => "image.delete",
    :chown       => "image.chown",
    :chmod       => "image.chmod",
    :chtype      => "image.chtype",
    :clone       => "image.clone",
    :rename      => "image.rename"
}
IMAGE_STATES =
%w{INIT READY USED DISABLED LOCKED ERROR CLONE DELETE USED_PERS}
SHORT_IMAGE_STATES =
{
    "INIT"      => "init",
    "READY"     => "rdy",
    "USED"      => "used",
    "DISABLED"  => "disa",
    "LOCKED"    => "lock",
    "ERROR"     => "err",
    "CLONE"     => "clon",
    "DELETE"    => "dele",
    "USED_PERS" => "used"
}
IMAGE_TYPES =
%w{OS CDROM DATABLOCK KERNEL RAMDISK CONTEXT}
SHORT_IMAGE_TYPES =
{
    "OS"        => "OS",
    "CDROM"     => "CD",
    "DATABLOCK" => "DB",
    "KERNEL"    => "KL",
    "RAMDISK"   => "RD",
    "CONTEXT"   => "CX"
}
DISK_TYPES =
%w{FILE CD_ROM BLOCK RBD}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PoolElement

#id, #name, new_with_id, #to_str

Methods inherited from XMLElement

#[], #add_element, #attr, #delete_element, #each, #each_xpath, #element_xml, #has_elements?, #initialize_xml, #name, #retrieve_elements, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml

Constructor Details

#initialize(xml, client) ⇒ Image

Class constructor



87
88
89
90
91
# File 'lib/opennebula/image.rb', line 87

def initialize(xml, client)
    super(xml,client)

    @client = client
end

Class Method Details

.build_xml(pe_id = nil) ⇒ Object

Creates an Image description with just its identifier this method should be used to create plain Image objects. id the id of the image

Example:

image = Image.new(Image.build_xml(3),rpc_client)


76
77
78
79
80
81
82
83
84
# File 'lib/opennebula/image.rb', line 76

def Image.build_xml(pe_id=nil)
    if pe_id
        image_xml = "<IMAGE><ID>#{pe_id}</ID></IMAGE>"
    else
        image_xml = "<IMAGE></IMAGE>"
    end

    XMLElement.build_xml(image_xml,'IMAGE')
end

Instance Method Details

#allocate(description, ds_id) ⇒ nil, OpenNebula::Error

Allocates a new Image in OpenNebula

Parameters:

  • description (String)

    A string containing the template of the Image.

  • ds_id (Integer)

    the target datastore ID

Returns:



111
112
113
# File 'lib/opennebula/image.rb', line 111

def allocate(description, ds_id)
    super(IMAGE_METHODS[:allocate],description, ds_id)
end

#chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u, other_m, other_a) ⇒ nil, OpenNebula::Error

Changes the Image permissions. Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change

Returns:



184
185
186
187
188
# File 'lib/opennebula/image.rb', line 184

def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
        other_m, other_a)
    super(IMAGE_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
        group_m, group_a, other_u, other_m, other_a)
end

#chmod_octet(octet) ⇒ nil, OpenNebula::Error

Changes the Image permissions.

Parameters:

  • octet (String)

    Permissions octed , e.g. 640

Returns:



175
176
177
# File 'lib/opennebula/image.rb', line 175

def chmod_octet(octet)
    super(IMAGE_METHODS[:chmod], octet)
end

#chown(uid, gid) ⇒ Object

Changes the owner/group

uid

Integer the new owner id. Set to -1 to leave the current one

gid

Integer the new group id. Set to -1 to leave the current one

return

nil in case of success or an Error object



166
167
168
# File 'lib/opennebula/image.rb', line 166

def chown(uid, gid)
    super(IMAGE_METHODS[:chown], uid, gid)
end

#chtype(type) ⇒ nil, OpenNebula::Error

Changes the Image type

Parameters:

  • type (String)

    new Image type

Returns:



194
195
196
197
198
199
200
201
# File 'lib/opennebula/image.rb', line 194

def chtype(type)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(IMAGE_METHODS[:chtype], @pe_id, type)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

#clone(name) ⇒ Integer, OpenNebula::Error

Clones this Image into a new one

Parameters:

  • name (String)

    for the new Image.

Returns:

  • (Integer, OpenNebula::Error)

    The new Image ID in case of success, Error otherwise



209
210
211
212
213
214
215
# File 'lib/opennebula/image.rb', line 209

def clone(name)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(IMAGE_METHODS[:clone], @pe_id, name)

    return rc
end

#deleteObject

Deletes the Image



158
159
160
# File 'lib/opennebula/image.rb', line 158

def delete()
    super(IMAGE_METHODS[:delete])
end

#disableObject

Disables an Image



133
134
135
# File 'lib/opennebula/image.rb', line 133

def disable
    set_enabled(false)
end

#enableObject

Enables an Image



128
129
130
# File 'lib/opennebula/image.rb', line 128

def enable
    set_enabled(true)
end

#gidObject

Returns the group identifier

return

Integer the element’s group ID



263
264
265
# File 'lib/opennebula/image.rb', line 263

def gid
    self['GID'].to_i
end

#infoObject Also known as: info!

Retrieves the information of the given Image.



98
99
100
# File 'lib/opennebula/image.rb', line 98

def info()
    super(IMAGE_METHODS[:info], 'IMAGE')
end

#nonpersistentObject

Makes the Image non persistent



153
154
155
# File 'lib/opennebula/image.rb', line 153

def nonpersistent
    set_persistent(false)
end

#persistentObject

Makes the Image persistent



148
149
150
# File 'lib/opennebula/image.rb', line 148

def persistent
    set_persistent(true)
end

#public?Boolean

Returns:

  • (Boolean)


267
268
269
270
271
272
273
# File 'lib/opennebula/image.rb', line 267

def public?
    if self['PERMISSIONS/GROUP_U'] == "1" || self['PERMISSIONS/OTHER_U'] == "1"
        true
    else
        false
    end
end

#publishObject

Publishes the Image, to be used by other users



138
139
140
# File 'lib/opennebula/image.rb', line 138

def publish
    set_publish(true)
end

#rename(name) ⇒ nil, OpenNebula::Error

Renames this Image

Parameters:

  • name (String)

    New name for the Image.

Returns:



223
224
225
# File 'lib/opennebula/image.rb', line 223

def rename(name)
    return call(IMAGE_METHODS[:rename], @pe_id, name)
end

#short_state_strObject

Returns the state of the Image (string value)



242
243
244
# File 'lib/opennebula/image.rb', line 242

def short_state_str
    SHORT_IMAGE_STATES[state_str]
end

#short_type_strObject

Returns the state of the Image (string value)



257
258
259
# File 'lib/opennebula/image.rb', line 257

def short_type_str
    SHORT_IMAGE_TYPES[type_str]
end

#stateObject

Returns the state of the Image (numeric value)



232
233
234
# File 'lib/opennebula/image.rb', line 232

def state
    self['STATE'].to_i
end

#state_strObject

Returns the state of the Image (string value)



237
238
239
# File 'lib/opennebula/image.rb', line 237

def state_str
    IMAGE_STATES[state]
end

#typeObject

Returns the type of the Image (numeric value)



247
248
249
# File 'lib/opennebula/image.rb', line 247

def type
    self['TYPE'].to_i
end

#type_strObject

Returns the type of the Image (string value)



252
253
254
# File 'lib/opennebula/image.rb', line 252

def type_str
    IMAGE_TYPES[type]
end

#unpublishObject

Unplubishes the Image



143
144
145
# File 'lib/opennebula/image.rb', line 143

def unpublish
    set_publish(false)
end

#update(new_template = nil, append = false) ⇒ nil, OpenNebula::Error

Replaces the template contents

Parameters:

  • new_template (String) (defaults to: nil)

    New template contents

  • append (true, false) (defaults to: false)

    True to append new attributes instead of replace the whole template

Returns:



123
124
125
# File 'lib/opennebula/image.rb', line 123

def update(new_template=nil, append=false)
    super(IMAGE_METHODS[:update], new_template, append ? 1 : 0)
end