diff --git a/src/cloud/occi/OCCIServer.rb b/src/cloud/occi/OCCIServer.rb old mode 100644 new mode 100755 index 575a70df09..227a893667 --- a/src/cloud/occi/OCCIServer.rb +++ b/src/cloud/occi/OCCIServer.rb @@ -144,15 +144,15 @@ end ################################################### -def submit_vm(params) - - if params['occixml'] - @vm_info=Crack::XML.parse(params['occixml']) +def submit_vm(request) + + if request.body + @vm_info=Crack::XML.parse(request.body.read) else halt 400, "OCCI XML representation of VM not present" end - @vm_info=@vm_info['COMPUTE'] + @vm_info=@vm_info['COMPUTE'] if @vm_info['STORAGE'].class==Array disks=@vm_info['STORAGE'] @@ -185,7 +185,7 @@ def submit_vm(params) @vm_info['NETWORK']['NIC']=nics - instance_type_name=params['InstanceType'] + instance_type_name=@vm_info['INSTANCE_TYPE'] instance_type=INSTANCE_TYPES[instance_type_name] halt 400, "Bad instance type" if !instance_type @@ -209,9 +209,9 @@ def submit_vm(params) end end -def change_state(params) - if params['occixml'] - vm_info=Crack::XML.parse(params['occixml']) +def change_state(request) + if request.body + vm_info=Crack::XML.parse(request.body.read) else halt 400, "OCCI XML representation of VM not present" end @@ -252,16 +252,25 @@ end post '/compute' do # Auth check protected! - - submit_vm(params) + + submit_vm(request) end get '/compute' do # Auth check protected! # Info retrieval - vmpool = VirtualMachinePoolOCCI.new(get_one_client) + user = get_user(@auth.credentials.first) + + if user[:id] == 0 + user_flag=-2 + else + user_flag=-1 + end + + vmpool = VirtualMachinePoolOCCI.new(get_one_client,user_flag) vmpool.info + # OCCI conversion begin vmpool.to_occi(CONFIG[:server]+":"+CONFIG[:port]) @@ -275,8 +284,8 @@ post '/network' do # Auth check protected! # Info retrieval from post params - if params - network_info=Crack::XML.parse(params.to_s) + if request.body + network_info=Crack::XML.parse(request.body.read) else halt 400, "OCCI XML representation of Virtual Network not present in the request" end @@ -388,10 +397,10 @@ delete '/compute/:id' do "The Compute resource has been successfully deleted" end -post '/compute/:id' do +put '/compute/:id' do protected! - change_state(params) + change_state(request) end get '/network/:id' do diff --git a/src/cloud/occi/commands/occi-compute b/src/cloud/occi/commands/occi-compute index 83e57be60e..b8885d4b40 100755 --- a/src/cloud/occi/commands/occi-compute +++ b/src/cloud/occi/commands/occi-compute @@ -47,8 +47,6 @@ # --debug, -D # Enables verbosity # -# --instance-type, -T -# Specifies the type of compute resource we want (compulsory for create) # # -------------------------------------------------------------------------- # @@ -91,7 +89,6 @@ opts = GetoptLong.new( ['--username', '-U',GetoptLong::REQUIRED_ARGUMENT], ['--password', '-P',GetoptLong::REQUIRED_ARGUMENT], ['--url', '-R',GetoptLong::REQUIRED_ARGUMENT], - ['--instance-type','-T',GetoptLong::REQUIRED_ARGUMENT], ['--debug', '-D',GetoptLong::NO_ARGUMENT] ) @@ -99,7 +96,6 @@ url = nil username = nil password = nil auth = nil -type = nil debug = false begin @@ -113,8 +109,6 @@ begin password = arg when '--url' url = arg - when '--instance-type' - type = arg when '--debug' debug = true end @@ -141,11 +135,6 @@ when 'list' occi_client.get_vms when 'create' - if !type - puts "#{$0}: missing compulsory instance type" - exit -1 - end - vm_xml = ARGV[1] if !vm_xml || !File.exists?(vm_xml) @@ -160,7 +149,7 @@ when 'create' exit -1 end - occi_client.post_vms(type, vm_xml) + occi_client.post_vms(vm_xml) when 'show' vm_id = ARGV[1] diff --git a/src/cloud/occi/commands/occi-network b/src/cloud/occi/commands/occi-network index c1b888c5e2..dce98b2206 100755 --- a/src/cloud/occi/commands/occi-network +++ b/src/cloud/occi/commands/occi-network @@ -26,6 +26,7 @@ # deletes the virtual network idenfitied by # # +# # OPTIONS # # -h, --help: @@ -43,6 +44,9 @@ # --debug, -D # Enables verbosity # +# --multipart, -M: +# Use 'multipart-post' library instead of Curb/Curl +# # -------------------------------------------------------------------------- # # Copyright 2002-2009, Distributed Systems Architecture Group, Universidad # diff --git a/src/cloud/occi/commands/occi-storage b/src/cloud/occi/commands/occi-storage index 782c05a123..30f907c055 100755 --- a/src/cloud/occi/commands/occi-storage +++ b/src/cloud/occi/commands/occi-storage @@ -22,10 +22,6 @@ # retrieves the OCCI XML representation of the storage resource # identified by # -# update -# updates the representation of the storage resource represented by the -# provided -# # delete # deletes the storage resource idenfitied by # @@ -46,6 +42,9 @@ # --debug, -D # Enables verbosity # +# --multipart, -M: +# Use 'multipart-post' library instead of Curb/Curl +# # -------------------------------------------------------------------------- # # Copyright 2002-2009, Distributed Systems Architecture Group, Universidad # @@ -88,7 +87,8 @@ opts = GetoptLong.new( ['--username', '-U',GetoptLong::REQUIRED_ARGUMENT], ['--password', '-P',GetoptLong::REQUIRED_ARGUMENT], ['--url', '-R',GetoptLong::REQUIRED_ARGUMENT], - ['--debug', '-D',GetoptLong::NO_ARGUMENT] + ['--debug', '-D',GetoptLong::NO_ARGUMENT], + ['--multipart', '-M',GetoptLong::NO_ARGUMENT] ) url = nil @@ -96,6 +96,7 @@ username = nil password = nil auth = nil debug = false +curb = true begin opts.each do |opt, arg| @@ -110,12 +111,20 @@ begin url = arg when '--debug' debug = true + when '--multipart' + curb = false end end rescue Exception => e exit -1 end +if !ARGV[0] + puts "#{$0}: [COMMAND] not present" + puts "#{$0}: Execute #{$0} -h for help." + exit -1 +end + begin occi_client = ONEOCCIClient::Client.new(url,username,password,debug) @@ -133,7 +142,7 @@ when 'create' exit -1 end - occi_client.post_image(image_xml) + occi_client.post_image(image_xml, curb) when 'list' occi_client.get_images diff --git a/src/cloud/occi/lib/ONEOCCIClient.rb b/src/cloud/occi/lib/ONEOCCIClient.rb old mode 100644 new mode 100755 index 21405a2cdd..f136f90bbf --- a/src/cloud/occi/lib/ONEOCCIClient.rb +++ b/src/cloud/occi/lib/ONEOCCIClient.rb @@ -1,22 +1,34 @@ #!/usr/bin/ruby require 'rubygems' -require 'curb' require 'uri' require 'OpenNebula' require 'crack' +begin + require 'curb' + CURL_LOADED=true +rescue LoadError + CURL_LOADED=false +end + +begin + require 'net/http/post/multipart' +rescue LoadError +end + + + module ONEOCCIClient + ##################################################################### - # - # + # Client Library to interface with the OpenNebula OCCI Service ##################################################################### class Client - ####################################################################### - # - # Initialized client library - # ####################################################################### + ###################################################################### + # Initialize client library + ###################################################################### def initialize(endpoint_str=nil, user=nil, pass=nil, debug_flag=false) @debug = debug_flag @@ -44,103 +56,96 @@ module ONEOCCIClient end - ####################################################################### - # Pool Resource Request Methods - ####################################################################### + ################################# + # Pool Resource Request Methods # + ################################# - ####################################################################### + ###################################################################### # Post a new VM to the VM Pool # :instance_type # :xmlfile - ####################################################################### - def post_vms(instance_type,xmlfile) - curl=Curl::Easy.new(@endpoint+"/compute") - curl.userpwd=@occiauth - curl.verbose=true if @debug - + ###################################################################### + def post_vms(xmlfile) xml=File.read(xmlfile) - - begin - curl.http_post( - Curl::PostField.content('occixml', xml), - Curl::PostField.content('InstanceType', instance_type) - ) - rescue Exception => e - error = OpenNebula::Error.new(e.message) - return error - end - - puts curl.body_str - end + + url = URI.parse(@endpoint+"/compute") + + req = Net::HTTP::Post.new(url.path) + req.body=xml - ####################################################################### - # Retieves the pool of Virtual Machines - ####################################################################### - def get_vms - curl=Curl::Easy.new(@endpoint+"/compute") - curl.userpwd=@occiauth - curl.verbose=true if @debug - - begin - curl.http_get - rescue Exception => e - error = OpenNebula::Error.new(e.message) - return error + auth=@occiauth.split(":") + req.basic_auth auth[0], auth[1] + + res = Net::HTTP.start(url.host, url.port) do |http| + http.request(req) end - puts curl.body_str + + pp res.body end - ####################################################################### + ###################################################################### + # Retieves the pool of Virtual Machines + ###################################################################### + def get_vms + url = URI.parse(@endpoint+"/compute") + req = Net::HTTP::Get.new(url.path) + + auth=@occiauth.split(":") + req.basic_auth auth[0], auth[1] + + res = Net::HTTP.start(url.host, url.port) {|http| + http.request(req) + } + puts res.body + end + + ###################################################################### # Post a new Network to the VN Pool # :xmlfile xml description of the Virtual Network - ####################################################################### + ###################################################################### def post_network(xmlfile) - curl=Curl::Easy.new(@endpoint+"/network") - curl.userpwd=@occiauth - curl.verbose=true if @debug - xml=File.read(xmlfile) - begin - curl.http_post(xml) - rescue Exception => e - error = OpenNebula::Error.new(e.message) - return error - end + url = URI.parse(@endpoint+"/network") - puts curl.body_str - end + req = Net::HTTP::Post.new(url.path) + req.body=xml - ####################################################################### - # Retieves the pool of Virtual Networks - ####################################################################### - def get_networks - curl=Curl::Easy.new(@endpoint+"/network") - curl.userpwd=@occiauth - curl.verbose=true if @debug + auth=@occiauth.split(":") - begin - curl.http_get - rescue Exception => e - error = OpenNebula::Error.new(e.message) - return error + req.basic_auth auth[0], auth[1] + + res = Net::HTTP.start(url.host, url.port) do |http| + http.request(req) end - puts curl.body_str + + puts res.body end - ####################################################################### + ###################################################################### + # Retieves the pool of Virtual Networks + ###################################################################### + def get_networks + url = URI.parse(@endpoint+"/network") + req = Net::HTTP::Get.new(url.path) + + auth=@occiauth.split(":") + req.basic_auth auth[0], auth[1] + + res = Net::HTTP.start(url.host, url.port) {|http| + http.request(req) + } + puts res.body + end + + ###################################################################### # Post a new Image to the Image Pool # :xmlfile - ####################################################################### - def post_image(xmlfile) + ###################################################################### + def post_image(xmlfile, curb=true) xml=File.read(xmlfile) image_info=Crack::XML.parse(xml) - - curl=Curl::Easy.new(@endpoint+"/storage") - curl.userpwd=@occiauth - curl.verbose=true if @debug - curl.multipart_form_post = true - + file_path = image_info['DISK']['URL'] m=file_path.match(/^\w+:\/\/(.*)$/) @@ -148,148 +153,172 @@ module ONEOCCIClient if m file_path="/"+m[1] end - - begin - curl.http_post( - Curl::PostField.content('occixml', xml), - Curl::PostField.file('file', file_path) - ) - rescue Exception => e - error = OpenNebula::Error.new(e.message) - return error - end - puts curl.body_str + if curb and CURL_LOADED + curl=Curl::Easy.new(@endpoint+"/storage") + curl.userpwd=@occiauth + curl.verbose=true if @debug + curl.multipart_form_post = true + + begin + curl.http_post( + Curl::PostField.content('occixml', xml), + Curl::PostField.file('file', file_path) + ) + rescue Exception => e + pp e.message + end + + puts curl.body_str + else + file=File.open(file_path) + + params=Hash.new + params["file"]=UploadIO.new(file, + 'application/octet-stream', file_path) + + params['occixml'] = xml + + url = URI.parse(@endpoint+"/storage") + + req = Net::HTTP::Post::Multipart.new(url.path, params) + + auth=@occiauth.split(":") + + req.basic_auth auth[0], auth[1] + + res = Net::HTTP.start(url.host, url.port) do |http| + http.request(req) + end + file.close + + puts res.body + end end - ####################################################################### + ###################################################################### # Retieves the pool of Images owned by the user - ####################################################################### + ###################################################################### def get_images - curl=Curl::Easy.new(@endpoint+"/storage") - curl.userpwd=@occiauth - curl.verbose=true if @debug + url = URI.parse(@endpoint+"/storage") + req = Net::HTTP::Get.new(url.path) - begin - curl.http_get - rescue Exception => e - error = OpenNebula::Error.new(e.message) - return error - end - puts curl.body_str + auth=@occiauth.split(":") + req.basic_auth auth[0], auth[1] + + res = Net::HTTP.start(url.host, url.port) {|http| + http.request(req) + } + puts res.body end - ####################################################################### - # Entity Resource Request Methods - ####################################################################### + #################################### + # Entity Resource Request Methods # + #################################### - ####################################################################### + ###################################################################### # :id VM identifier - ####################################################################### + ###################################################################### def get_vm(id) - curl=Curl::Easy.new(@endpoint+"/compute/" + id.to_s) - curl.userpwd=@occiauth - curl.verbose=true if @debug - - begin - curl.http_get - rescue Exception => e - error = OpenNebula::Error.new(e.message) - return error - end - puts curl.body_str + url = URI.parse(@endpoint+"/compute/" + id.to_s) + req = Net::HTTP::Get.new(url.path) + + auth=@occiauth.split(":") + req.basic_auth auth[0], auth[1] + + res = Net::HTTP.start(url.host, url.port) {|http| + http.request(req) + } + puts res.body end - ####################################################################### + ###################################################################### # Puts a new Compute representation in order to change its state # :xmlfile Compute OCCI xml representation - ####################################################################### + ###################################################################### def put_vm(xmlfile) xml=File.read(xmlfile) vm_info=Crack::XML.parse(xml) - - curl=Curl::Easy.new(@endpoint+"/compute/"+vm_info['ID']) - curl.userpwd=@occiauth - curl.verbose=true if @debug - - begin - curl.http_post(Curl::PostField.content('occixml', xml)) - rescue Exception => e - error = OpenNebula::Error.new(e.message) - return error - end + + url = URI.parse(@endpoint+'/compute/' + vm_info['COMPUTE']['ID']) - puts curl.body_str - end + req = Net::HTTP::Put.new(url.path) + req.body = xml - ####################################################################### - # :id Compute identifier - ####################################################################### - def delete_vm(id) - curl=Curl::Easy.new(@endpoint+"/compute/" + id.to_s) - curl.userpwd=@occiauth - curl.verbose=true if @debug - - begin - curl.http_delete - rescue Exception => e - error = OpenNebula::Error.new(e.message) - return error + auth=@occiauth.split(":") + req.basic_auth auth[0], auth[1] + + res = Net::HTTP.start(url.host, url.port) do |http| + http.request(req) end - puts curl.body_str + + pp res.body end - ####################################################################### + #################################################################### + # :id Compute identifier + #################################################################### + def delete_vm(id) + url = URI.parse(@endpoint+"/compute/" + id.to_s) + req = Net::HTTP::Delete.new(url.path) + + auth=@occiauth.split(":") + req.basic_auth auth[0], auth[1] + + res = Net::HTTP.start(url.host, url.port) {|http| + http.request(req) + } + puts res.body + end + + ###################################################################### # Retrieves a Virtual Network # :id Virtual Network identifier - ####################################################################### + ###################################################################### def get_network(id) - curl=Curl::Easy.new(@endpoint+"/network/" + id.to_s) - curl.userpwd=@occiauth - curl.verbose=true if @debug + url = URI.parse(@endpoint+"/network/" + id.to_s) + req = Net::HTTP::Get.new(url.path) - begin - curl.http_get - rescue Exception => e - error = OpenNebula::Error.new(e.message) - return error - end - puts curl.body_str + auth=@occiauth.split(":") + req.basic_auth auth[0], auth[1] + + res = Net::HTTP.start(url.host, url.port) {|http| + http.request(req) + } + puts res.body end - ####################################################################### + ###################################################################### # :id VM identifier - ####################################################################### + ###################################################################### def delete_network(id) - curl=Curl::Easy.new(@endpoint+"/network/" + id.to_s) - curl.userpwd=@occiauth - curl.verbose=true if @debug - - begin - curl.http_delete - rescue Exception => e - error = OpenNebula::Error.new(e.message) - return error - end - puts curl.body_str + url = URI.parse(@endpoint+"/network/" + id.to_s) + req = Net::HTTP::Delete.new(url.path) + + auth=@occiauth.split(":") + req.basic_auth auth[0], auth[1] + + res = Net::HTTP.start(url.host, url.port) {|http| + http.request(req) + } + puts res.body end - ####################################################################### + ####################################################################### # Retieves an Image # :image_uuid Image identifier - ####################################################################### + ###################################################################### def get_image(image_uuid) - curl=Curl::Easy.new(@endpoint+"/storage/"+image_uuid) - curl.userpwd=@occiauth - curl.verbose=true if @debug + url = URI.parse(@endpoint+"/storage/"+image_uuid) + req = Net::HTTP::Get.new(url.path) - begin - curl.http_get - rescue Exception => e - error = OpenNebula::Error.new(e.message) - return error - end - puts curl.body_str + auth=@occiauth.split(":") + req.basic_auth auth[0], auth[1] + + res = Net::HTTP.start(url.host, url.port) {|http| + http.request(req) + } + puts res.body end end end diff --git a/src/cloud/occi/lib/VirtualMachinePoolOCCI.rb b/src/cloud/occi/lib/VirtualMachinePoolOCCI.rb old mode 100644 new mode 100755 index 3440073e5c..235aa46eec --- a/src/cloud/occi/lib/VirtualMachinePoolOCCI.rb +++ b/src/cloud/occi/lib/VirtualMachinePoolOCCI.rb @@ -9,7 +9,10 @@ class VirtualMachinePoolOCCI < VirtualMachinePool pool_hash=Crack::XML.parse(to_xml) occi_xml = "" - pool_hash['VM_POOL']['VM'].each{|vm| + vmlist=[pool_hash['VM_POOL']['VM']].flatten + + vmlist.each{|vm| + pp vm occi_xml+='' } diff --git a/src/cloud/occi/templates/large.erb b/src/cloud/occi/templates/large.erb index 44b60c6d9e..be10722282 100644 --- a/src/cloud/occi/templates/large.erb +++ b/src/cloud/occi/templates/large.erb @@ -37,7 +37,7 @@ NIC = [ <% if nic['ip'] %> IP=<%= nic['ip'] %>, <% end %> - NETWORK=<%= nic['network']%>, + NETWORK="<%= nic['network']%>", NETWORK_ID=<%= nic['network_id'] %> ] <% end %> diff --git a/src/cloud/occi/templates/medium.erb b/src/cloud/occi/templates/medium.erb index eb8ba528e1..bb69f8d689 100644 --- a/src/cloud/occi/templates/medium.erb +++ b/src/cloud/occi/templates/medium.erb @@ -37,7 +37,7 @@ NIC = [ <% if nic['ip'] %> IP=<%= nic['ip'] %>, <% end %> - NETWORK=<%= nic['network']%>, + NETWORK="<%= nic['network']%>", NETWORK_ID=<%= nic['network_id'] %> ] <% end %> diff --git a/src/cloud/occi/templates/small.erb b/src/cloud/occi/templates/small.erb index c3157aefe0..6522078c00 100644 --- a/src/cloud/occi/templates/small.erb +++ b/src/cloud/occi/templates/small.erb @@ -37,7 +37,7 @@ NIC = [ <% if nic['ip'] %> IP=<%= nic['ip'] %>, <% end %> - NETWORK=<%= nic['network']%>, + NETWORK="<%= nic['network']%>", NETWORK_ID=<%= nic['network_id'] %> ] <% end %>