Revision b5baa499 src/cloud/ec2/lib/econe-server.rb

View differences:

src/cloud/ec2/lib/econe-server.rb
51 51
require 'uri'
52 52

  
53 53
require 'EC2QueryServer'
54
require 'econe_application'
54 55
require 'CloudAuth'
55 56

  
56 57
include OpenNebula
57 58

  
58
##############################################################################
59
# Parse Configuration file
60
##############################################################################
61 59
begin
62
    conf = YAML.load_file(CONFIGURATION_FILE)
60
    $ec2_app = EC2Application.new
63 61
rescue Exception => e
64
    STDERR.puts "Error parsing config file #{CONFIGURATION_FILE}: #{e.message}"
65
    exit 1
62
    STDERR.puts e.message
63
    exit -1
66 64
end
67 65

  
68
conf[:template_location] = TEMPLATE_LOCATION
69
conf[:views] = VIEWS_LOCATION
70
conf[:debug_level] ||= 3
71

  
72
CloudServer.print_configuration(conf)
73

  
74 66
##############################################################################
75 67
# Sinatra Configuration
76 68
##############################################################################
77
set :config, conf
78 69

  
79
include CloudLogger
80
enable_logging EC2_LOG, settings.config[:debug_level].to_i
70
disable :logging
81 71

  
82
if settings.config[:server]
83
    settings.config[:host] ||= settings.config[:server]
84
    warning = "Warning: :server: configuration parameter has been deprecated."
85
    warning << " Use :host: instead."
86
    settings.logger.warn warning
87
end
88

  
89
if CloudServer.is_port_open?(settings.config[:host],
90
                             settings.config[:port])
91
    settings.logger.error {
92
        "Port #{settings.config[:port]} busy, please shutdown " <<
93
        "the service or move occi server port."
94
    }
95
    exit -1
96
end
72
use Rack::CommonLogger, $ec2_app.logger
97 73

  
98
set :bind, settings.config[:host]
99
set :port, settings.config[:port]
100

  
101
begin
102
    ENV["ONE_CIPHER_AUTH"] = EC2_AUTH
103
    cloud_auth = CloudAuth.new(settings.config, settings.logger)
104
rescue => e
105
    settings.logger.error {"Error initializing authentication system"}
106
    settings.logger.error {e.message}
107
    exit -1
108
end
109

  
110
set :cloud_auth, cloud_auth
111

  
112
if conf[:ssl_server]
113
    uri = URI.parse(conf[:ssl_server])
114
    econe_host = uri.host
115
    econe_port = uri.port
116
    econe_path = uri.path
117
else
118
    econe_host = settings.config[:host]
119
    econe_port = settings.config[:port]
120
    econe_path = '/'
121
end
74
set :bind, $ec2_app.conf[:host]
75
set :port, $ec2_app.conf[:port]
122 76

  
123
set :econe_host, econe_host
124
set :econe_port, econe_port
125
set :econe_path, econe_path
77
set :run, false
126 78

  
127 79
##############################################################################
128 80
# Actions
......
130 82

  
131 83
before do
132 84
    begin
133
        params['econe_host'] = settings.econe_host
134
        params['econe_port'] = settings.econe_port
135
        params['econe_path'] = settings.econe_path
136
        username = settings.cloud_auth.auth(request.env, params)
85
        params['econe_host'] = $ec2_app.econe_host
86
        params['econe_port'] = $ec2_app.econe_port
87
        params['econe_path'] = $ec2_app.econe_path
88

  
89
        username = $ec2_app.authenticate(request.env, params)
137 90
    rescue Exception => e
138
        logger.error {e.message}
91
        $ec2_app.logger.error {e.message}
92

  
139 93
        rc = OpenNebula::Error.new(e.message)
140 94
        rc.ec2_code = "AuthFailure"
95

  
141 96
        error 401, rc.to_ec2
142 97
    end
143 98

  
144 99
    if username.nil?
145 100
        rc = OpenNebula::Error.new("The username or password is not correct")
146 101
        rc.ec2_code = "AuthFailure"
102

  
147 103
        error 401, rc.to_ec2
148 104
    else
149
        client          = settings.cloud_auth.client(username)
150
        oneadmin_client = settings.cloud_auth.client
