Class: OpenNebula::VirtualMachine

Inherits:
PoolElement show all
Defined in:
lib/OpenNebula/VirtualMachine.rb

Constant Summary collapse

VM_METHODS =
{
    :info     => "vm.info",
    :allocate => "vm.allocate",
    :action   => "vm.action",
    :migrate  => "vm.migrate",
    :deploy   => "vm.deploy",
    :savedisk => "vm.savedisk",
    :chown    => "vm.chown",
    :chmod    => "vm.chmod",
}
VM_STATE =
%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED}
LCM_STATE =
%w{LCM_INIT PROLOG BOOT RUNNING MIGRATE SAVE_STOP SAVE_SUSPEND
SAVE_MIGRATE PROLOG_MIGRATE PROLOG_RESUME EPILOG_STOP EPILOG
SHUTDOWN CANCEL FAILURE CLEANUP UNKNOWN}
SHORT_VM_STATES =
{
    "INIT"      => "init",
    "PENDING"   => "pend",
    "HOLD"      => "hold",
    "ACTIVE"    => "actv",
    "STOPPED"   => "stop",
    "SUSPENDED" => "susp",
    "DONE"      => "done",
    "FAILED"    => "fail"
}
SHORT_LCM_STATES =
{
    "PROLOG"        => "prol",
    "BOOT"          => "boot",
    "RUNNING"       => "runn",
    "MIGRATE"       => "migr",
    "SAVE_STOP"     => "save",
    "SAVE_SUSPEND"  => "save",
    "SAVE_MIGRATE"  => "save",
    "PROLOG_MIGRATE"=> "migr",
    "PROLOG_RESUME" => "prol",
    "EPILOG_STOP"   => "epil",
    "EPILOG"        => "epil",
    "SHUTDOWN"      => "shut",
    "CANCEL"        => "shut",
    "FAILURE"       => "fail",
    "CLEANUP"       => "clea",
    "UNKNOWN"       => "unkn"
}
MIGRATE_REASON =
%w{NONE ERROR STOP_RESUME USER CANCEL}
SHORT_MIGRATE_REASON =
{
    "NONE"          => "none",
    "ERROR"         => "erro",
    "STOP_RESUME"   => "stop",
    "USER"          => "user",
    "CANCEL"        => "canc"
}

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, #has_elements?, #initialize_xml, #name, #retrieve_elements, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml

Constructor Details

#initialize(xml, client) ⇒ VirtualMachine

Class constructor



109
110
111
# File 'lib/OpenNebula/VirtualMachine.rb', line 109

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

Class Method Details

.build_xml(pe_id = nil) ⇒ Object

Creates a VirtualMachine description with just its identifier this method should be used to create plain VirtualMachine objects. id the id of the vm

Example:

vnet = VirtualMachine.new(VirtualMachine.build_xml(3),rpc_client)


91
92
93
94
95
96
97
98
99
# File 'lib/OpenNebula/VirtualMachine.rb', line 91

def VirtualMachine.build_xml(pe_id=nil)
    if pe_id
        vm_xml = "<VM><ID>#{pe_id}</ID></VM>"
    else
        vm_xml = "<VM></VM>"
    end

    XMLElement.build_xml(vm_xml, 'VM')
end

.get_reason(reason) ⇒ Object



101
102
103
104
105
106
# File 'lib/OpenNebula/VirtualMachine.rb', line 101

def VirtualMachine.get_reason(reason)
    reason=MIGRATE_REASON[reason.to_i]
    reason_str=SHORT_MIGRATE_REASON[reason]

    reason_str
end

Instance Method Details

#allocate(description) ⇒ Object

Allocates a new VirtualMachine in OpenNebula

description A string containing the template of the VirtualMachine.



125
126
127
# File 'lib/OpenNebula/VirtualMachine.rb', line 125

def allocate(description)
    super(VM_METHODS[:allocate],description)
end

#cancelObject

Cancels a running VM



153
154
155
# File 'lib/OpenNebula/VirtualMachine.rb', line 153

def cancel
    action('cancel')
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 permissions. Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change

Returns:



262
263
264
265
266
# File 'lib/OpenNebula/VirtualMachine.rb', line 262

