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

Feature #3383: Add import running VMs from Sunstone

Add auto deploy
This commit is contained in:
Tino Vazquez 2015-02-06 18:59:25 +01:00
parent e78e4e179f
commit 07834dc6da
4 changed files with 202 additions and 4 deletions

View File

@ -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: "\

View File

@ -153,6 +153,10 @@ var create_host_tmpl =
<div class="vcenter_templates">\
</div>\
<br>\
<br>\
<div class="vcenter_vms">\
</div>\
<br>\
<div class="vcenter_networks">\
</div>\
<div class="row import_vcenter_clusters_div hidden">\
@ -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: '<span class="fa-stack fa-2x" style="color: #dfdfdf">'+
'<i class="fa fa-cloud fa-stack-2x"></i>'+
'<i class="fa fa-spinner fa-spin fa-stack-1x fa-inverse"></i>'+
'</span>'
}))
$('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("");
$('<div class="row">' +
'<div class="large-12 columns">' +
'<p style="color: #999">' + tr("Please select the vCenter running VMs to be imported to OpenNebula.") + '</p>' +
'</div>' +
'</div>').appendTo($(".content", opts.container))
$.each(response, function(datacenter_name, vms){
$('<div class="row">' +
'<div class="large-12 columns">' +
'<h5>' +
datacenter_name + ' ' + tr("DataCenter") +
'</h5>' +
'</div>' +
'</div>').appendTo($(".content", opts.container))
if (vms.length == 0) {
$('<div class="row">' +
'<div class="large-12 columns">' +
'<label>' +
tr("No new running VMs found in this DataCenter") +
'</label>' +
'</div>' +
'</div>').appendTo($(".content", opts.container))
} else {
$.each(vms, function(id, vm){
var trow = $('<div class="vcenter_vm">' +
'<div class="row">' +
'<div class="large-10 columns">' +
'<label>' +
'<input type="checkbox" class="vm_name" checked/> ' +
vm.name + '&emsp;<span style="color: #999">' + vm.host + '</span>' +
'</vm>' +
'<div class="large-12 columns vcenter_vm_response">'+
'</div>'+
'</div>' +
'<div class="large-2 columns vcenter_vm_result">'+
'</div>'+
'</div>'+
'</div>').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(
'<span class="fa-stack fa-2x" style="color: #dfdfdf">'+
'<i class="fa fa-cloud fa-stack-2x"></i>'+
'<i class="fa fa-spinner fa-spin fa-stack-1x fa-inverse"></i>'+
'</span>');
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(
'<span class="fa-stack fa-2x" style="color: #dfdfdf">'+
'<i class="fa fa-cloud fa-stack-2x"></i>'+
'<i class="fa fa-check fa-stack-1x fa-inverse"></i>'+
'</span>');
$(".vcenter_vm_response", vm_context).html('<p style="font-size:12px" class="running-color">'+
tr("VM imported successfully")+' ID:'+response.VM.ID+
'</p>');
},
error: function (request, error_json){
$(".vcenter_vm_response", vm_context).html('<span class="fa-stack fa-2x" style="color: #dfdfdf">'+
'<i class="fa fa-cloud fa-stack-2x"></i>'+
'<i class="fa fa-warning fa-stack-1x fa-inverse"></i>'+
'</span>');
$(".vcenter_vm_response", vm_context).html('<p style="font-size:12px" class="error-color">'+
(error_json.error.message || tr("Cannot contact server: is it running and reachable?"))+
'</p>');
}
});
})
$.each($(".network_name:checked", $create_host_dialog), function(){
var network_context = $(this).closest(".vcenter_network");

View File

@ -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(

View File

@ -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