Revision b5baa499

View differences:

install.sh
1162 1162
               src/cloud/ec2/lib/instance.rb \
1163 1163
               src/cloud/ec2/lib/keypair.rb \
1164 1164
               src/cloud/ec2/lib/net_ssh_replacement.rb \
1165
               src/cloud/ec2/lib/econe_application.rb \
1165 1166
               src/cloud/ec2/lib/econe-server.rb"
1166 1167

  
1167 1168
ECO_LIB_CLIENT_FILES="src/cloud/ec2/lib/EC2QueryClient.rb"
src/cloud/common/CloudServer.rb
67 67
        puts "         Server configuration         "
68 68
        puts "--------------------------------------"
69 69
        pp config
70
        puts "--------------------------------------"
71
        puts
70 72

  
71 73
        STDOUT.flush
72 74
    end
src/cloud/ec2/lib/ebs.rb
237 237
        return response.result(binding), 200
238 238
    end
239 239

  
240
end
240
end
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!
src/cloud/ec2/lib/econe_application.rb
1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs        #
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 'CloudServer'
18

  
19
class EC2Application
20
    ############################################################################
21
    # Configuration constants
22
    ############################################################################
23
    EC2_AUTH           = VAR_LOCATION + "/.one/ec2_auth"
24
    EC2_LOG            = LOG_LOCATION + "/econe-server.log"
25
    CONFIGURATION_FILE = ETC_LOCATION + "/econe.conf"
26

  
27
    TEMPLATE_LOCATION  = ETC_LOCATION + "/ec2query_templates"
28
    VIEWS_LOCATION     = RUBY_LIB_LOCATION + "/cloud/econe/views"
29

  
30
    ############################################################################
31
    # Attribute accesors
32
    ############################################################################
33
    attr_reader :conf
34
    attr_reader :logger
35
    attr_reader :econe_host, :econe_port, :econe_path
36

  
37
    ############################################################################
38
    # Initialization of the EC2 application server
39
    ############################################################################
40
    def initialize
41
        # ----------- Parse configuration -----------
42
        begin
43
            @conf = YAML.load_file(CONFIGURATION_FILE)
44
        rescue Exception => e
45
            raise "Error parsing file #{CONFIGURATION_FILE}: #{e.message}"
46
        end
47

  
48
        @conf[:template_location] = TEMPLATE_LOCATION
49
        @conf[:views] = VIEWS_LOCATION
50
        @conf[:debug_level] ||= 3
51

  
52
        @conf[:host] ||= @conf[:server] if @conf[:server]
53

  
54
        CloudServer.print_configuration(@conf)
55

  
56
        # ----------- Init logging system -----------
57
        @logger = CloudLogger::CloudLogger.new(EC2_LOG)
58

  
59
        @logger.level     = CloudLogger::DEBUG_LEVEL[@conf[:debug_level].to_i]
60
        @logger.formatter = proc do |severity, datetime, progname, msg|
61
             CloudLogger::MSG_FORMAT % [
62
                datetime.strftime( CloudLogger::DATE_FORMAT),
63
                severity[0..0],
64
                msg ]
65
        end
66

  
67
        # ----------- Init Authentication System -----------
68
        begin
69
            ENV["ONE_CIPHER_AUTH"] = EC2_AUTH
70
            @cloud_auth = CloudAuth.new(@conf, @logger)
71
        rescue => e
72
            raise "Error initializing authentication system: #{e.message}"
73
        end
74

  
75
        # ----------- Check port -----------
76
        if CloudServer.is_port_open?(@conf[:host], @conf[:port])
77
            raise "Port #{@conf[:port]} busy."
78
        end
79

  
80
        # ----------- Init EC2 attributes -----------
81
        if @conf[:ssl_server]
82
            uri = URI.parse(@conf[:ssl_server])
83
            @econe_host = uri.host
84
            @econe_port = uri.port
85
            @econe_path = uri.path
86
        else
87
            @econe_host = @conf[:host]
88
            @econe_port = @conf[:port]
89
            @econe_path = '/'
90
        end
91
    end
92

  
93
    ############################################################################
94
    # Authentication & route methods
95
    ############################################################################
96
    def authenticate(renv, rparams)
97
        @cloud_auth.auth(renv, rparams)
98
    end
99

  
100
    def do_http_request(params)
