Statistics
| Branch: | Tag: | Revision:

one / src / sunstone / models / OpenNebulaJSON / VirtualMachineJSON.rb @ b085ae95

History | View | Annotate | Download (6.8 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 'OpenNebulaJSON/JSONUtils'
18

    
19
module OpenNebulaJSON
20
    class VirtualMachineJSON < OpenNebula::VirtualMachine
21
        include JSONUtils
22

    
23
        def create(template_json)
24
            vm_hash = parse_json(template_json, 'vm')
25
            if OpenNebula.is_error?(vm_hash)
26
                return vm_hash
27
            end
28

    
29
            if vm_hash['vm_raw']
30
                template = vm_hash['vm_raw']
31
            else
32
                template = template_to_str(vm_hash)
33
            end
34

    
35
            self.allocate(template)
36
       end
37

    
38
        def perform_action(template_json)
39
            action_hash = parse_json(template_json,'action')
40
            if OpenNebula.is_error?(action_hash)
41
                return action_hash
42
            end
43

    
44
            rc = case action_hash['perform']
45
                when "cancel"       then self.cancel
46
                when "deploy"       then self.deploy(action_hash['params'])
47
                when "finalize"     then self.finalize
48
                when "hold"         then self.hold
49
                when "livemigrate"  then self.live_migrate(action_hash['params'])
50
                when "migrate"      then self.migrate(action_hash['params'])
51
                when "resume"       then self.resume
52
                when "release"      then self.release
53
                when "stop"         then self.stop
54
                when "suspend"      then self.suspend
55
                when "restart"      then self.restart
56
                when "saveas"       then self.save_as(action_hash['params'])
57
                when "shutdown"     then self.shutdown
58
                when "resubmit"     then self.resubmit
59
                when "startvnc"     then self.startvnc
60
                when "stopvnc"      then self.stopvnc
61
                else
62
                    error_msg = "#{action_hash['perform']} action not " <<
63
                                " available for this resource"
64
                    OpenNebula::Error.new(error_msg)
65
            end
66
        end
67

    
68
        def delete
69
            self.finalize
70
        end
71

    
72
        def deploy(params=Hash.new)
73
            super(params['host_id'])
74
        end
75

    
76
        def live_migrate(params=Hash.new)
77
            super(params['host_id'])
78
        end
79

    
80
        def migrate(params=Hash.new)
81
            super(params['host_id'])
82
        end
83

    
84
        def save_as(params=Hash.new)
85
            if params['image_type']
86
                image_type = params['image_type']
87
            else
88
                image_id = self["TEMPLATE/DISK[DISK_ID=\"#{params[:disk_id]}\"]/IMAGE_ID"]
89

    
90
                if (image_id != nil)
91
                    if self["TEMPLATE/DISK[DISK_ID=\"#{disk_id}\"]/SAVE_AS"]
92
                        error_msg = "Error: The disk #{disk_id} is already" <<
93
                                    " supposed to be saved"
94
                        return OpenNebula::Error.new(error_msg)
95
                    end
96

    
97
                    # Get the image type
98
                    image = OpenNebula::Image.new(
99
                                OpenNebula::Image.build_xml(image_id), @client)
100

    
101
                    result = image.info
102
                    if OpenNebula.is_error?(result)
103
                        return result
104
                    end
105

    
106
                    image_type = image.type_str
107
                end
108
            end
109

    
110
            # Build the template and allocate the new Image
111
            template = "NAME=\"#{params['image_name']}\"\n"
112
            template << "TYPE=#{image_type}\n" if image_type
113

    
114
            image = OpenNebula::Image.new(OpenNebula::Image.build_xml, @client)
115

    
116
            result = image.allocate(template)
117
            if OpenNebula.is_error?(result)
118
                return result
119
            end
120

    
121
            super(params['disk_id'].to_i, image.id)
122
        end
123
        
124
        def startvnc(port)
125
            result = self.info();
126
            if OpenNebula.is_error?(result)
127
                return result
128
            end
129
            
130
            if self['LCM_STATE'] != "3"
131
                return OpenNebula::Error.new("VM is not running");
132
            end
133
            
134
            if self['TEMPLATE/GRAPHICS/TYPE'] != "vnc"
135
                return OpenNebula::Error.new("VM has no VNC configured");
136
            end
137
                      
138
            if self['TEMPLATE/GRAPHICS/PORT']
139
                vnc_port = self['TEMPLATE/GRAPHICS/PORT']
140
            else
141
                return OpenNebula::Error.new("VM has no VNC port set");
142
            end
143
            
144
            if self['TEMPLATE/GRAPHICS/PASSWD']
145
                vnc_pw = self['TEMPLATE/GRAPHICS/PASSWD']
146
            else
147
                vnc_pw = ""
148
            end
149
            
150
            host = self['HISTORY/HOSTNAME']
151
            
152
            #we are ready for the party
153
            
154
            final_port = 29876+port.to_i;
155
            
156
            # puts "Launch noVNC on #{final_port} listenting to #{host}:#{vnc_port}"
157
            # So here we launch the noVNC server listening on the final_port 
158
            # and serving as proxy for the VM host on the configured VNC port.
159
            # TODO - This path is in public...            
160
            pipe = IO.popen("#{File.dirname(__FILE__)}/../../public/vendor/noVNC/utils/launch.sh --listen #{final_port} --vnc #{host}:#{vnc_port}")
161
            
162
            return {:pipe => pipe, :port => final_port, :password => vnc_pw}
163
            
164
        end
165
        
166
        def stopvnc(pipe)
167
            #am I allowed to do something affecting this machine?
168
            result = self.info();
169
            if OpenNebula.is_error?(result)
170
                return result
171
            end
172
            pid = pipe.pid
173
            # puts "Killing noVNC with pid #{pid}"
174
            Process.kill('KILL',pid)
175
            pipe.close
176
            return
177
        end
178
        
179
    end
180
end