Statistics
| Branch: | Tag: | Revision:

one / src / sunstone / models / SunstoneServer.rb @ b085ae95

History | View | Annotate | Download (8.73 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
ONE_LOCATION = ENV["ONE_LOCATION"]
18

    
19
if !ONE_LOCATION
20
    LOG_LOCATION = "/var/log/one"
21
    VAR_LOCATION = "/var/lib/one"
22
    RUBY_LIB_LOCATION = "/usr/lib/one/ruby"
23
else
24
    VAR_LOCATION = ONE_LOCATION+"/var"
25
    LOG_LOCATION = ONE_LOCATION+"/var"
26
    RUBY_LIB_LOCATION = ONE_LOCATION+"/lib/ruby"
27
end
28

    
29
$: << RUBY_LIB_LOCATION
30
$: << File.dirname(__FILE__)
31

    
32
require 'models/OpenNebulaJSON'
33
include OpenNebulaJSON
34

    
35
class SunstoneServer
36
    def initialize(username, password)
37
        # TBD one_client_user(name) from CloudServer
38
        @client = Client.new("dummy:dummy")
39
        @client.one_auth = "#{username}:#{password}"
40
    end
41

    
42
    ############################################################################
43
    #
44
    ############################################################################
45
    def self.authorize(user="", sha1_pass="")
46
        if user.empty? || sha1_pass.empty?
47
            return [401, false]
48
        end
49

    
50
        # TBD get_user_password(name) from CloudServer
51
        user_pool = UserPool.new(Client.new)
52
        rc = user_pool.info
53
        if OpenNebula.is_error?(rc)
54
            return [500, false]
55
        end
56

    
57
        user_pass = user_pool["USER[NAME=\"#{user}\"]/PASSWORD"]
58
        if user_pass == sha1_pass
59
            return [204, user_pool["USER[NAME=\"#{user}\"]/ID"]]
60
        else
61
            return [401, nil]
62
        end
63
    end
64

    
65
    ############################################################################
66
    #
67
    ############################################################################
68
    def get_pool(kind)
69
        user_flag = -2
70
        pool = case kind
71
            when "cluster" then ClusterPoolJSON.new(@client)
72
            when "host"    then HostPoolJSON.new(@client)
73
            when "image"   then ImagePoolJSON.new(@client, user_flag)
74
            when "vm"      then VirtualMachinePoolJSON.new(@client, user_flag)
75
            when "vnet"    then VirtualNetworkPoolJSON.new(@client, user_flag)
76
            when "user"    then UserPoolJSON.new(@client)
77
            else
78
                error = Error.new("Error: #{kind} resource not supported")
79
                return [404, error.to_json]
80
        end
81

    
82
        rc = pool.info
83
        if OpenNebula.is_error?(rc)
84
            return [500, rc.to_json]
85
        else
86
            return [200, pool.to_json]
87
        end
88
    end
89

    
90
    ############################################################################
91
    #
92
    ############################################################################
93
    def get_resource(kind, id)
94
        resource = retrieve_resource(kind, id)
95
        if OpenNebula.is_error?(resource)
96
            return [404, resource.to_json]
97
        else
98
            return [200, resource.to_json]
99
        end
100
    end
101

    
102
    ############################################################################
103
    #
104
    ############################################################################
105
    def create_resource(kind, template)
106
        resource = case kind
107
            when "cluster" then ClusterJSON.new(Cluster.build_xml, @client)
108
            when "host"    then HostJSON.new(Host.build_xml, @client)
109
            when "image"   then ImageJSON.new(Image.build_xml, @client)
110
            when "vm"      then VirtualMachineJSON.new(VirtualMachine.build_xml,@client)
111
            when "vnet"    then VirtualNetworkJSON.new(VirtualNetwork.build_xml, @client)
112
            when "user"    then UserJSON.new(User.build_xml, @client)
113
            else
114
                error = Error.new("Error: #{kind} resource not supported")
115
                return [404, error.to_json]
116
        end
117

    
118
        rc = resource.create(template)
119
        if OpenNebula.is_error?(rc)
120
            return [500, rc.to_json]
121
        else
122
            resource.info
123
            return [201, resource.to_json]
124
        end
125
    end
126

    
127
    ############################################################################
128
    #
129
    ############################################################################
130
    def delete_resource(kind, id)
131
        resource = retrieve_resource(kind, id)
132
        if OpenNebula.is_error?(resource)
133
            return [404, resource.to_json]
134
        end
135

    
136
        rc = resource.delete
137
        if OpenNebula.is_error?(rc)
138
            return [500, rc.to_json]
139
        else
140
            return [204, resource.to_json]
141
        end
142
    end
143

    
144
    ############################################################################
145
    #
146
    ############################################################################
147
    def perform_action(kind, id, action_json)
148
        resource = retrieve_resource(kind, id)
149
        if OpenNebula.is_error?(resource)
150
            return [404, resource.to_json]
151
        end
152

    
153
        rc = resource.perform_action(action_json)
154
        if OpenNebula.is_error?(rc)
155
            return [500, rc.to_json]
156
        else
157
            return [204, resource.to_json]
158
        end
159
    end
160

    
161
    ############################################################################
162
    #
163
    ############################################################################
164
    def get_configuration(user_id)
165
        if user_id != "0"
166
            return [401, ""]
167
        end
168

    
169
        one_config = VAR_LOCATION + "/config"
170
        config = Hash.new
171

    
172
        begin
173
            cfg = File.read(one_config)
174
        rescue Exception => e
175
            error = Error.new("Error reading config: #{e.inspect}")
176
            return [500, error.to_json]
177
        end
178

    
179
        cfg.lines do |line|
180
            m=line.match(/^([^=]+)=(.*)$/)
181

    
182
            if m
183
                name=m[1].strip.upcase
184
                value=m[2].strip
185
                config[name]=value
186
            end
187
        end
188

    
189
        return [200, config.to_json]
190
    end
191

    
192
    ############################################################################
193
    #
194
    ############################################################################
195
    def get_vm_log(id)
196
        resource = retrieve_resource("vm", id)
197
        if OpenNebula.is_error?(resource)
198
            return [404, nil]
199
        else
200
            if !ONE_LOCATION
201
                vm_log_file = LOG_LOCATION + "/#{id}.log"
202
            else
203
                vm_log_file = LOG_LOCATION + "/#{id}/vm.log"
204
            end
205

    
206
            begin
207
                log = File.read(vm_log_file)
208
            rescue Exception => e
209
                return [200, "Log for VM #{id} not available"]
210
            end
211

    
212
            return [200, log]
213
        end
214
    end
215

    
216

    
217
    ########################################################################
218
    # VNC
219
    ########################################################################
220

    
221
    def startvnc(id)
222
        rc = retrieve_resource("vm", id)
223
        if OpenNebula.is_error?(rc)
224
            return rc
225
        end
226
        return rc.startvnc(id)
227
    end
228
    
229
    def stopvnc(id,pipe)
230
       rc = retrieve_resource("vm", id)
231
       if OpenNebula.is_error?(rc)
232
           return rc
233
       end
234
       return rc.stopvnc(pipe)
235
       
236
    end
237

    
238

    
239
    private
240

    
241
    def retrieve_resource(kind, id)
242
        resource = case kind
243
            when "cluster" then ClusterJSON.new_with_id(id, @client)
244
            when "host"    then HostJSON.new_with_id(id, @client)
245
            when "image"   then ImageJSON.new_with_id(id, @client)
246
            when "vm"      then VirtualMachineJSON.new_with_id(id, @client)
247
            when "vnet"    then VirtualNetworkJSON.new_with_id(id, @client)
248
            when "user"    then UserJSON.new_with_id(id, @client)
249
            else
250
                error = Error.new("Error: #{kind} resource not supported")
251
                return error
252
        end
253

    
254
        rc = resource.info
255
        if OpenNebula.is_error?(rc)
256
            return rc
257
        else
258
            return resource
259
        end
260
    end
261
end