1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-30 22:50:10 +03:00

Feature #4215: Use new vrouter.instantiate method in cli and sunstone

This commit is contained in:
Carlos Martín 2016-01-18 15:46:26 +01:00
parent e537e105ec
commit e7b53d6bb1
8 changed files with 143 additions and 46 deletions

View File

@ -51,14 +51,6 @@ EOT
"information, such as the SIZE for each DISK"
}
VROUTER={
:name => "vrouter",
:large => "--vrouter vrid",
:format => Integer,
:description => "Creates a VM associated to the given Virtual Router. "+
"The NIC elements defined in the Virtual Router will be used"
}
def self.rname
"VMTEMPLATE"
end

View File

@ -17,6 +17,13 @@
require 'one_helper'
class OneVirtualRouterHelper < OpenNebulaHelper::OneHelper
ALL_TEMPLATE = {
:name => "all",
:large => "--all",
:description => "Show all template data"
}
def self.rname
"VROUTER"
end
@ -180,7 +187,7 @@ class OneVirtualRouterHelper < OpenNebulaHelper::OneHelper
while obj.has_elements?("/VROUTER/TEMPLATE/NIC")
obj.delete_element("/VROUTER/TEMPLATE/NIC")
end
end if !options[:all]
puts

View File

@ -55,8 +55,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
OneTemplateHelper::VM_NAME,
OneTemplateHelper::MULTIPLE,
OneTemplateHelper::USERDATA,
OneVMHelper::HOLD,
OneTemplateHelper::VROUTER,
OneVMHelper::HOLD
]
########################################################################
@ -224,10 +223,6 @@ cmd=CommandParser::CmdParser.new(ARGV) do
extra_template << "\n" << user_inputs
if !options[:vrouter].nil?
extra_template << "\n" << "VROUTER_ID = \"#{options[:vrouter]}\""
end
res = t.instantiate(name, on_hold, extra_template)
if !OpenNebula.is_error?(res)

View File

@ -29,6 +29,7 @@ $: << RUBY_LIB_LOCATION+"/cli"
require 'command_parser'
require 'one_helper/onevrouter_helper'
require 'one_helper/onetemplate_helper'
require 'one_helper/onevm_helper'
cmd=CommandParser::CmdParser.new(ARGV) do
@ -95,6 +96,70 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end
end
instantiate_desc = <<-EOT.unindent
Creates a new VM instance from the given Template. This VM can be
managed with the 'onevm' command.
The NIC elements defined in the Virtual Router will be used. The
source Template can be modified adding or replacing attributes with
the optional file argument, or with the options.
EOT
instantiate_options = [
OneTemplateHelper::VM_NAME,
OneTemplateHelper::MULTIPLE,
OneVMHelper::HOLD
]
command :instantiate, instantiate_desc, :vrouterid, :templateid, [:file, nil],
:options=>instantiate_options+OpenNebulaHelper::TEMPLATE_OPTIONS do
exit_code=0
if args[1] && OpenNebulaHelper.create_template_options_used?(options)
STDERR.puts "You cannot use both template file and template"<<
" creation options."
next -1
end
number = options[:multiple] || 1
user_inputs = nil
helper.perform_action(args[0], options, "instantiated") do |vr|
name = options[:name] || ""
t = OpenNebula::Template.new_with_id(args[1], helper.client)
on_hold = options[:hold] != nil
extra_template = ""
rc = t.info
if OpenNebula.is_error?(rc)
STDERR.puts rc.message
exit(-1)
end
if args[2]
extra_template = File.read(args[2])
else
res = OpenNebulaHelper.create_template(options, t)
if res.first != 0
STDERR.puts res.last
next -1
end
extra_template = res.last
end
user_inputs = OneTemplateHelper.get_user_inputs(t.to_hash) unless user_inputs
extra_template << "\n" << user_inputs
vr.instantiate(number, args[1], name, on_hold, extra_template)
end
end
delete_desc = <<-EOT.unindent
Deletes the given Virtual Router
EOT
@ -177,7 +242,8 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Shows information for the given Virtual Router
EOT
command :show, show_desc, :secgroupid, :options=>OpenNebulaHelper::XML do
command :show, show_desc, :vrouterid,
:options=>[OpenNebulaHelper::XML, OneVMHelper::ALL_TEMPLATE] do
helper.show_resource(args[0],options)
end

View File