151
        @econe_server   = EC2QueryServer.new(
152
                            client,
153
                            oneadmin_client,
154
                            settings.config,
155
                            settings.logger)
105
        params['econe_username'] = username
156 106
    end
157 107
end
158 108

  
159
post '/' do
160
    do_http_request(params)
161
end
109
# Response treatment
110
helpers do
111
    def logger
112
        $ec2_app.logger
113
    end
162 114

  
163
get '/' do
164
    do_http_request(params)
165
end
115
    def treat_response(result, rc)
116
        if OpenNebula::is_error?(result)
117
            logger.error(result.message)
118
            error CloudServer::HTTP_ERROR_CODE[result.errno],
119
                  result.to_ec2.gsub(/\n\s*/,'')
120
        end
166 121

  
167
def do_http_request(params)
168
    case params['Action']
169
        when 'UploadImage'
170
            result,rc = @econe_server.upload_image(params)
171
        when 'RegisterImage'
172
            result,rc = @econe_server.register_image(params)
173
        when 'DescribeImages'
174
            result,rc = @econe_server.describe_images(params)
175
        when 'RunInstances'
176
            result,rc = @econe_server.run_instances(params)
177
        when 'DescribeInstances'
178
            result,rc = @econe_server.describe_instances(params)
179
        when 'TerminateInstances'
180
            result,rc = @econe_server.terminate_instances(params)
181
        when 'StartInstances'
182
            result,rc = @econe_server.start_instances(params)
183
        when 'StopInstances'
184
            result,rc = @econe_server.stop_instances(params)
185
        when 'RebootInstances'
186
            result,rc = @econe_server.reboot_instances(params)
187
        when 'AllocateAddress'
188
            result,rc = @econe_server.allocate_address(params)
189
        when 'AssociateAddress'
190
            result,rc = @econe_server.associate_address(params)
191
        when 'DisassociateAddress'
192
            result,rc = @econe_server.disassociate_address(params)
193
        when 'ReleaseAddress'
194
            result,rc = @econe_server.release_address(params)
195
        when 'DescribeAddresses'
196
            result,rc = @econe_server.describe_addresses(params)
197
        when 'DescribeRegions'
198
            result,rc = @econe_server.describe_regions(params)
199
        when 'DescribeAvailabilityZones'
200
            result,rc = @econe_server.describe_availability_zones(params)
201
        when 'CreateVolume'
202
            result,rc = @econe_server.create_volume(params)
203
        when 'DescribeVolumes'
204
            result,rc = @econe_server.describe_volumes(params)
205
        when 'AttachVolume'
206
            result,rc = @econe_server.attach_volume(params)
207
        when 'DetachVolume'
208
            result,rc = @econe_server.detach_volume(params)
209
        when 'DeleteVolume'
210
            result,rc = @econe_server.delete_volume(params)
211
        when 'DescribeKeyPairs'
212
            result,rc = @econe_server.describe_keypairs(params)
213
        when 'CreateKeyPair'
214
            result,rc = @econe_server.create_keypair(params)
215
        when 'DeleteKeyPair'
216
            result,rc = @econe_server.delete_keypair(params)
217
        else
218
            result = OpenNebula::Error.new(
219
                "#{params['Action']} feature is not supported",
220
                OpenNebula::Error::ENO_EXISTS)
221
    end
122
        headers['Content-Type'] = 'application/xml'
222 123

  
223
    if OpenNebula::is_error?(result)
224
        logger.error(result.message)
225
        error CloudServer::HTTP_ERROR_CODE[result.errno], result.to_ec2.gsub(/\n\s*/,'')
226
    end
124
        status rc if rc
227 125

  
228
    headers['Content-Type'] = 'application/xml'
126
        logger.error { params['Action'] }
229 127

  
230
    if rc
231
        status rc
128
        result.gsub(/\n\s*/,'')
232 129
    end
130
end
233 131

  
234
    logger.error { params['Action'] }
132
post '/' do
133
    result, rc = $ec2_app.do_http_request(params)
134
    treat_response(result, rc)
135
end
235 136

  
236
    result.gsub(/\n\s*/,'')
137
get '/' do
138
    result, rc = $ec2_app.do_http_request(params)
139
    treat_response(result, rc)
237 140
end
141

  
142
Sinatra::Application.run!

Also available in: Unified diff