OCCI modifications for CentOS

minor bug fixes for ticket #141

git-svn-id: http://svn.opennebula.org/one/trunk@808 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
Constantino Vázquez Blanco committed 2009-09-30 11:28:47 +00:00
1 parent 4c385e763c
commit 8dc2a5fd76
9 files changed
+266 -223

No files matched your search

Regular → Executable
+25 -16
View File
@@ -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
+1 -12
View File
@@ -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]
+4
View File
@@ -26,6 +26,7 @@
# deletes the virtual network idenfitied by <network id>
#
#
#
# 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 #
+15 -6
View File
@@ -22,10 +22,6 @@
# retrieves the OCCI XML representation of the storage resource
# identified by <storage id>
#
# update <occi xml file>
# updates the representation of the storage resource represented by the
# provided <occi xml file>
#
# delete <storage id>
# deletes the storage resource idenfitied by <storage id>
#
@@ -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
+214 -185
View File
@@ -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
+4 -1
View File
@@ -9,7 +9,10 @@ class VirtualMachinePoolOCCI < VirtualMachinePool
pool_hash=Crack::XML.parse(to_xml)
occi_xml = "<COMPUTES>"
pool_hash['VM_POOL']['VM'].each{|vm|
vmlist=[pool_hash['VM_POOL']['VM']].flatten
vmlist.each{|vm|
pp vm
occi_xml+='<COMPUTE id="' + vm['ID'].strip + '"' +
' href="' + base_url + '/compute/' + vm['ID'].strip + '"/>'
}
+1 -1
View File
@@ -37,7 +37,7 @@ NIC = [
<% if nic['ip'] %>
IP=<%= nic['ip'] %>,
<% end %>
NETWORK=<%= nic['network']%>,
NETWORK="<%= nic['network']%>",
NETWORK_ID=<%= nic['network_id'] %>
]
<% end %>
+1 -1
View File
@@ -37,7 +37,7 @@ NIC = [
<% if nic['ip'] %>
IP=<%= nic['ip'] %>,
<% end %>
NETWORK=<%= nic['network']%>,
NETWORK="<%= nic['network']%>",
NETWORK_ID=<%= nic['network_id'] %>
]
<% end %>
+1 -1
View File
@@ -37,7 +37,7 @@ NIC = [
<% if nic['ip'] %>
IP=<%= nic['ip'] %>,
<% end %>
NETWORK=<%= nic['network']%>,
NETWORK="<%= nic['network']%>",
NETWORK_ID=<%= nic['network_id'] %>
]
<% end %>