From 17749cb5e791c50f3d8399987d23a49b326301e6 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 3 Jan 2017 01:22:10 +0100 Subject: [PATCH] F #2347: Allocate, delete and info API calls for the VMGroup --- include/Nebula.h | 16 +- include/RequestManagerAllocate.h | 29 +++ include/RequestManagerDelete.h | 21 ++- include/RequestManagerInfo.h | 16 ++ include/RequestManagerPoolInfoFilter.h | 19 ++ include/VMGroup.h | 6 +- install.sh | 6 + src/cli/etc/onevmgroup.yaml | 25 +++ src/cli/one_helper/onevmgroup_helper.rb | 121 +++++++++++++ src/cli/onevmgroup | 218 +++++++++++++++++++++++ src/nebula/Nebula.cc | 3 + src/nebula/SConstruct | 1 + src/oca/ruby/opennebula.rb | 2 + src/oca/ruby/opennebula/vm_group.rb | 173 ++++++++++++++++++ src/oca/ruby/opennebula/vm_group_pool.rb | 78 ++++++++ src/rm/RequestManager.cc | 12 ++ src/rm/RequestManagerAllocate.cc | 22 +++ src/vm_group/VMGroup.cc | 6 +- src/vm_group/VMGroupPool.cc | 61 +++++++ 19 files changed, 827 insertions(+), 8 deletions(-) create mode 100644 src/cli/etc/onevmgroup.yaml create mode 100644 src/cli/one_helper/onevmgroup_helper.rb create mode 100755 src/cli/onevmgroup create mode 100644 src/oca/ruby/opennebula/vm_group.rb create mode 100644 src/oca/ruby/opennebula/vm_group_pool.rb create mode 100644 src/vm_group/VMGroupPool.cc diff --git a/include/Nebula.h b/include/Nebula.h index e8a328d4ae..8a95086fa2 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -37,6 +37,7 @@ #include "VirtualRouterPool.h" #include "MarketPlacePool.h" #include "MarketPlaceAppPool.h" +#include "VMGroupPool.h" #include "VirtualMachineManager.h" #include "LifeCycleManager.h" @@ -156,6 +157,11 @@ public: return apppool; }; + VMGroupPool * get_vmgrouppool() + { + return vmgrouppool; + }; + // -------------------------------------------------------------- // Manager Accessors // -------------------------------------------------------------- @@ -580,10 +586,10 @@ private: "/DEFAULT_GROUP_QUOTAS/VM_QUOTA"), system_db(0), db(0), vmpool(0), hpool(0), vnpool(0), upool(0), ipool(0), gpool(0), tpool(0), - dspool(0), clpool(0), docpool(0), zonepool(0), - secgrouppool(0), vdcpool(0), vrouterpool(0), marketpool(0), apppool(0), - lcm(0), vmm(0), im(0), tm(0), dm(0), rm(0), hm(0), authm(0), - aclm(0), imagem(0), marketm(0), ipamm(0) + dspool(0), clpool(0), docpool(0), zonepool(0), secgrouppool(0), + vdcpool(0), vrouterpool(0), marketpool(0), apppool(0), vmgrouppool(0), + lcm(0), vmm(0), im(0), tm(0), dm(0), rm(0), hm(0), authm(0), aclm(0), + imagem(0), marketm(0), ipamm(0) { const char * nl = getenv("ONE_LOCATION"); @@ -634,6 +640,7 @@ private: delete vrouterpool; delete marketpool; delete apppool; + delete vmgrouppool; delete vmm; delete lcm; delete im; @@ -718,6 +725,7 @@ private: VirtualRouterPool * vrouterpool; MarketPlacePool * marketpool; MarketPlaceAppPool * apppool; + VMGroupPool * vmgrouppool; // --------------------------------------------------------------- // Nebula Managers diff --git a/include/RequestManagerAllocate.h b/include/RequestManagerAllocate.h index bf002e734c..9f099eeac7 100644 --- a/include/RequestManagerAllocate.h +++ b/include/RequestManagerAllocate.h @@ -679,4 +679,33 @@ private: /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +class VMGroupAllocate : public RequestManagerAllocate +{ +public: + VMGroupAllocate(): + RequestManagerAllocate("VMGroupAllocate", + "Allocates a new vm group", + "A:ss", + true) + { + Nebula& nd = Nebula::instance(); + pool = nd.get_vmgrouppool(); + auth_object = PoolObjectSQL::VMGROUP; + }; + + ~VMGroupAllocate(){}; + + /* --------------------------------------------------------------------- */ + + Template * get_object_template() + { + return new Template; + }; + + Request::ErrorCode pool_allocate(xmlrpc_c::paramList const& paramList, + Template * tmpl, + int& id, + RequestAttributes& att); +}; + #endif diff --git a/include/RequestManagerDelete.h b/include/RequestManagerDelete.h index b4382818d5..ea882363ad 100644 --- a/include/RequestManagerDelete.h +++ b/include/RequestManagerDelete.h @@ -449,7 +449,24 @@ protected: int drop(PoolObjectSQL * obj, bool resive, RequestAttributes& att); }; -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class VMGroupDelete : public RequestManagerDelete +{ +public: + VMGroupDelete(): + RequestManagerDelete("VMGroupDelete", + "Deletes a vm group") + { + Nebula& nd = Nebula::instance(); + pool = nd.get_vmgrouppool(); + auth_object = PoolObjectSQL::VMGROUP; + }; + + ~VMGroupDelete(){}; +}; #endif + diff --git a/include/RequestManagerInfo.h b/include/RequestManagerInfo.h index 049e43bc24..4e2a96e17f 100644 --- a/include/RequestManagerInfo.h +++ b/include/RequestManagerInfo.h @@ -371,7 +371,23 @@ public: ~MarketPlaceAppInfo(){}; }; + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +class VMGroupInfo : public RequestManagerInfo +{ +public: + VMGroupInfo(): + RequestManagerInfo("VMGroupInfo", + "Returns vm group information") + { + Nebula& nd = Nebula::instance(); + pool = nd.get_vmgrouppool(); + auth_object = PoolObjectSQL::VMGROUP; + }; + + ~VMGroupInfo(){}; +}; + #endif diff --git a/include/RequestManagerPoolInfoFilter.h b/include/RequestManagerPoolInfoFilter.h index 1a3098f5ad..6f83e52389 100644 --- a/include/RequestManagerPoolInfoFilter.h +++ b/include/RequestManagerPoolInfoFilter.h @@ -553,4 +553,23 @@ public: ~MarketPlaceAppPoolInfo(){}; }; +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class VMGroupPoolInfo : public RequestManagerPoolInfoFilter +{ +public: + VMGroupPoolInfo(): + RequestManagerPoolInfoFilter("VMGroupPoolInfo", + "Returns the vm group pool", + "A:siii") + { + Nebula& nd = Nebula::instance(); + pool = nd.get_vmgrouppool(); + auth_object = PoolObjectSQL::VMGROUP; + }; + + ~VMGroupPoolInfo(){}; +}; + #endif diff --git a/include/VMGroup.h b/include/VMGroup.h index 6c6d729813..28fe029e2f 100644 --- a/include/VMGroup.h +++ b/include/VMGroup.h @@ -277,7 +277,11 @@ private: * @param error string describing the error if any * @return 0 on success */ - int post_update_template(string& error); + int post_update_template(string& error) + { + //TODO + return 0; + }; /** * Factory method for VMGroup templates diff --git a/install.sh b/install.sh index 04632755c2..e5ba5b9c7f 100755 --- a/install.sh +++ b/install.sh @@ -550,6 +550,7 @@ BIN_FILES="src/nebula/oned \ src/cli/oneflow \ src/cli/oneflow-template \ src/cli/onesecgroup \ + src/cli/onevmgroup \ src/cli/onevdc \ src/cli/onevrouter \ src/cli/onemarket \ @@ -1280,6 +1281,8 @@ RUBY_OPENNEBULA_LIB_FILES="src/oca/ruby/opennebula/acl_pool.rb \ src/oca/ruby/opennebula/pool.rb \ src/oca/ruby/opennebula/security_group_pool.rb \ src/oca/ruby/opennebula/security_group.rb \ + src/oca/ruby/opennebula/vm_group_pool.rb \ + src/oca/ruby/opennebula/vm_group.rb \ src/oca/ruby/opennebula/system.rb \ src/oca/ruby/opennebula/template_pool.rb \ src/oca/ruby/opennebula/template.rb \ @@ -1440,6 +1443,7 @@ ONE_CLI_LIB_FILES="src/cli/one_helper/onegroup_helper.rb \ src/cli/one_helper/onevdc_helper.rb \ src/cli/one_helper/oneacct_helper.rb \ src/cli/one_helper/onesecgroup_helper.rb \ + src/cli/one_helper/onevmgroup_helper.rb \ src/cli/one_helper/onevrouter_helper.rb \ src/cli/one_helper/onemarketapp_helper.rb \ src/cli/one_helper/onemarket_helper.rb" @@ -1459,6 +1463,7 @@ CLI_BIN_FILES="src/cli/onevm \ src/cli/oneflow-template \ src/cli/oneacct \ src/cli/onesecgroup \ + src/cli/onevmgroup \ src/cli/oneshowback \ src/cli/onevdc \ src/cli/onevrouter \ @@ -1478,6 +1483,7 @@ CLI_CONF_FILES="src/cli/etc/onegroup.yaml \ src/cli/etc/onezone.yaml \ src/cli/etc/oneacct.yaml \ src/cli/etc/onesecgroup.yaml \ + src/cli/etc/onevmgroup.yaml \ src/cli/etc/oneshowback.yaml \ src/cli/etc/onevdc.yaml \ src/cli/etc/onevrouter.yaml \ diff --git a/src/cli/etc/onevmgroup.yaml b/src/cli/etc/onevmgroup.yaml new file mode 100644 index 0000000000..f164e8d358 --- /dev/null +++ b/src/cli/etc/onevmgroup.yaml @@ -0,0 +1,25 @@ +--- +:ID: + :desc: ONE identifier for the VM Group + :size: 4 + +:NAME: + :desc: Name of the VM Group + :size: 20 + :left: true + +:USER: + :desc: Username of the VM Group owner + :size: 15 + :left: true + +:GROUP: + :desc: Group of the VM Group + :size: 15 + :left: true + +:default: +- :ID +- :USER +- :GROUP +- :NAME diff --git a/src/cli/one_helper/onevmgroup_helper.rb b/src/cli/one_helper/onevmgroup_helper.rb new file mode 100644 index 0000000000..3bd12a131a --- /dev/null +++ b/src/cli/one_helper/onevmgroup_helper.rb @@ -0,0 +1,121 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +require 'one_helper' + +class OneVMGroupHelper < OpenNebulaHelper::OneHelper + def self.rname + "VM_GROUP" + end + + def self.conf_file + "onevmgroup.yaml" + end + + def format_pool(options) + config_file = self.class.table_conf + + table = CLIHelper::ShowTable.new(config_file, self) do + column :ID, "ONE identifier for the VM Group", :size=>4 do |d| + d["ID"] + end + + column :NAME, "Name of the VM Group", :left, :size=>20 do |d| + d["NAME"] + end + + column :USER, "Username of the VM Group owner", :left, + :size=>15 do |d| + helper.user_name(d, options) + end + + column :GROUP, "Group of the VM Group", :left, :size=>15 do |d| + helper.group_name(d, options) + end + + default :ID, :USER, :GROUP, :NAME + end + + table + end + + private + + def factory(id=nil) + if id + OpenNebula::VMGroup.new_with_id(id, @client) + else + xml=OpenNebula::VMGroup.build_xml + OpenNebula::VMGroup.new(xml, @client) + end + end + + def factory_pool(user_flag=-2) + OpenNebula::VMGroupPool.new(@client, user_flag) + end + + def format_resource(vmgroup, options = {}) + str="%-15s: %-20s" + str_h1="%-80s" + + CLIHelper.print_header( + str_h1 % "VM GROUP #{vmgroup['ID']} INFORMATION") + puts str % ["ID", vmgroup.id.to_s] + puts str % ["NAME", vmgroup.name] + puts str % ["USER", vmgroup['UNAME']] + puts str % ["GROUP", vmgroup['GNAME']] + + CLIHelper.print_header(str_h1 % "PERMISSIONS",false) + + puts + + ["OWNER", "GROUP", "OTHER"].each { |e| + mask = "---" + mask[0] = "u" if vmgroup["PERMISSIONS/#{e}_U"] == "1" + mask[1] = "m" if vmgroup["PERMISSIONS/#{e}_M"] == "1" + mask[2] = "a" if vmgroup["PERMISSIONS/#{e}_A"] == "1" + + puts str % [e, mask] + } + + puts + + CLIHelper.print_header(str_h1 % "ROLES", false) + + if !vmgroup.to_hash['VM_GROUP']['TEMPLATE']['ROLE'].nil? + roles = [vmgroup.to_hash['VM_GROUP']['TEMPLATE']['ROLE']].flatten + end + + CLIHelper::ShowTable.new(nil, self) do + column :ID, "", :left, :size=>4 do |d| + d["ID"] + end + + column :NAME, "", :left, :size=>16 do |d| + d["NAME"] + end + + column :VIRTUAL_MACHINES, "", :left, :size=>32 do |d| + d["VMS"] + end + end.show(roles, {}) + + puts + + CLIHelper.print_header(str_h1 % "TEMPLATE CONTENTS",false) + puts vmgroup.template_str + end +end diff --git a/src/cli/onevmgroup b/src/cli/onevmgroup new file mode 100755 index 0000000000..e0eeb05d3d --- /dev/null +++ b/src/cli/onevmgroup @@ -0,0 +1,218 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +ONE_LOCATION=ENV["ONE_LOCATION"] + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" +end + +$: << RUBY_LIB_LOCATION +$: << RUBY_LIB_LOCATION+"/cli" + +require 'command_parser' +require 'one_helper/onevmgroup_helper' + +cmd=CommandParser::CmdParser.new(ARGV) do + usage "`onevmgroup` [] []" + version OpenNebulaHelper::ONE_VERSION + + helper = OneVMGroupHelper.new + + before_proc do + helper.set_client(options) + end + + ######################################################################## + # Global Options + ######################################################################## + set :option, CommandParser::OPTIONS+OpenNebulaHelper::CLIENT_OPTIONS + + list_options = CLIHelper::OPTIONS + list_options << OpenNebulaHelper::XML + list_options << OpenNebulaHelper::NUMERIC + list_options << OpenNebulaHelper::DESCRIBE + + ######################################################################## + # Formatters for arguments + ######################################################################## + set :format, :groupid, OpenNebulaHelper.rname_to_id_desc("GROUP") do |arg| + OpenNebulaHelper.rname_to_id(arg, "GROUP") + end + + set :format, :userid, OpenNebulaHelper.rname_to_id_desc("USER") do |arg| + OpenNebulaHelper.rname_to_id(arg, "USER") + end + + set :format, :vmgroupid, OneVMGroupHelper.to_id_desc do |arg| + helper.to_id(arg) + end + + set :format, :vmgroupid_list, OneVMGroupHelper.list_to_id_desc do |arg| + helper.list_to_id(arg) + end + + set :format, :filterflag, OneVMGroupHelper.filterflag_to_i_desc do |arg| + helper.filterflag_to_i(arg) + end + + ######################################################################## + # Commands + ######################################################################## + + create_desc = <<-EOT.unindent + Creates a new VM Group from the given description + EOT + + command :create, create_desc, :file do + helper.create_resource(options) do |obj| + begin + template = File.read(args[0]) + + obj.allocate(template) + rescue => e + STDERR.puts e.messsage + exit -1 + end + end + end + + delete_desc = <<-EOT.unindent + Deletes the VM Group + EOT + + command :delete, delete_desc, [:range, :vmgroupid_list] do + helper.perform_actions(args[0],options,"deleted") do |obj| + obj.delete + end + end + + list_desc = <<-EOT.unindent + Lists VM Group in the pool + EOT + + command :list, list_desc, [:filterflag, nil], :options=>list_options do + helper.list_pool(options, false, args[0]) + end + + show_desc = <<-EOT.unindent + Shows information for the given VM Group + EOT + + command :show, show_desc, :vmgroupid, :options=>OpenNebulaHelper::XML do + helper.show_resource(args[0],options) + end + +=begin + clone_desc = <<-EOT.unindent + Creates a new Security Group from an existing one + EOT + + command :clone, clone_desc, :secgroupid, :name do + helper.perform_action(args[0],options,"cloned") do |obj| + res = obj.clone(args[1]) + + if !OpenNebula.is_error?(res) + puts "ID: #{res}" + else + puts res.message + end + end + end + + chgrp_desc = <<-EOT.unindent + Changes the Security Group's group + EOT + + command :chgrp, chgrp_desc,[:range, :secgroupid_list], :groupid do + helper.perform_actions(args[0],options,"Group changed") do |obj| + obj.chown(-1, args[1].to_i) + end + end + + chown_desc = <<-EOT.unindent + Changes the Security Group's owner and group + EOT + + command :chown, chown_desc, [:range, :secgroupid_list], :userid, + [:groupid,nil] do + gid = args[2].nil? ? -1 : args[2].to_i + helper.perform_actions(args[0],options,"Owner/Group changed") do |obj| + obj.chown(args[1].to_i, gid) + end + end + + chmod_desc = <<-EOT.unindent + Changes the Security Group permissions + EOT + + command :chmod, chmod_desc, [:range, :secgroupid_list], :octet do + helper.perform_actions(args[0],options, "Permissions changed") do |t| + t.chmod_octet(args[1]) + end + end + + update_desc = <<-EOT.unindent + Update the template contents. If a path is not provided the editor will + be launched to modify the current content. + EOT + + command :update, update_desc, :secgroupid, [:file, nil], + :options=>OpenNebulaHelper::APPEND do + helper.perform_action(args[0],options,"modified") do |obj| + if options[:append] + str = OpenNebulaHelper.append_template(args[0], obj, args[1]) + else + str = OpenNebulaHelper.update_template(args[0], obj, args[1]) + end + + helper.set_client(options) + obj = helper.retrieve_resource(obj.id) + + obj.update(str, options[:append]) + end + end + + rename_desc = <<-EOT.unindent + Renames the Security Group + EOT + + command :rename, rename_desc, :secgroupid, :name do + helper.perform_action(args[0],options,"renamed") do |o| + o.rename(args[1]) + end + end + + commit_desc = <<-EOT.unindent + Commit SG changes to associated VMs. This command is to propagate + security group rules to VMs when they are updated. This operation takes + time to iterate over all VMs in the security group, progress can be + checked through the outdated, updating and error VM sets. + EOT + + command :commit, commit_desc, :secgroupid, + :options =>[OneSecurityGroupHelper::RECOVER] do + helper.perform_action(args[0], options, "commit") do |o| + o.commit(options[:recover]==true) + end + end + +=end +end diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index f08daff7f2..08b3dc8493 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -323,6 +323,7 @@ void Nebula::start(bool bootstrap_only) rc += GroupQuotas::bootstrap(db); rc += SecurityGroupPool::bootstrap(db); rc += VirtualRouterPool::bootstrap(db); + rc += VMGroupPool::bootstrap(db); // Create the system tables only if bootstrap went well if (rc == 0) @@ -597,6 +598,8 @@ void Nebula::start(bool bootstrap_only) marketpool = new MarketPlacePool(db, is_federation_slave()); apppool = new MarketPlaceAppPool(db, is_federation_slave()); + vmgrouppool = new VMGroupPool(db); + default_user_quota.select(); default_group_quota.select(); } diff --git a/src/nebula/SConstruct b/src/nebula/SConstruct index 4c1e2a24c1..fe7c3216d0 100644 --- a/src/nebula/SConstruct +++ b/src/nebula/SConstruct @@ -67,6 +67,7 @@ env.Prepend(LIBS=[ 'nebula_vrouter', 'nebula_marketplace', 'nebula_ipamm', + 'nebula_vmgroup', 'crypto', 'xml2' ]) diff --git a/src/oca/ruby/opennebula.rb b/src/oca/ruby/opennebula.rb index f066cffe0e..8b995fe9dc 100644 --- a/src/oca/ruby/opennebula.rb +++ b/src/oca/ruby/opennebula.rb @@ -62,6 +62,8 @@ require 'opennebula/marketplace' require 'opennebula/marketplace_pool' require 'opennebula/marketplaceapp' require 'opennebula/marketplaceapp_pool' +require 'opennebula/vm_group' +require 'opennebula/vm_group_pool' module OpenNebula diff --git a/src/oca/ruby/opennebula/vm_group.rb b/src/oca/ruby/opennebula/vm_group.rb new file mode 100644 index 0000000000..c5f4bbf977 --- /dev/null +++ b/src/oca/ruby/opennebula/vm_group.rb @@ -0,0 +1,173 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +require 'opennebula/pool_element' + +module OpenNebula + class VMGroup < PoolElement + ####################################################################### + # Constants and Class Methods + ####################################################################### + VMGROUP_METHODS = { + :allocate => "vmgroup.allocate", + :info => "vmgroup.info", + #:update => "vmgroup.update", + :delete => "vmgroup.delete", + #:chown => "vmgroup.chown", + #:chmod => "vmgroup.chmod", + #:clone => "vmgroup.clone", + #:rename => "vmgroup.rename" + } + + # Creates a VMGroup description with just its identifier + # this method should be used to create plain VMGroup objects. + # @param pe_id [Integer] the id of the object + def VMGroup.build_xml(pe_id=nil) + if pe_id + obj_xml = "#{pe_id}" + else + obj_xml = "" + end + + XMLElement.build_xml(obj_xml,'VM_GROUP') + end + + # Class constructor + def initialize(xml, client) + super(xml,client) + + @client = client + end + + ####################################################################### + # XML-RPC Methods for the VMGroup Object + ####################################################################### + + # Retrieves the information of the VMGroup. + def info() + super(VMGROUP_METHODS[:info], 'VM_GROUP') + end + + alias_method :info!, :info + + # Allocates a new VMGroup in OpenNebula + # + # @param description [String] The contents of the VMGroup. + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def allocate(description) + super(VMGROUP_METHODS[:allocate], description) + end + + # Deletes the SecurityGroup + def delete() + super(VMGROUP_METHODS[:delete]) + end +=begin + # Replaces the securitygroup contents + # + # @param new_securitygroup [String] New securitygroup contents + # @param append [true, false] True to append new attributes instead of + # replace the whole securitygroup + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def update(new_securitygroup, append=false) + super(SECGROUP_METHODS[:update], new_securitygroup, append ? 1 : 0) + end + + # Changes the owner/group + # + # @param uid [Integer] the new owner id. Set to -1 to leave the current one + # @param gid [Integer] the new group id. Set to -1 to leave the current one + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def chown(uid, gid) + super(SECGROUP_METHODS[:chown], uid, gid) + end + + # Changes the SecurityGroup permissions. + # + # @param octet [String] Permissions octed , e.g. 640 + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def chmod_octet(octet) + super(SECGROUP_METHODS[:chmod], octet) + end + + # Changes the SecurityGroup permissions. + # Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u, + other_m, other_a) + super(SECGROUP_METHODS[:chmod], owner_u, owner_m, owner_a, group_u, + group_m, group_a, other_u, other_m, other_a) + end + + # Clones this SecurityGroup into a new one + # + # @param [String] name for the new SecurityGroup. + # + # @return [Integer, OpenNebula::Error] The new SecurityGroup ID in case + # of success, Error otherwise + def clone(name) + return Error.new('ID not defined') if !@pe_id + + rc = @client.call(SECGROUP_METHODS[:clone], @pe_id, name) + + return rc + end + + # Renames this SecurityGroup + # + # @param name [String] New name for the SecurityGroup. + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def rename(name) + return call(SECGROUP_METHODS[:rename], @pe_id, name) + end + + # Commit SG changes to associated VMs + # + # @param recover [Bool] If true will only operate on outdated and error + # VMs. This is intended for retrying updates of VMs or reinitialize the + # updating process if oned stopped or fail. + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def commit(recover) + return call(SECGROUP_METHODS[:commit], @pe_id, recover) + end +=end + ####################################################################### + # Helpers to get VMGroup information + ####################################################################### + + # Returns the group identifier + # [return] _Integer_ the element's group ID + def gid + self['GID'].to_i + end + + def owner_id + self['UID'].to_i + end + end +end diff --git a/src/oca/ruby/opennebula/vm_group_pool.rb b/src/oca/ruby/opennebula/vm_group_pool.rb new file mode 100644 index 0000000000..b0dacfbd1b --- /dev/null +++ b/src/oca/ruby/opennebula/vm_group_pool.rb @@ -0,0 +1,78 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +require 'opennebula/pool' + +module OpenNebula + class VMGroupPool < Pool + ####################################################################### + # Constants and Class attribute accessors + ####################################################################### + VMGROUP_POOL_METHODS = { + :info => "vmgrouppool.info" + } + + ####################################################################### + # Class constructor & Pool Methods + ####################################################################### + + # +client+ a Client object that represents an XML-RPC connection + # +user_id+ used to refer to a Pool with Templates from that user + def initialize(client, user_id=-1) + super('VM_GROUP_POOL','VM_GROUP',client) + + @user_id = user_id + end + + # Factory method to create Template objects + def factory(element_xml) + OpenNebula::VMGroup.new(element_xml,@client) + end + + ####################################################################### + # XML-RPC Methods for the Template Object + ####################################################################### + + # Retrieves all or part of the objects in the pool. + def info(*args) + case args.size + when 0 + info_filter(VMGROUP_POOL_METHODS[:info], @user_id, -1, -1) + when 3 + info_filter(VMGROUP_POOL_METHODS[:info], args[0], args[1], + args[2]) + end + end + + def info_all() + return super(VMGROUP_POOL_METHODS[:info]) + end + + def info_mine() + return super(VMGROUP_POOL_METHODS[:info]) + end + + def info_group() + return super(VMGROUP_POOL_METHODS[:info]) + end + + alias_method :info!, :info + alias_method :info_all!, :info_all + alias_method :info_mine!, :info_mine + alias_method :info_group!, :info_group + end +end + diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index 3f63410399..8be0bbd0ea 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -378,6 +378,7 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr doc_allocate(new DocumentAllocate()); xmlrpc_c::methodPtr secg_allocate(new SecurityGroupAllocate()); xmlrpc_c::methodPtr vrouter_allocate(new VirtualRouterAllocate()); + xmlrpc_c::methodPtr vmg_allocate(new VMGroupAllocate()); // Clone Methods xmlrpc_c::methodPtr template_clone(new VMTemplateClone()); @@ -394,6 +395,7 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr doc_delete(new DocumentDelete()); xmlrpc_c::methodPtr secg_delete(new SecurityGroupDelete()); xmlrpc_c::methodPtr vrouter_delete(new VirtualRouterDelete()); + xmlrpc_c::methodPtr vmg_delete(new VMGroupDelete()); // Info Methods xmlrpc_c::methodPtr vm_info(new VirtualMachineInfo()); @@ -406,6 +408,7 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr doc_info(new DocumentInfo()); xmlrpc_c::methodPtr secg_info(new SecurityGroupInfo()); xmlrpc_c::methodPtr vrouter_info(new VirtualRouterInfo()); + xmlrpc_c::methodPtr vmg_info(new VMGroupInfo()); // Lock Methods xmlrpc_c::methodPtr doc_lock(new DocumentLock()); @@ -421,6 +424,7 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr clusterpool_info(new ClusterPoolInfo()); xmlrpc_c::methodPtr docpool_info(new DocumentPoolInfo()); xmlrpc_c::methodPtr secgpool_info(new SecurityGroupPoolInfo()); + xmlrpc_c::methodPtr vmgpool_info(new VMGroupPoolInfo()); xmlrpc_c::methodPtr vrouter_pool_info(new VirtualRouterPoolInfo()); // Host Methods @@ -829,6 +833,14 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.secgrouppool.info",secgpool_info); + /* VM Group objects related methods*/ + + RequestManagerRegistry.addMethod("one.vmgroup.allocate", vmg_allocate); + RequestManagerRegistry.addMethod("one.vmgroup.delete", vmg_delete); + RequestManagerRegistry.addMethod("one.vmgroup.info", vmg_info); + + RequestManagerRegistry.addMethod("one.vmgrouppool.info", vmgpool_info); + /* Vdc related methods */ xmlrpc_c::method * vdc_allocate_pt; diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index 3a2042c91d..c16f8d8fb5 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -1195,3 +1195,25 @@ Request::ErrorCode MarketPlaceAppAllocate::pool_allocate( return Request::SUCCESS; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +Request::ErrorCode VMGroupAllocate::pool_allocate( + xmlrpc_c::paramList const& paramList, + Template * tmpl, + int& id, + RequestAttributes& att) +{ + VMGroupPool * vmgpool = static_cast(pool); + + int rc = vmgpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask + ,tmpl, &id, att.resp_msg); + + if (rc < 0) + { + return Request::INTERNAL; + } + + return Request::SUCCESS; +} + diff --git a/src/vm_group/VMGroup.cc b/src/vm_group/VMGroup.cc index c9d7a5c4fd..663581daf0 100644 --- a/src/vm_group/VMGroup.cc +++ b/src/vm_group/VMGroup.cc @@ -322,6 +322,8 @@ error_common: int VMGroup::insert(SqlDB *db, string& error_str) { + int role_id; + vector::iterator it; vector roles; @@ -340,8 +342,10 @@ int VMGroup::insert(SqlDB *db, string& error_str) get_template_attribute("ROLE", roles); - for ( it = roles.begin(); it != roles.end(); it++ ) + for ( it = roles.begin(), role_id = 0; it != roles.end(); ++it, ++role_id ) { + (*it)->replace("ID", role_id); + if (add_role(*it, error_str) == -1) { return -1; diff --git a/src/vm_group/VMGroupPool.cc b/src/vm_group/VMGroupPool.cc new file mode 100644 index 0000000000..feb513fe60 --- /dev/null +++ b/src/vm_group/VMGroupPool.cc @@ -0,0 +1,61 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + + +#include "VMGroupPool.h" + +int VMGroupPool::allocate(int uid, int gid, const string& uname, + const string& gname, int umask, Template * vmgroup_template, int * oid, + string& error_str) +{ + VMGroup * vmgrp; + VMGroup * vmgrp_aux = 0; + + string name; + + ostringstream os; + + vmgrp = new VMGroup(uid, gid, uname, gname, umask, vmgroup_template); + + vmgrp->get_template_attribute("NAME", name); + + if ( !PoolObjectSQL::name_is_valid(name, error_str) ) + { + goto error_name; + } + + vmgrp_aux = get(name, uid, false); + + if( vmgrp_aux != 0 ) + { + goto error_duplicated; + } + + *oid = PoolSQL::allocate(vmgrp, error_str); + + return *oid; + +error_duplicated: + os << "NAME is already taken by VMGroup " << vmgrp_aux->get_oid() << "."; + error_str = os.str(); + +error_name: + delete vmgrp; + *oid = -1; + + return *oid; +} +