101

  
102
        econe_server = EC2QueryServer.new(
103
            @cloud_auth.client(params['econe_username']), @cloud_auth.client,
104
            @conf, @logger)
105

  
106
        case params['Action']
107
        when 'UploadImage'
108
            result,rc = econe_server.upload_image(params)
109
        when 'RegisterImage'
110
            result,rc = econe_server.register_image(params)
111
        when 'DescribeImages'
112
            result,rc = econe_server.describe_images(params)
113
        when 'RunInstances'
114
            result,rc = econe_server.run_instances(params)
115
        when 'DescribeInstances'
116
            result,rc = econe_server.describe_instances(params)
117
        when 'TerminateInstances'
118
            result,rc = econe_server.terminate_instances(params)
119
        when 'StartInstances'
120
            result,rc = econe_server.start_instances(params)
121
        when 'StopInstances'
122
            result,rc = econe_server.stop_instances(params)
123
        when 'RebootInstances'
124
            result,rc = econe_server.reboot_instances(params)
125
        when 'AllocateAddress'
126
            result,rc = econe_server.allocate_address(params)
127
        when 'AssociateAddress'
128
            result,rc = econe_server.associate_address(params)
129
        when 'DisassociateAddress'
130
            result,rc = econe_server.disassociate_address(params)
131
        when 'ReleaseAddress'
132
            result,rc = econe_server.release_address(params)
133
        when 'DescribeAddresses'
134
            result,rc = econe_server.describe_addresses(params)
135
        when 'DescribeRegions'
136
            result,rc = econe_server.describe_regions(params)
137
        when 'DescribeAvailabilityZones'
138
            result,rc = econe_server.describe_availability_zones(params)
139
        when 'CreateVolume'
140
            result,rc = econe_server.create_volume(params)
141
        when 'DescribeVolumes'
142
            result,rc = econe_server.describe_volumes(params)
143
        when 'AttachVolume'
144
            result,rc = econe_server.attach_volume(params)
145
        when 'DetachVolume'
146
            result,rc = econe_server.detach_volume(params)
147
        when 'DeleteVolume'
148
            result,rc = econe_server.delete_volume(params)
149
        when 'DescribeKeyPairs'
150
            result,rc = econe_server.describe_keypairs(params)
151
        when 'CreateKeyPair'
152
            result,rc = econe_server.create_keypair(params)
153
        when 'DeleteKeyPair'
154
            result,rc = econe_server.delete_keypair(params)
155
        else
156
            result = OpenNebula::Error.new(
157
                "#{params['Action']} feature is not supported",
158
                OpenNebula::Error::ENO_EXISTS)
159
        end
160

  
161
        return result, rc
162
    end
163
end
src/cloud/ec2/lib/views/describe_instances.erb
14 14
        <% vmpool.each do |vm| %>
15 15
        <item>
16 16
          <%= render_instance_id(vm) %>
17
          <imageId><%= vm['TEMPLATE/IMAGE_ID'] %></imageId>
17
          <imageId><%= vm['USER_TEMPLATE/IMAGE_ID'] %></imageId>
18 18
          <instanceState>
19 19
              <%= render_state(vm) %>
20 20
          </instanceState>
......
30 30
          <keyName><%= vm['TEMPLATE/CONTEXT/EC2_KEYNAME'] %></keyName>
31 31
          <% end %>
32 32
          <productCodes/>
33
          <instanceType><%= vm['TEMPLATE/INSTANCE_TYPE'] %></instanceType>
33
          <instanceType><%= vm['USER_TEMPLATE/INSTANCE_TYPE'] %></instanceType>
34 34
          <%= render_launch_time(vm) %>
35 35
          <placement>
36 36
            <availabilityZone>default</availabilityZone>
src/cloud/occi/lib/occi-server.rb
81 81

  
82 82
# Set the host and port
83 83
set :bind, $occi_app.conf[:host]
84

  
85 84
set :port, $occi_app.conf[:port]
86 85

  
87 86
set :run, false
src/cloud/occi/lib/occi_application.rb
17 17
require 'CloudServer'
18 18

  
19 19
class OCCIApplication
20

  
21
#    include CloudLogger
22

  
23 20
    #Configuration constants
24 21
    OCCI_AUTH  = VAR_LOCATION + "/.one/occi_auth"
25 22
    OCCI_LOG   = LOG_LOCATION + "/occi-server.log"

Also available in: Unified diff