def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
        other_m, other_a)
    super(VM_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 permissions.

Parameters:

  • octet (String)

    Permissions octed , e.g. 640

Returns:



253
254
255
# File 'lib/OpenNebula/VirtualMachine.rb', line 253

def chmod_octet(octet)
    super(VM_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



244
245
246
# File 'lib/OpenNebula/VirtualMachine.rb', line 244

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

#deploy(host_id) ⇒ Object

Initiates the instance of the VM on the target host.

host_id The host id (hid) of the target host where the VM will be instantiated.



133
134
135
136
137
138
139
140
# File 'lib/OpenNebula/VirtualMachine.rb', line 133

def deploy(host_id)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(VM_METHODS[:deploy], @pe_id, host_id.to_i)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

#finalizeObject

Deletes a VM from the pool



183
184
185
# File 'lib/OpenNebula/VirtualMachine.rb', line 183

def finalize
    action('finalize')
end

#gidObject

Returns the group identifier

return

Integer the element’s group ID



305
306
307
# File 'lib/OpenNebula/VirtualMachine.rb', line 305

def gid
    self['GID'].to_i
end

#holdObject

Sets a VM to hold state, scheduler will not deploy it



158
159
160
# File 'lib/OpenNebula/VirtualMachine.rb', line 158

def hold
    action('hold')
end

#infoObject

Retrieves the information of the given VirtualMachine.



118
119
120
# File 'lib/OpenNebula/VirtualMachine.rb', line 118

def info()
    super(VM_METHODS[:info], 'VM')
end

#lcm_stateObject

Returns the LCM state of the VirtualMachine (numeric value)



283
284
285
# File 'lib/OpenNebula/VirtualMachine.rb', line 283

def lcm_state
    self['LCM_STATE'].to_i
end

#lcm_state_strObject

Returns the LCM state of the VirtualMachine (string value)



288
289
290
# File 'lib/OpenNebula/VirtualMachine.rb', line 288

def lcm_state_str
    LCM_STATE[lcm_state]
end

#live_migrate(host_id) ⇒ Object

Migrates a running VM to another host without downtime



208
209
210
211
212
213
214
215
# File 'lib/OpenNebula/VirtualMachine.rb', line 208

def live_migrate(host_id)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(VM_METHODS[:migrate], @pe_id, host_id.to_i, true)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

#migrate(host_id) ⇒ Object

Saves a running VM and starts it again in the specified host



198
199
200
201
202
203
204
205
# File 'lib/OpenNebula/VirtualMachine.rb', line 198

def migrate(host_id)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(VM_METHODS[:migrate], @pe_id, host_id.to_i, false)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

#rebootObject

Shutdowns an already deployed VM



148
149
150
# File 'lib/OpenNebula/VirtualMachine.rb', line 148

def reboot
    action('reboot')
end

#releaseObject

Releases a VM from hold state



163
164
165
# File 'lib/OpenNebula/VirtualMachine.rb', line 163

def release
    action('release')
end

#restartObject

Forces a re-deployment of a VM in UNKNOWN or BOOT state



188
189
190
# File 'lib/OpenNebula/VirtualMachine.rb', line 188

def restart
    action('restart')
end

#resubmitObject

Resubmits a VM to PENDING state



193
194
195
# File 'lib/OpenNebula/VirtualMachine.rb', line 193

def resubmit
    action('resubmit')
end

#resumeObject

Resumes the execution of a saved VM



178
179
180
# File 'lib/OpenNebula/VirtualMachine.rb', line 178

def resume
    action('resume')
end

#save_as(disk_id, image_name, image_type = "") ⇒ Integer, OpenNebula::Error

Set the specified vm’s disk to be saved in a new image when the VirtualMachine shutdowns

Parameters:

  • disk_id (Integer)

    ID of the disk to be saved

  • image_name (String)

    Name for the new image where the disk will be saved

  • image_type (String) (defaults to: "")

    Type of the new image. Set to empty string to use the default type

Returns:

  • (Integer, OpenNebula::Error)

    the new Image ID in case of success, error otherwise



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/OpenNebula/VirtualMachine.rb', line 228

def save_as(disk_id, image_name, image_type="")
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(VM_METHODS[:savedisk],
                      @pe_id,
                      disk_id,
                      image_name,
                      image_type)

    return rc
end

#shutdownObject

Shutdowns an already deployed VM



143
144
145
# File 'lib/OpenNebula/VirtualMachine.rb', line 143

def shutdown
    action('shutdown')
end

#stateObject

Returns the VM state of the VirtualMachine (numeric value)



273
274
275
# File 'lib/OpenNebula/VirtualMachine.rb', line 273

def state
    self['STATE'].to_i
end

#state_strObject

Returns the VM state of the VirtualMachine (string value)



278
279
280
# File 'lib/OpenNebula/VirtualMachine.rb', line 278

def state_str
    VM_STATE[state]
end

#statusObject

Returns the short status string for the VirtualMachine



293
294
295
296
297
298
299
300
301
# File 'lib/OpenNebula/VirtualMachine.rb', line 293

def status
    short_state_str=SHORT_VM_STATES[state_str]

    if short_state_str=="actv"
        short_state_str=SHORT_LCM_STATES[lcm_state_str]
    end

    short_state_str
end

#stopObject

Stops a running VM



168
169
170
# File 'lib/OpenNebula/VirtualMachine.rb', line 168

def stop
    action('stop')
end

#suspendObject

Saves a running VM



173
174
175
# File 'lib/OpenNebula/VirtualMachine.rb', line 173

def suspend
    action('suspend')
end