Statistics
| Branch: | Tag: | Revision:

one / src / oca / ruby / OpenNebula / VirtualMachine.rb @ 6329d8b3

History | View | Annotate | Download (8.85 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
3
#                                                                            #
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
5
# not use this file except in compliance with the License. You may obtain    #
6
# a copy of the License at                                                   #
7
#                                                                            #
8
# http://www.apache.org/licenses/LICENSE-2.0                                 #
9
#                                                                            #
10
# Unless required by applicable law or agreed to in writing, software        #
11
# distributed under the License is distributed on an "AS IS" BASIS,          #
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13
# See the License for the specific language governing permissions and        #
14
# limitations under the License.                                             #
15
#--------------------------------------------------------------------------- #
16

    
17
require 'OpenNebula/Pool'
18

    
19
module OpenNebula
20
    class VirtualMachine < PoolElement
21
        #######################################################################
22
        # Constants and Class Methods
23
        #######################################################################
24
        VM_METHODS = {
25
            :info     => "vm.info",
26
            :allocate => "vm.allocate",
27
            :action   => "vm.action",
28
            :migrate  => "vm.migrate",
29
            :deploy   => "vm.deploy",
30
            :savedisk => "vm.savedisk"
31
        }
32

    
33
        VM_STATE=%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED}
34

    
35
        LCM_STATE=%w{LCM_INIT PROLOG BOOT RUNNING MIGRATE SAVE_STOP SAVE_SUSPEND
36
            SAVE_MIGRATE PROLOG_MIGRATE PROLOG_RESUME EPILOG_STOP EPILOG
37
            SHUTDOWN CANCEL FAILURE CLEANUP UNKNOWN}
38

    
39
        SHORT_VM_STATES={
40
            "INIT"      => "init",
41
            "PENDING"   => "pend",
42
            "HOLD"      => "hold",
43
            "ACTIVE"    => "actv",
44
            "STOPPED"   => "stop",
45
            "SUSPENDED" => "susp",
46
            "DONE"      => "done",
47
            "FAILED"    => "fail"
48
        }
49

    
50
        SHORT_LCM_STATES={
51
            "PROLOG"        => "prol",
52
            "BOOT"          => "boot",
53
            "RUNNING"       => "runn",
54
            "MIGRATE"       => "migr",
55
            "SAVE_STOP"     => "save",
56
            "SAVE_SUSPEND"  => "save",
57
            "SAVE_MIGRATE"  => "save",
58
            "PROLOG_MIGRATE"=> "migr",
59
            "PROLOG_RESUME" => "prol",
60
            "EPILOG_STOP"   => "epil",
61
            "EPILOG"        => "epil",
62
            "SHUTDOWN"      => "shut",
63
            "CANCEL"        => "shut",
64
            "FAILURE"       => "fail",
65
            "CLEANUP"       => "clea",
66
            "UNKNOWN"       => "unkn"
67
        }
68

    
69
        MIGRATE_REASON=%w{NONE ERROR STOP_RESUME USER CANCEL}
70

    
71
        SHORT_MIGRATE_REASON={
72
            "NONE"          => "none",
73
            "ERROR"         => "erro",
74
            "STOP_RESUME"   => "stop",
75
            "USER"          => "user",
76
            "CANCEL"        => "canc"
77
        }
78

    
79
        # Creates a VirtualMachine description with just its identifier
80
        # this method should be used to create plain VirtualMachine objects.
81
        # +id+ the id of the vm
82
        #
83
        # Example:
84
        #   vnet = VirtualMachine.new(VirtualMachine.build_xml(3),rpc_client)
85
        #
86
        def VirtualMachine.build_xml(pe_id=nil)
87
            if pe_id
88
                vm_xml = "<VM><ID>#{pe_id}</ID></VM>"
89
            else
90
                vm_xml = "<VM></VM>"
91
            end
92

    
93
            XMLElement.build_xml(vm_xml, 'VM')
94
        end
95

    
96
        def VirtualMachine.get_reason(reason)
97
            reason=MIGRATE_REASON[reason.to_i]
98
            reason_str=SHORT_MIGRATE_REASON[reason]
99

    
100
            reason_str
101
        end
102

    
103
        #######################################################################
104
        # Class constructor
105
        #######################################################################
106
        def initialize(xml, client)
107
            super(xml,client)
108

    
109
            @element_name = "VM"
110
            @client       = client
111
        end
112

    
113
        #######################################################################
