diff --git a/src/cli/onevcenter b/src/cli/onevcenter
index 211b480702..f492a2e61b 100755
--- a/src/cli/onevcenter
+++ b/src/cli/onevcenter
@@ -277,9 +277,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
"#{rc.message}\n"
end
- host_id = OpenNebulaHelper.rname_to_id(v[:host], "HOST")[1]
-
- rc = one_v.deploy host_id
+ rc = one_v.deploy v[:host_id]
if ::OpenNebula.is_error?(rc)
STDOUT.puts " Error creating Virtual Machine: "\
diff --git a/src/sunstone/public/js/plugins/hosts-tab.js b/src/sunstone/public/js/plugins/hosts-tab.js
index e6fec29434..64b509b44b 100644
--- a/src/sunstone/public/js/plugins/hosts-tab.js
+++ b/src/sunstone/public/js/plugins/hosts-tab.js
@@ -153,6 +153,10 @@ var create_host_tmpl =
\
@@ -1148,6 +1152,100 @@ function fillVCenterTemplates(opts) {
return false;
}
+/*
+ Retrieve the list of running VMs from vCenter and fill the container with them
+
+ opts = {
+ datacenter: "Datacenter Name",
+ cluster: "Cluster Name",
+ container: Jquery div to inject the html,
+ vcenter_user: vCenter Username,
+ vcenter_password: vCenter Password,
+ vcenter_host: vCenter Host
+ }
+ */
+function fillVCenterVMs(opts) {
+ var path = '/vcenter/vms';
+ opts.container.html(generateAdvancedSection({
+ html_id: path,
+ title: tr("Runing VMs"),
+ content: '
'+
+ ''+
+ ''+
+ ''
+ }))
+
+ $('a', opts.container).trigger("click")
+
+ $.ajax({
+ url: path,
+ type: "GET",
+ data: {timeout: false},
+ dataType: "json",
+ headers: {
+ "X_VCENTER_USER": opts.vcenter_user,
+ "X_VCENTER_PASSWORD": opts.vcenter_password,
+ "X_VCENTER_HOST": opts.vcenter_host
+ },
+ success: function(response){
+ $(".content", opts.container).html("");
+
+ $('
' +
+ '
' +
+ '
' + tr("Please select the vCenter running VMs to be imported to OpenNebula.") + '
' +
+ '
' +
+ '
').appendTo($(".content", opts.container))
+
+ $.each(response, function(datacenter_name, vms){
+ $('
' +
+ '
' +
+ '
' +
+ datacenter_name + ' ' + tr("DataCenter") +
+ '
' +
+ '' +
+ '
').appendTo($(".content", opts.container))
+
+ if (vms.length == 0) {
+ $('
' +
+ '
' +
+ '' +
+ '
' +
+ '
').appendTo($(".content", opts.container))
+ } else {
+ $.each(vms, function(id, vm){
+ var trow = $('
' +
+ '
' +
+ '
' +
+ '
'+
+ '
'+
+ '
'+
+ '
').appendTo($(".content", opts.container))
+
+ $(".vm_name", trow).data("vm_name", vm.name)
+ $(".vm_name", trow).data("one_vm", vm.one)
+ $(".vm_name", trow).data("vm_to_host", vm.host_id)
+ });
+ };
+ });
+ },
+ error: function(response){
+ opts.container.html("");
+ onError({}, OpenNebula.Error(response));
+ }
+ });
+
+ return false;
+}
+
/*
Retrieve the list of networks from vCenter and fill the container with them
@@ -1442,6 +1540,7 @@ function setupCreateHostDialog(){
});
var templates_container = $(".vcenter_templates", $create_host_dialog);
+ var vms_container = $(".vcenter_vms", $create_host_dialog);
var networks_container = $(".vcenter_networks", $create_host_dialog);
var vcenter_user = $("#vcenter_user", $create_host_dialog).val();
@@ -1455,6 +1554,13 @@ function setupCreateHostDialog(){
vcenter_host: vcenter_host
});
+ fillVCenterVMs({
+ container: vms_container,
+ vcenter_user: vcenter_user,
+ vcenter_password: vcenter_password,
+ vcenter_host: vcenter_host
+ });
+
fillVCenterNetworks({
container: networks_container,
vcenter_user: vcenter_user,
@@ -1575,6 +1681,60 @@ function setupCreateHostDialog(){
});
})
+ $.each($(".vm_name:checked", $create_host_dialog), function(){
+ var vm_context = $(this).closest(".vcenter_vm");
+
+ $(".vcenter_vm_result:not(.success)", vm_context).html(
+ '
'+
+ ''+
+ ''+
+ '');
+
+ var vm_json = {
+ "vm": {
+ "vm_raw": $(this).data("one_vm")
+ }
+ };
+
+ var host_id_to_deploy = $(this).data("vm_to_host");
+
+ OpenNebula.VM.create({
+ timeout: true,
+ data: vm_json,
+ success: function(request, response) {
+ OpenNebula.Helper.clear_cache("VM");
+
+ var extra_info = {};
+
+ extra_info['host_id'] = host_id_to_deploy;
+ extra_info['ds_id'] = -1;
+ extra_info['enforce'] = true;
+
+ Sunstone.runAction("VM.deploy_action", response.VM.ID, extra_info);
+
+ $(".vcenter_vm_result", vm_context).addClass("success").html(
+ '
'+
+ ''+
+ ''+
+ '');
+
+ $(".vcenter_vm_response", vm_context).html('
'+
+ tr("VM imported successfully")+' ID:'+response.VM.ID+
+ '
');
+ },
+ error: function (request, error_json){
+ $(".vcenter_vm_response", vm_context).html('
'+
+ ''+
+ ''+
+ '');
+
+ $(".vcenter_vm_response", vm_context).html('
'+
+ (error_json.error.message || tr("Cannot contact server: is it running and reachable?"))+
+ '
');
+ }
+ });
+ })
+
$.each($(".network_name:checked", $create_host_dialog), function(){
var network_context = $(this).closest(".vcenter_network");
diff --git a/src/sunstone/routes/vcenter.rb b/src/sunstone/routes/vcenter.rb
index 5718dbf2b2..bb60cd7bef 100644
--- a/src/sunstone/routes/vcenter.rb
+++ b/src/sunstone/routes/vcenter.rb
@@ -87,6 +87,26 @@ get '/vcenter/templates' do
end
end
+
+get '/vcenter/vms' do
+ begin
+ vms = vcenter_client.running_vms(
+ $cloud_auth.client(session[:user], session[:active_zone_endpoint]))
+ if vms.nil?
+ msg = "No datacenter found"
+ logger.error("[vCenter] " + msg)
+ error = Error.new(msg)
+ error 404, error.to_json
+ end
+
+ [200, vms.to_json]
+ rescue Exception => e
+ logger.error("[vCenter] " + e.message)
+ error = Error.new(e.message)
+ error 403, error.to_json
+ end
+end
+
get '/vcenter/networks' do
begin
networks = vcenter_client.vcenter_networks(
diff --git a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb
index 3688b1e8a7..93078ad0b2 100644
--- a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb
+++ b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb
@@ -292,10 +292,14 @@ class VIClient
if !vmpool["VM/USER_TEMPLATE/PUBLIC_CLOUD[\
TYPE=\"vcenter\" \
and VM_TEMPLATE=\"#{vi_tmp.vm.config.uuid}\"]"]
+
+ host_id = name_to_id(container_hostname,hostpool,"HOST")[1]
+
one_tmp << {
:name => vi_tmp.vm.name,
:uuid => vi_tmp.vm.config.uuid,
:host => container_hostname,
+ :host_id => host_id,
:one => vi_tmp.vm_to_one
}
end
@@ -305,7 +309,23 @@ class VIClient
}
return running_vms
- end
+ end
+
+ def name_to_id(name, pool, ename)
+ objects=pool.select {|object| object.name==name }
+
+ if objects.length>0
+ if objects.length>1
+ return -1, "There are multiple #{ename}s with name #{name}."
+ else
+ result = objects.first.id
+ end
+ else
+ return -1, "#{ename} named #{name} not found."
+ end
+
+ return 0, result
+ end
########################################################################
# Builds a hash with the Datacenter / CCR (Distributed)Networks