diff --git a/include/RequestManagerVirtualMachine.h b/include/RequestManagerVirtualMachine.h index 6530741303..ca61931a76 100644 --- a/include/RequestManagerVirtualMachine.h +++ b/include/RequestManagerVirtualMachine.h @@ -62,10 +62,16 @@ protected: string& vnm, string& tm, string& ds_location, - int& ds_id, + int& ds_id, RequestAttributes& att, PoolObjectAuth& host_perms); + bool check_host(int hid, + int cpu, + int mem, + int disk, + string& error); + int add_history(VirtualMachine * vm, int hid, const string& hostname, @@ -106,7 +112,7 @@ public: VirtualMachineDeploy(): RequestManagerVirtualMachine("VirtualMachineDeploy", "Deploys a virtual machine", - "A:sii") + "A:siib") { auth_op = AuthRequest::ADMIN; }; @@ -126,7 +132,7 @@ public: VirtualMachineMigrate(): RequestManagerVirtualMachine("VirtualMachineMigrate", "Migrates a virtual machine", - "A:siib") + "A:siibb") { auth_op = AuthRequest::ADMIN; }; diff --git a/src/cli/onevm b/src/cli/onevm index 0e1f3c15de..fc1cf77cb5 100755 --- a/src/cli/onevm +++ b/src/cli/onevm @@ -56,6 +56,13 @@ cmd=CommandParser::CmdParser.new(ARGV) do :description => "Device where the image will be attached" } + ENFORCE={ + :name => "enforce", + :short => "-e", + :large => "--enforce", + :description => "Enforce that the host capacity is not exceeded" + } + ######################################################################## # Global Options ######################################################################## @@ -287,12 +294,16 @@ cmd=CommandParser::CmdParser.new(ARGV) do States: PENDING EOT - command :deploy, deploy_desc, [:range,:vmid_list], :hostid do + command :deploy, deploy_desc, [:range,:vmid_list], :hostid, :options=>[ENFORCE] do host_id = args[1] verbose = "deploying in host #{host_id}" helper.perform_actions(args[0],options,verbose) do |vm| - vm.deploy(host_id) + if !options[:enforce].nil? + vm.deploy(host_id, options[:enforce]) + else + vm.deploy(host_id) + end end end @@ -302,12 +313,16 @@ cmd=CommandParser::CmdParser.new(ARGV) do States: RUNNING EOT - command :livemigrate, livemigrate_desc, [:range,:vmid_list], :hostid do + command :livemigrate, livemigrate_desc, [:range,:vmid_list], :hostid, :options=>[ENFORCE] do host_id = args[1] verbose = "live migrating to #{host_id}" helper.perform_actions(args[0],options,verbose) do |vm| - vm.live_migrate(host_id) + if !options[:enforce].nil? + vm.live_migrate(host_id, options[:enforce]) + else + vm.live_migrate(host_id) + end end end @@ -317,12 +332,16 @@ cmd=CommandParser::CmdParser.new(ARGV) do States: RUNNING EOT - command :migrate, migrate_desc, [:range,:vmid_list], :hostid do + command :migrate, migrate_desc, [:range,:vmid_list], :hostid, :options=>[ENFORCE] do host_id = args[1] verbose = "migrating to #{host_id}" helper.perform_actions(args[0],options,verbose) do |vm| - vm.migrate(host_id) + if !options[:enforce].nil? + vm.migrate(host_id, options[:enforce]) + else + vm.migrate(host_id) + end end end diff --git a/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java b/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java index d402c835d2..f32359bda6 100644 --- a/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java +++ b/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java @@ -290,6 +290,21 @@ public class VirtualMachine extends PoolElement{ return response; } + /** + * Initiates the instance of the VM on the target host. + * + * @param hostId The host id (hid) of the target host where + * the VM will be instantiated. + * @param enforce If it is set to true, the host capacity + * will be checked, and the deployment will fail if the host is + * overcommited. Defaults to false + * @return If an error occurs the error message contains the reason. + */ + public OneResponse deploy(int hostId, boolean enforce) + { + return client.call(DEPLOY, id, hostId, enforce); + } + /** * Initiates the instance of the VM on the target host. * @@ -299,7 +314,7 @@ public class VirtualMachine extends PoolElement{ */ public OneResponse deploy(int hostId) { - return client.call(DEPLOY, id, hostId); + return deploy(hostId, false); } /** @@ -337,11 +352,14 @@ public class VirtualMachine extends PoolElement{ * the vm. * @param live If true we are indicating that we want livemigration, * otherwise false. + * @param enforce If it is set to true, the host capacity + * will be checked, and the deployment will fail if the host is + * overcommited. Defaults to false * @return If an error occurs the error message contains the reason. */ - public OneResponse migrate(int hostId, boolean live) + public OneResponse migrate(int hostId, boolean live, boolean enforce) { - return client.call(MIGRATE, id, hostId, live); + return client.call(MIGRATE, id, hostId, live, enforce); } /** @@ -628,6 +646,24 @@ public class VirtualMachine extends PoolElement{ return action("unresched"); } + /** + * Migrates the virtual machine to the target host (hid). + *
+ * It does the same as {@link VirtualMachine#migrate(int, boolean)} + * with live set to false. + * + * @param hostId The target host id (hid) where we want to migrate + * the vm. + * @param enforce If it is set to true, the host capacity + * will be checked, and the deployment will fail if the host is + * overcommited. Defaults to false + * @return If an error occurs the error message contains the reason. + */ + public OneResponse migrate(int hostId, boolean enforce) + { + return migrate(hostId, false, enforce); + } + /** * Migrates the virtual machine to the target host (hid). *
@@ -643,6 +679,25 @@ public class VirtualMachine extends PoolElement{ return migrate(hostId, false); } + /** + * Performs a live migration of the virtual machine to the + * target host (hid). + *
+ * It does the same as {@link VirtualMachine#migrate(int, boolean)} + * with live set to true. + * + * @param hostId The target host id (hid) where we want to migrate + * the vm. + * @param enforce If it is set to true, the host capacity + * will be checked, and the deployment will fail if the host is + * overcommited. Defaults to false + * @return If an error occurs the error message contains the reason. + */ + public OneResponse liveMigrate(int hostId, boolean enforce) + { + return migrate(hostId, true, enforce); + } + /** * Performs a live migration of the virtual machine to the * target host (hid). @@ -656,7 +711,7 @@ public class VirtualMachine extends PoolElement{ */ public OneResponse liveMigrate(int hostId) { - return migrate(hostId, true); + return liveMigrate(hostId, false); } public int state() diff --git a/src/oca/ruby/OpenNebula/VirtualMachine.rb b/src/oca/ruby/OpenNebula/VirtualMachine.rb index 5e7c061c8b..d4efcb6a18 100644 --- a/src/oca/ruby/OpenNebula/VirtualMachine.rb +++ b/src/oca/ruby/OpenNebula/VirtualMachine.rb @@ -140,15 +140,16 @@ module OpenNebula # Initiates the instance of the VM on the target host. # - # +host_id+ The host id (hid) of the target host where - # the VM will be instantiated. - def deploy(host_id) - return Error.new('ID not defined') if !@pe_id - - rc = @client.call(VM_METHODS[:deploy], @pe_id, host_id.to_i) - rc = nil if !OpenNebula.is_error?(rc) - - return rc + # @param host_id [Interger] The host id (hid) of the target host where + # the VM will be instantiated. + # @param enforce [true|false] If it is set to true, the host capacity + # will be checked, and the deployment will fail if the host is + # overcommited. Defaults to false + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def deploy(host_id, enforce=false) + return call(VM_METHODS[:deploy], @pe_id, host_id.to_i, enforce) end # Shutdowns an already deployed VM @@ -247,23 +248,31 @@ module OpenNebula end # Saves a running VM and starts it again in the specified host - def migrate(host_id) - return Error.new('ID not defined') if !@pe_id - - rc = @client.call(VM_METHODS[:migrate], @pe_id, host_id.to_i, false) - rc = nil if !OpenNebula.is_error?(rc) - - return rc + # + # @param host_id [Interger] The host id (hid) of the target host where + # the VM will be migrated. + # @param enforce [true|false] If it is set to true, the host capacity + # will be checked, and the deployment will fail if the host is + # overcommited. Defaults to false + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def migrate(host_id, enforce=false) + return call(VM_METHODS[:migrate], @pe_id, host_id.to_i, false, enforce) end # Migrates a running VM to another host without downtime - def live_migrate(host_id) - return Error.new('ID not defined') if !@pe_id - - rc = @client.call(VM_METHODS[:migrate], @pe_id, host_id.to_i, true) - rc = nil if !OpenNebula.is_error?(rc) - - return rc + # + # @param host_id [Interger] The host id (hid) of the target host where + # the VM will be migrated. + # @param enforce [true|false] If it is set to true, the host capacity + # will be checked, and the deployment will fail if the host is + # overcommited. Defaults to false + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def live_migrate(host_id, enforce=false) + return call(VM_METHODS[:migrate], @pe_id, host_id.to_i, true, enforce) end # Set the specified vm's disk to be saved in a new image diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index 016354557d..5ebda25003 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -200,6 +200,46 @@ int RequestManagerVirtualMachine::get_host_information(int hid, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +bool RequestManagerVirtualMachine::check_host(int hid, + int cpu, + int mem, + int disk, + string& error) +{ + Nebula& nd = Nebula::instance(); + HostPool * hpool = nd.get_hpool(); + + Host * host; + bool test; + + host = hpool->get(hid, true); + + if (host == 0) + { + error = "Host no longer exists"; + return false; + } + + test = host->test_capacity(cpu, mem, disk); + + if (!test) + { + ostringstream oss; + + oss << object_name(PoolObjectSQL::HOST) + << " " << hid << " does not have enough capacity."; + + error = oss.str(); + } + + host->unlock(); + + return test; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + VirtualMachine * RequestManagerVirtualMachine::get_vm(int id, RequestAttributes& att) { @@ -387,31 +427,38 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList, string ds_location; int ds_id; - int id = xmlrpc_c::value_int(paramList.getInt(1)); - int hid = xmlrpc_c::value_int(paramList.getInt(2)); + int id = xmlrpc_c::value_int(paramList.getInt(1)); + int hid = xmlrpc_c::value_int(paramList.getInt(2)); + bool enforce = xmlrpc_c::value_boolean(paramList.getBoolean(3)); bool auth = false; - if (get_host_information( - hid, hostname, vmm_mad, vnm_mad, tm_mad, ds_location, ds_id, att, - host_perms) != 0) + if (get_host_information(hid, + hostname, + vmm_mad, + vnm_mad, + tm_mad, + ds_location, + ds_id, + att, + host_perms) != 0) { return; } auth = vm_authorization(id, 0, 0, att, &host_perms, 0, auth_op); - if ( auth == false ) + if (auth == false) { return; } - if ( (vm = get_vm(id, att)) == 0 ) + if ((vm = get_vm(id, att)) == 0) { return; } - if ( vm->get_state() != VirtualMachine::PENDING ) + if (vm->get_state() != VirtualMachine::PENDING) { failure_response(ACTION, request_error("Wrong state to perform action",""), @@ -421,8 +468,36 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList, return; } - if ( add_history( - vm,hid,hostname,vmm_mad,vnm_mad,tm_mad,ds_location,ds_id,att) != 0) + if (enforce) + { + int cpu, mem, disk; + string error; + + vm->get_requirements(cpu, mem, disk); + + vm->unlock(); + + if (check_host(hid, cpu, mem, disk, error) == false) + { + failure_response(ACTION, request_error(error,""), att); + return; + } + + if ((vm = get_vm(id, att)) == 0) + { + return; + } + } + + if (add_history(vm, + hid, + hostname, + vmm_mad, + vnm_mad, + tm_mad, + ds_location, + ds_id, + att) != 0) { vm->unlock(); return; @@ -459,27 +534,34 @@ void VirtualMachineMigrate::request_execute(xmlrpc_c::paramList const& paramList string aux_st; int current_hid; - int id = xmlrpc_c::value_int(paramList.getInt(1)); - int hid = xmlrpc_c::value_int(paramList.getInt(2)); - bool live = xmlrpc_c::value_boolean(paramList.getBoolean(3)); + int id = xmlrpc_c::value_int(paramList.getInt(1)); + int hid = xmlrpc_c::value_int(paramList.getInt(2)); + bool live = xmlrpc_c::value_boolean(paramList.getBoolean(3)); + bool enforce = xmlrpc_c::value_boolean(paramList.getBoolean(4)); bool auth = false; - if (get_host_information( - hid, hostname, vmm_mad, vnm_mad, tm_mad, ds_location, ds_id, att, - host_perms) != 0) + if (get_host_information(hid, + hostname, + vmm_mad, + vnm_mad, + tm_mad, + ds_location, + ds_id, + att, + host_perms) != 0) { return; } auth = vm_authorization(id, 0, 0, att, &host_perms, 0, auth_op); - if ( auth == false ) + if (auth == false) { return; } - if ( (vm = get_vm(id, att)) == 0 ) + if ((vm = get_vm(id, att)) == 0) { return; } @@ -498,11 +580,35 @@ void VirtualMachineMigrate::request_execute(xmlrpc_c::paramList const& paramList current_hid = vm->get_hid(); - vm->unlock(); + if (enforce) + { + int cpu, mem, disk; + string error; - if (get_host_information( - current_hid, aux_st, aux_st, aux_st, aux_st, aux_st, current_ds_id, - att, aux_perms) != 0) + vm->get_requirements(cpu, mem, disk); + + vm->unlock(); + + if (check_host(hid, cpu, mem, disk, error) == false) + { + failure_response(ACTION, request_error(error,""), att); + return; + } + } + else + { + vm->unlock(); + } + + if (get_host_information(current_hid, + aux_st, + aux_st, + aux_st, + aux_st, + aux_st, + current_ds_id, + att, + aux_perms) != 0) { return; } @@ -529,14 +635,21 @@ void VirtualMachineMigrate::request_execute(xmlrpc_c::paramList const& paramList return; } - if ( add_history(vm, hid, hostname, vmm_mad, vnm_mad, tm_mad, ds_location, - ds_id, att) != 0) + if (add_history(vm, + hid, + hostname, + vmm_mad, + vnm_mad, + tm_mad, + ds_location, + ds_id, + att) != 0) { vm->unlock(); return; } - if ( live == true ) + if (live == true) { dm->live_migrate(vm); } diff --git a/src/scheduler/src/pool/VirtualMachinePoolXML.cc b/src/scheduler/src/pool/VirtualMachinePoolXML.cc index 1b4852cb69..244d6afd57 100644 --- a/src/scheduler/src/pool/VirtualMachinePoolXML.cc +++ b/src/scheduler/src/pool/VirtualMachinePoolXML.cc @@ -112,27 +112,28 @@ int VirtualMachinePoolXML::dispatch(int vid, int hid, bool resched) const try { - //TODO Get live migration from config file if (resched == true) { client->call(client->get_endpoint(), // serverUrl "one.vm.migrate", // methodName - "siib", // arguments format + "siibb", // arguments format &deploy_result, // resultP client->get_oneauth().c_str(), // argument 0 (AUTH) vid, // argument 1 (VM) hid, // argument 2 (HOST) - live_resched); // argument 3 (LIVE) + live_resched, // argument 3 (LIVE) + false); // argument 4 (ENFORCE) } else { client->call(client->get_endpoint(), // serverUrl "one.vm.deploy", // methodName - "sii", // arguments format + "siib", // arguments format &deploy_result, // resultP client->get_oneauth().c_str(), // argument 0 (AUTH) vid, // argument 1 (VM) - hid); // argument 2 (HOST) + hid, // argument 2 (HOST) + false); // argument 3 (ENFORCE) } } catch (exception const& e)