Statistics
| Branch: | Tag: | Revision:

one / src / sunstone / sunstone-server.rb @ b085ae95

History | View | Annotate | Download (7.06 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 = params[:id]
168
    vnc_hash = session['vnc']
169
    
170
    if !vnc_hash
171
        msg = "Ups, no VNC sessions information found. Cannot stop VNC"
172
        return [500, OpenNebula::Error.new(msg).to_json]
173
    elsif !vnc_hash[vm]
174
        msg = "It seems there is no VNC proxy running for this machine"
175
        return [500, OpenNebula::Error.new(msg).to_json]
176
    end
177
    
178
    pipe = session['vnc'][vm]
179
    rc = @SunstoneServer.stopvnc(vm,pipe)
180
    
181
    if (OpenNebula.is_error?(rc))
182
        return [500, rc.to_json]
183
    else
184
        session['vnc'].delete(vm)
185
        return    
186
    end
187
    
188
end
189

    
190
post '/vm/:id/startvnc' do
191
    vm = params[:id]
192
    info = @SunstoneServer.startvnc(vm)
193
    
194
    if OpenNebula.is_error?(info)
195
        return [500,info.to_json]
196
    end
197
    
198
    port = info[:port] #port to connect to
199
    pw = info[:password]
200
    pipe = info[:pipe]
201
    
202
    vnc_hash = session['vnc']
203
    
204
    if !vnc_hash 
205
        session['vnc']= {}
206
    elsif vnc_hash and vnc_hash[vm]
207
        return [500, OpenNebula::Error.new("There is a VNC server running for this VM").to_json]
208
    end
209
    
210
    session['vnc'][vm]=pipe;
211
    return {:port => port, :password => pw }.to_json
212
    
213
end
214

    
215
##############################################################################
216
# Perform an action on a Resource
217
##############################################################################
218
post '/:resource/:id/action' do
219
    @SunstoneServer.perform_action(params[:resource], params[:id], request.body.read)
220
end
221