1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

Simplify vCenter wild VM import code

This commit is contained in:
Tino Vazquez 2017-07-17 17:20:50 +02:00
parent 9e19ea5148
commit a077d0055b
2 changed files with 31 additions and 345 deletions

View File

@ -68,193 +68,6 @@ define(function(require) {
FUNCTION DEFINITIONS
*/
function rollback_nics_and_disk(error_message, vmName, rollback_items, that, context, wild_row) {
var rollback_index = 0;
function nextRollback() {
if (rollback_items.length == rollback_index) {
var msg = Locale.tr("Could not import the wild VM " + vmName + " due to " + error_message + ". A rollback has been applied.");
Notifier.notifyError(msg);
$("#import_wilds", context).removeAttr("disabled").off("click.disable");
$("#import_wilds", context).html(Locale.tr("Import Wilds"));
} else {
if (rollback_items[rollback_index].type === "NETWORK") {
var path = '/vcenter/network_rollback/' + rollback_items[rollback_index].id;
$.ajax({
url: path,
type: "POST",
data: {timeout: false},
dataType: "json",
success: function(response){
++rollback_index;
nextRollback();
},
error: function(response){
var msg = OpenNebulaError(response).error.message;
Notifier.notifyError(msg);
$("#import_wilds", context).removeAttr("disabled").off("click.disable");
$("#import_wilds", context).html(Locale.tr("Import Wilds")); }
});
}
if (rollback_items[rollback_index].type === "IMAGE") {
var path = '/vcenter/image_rollback/' + rollback_items[rollback_index].id;
$.ajax({
url: path,
type: "POST",
data: {timeout: false},
dataType: "json",
success: function(response){
++rollback_index;
nextRollback();
},
error: function(response){
var msg = OpenNebulaError(response).error.message;
Notifier.notifyError(msg);
$("#import_wilds", context).removeAttr("disabled").off("click.disable");
$("#import_wilds", context).html(Locale.tr("Import Wilds"));
}
});
}
}
}
nextRollback();
}
function import_images_and_nets(disks_and_nets, importHostId, vmName, that, context, wild_row) {
var index = 0;
var template = "";
var rollback = [];
var duplicated_nics = {};
function getNext() {
// Update the template
if (disks_and_nets.length == index) {
// Create the VM in OpenNebula
var dataJSON = {
'id': importHostId,
'extra_param': {
'name': vmName
}
};
OpenNebulaHost.import_wild({
timeout: true,
data: dataJSON,
success: function(request, response) {
OpenNebulaAction.clear_cache("VM");
Notifier.notifyCustom(Locale.tr("VM imported"),
Navigation.link(" ID: " + response.VM.ID, "vms-tab", response.VM.ID),
false);
// Delete row (shouldn't be there in next monitorization)
that.dataTableWildHosts.fnDeleteRow(wild_row);
$("#import_wilds", context).removeAttr("disabled").off("click.disable");
$("#import_wilds", context).html(Locale.tr("Import Wilds"));
},
error: function (request, error_json) {
rollback_nics_and_disk(error_json.error.message, vmName, rollback, that, context, wild_row);
}
});
} else {
if (disks_and_nets[index].type === "NEW_DISK") {
var image_json = {
"image": {
"image_raw": disks_and_nets[index].image_tmpl
},
"ds_id" : disks_and_nets[index].ds_id
};
OpenNebulaImage.create({
timeout: true,
data: image_json,
success: function(request, response) {
var image_id = response.IMAGE.ID;
var image_uname = response.IMAGE.UNAME;
++index;
var rollback_info = { type: "IMAGE", id: image_id};
rollback.push(rollback_info);
getNext();
},
error: function (request, error_json) {
var error_message_str = error_json.error.message;
// Rollback
var msg = (error_json.error.message || Locale.tr("Cannot contact server: is it running and reachable?"));
Notifier.notifyError(msg);
$("#import_wilds", context).removeAttr("disabled").off("click.disable");
$("#import_wilds", context).html(Locale.tr("Import Wilds"));
rollback_nics_and_disk(error_json.error.message, vmName, rollback, that, context, wild_row);
}
});
}
if (disks_and_nets[index].type === "EXISTING_DISK") {
++index;
getNext();
}
if (disks_and_nets[index].type === "NEW_NIC") {
var vnet_json = {
"vnet": {
"vnet_raw": disks_and_nets[index].network_tmpl
}
};
var one_cluster_id = disks_and_nets[index].one_cluster_id;
OpenNebulaNetwork.create({
timeout: true,
data: vnet_json,
success: function(request, response) {
var network_id = response.VNET.ID;
if (one_cluster_id != -1) {
Sunstone.runAction("Cluster.addvnet",one_cluster_id,response.VNET.ID);
// Remove vnet from cluster default 0
Sunstone.runAction("Cluster.delvnet",0,response.VNET.ID);
}
++index;
var rollback_info = { type: "NETWORK", id: network_id};
rollback.push(rollback_info);
getNext();
},
error: function (request, error_json) {
// Rollback
var msg = (error_json.error.message || Locale.tr("Cannot contact server: is it running and reachable?"));
Notifier.notifyError(msg);
$("#import_wilds", context).removeAttr("disabled").off("click.disable");
$("#import_wilds", context).html(Locale.tr("Import Wilds"));
//rollback_nics_and_disk(error_json.error.message, template_id, rollback, row_context);
}
});
}
if (disks_and_nets[index].type == "EXISTING_NIC") {
++index;
getNext();
}
if (disks_and_nets[index].type === "DUPLICATED_NIC") {
++index;
getNext();
}
}
}
getNext();
}
function _html() {
return TemplateWilds();
@ -332,78 +145,44 @@ define(function(require) {
var aData = that.dataTableWildHosts.fnGetData(wild_row);
var vmName = aData[1];
var remoteID = aData[2];
if (remoteID.startsWith("vm-")) {
var path = '/vcenter/wild/' + remoteID;
$.ajax({
url: path,
type: "GET",
data: {timeout: false},
headers: {
"X-VCENTER-USER": that.element.TEMPLATE.VCENTER_USER,
"X-VCENTER-PASSWORD": that.element.TEMPLATE.VCENTER_PASSWORD,
"X-VCENTER-HOST": that.element.TEMPLATE.VCENTER_HOST
},
dataType: "json",
success: function(response){
var disks_and_nets = response.disks.concat(response.nics)
import_images_and_nets(disks_and_nets, importHostId, vmName, that, context, wild_row);
},
error: function(response){
var msg;
if (response.responseJSON && response.responseJSON.error.message){
msg = response.responseJSON.error.message;
} else {
msg = Locale.tr("Cannot contact server: is it running and reachable?");
}
var dataJSON = {
'id': importHostId,
'extra_param': {
'name': vmName
}
};
Notifier.notifyError(msg);
// Create the VM in OpenNebula
OpenNebulaHost.import_wild({
timeout: true,
data: dataJSON,
success: function(request, response) {
OpenNebulaAction.clear_cache("VM");
Notifier.notifyCustom(Locale.tr("VM imported"),
Navigation.link(" ID: " + response.VM.ID, "vms-tab", response.VM.ID),
false);
$("#import_wilds", context).removeAttr("disabled").off("click.disable");
$("#import_wilds", context).html(Locale.tr("Import Wilds"));
}
});
// Delete row (shouldn't be there in next monitorization)
that.dataTableWildHosts.fnDeleteRow(wild_row);
} else {
var dataJSON = {
'id': importHostId,
'extra_param': {
'name': vmName
$("#import_wilds", context).removeAttr("disabled").off("click.disable");
$("#import_wilds", context).html(Locale.tr("Import Wilds"));
},
error: function (request, error_json) {
var msg;
if (error_json.error.message){
msg = error_json.error.message;
} else {
msg = Locale.tr("Cannot contact server: is it running and reachable?");
}
};
// Create the VM in OpenNebula
OpenNebulaHost.import_wild({
timeout: true,
data: dataJSON,
success: function(request, response) {
OpenNebulaAction.clear_cache("VM");
Notifier.notifyCustom(Locale.tr("VM imported"),
Navigation.link(" ID: " + response.VM.ID, "vms-tab", response.VM.ID),
false);
Notifier.notifyError(msg);
// Delete row (shouldn't be there in next monitorization)
that.dataTableWildHosts.fnDeleteRow(wild_row);
$("#import_wilds", context).removeAttr("disabled").off("click.disable");
$("#import_wilds", context).html(Locale.tr("Import Wilds"));
},
error: function (request, error_json) {
var msg;
if (error_json.error.message){
msg = error_json.error.message;
} else {
msg = Locale.tr("Cannot contact server: is it running and reachable?");
}
Notifier.notifyError(msg);
$("#import_wilds", context).removeAttr("disabled").off("click.disable");
$("#import_wilds", context).html(Locale.tr("Import Wilds"));
}
});
}
$("#import_wilds", context).removeAttr("disabled").off("click.disable");
$("#import_wilds", context).html(Locale.tr("Import Wilds"));
}
});
});
});

View File

@ -397,99 +397,6 @@ get '/vcenter/template/:vcenter_ref/:template_id' do
end
end
get '/vcenter/wild/:vcenter_ref' do
begin
t = {}
template = nil
vm_ref = params[:vcenter_ref]
sunstone = true
wild = true
if !vm_ref || vm_ref.empty?
msg = "No VM moref for Wild VM specified"
logger.error("[vCenter] " + msg)
error = Error.new(msg)
error 404, error.to_json
end
vc_uuid = vcenter_client.vim.serviceContent.about.instanceUuid
dpool = VCenterDriver::VIHelper.one_pool(OpenNebula::DatastorePool)
if dpool.respond_to?(:message)
msg = "Could not get OpenNebula DatastorePool: #{dpool.message}"
logger.error("[vCenter] " + msg)
error = Error.new(msg)
error 404, error.to_json
end
ipool = VCenterDriver::VIHelper.one_pool(OpenNebula::ImagePool)
if ipool.respond_to?(:message)
msg = "Could not get OpenNebula ImagePool: #{ipool.message}"
logger.error("[vCenter] " + msg)
error = Error.new(msg)
error 404, error.to_json
end
npool = VCenterDriver::VIHelper.one_pool(OpenNebula::VirtualNetworkPool)
if npool.respond_to?(:message)
msg = "Could not get OpenNebula VirtualNetworkPool: #{npool.message}"
logger.error("[vCenter] " + msg)
error = Error.new(msg)
error 404, error.to_json
end
hpool = VCenterDriver::VIHelper.one_pool(OpenNebula::HostPool)
if hpool.respond_to?(:message)
msg = "Could not get OpenNebula HostPool: #{hpool.message}"
logger.error("[vCenter] " + msg)
error = Error.new(msg)
error 404, error.to_json
end
vcenter_vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vcenter_client)
vm_name = vcenter_vm["name"]
# Get disks information for template
error, template_disks = vcenter_vm.import_vcenter_disks(vc_uuid, dpool, ipool, sunstone)
if !error.empty?
msg = error
logger.error("[vCenter] " + msg)
error = Error.new(msg)
error 404, error.to_json
end
t[:disks] = template_disks
# Get nics information for template
# Create images or get nics information for template
error, template_nics = vcenter_vm.import_vcenter_nics(vc_uuid,
npool,
hpool,
vcenter_client.vim.host,
vm_ref,
wild,
sunstone,
vm_name)
if !error.empty?
msg = error
logger.error("[vCenter] " + msg)
error = Error.new(msg)
error 404, error.to_json
end
t[:nics] = template_nics
[200, t.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
dc_folder = VCenterDriver::DatacenterFolder.new(vcenter_client)