Statistics
| Branch: | Tag: | Revision:

one / src / sunstone / sunstone-server.rb @ 6509e422

History | View | Annotate | Download (6.74 KB)

1
#!/usr/bin/env ruby
2

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

    
19
##############################################################################
20
# Required libraries
21
##############################################################################
22
require 'rubygems'
23
require 'sinatra'
24

    
25
require 'models/SunstoneServer'
26

    
27

    
28
##############################################################################
29
# Sinatra Configuration
30
##############################################################################
31
use Rack::Session::Pool
32

    
33

    
34
##############################################################################
35
# Helpers
36
##############################################################################
37
helpers do
38
    def authorized?
39
        session[:ip] && session[:ip]==request.ip ? true : false
40
    end
41

    
42
    def build_session
43
        auth = Rack::Auth::Basic::Request.new(request.env)
44
        if auth.provided? && auth.basic? && auth.credentials
45
            user = auth.credentials[0]
46
            sha1_pass = Digest::SHA1.hexdigest(auth.credentials[1])
47

    
48
            rc = SunstoneServer.authorize(user, sha1_pass)
49
            if rc[1]
50
                session[:user]     = user
51
                session[:user_id]  = rc[1]
52
                session[:password] = sha1_pass
53
                session[:ip]       = request.ip
54
                session[:remember] = params[:remember]
55

    
56
                if params[:remember]
57
                    env['rack.session.options'][:expire_after] = 30*60*60*24
58
                end
59

    
60
                return [204, ""]
61
            else
62
                return [rc.first, ""]
63
            end
64
        end
65

    
66
        return [401, ""]
67
    end
68

    
69
    def destroy_session
70
        session.clear
71
        return [204, ""]
72
    end
73
end
74

    
75
before do
76
    unless request.path=='/login' || request.path=='/'
77
        halt 401 unless authorized?
78

    
79
        @SunstoneServer = SunstoneServer.new(session[:user], session[:password])
80
    end
81
end
82

    
83
after do
84
    unless request.path=='/login' || request.path=='/'
85
        unless session[:remember]
86
            if params[:timeout] == true
87
                env['rack.session.options'][:defer] = true
88
            else
89
                env['rack.session.options'][:expire_after] = 60*10
90
            end
91
        end
92
    end
93
end
94

    
95
##############################################################################
96
# HTML Requests
97
##############################################################################
98
get '/' do
99
    redirect '/login' unless authorized?
100

    
101
    time = Time.now + 60
102
    response.set_cookie("one-user",
103
                        :value=>"#{session[:user]}",
104
                        :expires=>time)
105
    response.set_cookie("one-user_id",
106
                        :value=>"#{session[:user_id]}",
107
                        :expires=>time)
108

    
109
    File.read(File.dirname(__FILE__)+'/templates/index.html')
110
end
111

    
112
get '/login' do
113
    File.read(File.dirname(__FILE__)+'/templates/login.html')
114
end
115

    
116
##############################################################################
117
# Login
118
##############################################################################
119
post '/login' do
120
    build_session
121
end
122

    
123
post '/logout' do
124
    destroy_session
125
end
126

    
127
##############################################################################
128
# Config and Logs
129
##############################################################################
130
get '/config' do
131
    @SunstoneServer.get_configuration(session[:user_id])
132
end
133

    
134
get '/vm/:id/log' do
135
    @SunstoneServer.get_vm_log(params[:id])
136
end
137

    
138
##############################################################################
139
# GET Pool information
140
##############################################################################
141
get '/:pool' do
142
    @SunstoneServer.get_pool(params[:pool])
143
end
144

    
145
##############################################################################
146
# GET Resource information
147
##############################################################################
148
get '/:resource/:id' do
149
    @SunstoneServer.get_resource(params[:resource], params[:id])
150
end
151

    
152
##############################################################################
153
# Delete Resource
154
##############################################################################
155
delete '/:resource/:id' do
156
    @SunstoneServer.delete_resource(params[:resource], params[:id])
157
end
158

    
159
##############################################################################
160
# Create a new Resource
161
##############################################################################
162
post '/:pool' do
163
    @SunstoneServer.create_resource(params[:pool], request.body.read)
164
end
165

    
166
post '/vm/:id/stopvnc' do
167
    vm_id = params[:id]
168
    vnc_hash = session['vnc']
169

    
170
    if !vnc_hash || !vnc_hash[vm_id]
171
        msg = "It seems there is no VNC proxy running for this machine"
172
        return [403, OpenNebula::Error.new(msg).to_json]
173
    end
174

    
175
    rc = @SunstoneServer.stopvnc(vm_id, vnc_hash[vm_id])
176
    if rc[0] == 200
177
        session['vnc'].delete(vm_id)
178
    end
179

    
180
    rc
181
end
182

    
183
post '/vm/:id/startvnc' do
184
    vm_id = params[:id]
185

    
186
    vnc_hash = session['vnc']
187

    
188
    if !vnc_hash
189
        session['vnc']= {}
190
    elsif vnc_hash[vm_id]
191
        msg = "There is a VNC server running for this VM"
192
        return [403, OpenNebula::Error.new(msg).to_json]
193
    end
194

    
195
    rc = @SunstoneServer.startvnc(vm_id)
196
    if rc[0] == 200
197
        info = rc[1]
198
        session['vnc'][vm_id] = info[:pipe]
199
        info.delete(:pipe)
200

    
201
        [200, info.to_json]
202
    else
203
        rc
204
    end
205
end
206

    
207
##############################################################################
208
# Perform an action on a Resource
209
##############################################################################
210
post '/:resource/:id/action' do
211
    @SunstoneServer.perform_action(params[:resource], params[:id], request.body.read)
212
end
213