Class: OpenNebula::VirtualMachine

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

Constant Summary collapse

VM_METHODS =

Constants and Class Methods

{
    :info     => "vm.info",
    :allocate => "vm.allocate",
    :action   => "vm.action",
    :migrate  => "vm.migrate",
    :deploy   => "vm.deploy",
    :savedisk => "vm.savedisk",
    :chown    => "vm.chown"
}
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

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

Constructor Details

#initialize(xml, client) ⇒ VirtualMachine

Class constructor



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

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

    @element_name = "VM"
    @client       = 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)


87
88
89
90
91
92
93
94
95
# File 'lib/OpenNebula/VirtualMachine.rb', line 87

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



97
98
99
100
101
102
# File 'lib/OpenNebula/VirtualMachine.rb', line 97

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.



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

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

#cancelObject

Cancels a running VM



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

def cancel
    action('cancel')
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



234
235
236
# File 'lib/OpenNebula/VirtualMachine.rb', line 234

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.



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

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



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

def finalize
    action('finalize')
end

#gidObject

Returns the group identifier

return

Integer the element’s group ID



275
276
277
# File 'lib/OpenNebula/VirtualMachine.rb', line 275

def gid
    self['GID'].to_i
end

#holdObject

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



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

def hold
    action('hold')
end

#infoObject

Retrieves the information of the given VirtualMachine.



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

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

#lcm_stateObject

Returns the LCM state of the VirtualMachine (numeric value)



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

def lcm_state
    self['LCM_STATE'].to_i
end

#lcm_state_strObject

Returns the LCM state of the VirtualMachine (string value)



258
259
260
# File 'lib/OpenNebula/VirtualMachine.rb', line 258

def lcm_state_str
    LCM_STATE[lcm_state]
end

#live_migrate(host_id) ⇒ Object

Migrates a running VM to another host without downtime



204
205
206
207
208
209
210
211
# File 'lib/OpenNebula/VirtualMachine.rb', line 204

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



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

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

#releaseObject

Releases a VM from hold state



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

def release
    action('release')
end

#restartObject

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



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

def restart
    action('restart')
end

#resubmitObject

Resubmits a VM to PENDING state



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

def resubmit
    action('resubmit')
end

#resumeObject

Resumes the execution of a saved VM



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

def resume
    action('resume')
end

#save_as(disk_id, image_name) ⇒ 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

Returns:

  • (Integer, OpenNebula::Error)

    the new Image ID in case of success, error otherwise



222
223
224
225
226
227
228
# File 'lib/OpenNebula/VirtualMachine.rb', line 222

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

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

    return rc
end

#shutdownObject

Shutdowns an already deployed VM



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

def shutdown
    action('shutdown')
end

#stateObject

Returns the VM state of the VirtualMachine (numeric value)



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

def state
    self['STATE'].to_i
end

#state_strObject

Returns the VM state of the VirtualMachine (string value)



248
249
250
# File 'lib/OpenNebula/VirtualMachine.rb', line 248

def state_str
    VM_STATE[state]
end

#statusObject

Returns the short status string for the VirtualMachine



263
264
265
266
267
268
269
270
271
# File 'lib/OpenNebula/VirtualMachine.rb', line 263

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



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

def stop
    action('stop')
end

#suspendObject

Saves a running VM



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

def suspend
    action('suspend')
end