114
        # XML-RPC Methods for the Virtual Machine Object
115
        #######################################################################
116

    
117
        # Retrieves the information of the given VirtualMachine.
118
        def info()
119
            super(VM_METHODS[:info], 'VM')
120
        end
121

    
122
        # Allocates a new VirtualMachine in OpenNebula
123
        #
124
        # +description+ A string containing the template of the VirtualMachine.
125
        def allocate(description)
126
            super(VM_METHODS[:allocate],description)
127
        end
128

    
129
        # Initiates the instance of the VM on the target host.
130
        #
131
        # +host_id+ The host id (hid) of the target host where
132
        # the VM will be instantiated.
133
        def deploy(host_id)
134
            return Error.new('ID not defined') if !@pe_id
135

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

    
139
            return rc
140
        end
141

    
142
        # Shutdowns an already deployed VM
143
        def shutdown
144
            action('shutdown')
145
        end
146

    
147
        # Cancels a running VM
148
        def cancel
149
            action('cancel')
150
        end
151

    
152
        # Sets a VM to hold state, scheduler will not deploy it
153
        def hold
154
            action('hold')
155
        end
156

    
157
        # Releases a VM from hold state
158
        def release
159
            action('release')
160
        end
161

    
162
        # Stops a running VM
163
        def stop
164
            action('stop')
165
        end
166

    
167
        # Saves a running VM
168
        def suspend
169
            action('suspend')
170
        end
171

    
172
        # Resumes the execution of a saved VM
173
        def resume
174
            action('resume')
175
        end
176

    
177
        # Deletes a VM from the pool
178
        def finalize
179
            action('finalize')
180
        end
181

    
182
        # Forces a re-deployment of a VM in UNKNOWN or BOOT state
183
        def restart
184
            action('restart')
185
        end
186

    
187
        # Resubmits a VM to PENDING state
188
        def resubmit
189
            action('resubmit')
190
        end
191

    
192
        # Saves a running VM and starts it again in the specified host
193
        def migrate(host_id)
194
            return Error.new('ID not defined') if !@pe_id
195

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

    
199
            return rc
200
        end
201

    
202
        # Migrates a running VM to another host without downtime
203
        def live_migrate(host_id)
204
            return Error.new('ID not defined') if !@pe_id
205

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

    
209
            return rc
210
        end
211

    
212
        # Set the specified vm's disk to be saved in a new image
213
        # when the VirtualMachine shutdowns
214
        #
215
        # +disk_id+ ID of the disk to be saved
216
        #
217
        # +image_name+ Name for the new image where the disk will be saved
218
        def save_as(disk_id, image_name)
219
            return Error.new('ID not defined') if !@pe_id
220

    
221
            rc = @client.call(VM_METHODS[:savedisk], @pe_id, disk_id, image_name)
222
            rc = nil if !OpenNebula.is_error?(rc)
223

    
224
            return rc
225
        end
226

    
227
        #######################################################################
228
        # Helpers to get VirtualMachine information
229
        #######################################################################
230

    
231
        # Returns the VM state of the VirtualMachine (numeric value)
232
        def state
233
            self['STATE'].to_i
234
        end
235

    
236
        # Returns the VM state of the VirtualMachine (string value)
237
        def state_str
238
            VM_STATE[state]
239
        end
240

    
241
        # Returns the LCM state of the VirtualMachine (numeric value)
242
        def lcm_state
243
            self['LCM_STATE'].to_i
244
        end
245

    
246
        # Returns the LCM state of the VirtualMachine (string value)
247
        def lcm_state_str
248
            LCM_STATE[lcm_state]
249
        end
250

    
251
        # Returns the short status string for the VirtualMachine
252
        def status
253
            short_state_str=SHORT_VM_STATES[state_str]
254

    
255
            if short_state_str=="actv"
256
                short_state_str=SHORT_LCM_STATES[lcm_state_str]
257
            end
258

    
259
            short_state_str
260
        end
261

    
262
        # Returns the group identifier
263
        # [return] _Integer_ the element's group ID
264
        def gid
265
            self['GID'].to_i
266
        end
267

    
268
    private
269
        def action(name)
270
            return Error.new('ID not defined') if !@pe_id
271

    
272
            rc = @client.call(VM_METHODS[:action], name, @pe_id)
273
            rc = nil if !OpenNebula.is_error?(rc)
274

    
275
            return rc
276
        end
277
    end
278
end