mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-18 10:53:13 +09:00
Merge branch 'feature-1356'
Conflicts: src/cli/command_parser.rb
This commit is contained in:
commit
51d032b35d
5 files changed
+214
-13
No files matched your search
@@ -37,7 +37,7 @@ Usage:
|
||||
Commands:
|
||||
|
||||
* create <occi xml file>
|
||||
creates a new compute resource described by the provided
|
||||
creates a new compute resource described by the provided
|
||||
<occi xml file>
|
||||
|
||||
* list
|
||||
@@ -54,6 +54,11 @@ Commands:
|
||||
* delete <compute id>
|
||||
deletes the compute resource idenfitied by <compute id>
|
||||
|
||||
* attachdisk <compute id> <storage id>
|
||||
attaches an storage to a given compute
|
||||
|
||||
* detachdisk <compute id> <disk id>
|
||||
detaches the target disk form the compute
|
||||
|
||||
Options:
|
||||
|
||||
@@ -133,7 +138,7 @@ begin
|
||||
end
|
||||
rescue Exception => e
|
||||
exit(-1)
|
||||
end
|
||||
end
|
||||
|
||||
begin
|
||||
occi_client = OCCIClient::Client.new(url,username,password, timeout, debug, plain)
|
||||
@@ -146,7 +151,7 @@ if !ARGV[0]
|
||||
puts "#{cmd_name}: [COMMAND] not present"
|
||||
puts "#{cmd_name}: Execute #{cmd_name} -h for help."
|
||||
exit(-1)
|
||||
end
|
||||
end
|
||||
|
||||
case ARGV[0].downcase
|
||||
when 'list'
|
||||
@@ -165,13 +170,13 @@ case ARGV[0].downcase
|
||||
when 'show'
|
||||
vm_id = ARGV[1]
|
||||
|
||||
if !vm_id
|
||||
if !vm_id
|
||||
puts "#{cmd_name} show: missing VM-ID parameter"
|
||||
exit(-1)
|
||||
end
|
||||
|
||||
rc = occi_client.get_vm(vm_id)
|
||||
|
||||
|
||||
when 'update'
|
||||
vm_xml = ARGV[1]
|
||||
|
||||
@@ -181,17 +186,53 @@ case ARGV[0].downcase
|
||||
end
|
||||
|
||||
rc = occi_client.put_vm(vm_xml)
|
||||
|
||||
|
||||
when 'delete'
|
||||
vm_id = ARGV[1]
|
||||
|
||||
if !vm_id
|
||||
if !vm_id
|
||||
puts "#{cmd_name} delete: missing VM-ID parameter"
|
||||
exit -1
|
||||
end
|
||||
|
||||
rc = occi_client.delete_vm(vm_id)
|
||||
|
||||
|
||||
when 'attachdisk'
|
||||
vm_id = ARGV[1]
|
||||
image_id = ARGV[2]
|
||||
|
||||
if !image_id
|
||||
puts "#{cmd_name} show: missing storage id parameter"
|
||||
exit(-1)
|
||||
end
|
||||
|
||||
xml = "<ACTION>"
|
||||
xml << " <PERFORM>ATTACHDISK</PERFORM>"
|
||||
xml << " <PARAMS>"
|
||||
xml << " <STORAGE href=\"#{url}/storage/#{image_id.to_s}\"/>"
|
||||
xml << " </PARAMS>"
|
||||
xml << "</ACTION>"
|
||||
|
||||
rc = occi_client.action_vm(vm_id, xml)
|
||||
|
||||
when 'detachdisk'
|
||||
vm_id = ARGV[1]
|
||||
disk_id = ARGV[2]
|
||||
|
||||
if !disk_id
|
||||
puts "#{cmd_name} show: missing disk id parameter"
|
||||
exit(-1)
|
||||
end
|
||||
|
||||
xml = "<ACTION>"
|
||||
xml << " <PERFORM>DETACHDISK</PERFORM>"
|
||||
xml << " <PARAMS>"
|
||||
xml << " <DISK id=\"#{disk_id}\"/>"
|
||||
xml << " </PARAMS>"
|
||||
xml << "</ACTION>"
|
||||
|
||||
rc = occi_client.action_vm(vm_id, xml)
|
||||
|
||||
else
|
||||
puts "Command #{ARGV[0]} not valid."
|
||||
exit(-1)
|
||||
@@ -200,7 +241,7 @@ end
|
||||
if CloudClient::is_error?(rc)
|
||||
puts rc.to_s()
|
||||
exit(-1)
|
||||
else
|
||||
else
|
||||
str, code = print_xml(rc)
|
||||
puts str
|
||||
exit(code)
|
||||
|
||||
@@ -51,10 +51,14 @@ Commands:
|
||||
* 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>
|
||||
|
||||
* clone <storage id> <name>
|
||||
clones the storage resource idenfitied by <storage id> in a new one
|
||||
using the given <name>
|
||||
|
||||
|
||||
Options:
|
||||
|
||||
@@ -205,6 +209,28 @@ case ARGV[0].downcase
|
||||
|
||||
rc = occi_client.delete_image(image_id)
|
||||
|
||||
when 'clone'
|
||||
image_id = ARGV[1]
|
||||
name = ARGV[2]
|
||||
|
||||
if !image_id
|
||||
puts "#{cmd_name} clone: missing storage id parameter"
|
||||
exit(-1)
|
||||
end
|
||||
|
||||
if !name
|
||||
puts "#{cmd_name} clone: missing name parameter"
|
||||
exit(-1)
|
||||
end
|
||||
|
||||
xml = "<ACTION>"
|
||||
xml << " <PERFORM>CLONE</PERFORM>"
|
||||
xml << " <PARAMS>"
|
||||
xml << " <NAME>#{name}</NAME>"
|
||||
xml << " </PARAMS>"
|
||||
xml << "</ACTION>"
|
||||
|
||||
rc = occi_client.action_image(image_id, xml)
|
||||
else
|
||||
puts "Command #{ARGV[0]} not valid."
|
||||
exit(-1)
|
||||
@@ -213,7 +239,7 @@ end
|
||||
if CloudClient::is_error?(rc)
|
||||
puts rc.to_s()
|
||||
exit(-1)
|
||||
else
|
||||
else
|
||||
str, code = print_xml(rc)
|
||||
puts str
|
||||
exit(code)
|
||||
|
||||
@@ -233,6 +233,10 @@ module OCCIClient
|
||||
delete('/compute/'+id.to_s)
|
||||
end
|
||||
|
||||
def action_vm(id, xml)
|
||||
post('/compute/'+id.to_s+'/action', xml, false)
|
||||
end
|
||||
|
||||
######################################################################
|
||||
# Retrieves a Virtual Network
|
||||
# :id Virtual Network identifier
|
||||
@@ -279,6 +283,10 @@ module OCCIClient
|
||||
delete('/storage/'+id.to_s)
|
||||
end
|
||||
|
||||
def action_image(id, xml)
|
||||
post('/storage/'+id.to_s+'/action', xml, false)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get(path, verbose=false)
|
||||
@@ -289,11 +297,15 @@ module OCCIClient
|
||||
do_request(url, req)
|
||||
end
|
||||
|
||||
def post(path, xmlfile)
|
||||
def post(path, xmlfile, file=true)
|
||||
url = URI.parse(@endpoint+path)
|
||||
req = Net::HTTP::Post.new(url.path)
|
||||
|
||||
req.body=File.read(xmlfile)
|
||||
if file
|
||||
req.body=File.read(xmlfile)
|
||||
else
|
||||
req.body=xmlfile
|
||||
end
|
||||
|
||||
do_request(url, req)
|
||||
end
|
||||
|
||||
@@ -298,6 +298,73 @@ class OCCIServer < CloudServer
|
||||
return "", 204
|
||||
end
|
||||
|
||||
# Attach a disk to an existing COMPUTE
|
||||
# @param [Hash] request hash containing the data of the request
|
||||
# @param [Hash] params hash containing the params of the request
|
||||
# @param [OpenNebula::XMLElement] action_xml contains the body of the request
|
||||
# @return [[String, OpenNebula:Error], Integer]
|
||||
def attach_disk(request, params, action_xml)
|
||||
# --- Get the VM ---
|
||||
vm = VirtualMachineOCCI.new(
|
||||
VirtualMachine.build_xml(params[:id]),
|
||||
@client)
|
||||
|
||||
image_href = action_xml.attr('PARAMS/STORAGE','href')
|
||||
if !image_href
|
||||
error = OpenNebula::Error.new("You have to specify an STORAGE " <<
|
||||
"to be attached")
|
||||
return error, CloudServer::HTTP_ERROR_CODE[error.errno]
|
||||
end
|
||||
|
||||
image_id = image_href.split('/').last
|
||||
target = action_xml['PARAMS/TARGET']
|
||||
|
||||
if target
|
||||
template = "DISK = [ IMAGE_ID = #{image_id}, TARGET = #{target} ]"
|
||||
else
|
||||
template = "DISK = [ IMAGE_ID = #{image_id}, DEV_PREFIX = sd ]"
|
||||
end
|
||||
|
||||
# --- Attach DISK ---
|
||||
result = vm.attachdisk(template)
|
||||
if OpenNebula.is_error?(result)
|
||||
return result, CloudServer::HTTP_ERROR_CODE[result.errno]
|
||||
end
|
||||
|
||||
vm.info
|
||||
|
||||
return to_occi_xml(vm, :code=>202)
|
||||
end
|
||||
|
||||
# Detach a DISK from an existing COMOUTE
|
||||
# @param [Hash] request hash containing the data of the request
|
||||
# @param [Hash] params hash containing the params of the request
|
||||
# @param [OpenNebula::XMLElement] action_xml contains the body of the request
|
||||
# @return [[String, OpenNebula:Error], Integer]
|
||||
def detach_disk(request, params, action_xml)
|
||||
# --- Get the VM ---
|
||||
vm = VirtualMachineOCCI.new(
|
||||
VirtualMachine.build_xml(params[:id]),
|
||||
@client)
|
||||
|
||||
disk_id = action_xml.attr('PARAMS/DISK','id')
|
||||
if !disk_id
|
||||
error = OpenNebula::Error.new("You have to specify a DISK " <<
|
||||
"to be detached")
|
||||
return error, CloudServer::HTTP_ERROR_CODE[error.errno]
|
||||
end
|
||||
|
||||
# --- Detach DISK ---
|
||||
result = vm.detachdisk(disk_id.to_i)
|
||||
if OpenNebula.is_error?(result)
|
||||
return result, CloudServer::HTTP_ERROR_CODE[result.errno]
|
||||
end
|
||||
|
||||
vm.info
|
||||
|
||||
return to_occi_xml(vm, :code=>202)
|
||||
end
|
||||
|
||||
# Updates a COMPUTE resource
|
||||
# request:: _Hash_ hash containing the data of the request
|
||||
# [return] _String_,_Integer_ Update confirmation msg or error,
|
||||
@@ -481,6 +548,31 @@ class OCCIServer < CloudServer
|
||||
return to_occi_xml(image, :code=>200)
|
||||
end
|
||||
|
||||
# Clone a STORAGE resource
|
||||
# request:: _Hash_ hash containing the data of the request
|
||||
# [return] _String_,_Integer_ STORAGE occi representation or error,
|
||||
# status code
|
||||
def clone_storage(request, params, action_xml)
|
||||
# --- Get the Image ---
|
||||
image = ImageOCCI.new(
|
||||
Image.build_xml(params[:id]),
|
||||
@client)
|
||||
|
||||
rc = image.clone(action_xml["PARAMS/NAME"])
|
||||
if OpenNebula.is_error?(rc)
|
||||
return rc, CloudServer::HTTP_ERROR_CODE[rc.errno]
|
||||
end
|
||||
|
||||
new_image = ImageOCCI.new(
|
||||
Image.build_xml(rc),
|
||||
@client)
|
||||
|
||||
new_image.info
|
||||
|
||||
# --- Prepare XML Response ---
|
||||
return to_occi_xml(new_image, :code=>201)
|
||||
end
|
||||
|
||||
# Deletes a STORAGE resource (Not yet implemented)
|
||||
# request:: _Hash_ hash containing the data of the request
|
||||
# [return] _String_,_Integer_ Delete confirmation msg or error,
|
||||
|
||||
@@ -315,6 +315,22 @@ get '/compute/:id' do
|
||||
treat_response(result,rc)
|
||||
end
|
||||
|
||||
post '/compute/:id/action' do
|
||||
xml = XMLElement.new
|
||||
xml.initialize_xml(request.body, "ACTION")
|
||||
|
||||
result,rc = case xml['PERFORM'].downcase
|
||||
when 'attachdisk' then
|
||||
@occi_server.attach_disk(request, params, xml)
|
||||
when 'detachdisk' then
|
||||
@occi_server.detach_disk(request, params, xml)
|
||||
else
|
||||
halt 403, "Action #{xml['PERFORM']} not supported"
|
||||
end
|
||||
|
||||
treat_response(result,rc)
|
||||
end
|
||||
|
||||
delete '/compute/:id' do
|
||||
result,rc = @occi_server.delete_compute(request, params)
|
||||
treat_response(result,rc)
|
||||
@@ -345,6 +361,20 @@ get '/storage/:id' do
|
||||
treat_response(result,rc)
|
||||
end
|
||||
|
||||
post '/storage/:id/action' do
|
||||
xml = XMLElement.new
|
||||
xml.initialize_xml(request.body, "ACTION")
|
||||
|
||||
result, rc = case xml['PERFORM'].downcase
|
||||
when 'clone' then
|
||||
@occi_server.clone_storage(request, params, xml)
|
||||
else
|
||||
halt 403, "Action #{xml['PERFORM']} not supported"
|
||||
end
|
||||
|
||||
treat_response(result,rc)
|
||||
end
|
||||
|
||||
delete '/storage/:id' do
|
||||
result,rc = @occi_server.delete_storage(request, params)
|
||||
treat_response(result,rc)
|
||||
|
||||
Reference in new issue
Block a user