Revision 9f5a24e9 src/cloud/ec2/lib/EC2QueryServer.rb

View differences:

src/cloud/ec2/lib/EC2QueryServer.rb
19 19
require 'erb'
20 20
require 'time'
21 21
require 'AWS'
22
require 'CloudServer'
23 22
require 'base64'
24 23

  
24
require 'CloudServer'
25

  
26
require 'ImageEC2'
27

  
25 28
###############################################################################
26 29
# The EC2Query Server implements a EC2 compatible server based on the 
27 30
# OpenNebula Engine
......
149 152
    ###########################################################################
150 153

  
151 154
    def upload_image(params)
152
        user  = get_user(params['AWSAccessKeyId'])
155
        user       = get_user(params['AWSAccessKeyId'])
156
        one_client = one_client_user(user)
153 157

  
154
        image   = add_image(user[:id],params["file"][:tempfile])
155
        erb_img_id = image.id
156
	    erb_version = params['Version']
158
        image = ImageEC2.new(Image.build_xml, one_client)
159
        
160
        rc = add_image(image, params['file'])
161
        if OpenNebula.is_error?(rc)
162
            return OpenNebula::Error.new('Unsupported'),400 
163
        end
164
        
165
        erb_version = params['Version']
157 166

  
158 167
        response = ERB.new(File.read(@config[:views]+"/register_image.erb"))
159 168
        return response.result(binding), 200
160 169
    end
161 170
    
162 171
    def register_image(params)
163
        user  = get_user(params['AWSAccessKeyId'])
164
        image = get_image(params['ImageLocation'])
172
        user       = get_user(params['AWSAccessKeyId'])
173
        one_client = one_client_user(user)
174
        
175
        tmp, img=params['ImageLocation'].split('-')
165 176

  
166
        if !image
177
        image = Image.new(Image.build_xml(img.to_i), one_client)
178
        
179
        # Enable the new Image
180
        rc = image.info
181
        if OpenNebula.is_error?(rc)
167 182
            return OpenNebula::Error.new('InvalidAMIID.NotFound'), 400
168
        elsif user[:id] != image[:owner]
169
            return OpenNebula::Error.new('AuthFailure'), 400
170 183
        end
171

  
172
        erb_img_id=image.id
184
        
185
        image.enable
186
        
173 187
	    erb_version = params['Version']
174 188

  
175 189
        response = ERB.new(File.read(@config[:views]+"/register_image.erb"))
......
177 191
    end
178 192

  
179 193
    def describe_images(params)
180
        erb_user    = get_user(params['AWSAccessKeyId'])
181
        erb_images  = Image.filter(:owner => erb_user[:id])
194
        user       = get_user(params['AWSAccessKeyId'])
195
        one_client = one_client_user(user) 
196

  
197
        user_flag=-1
198
        erb_impool = ImagePool.new(one_client, user_flag)
199
        erb_impool.info
200
        
201
        erb_user_name = user[:name]
182 202
	    erb_version = params['Version']
183
       
203
		
184 204
        response = ERB.new(File.read(@config[:views]+"/describe_images.erb"))
185
        
186
        return response.result(binding), 200
205
        a = response.result(binding)
206
        pp a
207
        return a, 200
187 208
    end
188 209

  
189 210
    ###########################################################################
......
191 212
    ###########################################################################
192 213

  
193 214
    def run_instances(params)
194
        # Get the instance type
215
        user       = get_user(params['AWSAccessKeyId'])
216
        one_client = one_client_user(user)
217

  
218
        # Get the instance type and path
195 219
        instance_type_name = params['InstanceType']
196 220
        instance_type      = @instance_types[instance_type_name]
197 221
        
198
        return OpenNebula::Error.new('Unsupported'),400 if !instance_type
199

  
222
        path = get_template_path(params['InstanceType'])
223
        if OpenNebula.is_error?(path)
224
            return OpenNebula::Error.new('Unsupported'),400
225
        end
226
        
200 227
        # Get the image
201 228
	    tmp, img=params['ImageId'].split('-')
202
        image = get_image(img.to_i)
203
        
204
        return OpenNebula::Error.new('InvalidAMIID.NotFound'),400 if !image
205

  
206
        # Get the user
207
        user       = get_user(params['AWSAccessKeyId'])
208
        one_client = one_client_user(user) 
209
        erb_user_name = user[:name]
210 229
   
211 230
        # Build the VM 
212 231
        erb_vm_info=Hash.new
213

  
214
        
215
        erb_vm_info[:img_path]      = image.path
216
        erb_vm_info[:img_id]        = params['ImageId']
232
        erb_vm_info[:img_id]        = img.to_i
233
        erb_vm_info[:ec2_img_id]    = params['ImageId']
217 234
        erb_vm_info[:instance_type] = instance_type_name
218
        erb_vm_info[:template]      = @config[:template_location] + 
219
                                       "/#{instance_type['TEMPLATE']}"
235
        erb_vm_info[:template]      = path
220 236
        erb_vm_info[:user_data]     = params['UserData']
221 237
        
222 238
        template      = ERB.new(File.read(erb_vm_info[:template]))
223 239
        template_text = template.result(binding)
224
 
225
        #Start the VM.
240

  
241
        # Start the VM.
226 242
        vm = VirtualMachine.new(VirtualMachine.build_xml, one_client)
227 243

  
228 244
        rc = vm.allocate(template_text)
245
        if OpenNebula::is_error?(rc)
246
            return OpenNebula::Error.new('Unsupported'),400
247
        end
229 248
        
230
        return OpenNebula::Error.new('Unsupported'),400 if OpenNebula::is_error?(rc)
231

  
232 249
        vm.info
233 250
     
234 251
        erb_vm_info[:vm_id]=vm.id
235 252
        erb_vm_info[:vm]=vm
236

  
253
        erb_vm_info[:user_name]  = user[:name]
254
        
237 255
	    erb_version = params['Version']
238 256
        
239 257
        response = ERB.new(File.read(@config[:views]+"/run_instances.erb"))
......
248 266

  
249 267
        erb_user_name = user[:name]
250 268

  
251
        if user[:id]==0
252
            user_flag=-2
253
        else
254
            user_flag=-1
255
        end
256

  
269
        user_flag=-1
257 270
        erb_vmpool = VirtualMachinePool.new(one_client, user_flag)
258 271
        erb_vmpool.info
259 272

  

Also available in: Unified diff