1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-25 06:03:36 +03:00

F #4287: service template recursive clone (#4289)

This commit is contained in:
Alejandro Huertas Herrero 2020-03-06 11:54:18 +01:00 committed by GitHub
parent 7505a82c78
commit f3d8bac9c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 109 additions and 7 deletions

View File

@ -61,6 +61,19 @@ CommandParser::CmdParser.new(ARGV) do
:description => 'Instance multiple templates'
}
RECURSIVE = {
:name => 'recursive',
:short => '-r',
:large => '--recursive',
:description => 'Clone the template recursively (templates and images)'
}
RECURSIVE_TEMPLATES = {
:name => 'recursive_templates',
:large => '--recursive-templates',
:description => 'Clone the template recursively (just templates)'
}
usage '`oneflow-template` <command> [<args>] [<options>]'
version OpenNebulaHelper::ONE_VERSION
@ -293,10 +306,22 @@ CommandParser::CmdParser.new(ARGV) do
Creates a new Service Template from an existing one
EOT
command :clone, clone_desc, :templateid, :name do
command :clone,
clone_desc,
:templateid,
:name,
:options => [RECURSIVE, RECURSIVE_TEMPLATES] do
params = {}
params['name'] = args[1]
if options.key?(:recursive)
params['recursive'] = 'all'
elsif options.key?(:recursive_templates)
params['recurisve'] = 'templates'
else
params['recursive'] = 'none'
end
json = Service.build_json_action('clone', params)
client = helper.client(options)

View File

@ -267,6 +267,77 @@ module OpenNebula
super(template.to_json)
end
# Clone service template and the VM templates asssociated to it
#
# @param name [String] New template name
# @param mode [Symbol] Cloning mode (:all, :templates)
#
# @return [Integer] New document ID
def clone_recursively(name, mode)
recursive = mode == 'all'
# clone the document to get new ID
new_id = clone(name)
return new_id if OpenNebula.is_error?(new_id)
doc = OpenNebula::ServiceTemplate.new_with_id(new_id, @client)
rc = doc.info
return rc if OpenNebula.is_error?(rc)
body = JSON.parse(doc["TEMPLATE/#{TEMPLATE_TAG}"])
cloned_templates = {}
# iterate over roles to clone templates
rc = body['roles'].each do |role|
t_id = role['vm_template']
# if the template has already been cloned, just update the value
if cloned_templates.keys.include?(t_id)
role['vm_template'] = cloned_templates[t_id]
next
end
template = OpenNebula::Template.new_with_id(t_id, @client)
rc = template.info
break rc if OpenNebula.is_error?(rc)
rc = template.clone("#{template.name}-#{name}", recursive)
break rc if OpenNebula.is_error?(rc)
# add new ID to the hash
cloned_templates[t_id] = rc
role['vm_template'] = rc
end
# if any error, rollback and delete the left templates
if OpenNebula.is_error?(rc)
cloned_templates.each do |_, value|
template = OpenNebula::Template.new_with_id(value, @client)
rc = template.info
break rc if OpenNebula.is_error?(rc)
rc = template.delete(recursive)
break rc if OpenNebula.is_error?(rc)
end
return rc
end
# update the template with the new body
doc.update(body.to_json)
# return the new document ID
new_id
end
# Replaces the raw template contents
#
# @param template [String] New template contents, in the form KEY = VAL

View File

@ -610,16 +610,22 @@ post '/service_template/:id/action' do
status 204
service_template.rename(opts['name'])
when 'clone'
rc = service_template.clone(opts['name'])
if opts['recursive'] != 'none'
rc = service_template.clone_recursively(opts['name'],
opts['recursive'])
else
rc = service_template.clone(opts['name'])
end
if OpenNebula.is_error?(rc)
error CloudServer::HTTP_ERROR_CODE[rc.errno], rc.message
return internal_error(rc.message, GENERAL_EC)
end
new_stemplate = OpenNebula::ServiceTemplate.new_with_id(rc, @client)
new_stemplate.info
if OpenNebula.is_error?(new_stemplate)
error CloudServer::HTTP_ERROR_CODE[new_stemplate.errno],
new_stemplate.message
rc = new_stemplate.info
if OpenNebula.is_error?(rc)
return internal_error(rc.message, GENERAL_EC)
end
status 201