@ -25,6 +25,7 @@ module OpenNebula
VIRTUAL_ROUTER_METHODS = {
:allocate => "vrouter.allocate",
:instantiate => "vrouter.instantiate",
:info => "vrouter.info",
:update => "vrouter.update",
:delete => "vrouter.delete",
@ -78,6 +79,26 @@ module OpenNebula
super(VIRTUAL_ROUTER_METHODS[:allocate], description)
end
# Creates VM instances from a VM Template. New VMs will be associated
# to this Virtual Router, and its Virtual Networks
#
# @para n_vms [Integer] Number of VMs to instantiate
# @para template_id [Integer] VM Template id to instantiate
# @param name [String] Name for the VM instances. If it is an empty
# string OpenNebula will set a default name
# @param hold [true,false] false to create the VM in pending state,
# true to create it on hold
# @param template [String] User provided Template to merge with the
# one being instantiated
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def instantiate(n_vms, template_id, name="", hold=false, template="")
return call(VIRTUAL_ROUTER_METHODS[:instantiate], @pe_id,
n_vms.to_i, template_id.to_i, name, hold, template)
end
# Deletes the VirtualRouter
def delete()
super(VIRTUAL_ROUTER_METHODS[:delete])

View File

@ -44,6 +44,7 @@ module OpenNebulaJSON
end
rc = case action_hash['perform']
when "instantiate" then self.instantiate(action_hash['params'])
when "update" then self.update(action_hash['params'])
when "chown" then self.chown(action_hash['params'])
when "chmod" then self.chmod_json(action_hash['params'])
@ -55,6 +56,26 @@ module OpenNebulaJSON
end
end
def instantiate(params=Hash.new)
if params['template']
select_capacity = self['TEMPLATE/SUNSTONE_CAPACITY_SELECT']
if (select_capacity && select_capacity.upcase == "NO")
params['template'].delete("CPU")
params['template'].delete("MEMORY")
end
select_network = self['TEMPLATE/SUNSTONE_NETWORK_SELECT']
if (select_network && select_network.upcase == "NO")
params['template'].delete("NIC")
end
template = template_to_str(params['template'])
super(params['n_vms'], params['template_id'], params['vm_name'], params['hold'], template)
else
super(params['n_vms'], params['template_id'], params['vm_name'], params['hold'])
end
end
def update(params=Hash.new)
super(params['template_raw'])
end

View File

@ -25,6 +25,10 @@ define(function(require) {
"create" : function(params) {
OpenNebulaAction.create(params, RESOURCE);
},
"instantiate" : function(params) {
var action_obj = params.data.extra_param ? params.data.extra_param : {};
OpenNebulaAction.simple_action(params, RESOURCE, "instantiate", action_obj);
},
"del" : function(params) {
OpenNebulaAction.del(params, RESOURCE);
},

View File

@ -163,41 +163,32 @@ define(function(require) {
timeout: true,
success: function (request, response) {
// TODO: close form panel only on instantiate success
Sunstone.resetFormPanel(TAB_ID, FORM_PANEL_ID);
Sunstone.hideFormPanel(TAB_ID);
var extra_msg = "";
if (n_times > 1) {
extra_msg = n_times + " times";
}
Notifier.notifySubmit("Template.instantiate", tmplId, extra_msg);
var extra_info = {
'hold': hold
};
//TODO: add support for vm_name.replace(/%i/gi, i), or remove tooltip
var tmpl = WizardFields.retrieve($(".template_user_inputs", context));
tmpl["VROUTER_ID"] = response.VROUTER.ID;
extra_info['template'] = tmpl;
var extra_info = {
'n_vms': n_times,
'template_id': tmplId,
'vm_name': vm_name,
'hold': hold,
'template': tmpl
};
for (var i = 0; i < n_times; i++) {
extra_info['vm_name'] = vm_name.replace(/%i/gi, i);
OpenNebulaVirtualRouter.instantiate({
data:{
id: response.VROUTER.ID,
extra_param: extra_info
},
timeout: true,
success: function(request, response){
OpenNebulaAction.clear_cache("VM");
OpenNebulaTemplate.instantiate({
data:{
id: tmplId,
extra_param: extra_info
},
timeout: true,
success: function(request, response){
OpenNebulaAction.clear_cache("VM");
},
error: Notifier.onError
});
}
Sunstone.resetFormPanel(TAB_ID, FORM_PANEL_ID);
Sunstone.hideFormPanel(TAB_ID);
},
error: Notifier.onError
});
},
error: function(request, response) {
Sunstone.hideFormPanelLoading(TAB_ID);