diff --git a/install.sh b/install.sh index 5edb65855c..3412a04599 100755 --- a/install.sh +++ b/install.sh @@ -933,12 +933,13 @@ AUTH_PLAIN_FILES="src/authm_mad/remotes/plain/authenticate" #------------------------------------------------------------------------------- NETWORK_FILES="src/vnm_mad/remotes/lib/opennebula_network.rb \ + src/vnm_mad/remotes/lib/vnmmad.rb \ src/vnm_mad/remotes/OpenNebulaNetwork.conf \ src/vnm_mad/remotes/lib/one_firewall.rb \ src/vnm_mad/remotes/lib/one_sg.rb \ src/vnm_mad/remotes/lib/address.rb \ src/vnm_mad/remotes/lib/command.rb \ - src/vnm_mad/remotes/lib/command.rb \ + src/vnm_mad/remotes/lib/vm.rb \ src/vnm_mad/remotes/lib/security_groups.rb \ src/vnm_mad/remotes/lib/security_groups_iptables.rb \ src/vnm_mad/remotes/lib/nic.rb" diff --git a/src/vnm_mad/remotes/802.1Q/HostManaged.rb b/src/vnm_mad/remotes/802.1Q/HostManaged.rb index b6883661d6..88bd38af1f 100644 --- a/src/vnm_mad/remotes/802.1Q/HostManaged.rb +++ b/src/vnm_mad/remotes/802.1Q/HostManaged.rb @@ -14,9 +14,9 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -require 'OpenNebulaNetwork' +require 'vnmmad' -class OpenNebulaHM < OpenNebulaNetwork +class OpenNebulaHM < VNMMAD::OpenNebulaNetwork DRIVER = "802.1Q" XPATH_FILTER = "TEMPLATE/NIC[VLAN='YES']" @@ -34,6 +34,7 @@ class OpenNebulaHM < OpenNebulaNetwork lock vm_id = @vm['ID'] + process do |nic| bridge = nic[:bridge] dev = nic[:phydev] @@ -71,18 +72,18 @@ class OpenNebulaHM < OpenNebulaNetwork end def create_bridge(bridge) - OpenNebula.exec_and_log("#{COMMANDS[:brctl]} addbr #{bridge}") + OpenNebula.exec_and_log("#{VNMMAD::COMMANDS[:brctl]} addbr #{bridge}") @bridges[bridge] = Array.new end def device_exists?(dev, vlan=nil) dev = "#{dev}.#{vlan}" if vlan - `#{COMMANDS[:ip]} link show #{dev}` + `#{VNMMAD::COMMANDS[:ip]} link show #{dev}` $?.exitstatus == 0 end def create_dev_vlan(dev, vlan) - cmd = "#{COMMANDS[:ip]} link add link #{dev}" + cmd = "#{VNMMAD::COMMANDS[:ip]} link add link #{dev}" cmd << " name #{dev}.#{vlan} type vlan id #{vlan}" OpenNebula.exec_and_log(cmd) @@ -96,12 +97,12 @@ class OpenNebulaHM < OpenNebulaNetwork def attach_brigde_dev(bridge, dev, vlan=nil) dev = "#{dev}.#{vlan}" if vlan - OpenNebula.exec_and_log("#{COMMANDS[:brctl]} addif #{bridge} #{dev}") + OpenNebula.exec_and_log("#{VNMMAD::COMMANDS[:brctl]} addif #{bridge} #{dev}") @bridges[bridge] << dev end def ifup(dev, vlan=nil) dev = "#{dev}.#{vlan}" if vlan - OpenNebula.exec_and_log("#{COMMANDS[:ip]} link set #{dev} up") + OpenNebula.exec_and_log("#{VNMMAD::COMMANDS[:ip]} link set #{dev} up") end end diff --git a/src/vnm_mad/remotes/lib/command.rb b/src/vnm_mad/remotes/lib/command.rb index 3242283d29..d1e65a3de7 100644 --- a/src/vnm_mad/remotes/lib/command.rb +++ b/src/vnm_mad/remotes/lib/command.rb @@ -18,6 +18,20 @@ module VNMMAD module VNMNetwork + # This module include implementation specific functions. It MUST not be + # be used in other VNMAD classes. + module Configuration + # Return the command to talk to the Xen hypervisor xm or xl for + # Xen 3 and 4 + def self.get_xen_command + if system("ps axuww | grep -v grep | grep '\\bxen\\b'") + "sudo xm" + else + "sudo xl" + end + end + end + # Command configuration for common network commands. This CAN be adjust # to local installations. Any modification requires to sync the hosts with # onehost sync command. @@ -41,7 +55,7 @@ module VNMNetwork # Adds a new command to the command array # @param cmd [String] the command, it can be a key defined in COMMANDS # @para args[Array] Arguments for the command - def << (cmd, *args) + def add (cmd, *args) if COMMANDS.keys.include?(cmd.to_sym) cmd_str = "#{COMMANDS[cmd.to_sym]} #{args.join(' ')}" else @@ -67,20 +81,6 @@ module VNMNetwork return out end end - - # This module include implementation specific functions. It MUST not be - # be used in other VNMAD classes. - module Configuration - # Return the command to talk to the Xen hypervisor xm or xl for - # Xen 3 and 4 - def self.get_xen_command - if system("ps axuww | grep -v grep | grep '\\bxen\\b'") - "sudo xm" - else - "sudo xl" - end - end - end end end \ No newline at end of file diff --git a/src/vnm_mad/remotes/lib/one_firewall.rb b/src/vnm_mad/remotes/lib/one_firewall.rb index a4b66eccbf..859090f580 100644 --- a/src/vnm_mad/remotes/lib/one_firewall.rb +++ b/src/vnm_mad/remotes/lib/one_firewall.rb @@ -95,7 +95,7 @@ module VNMMAD vm_id = @vm['ID'] process do |nic| chain = "one-#{vm_id}-#{nic[:network_id]}" - iptables_out = `#{COMMANDS[:iptables]} -n -v --line-numbers -L FORWARD` + iptables_out = `#{VNMMAD::COMMANDS[:iptables]} -n -v --line-numbers -L FORWARD` if m = iptables_out.match(/.*#{chain}.*/) rule_num = m[0].split(/\s+/)[0] purge_chain(chain, rule_num) @@ -108,7 +108,7 @@ module VNMMAD ######################################################################## # Methods to deal with iptables rules ######################################################################## - private: + private def purge_chain(chain, rule_num) rules = Array.new @@ -161,7 +161,7 @@ module VNMMAD end def tap_to_chain(tap, chain) - iptables_out = `#{COMMANDS[:iptables]} -n -v --line-numbers -L FORWARD` + iptables_out = `#{VNMMAD::COMMANDS[:iptables]} -n -v --line-numbers -L FORWARD` # Insert the rule on top of the 'opennebula' chain if it exists, so it # doesn't conflict with the security groups driver @@ -186,13 +186,13 @@ module VNMMAD end def chain_exists?(chain) - iptables_nl =`#{COMMANDS[:iptables]} -nL` + iptables_nl =`#{VNMMAD::COMMANDS[:iptables]} -nL` chains = iptables_nl.scan(/(one-.*?) .*references/).flatten chains.include? chain end def rule(rule) - "#{COMMANDS[:iptables]} #{rule}" + "#{VNMMAD::COMMANDS[:iptables]} #{rule}" end end end \ No newline at end of file diff --git a/src/vnm_mad/remotes/lib/opennebula_network.rb b/src/vnm_mad/remotes/lib/opennebula_network.rb index d1a3bd4c3e..ac2baba805 100644 --- a/src/vnm_mad/remotes/lib/opennebula_network.rb +++ b/src/vnm_mad/remotes/lib/opennebula_network.rb @@ -14,39 +14,6 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -$: << File.dirname(__FILE__) -$: << File.join(File.dirname(__FILE__), '..') - -require 'rexml/document' -require 'base64' -require 'yaml' - -require 'one_firewall' -require 'one_sg' -require 'vm' -require 'nic' -require 'address' -require 'security_groups' -require 'security_groups_iptables' - -require 'scripts_common' - -include OpenNebula - -begin - CONF = YAML.load_file( - File.join(File.dirname(__FILE__), "OpenNebulaNetwork.conf") - ) -rescue - CONF = { - :start_vlan => 2 - } -end - -# Set PATH -ENV['PATH'] = "#{ENV['PATH']}:/bin:/sbin:/usr/bin" - - ################################################################################ # The VNMMAD module provides the basic abstraction to implement custom # virtual network drivers. The VNMAD module includes: @@ -76,8 +43,8 @@ module VNMMAD @hypervisor = hypervisor end - @vm = VM.new(REXML::Document.new(vm_tpl).root, xpath_filter, - deploy_id, @hypervisor) + @vm = VNMNetwork::VM.new(REXML::Document.new(vm_tpl).root, + xpath_filter, deploy_id, @hypervisor) end # Creates a new OpenNebulaNetwork using: diff --git a/src/vnm_mad/remotes/lib/security_groups.rb b/src/vnm_mad/remotes/lib/security_groups.rb index 34a82f0cd5..494b2b1b46 100644 --- a/src/vnm_mad/remotes/lib/security_groups.rb +++ b/src/vnm_mad/remotes/lib/security_groups.rb @@ -46,14 +46,14 @@ module VNMNetwork ############################################################################ class Rule # Rule type. - TYPES = { + TYPES = [ :protocol, # Type 1: block the whole protocol :portrange, # Type 2a: block a port range within a protocol :icmp_type, # Type 2b: block selected icmp types :net, # Type 3: block a whole protocol for a network :net_portrange, # Type 4a: block a port range from a network :net_icmp_type # Type 4b: block selected icmp types from a network - } + ] # Initialize a new rule. def initialize(rule) @@ -92,7 +92,6 @@ module VNMNetwork when :net_icmp_type process_net_icmp_type(cmds, vars) - end end end @@ -114,7 +113,7 @@ module VNMNetwork end end - private: + private # ICMP Codes for each ICMP type ICMP_TYPES_EXPANDED = { @@ -140,7 +139,6 @@ module VNMNetwork return :icmp_type if !@icmp_type.nil? return :portrange if !@range.nil? return :protocol - end else return :net_icmp_type if !@icmp_type.nil? return :net_portrange if !@range.nil? diff --git a/src/vnm_mad/remotes/lib/security_groups_iptables.rb b/src/vnm_mad/remotes/lib/security_groups_iptables.rb index 86ec08d0b2..c709eba104 100644 --- a/src/vnm_mad/remotes/lib/security_groups_iptables.rb +++ b/src/vnm_mad/remotes/lib/security_groups_iptables.rb @@ -26,14 +26,14 @@ module SGIPTables ######################################################################## # Implementation of each rule type ######################################################################## - private: + private # Implements the :protocol rule. Example: # iptables -A one-3-0-i -p tcp -j RETURN def process_protocol(cmds, vars) chain = @rule_type == :inbound ? vars[:chain_in] : vars[:chain_out] - cmds << :iptables "-A #{chain} -p #{@protocol} -j RETURN" + cmds.add :iptables, "-A #{chain} -p #{@protocol} -j RETURN" end # Implements the :portrange rule. Example: @@ -41,7 +41,7 @@ module SGIPTables def process_portrange(cmds, vars) chain = @rule_type == :inbound ? vars[:chain_in] : vars[:chain_out] - cmds << :iptables "-A #{chain} -p #{@protocol} -m multiport" \ + cmds.add :iptables, "-A #{chain} -p #{@protocol} -m multiport" \ " --dports #{@range} -j RETURN" end @@ -50,7 +50,7 @@ module SGIPTables def process_icmp_type(cmds, vars) chain = @rule_type == :inbound ? vars[:chain_in] : vars[:chain_out] - cmds << :iptables "-A #{chain} -p icmp --icmp-type #{@icmp_type}" \ + cmds.add :iptables, "-A #{chain} -p icmp --icmp-type #{@icmp_type}" \ " -j RETURN" end @@ -64,17 +64,17 @@ module SGIPTables set = "#{vars[:set_sg_in]}-#{@protocol}-n" dir = "src" else - chain = : vars[:chain_out] + chain = vars[:chain_out] set = "#{vars[:set_sg_out]}-#{@protocol}-n" dir = "dst" end - cmds << :ipset "create #{set} hash:net" - cmds << :iptables "-A #{chain} -p #{@protocol} -m set" \ + cmds.add :ipset, "create #{set} hash:net" + cmds.add :iptables, "-A #{chain} -p #{@protocol} -m set" \ " --match-set #{set} #{dir} -j RETURN" net.each do |n| - cmds << :ipset "add -exist #{set} #{n}" + cmds.add :ipset, "add -exist #{set} #{n}" end end @@ -88,20 +88,20 @@ module SGIPTables set = "#{vars[:set_sg_in]}-nr" dir = "src,dst" else - chain = : vars[:chain_out] + chain = vars[:chain_out] set = "#{vars[:set_sg_out]}-nr" dir = "dst,dst" end - cmds << :ipset "create #{set} hash:net,port" - cmds << :iptables "-A #{chain} -m set --match-set" \ + cmds.add :ipset, "create #{set} hash:net,port" + cmds.add :iptables, "-A #{chain} -m set --match-set" \ "#{set} #{dir} -j RETURN" net.each do |n| @range.split(",").each do |r| r.gsub!(":","-") net_range = "#{n},#{@protocol}:#{r}" - cmds << :ipset "add -exist #{set} #{net_range}" + cmds.add :ipset, "add -exist #{set} #{net_range}" end end end @@ -116,17 +116,17 @@ module SGIPTables set = "#{vars[:set_sg_in]}-ni" dir = "src,dst" else - chain = : vars[:chain_out] + chain = vars[:chain_out] set = "#{vars[:set_sg_out]}-ni" dir = "dst,dst" end - cmds << :ipset "create #{set} hash:net,port" - cmds << :iptables "-A #{chain} -m set --match-set #{set} #{dir} -j RETURN" + cmds.add :ipset, "create #{set} hash:net,port" + cmds.add :iptables, "-A #{chain} -m set --match-set #{set} #{dir} -j RETURN" net.each do |n| icmp_type_expand.each do |type_code| - cmds << :ipset "add -exist #{set} #{n},icmp:#{type_code}" + cmds.add :ipset, "add -exist #{set} #{n},icmp:#{type_code}" end if rule.icmp_type_expand end end @@ -159,17 +159,17 @@ module SGIPTables def self.info commands = VNMNetwork::Commands.new - commands << :iptables "-S" + commands.add :iptables, "-S" iptables_s = commands.run! iptables_forwards = "" if iptables_s.match(/^-N #{GLOBAL_CHAIN}$/) - commands.iptables("-L #{GLOBAL_CHAIN} --line-numbers") + commands.add :iptables, "-L #{GLOBAL_CHAIN} --line-numbers" iptables_forwards = commands.run! end - commands << :ipset "list -name" + commands.add :ipset, "list -name" ipset_list = commands.run! { @@ -190,9 +190,9 @@ module SGIPTables commands = VNMNetwork::Commands.new - commands.iptables "-N #{GLOBAL_CHAIN}" - commands.iptables "-A FORWARD -m physdev --physdev-is-bridged -j #{GLOBAL_CHAIN}" - commands.iptables "-A #{GLOBAL_CHAIN} -j ACCEPT" + commands.add :iptables, "-N #{GLOBAL_CHAIN}" + commands.add :iptables, "-A FORWARD -m physdev --physdev-is-bridged -j #{GLOBAL_CHAIN}" + commands.add :iptables, "-A #{GLOBAL_CHAIN} -j ACCEPT" commands.run! end @@ -253,26 +253,26 @@ module SGIPTables chain_out = vars[:chain_out] # create chains - commands << :iptables "-N #{chain_in}" # inbound - commands << :iptables "-N #{chain_out}" # outbound + commands.add :iptables, "-N #{chain_in}" # inbound + commands.add :iptables, "-N #{chain_out}" # outbound # Send traffic to the NIC chains - commands << :iptables "-I #{GLOBAL_CHAIN} -m physdev --physdev-out #{nic[:tap]} --physdev-is-bridged -j #{chain_in}" - commands << :iptables "-I #{GLOBAL_CHAIN} -m physdev --physdev-in #{nic[:tap]} --physdev-is-bridged -j #{chain_out}" + commands.add :iptables, "-I #{GLOBAL_CHAIN} -m physdev --physdev-out #{nic[:tap]} --physdev-is-bridged -j #{chain_in}" + commands.add :iptables, "-I #{GLOBAL_CHAIN} -m physdev --physdev-in #{nic[:tap]} --physdev-is-bridged -j #{chain_out}" # Mac-spofing if nic[:filter_mac_spoofing] == "YES" - commands << :iptables "-A #{chain_out} -m mac ! --mac-source #{nic[:mac]} -j DROP" + commands.add :iptables, "-A #{chain_out} -m mac ! --mac-source #{nic[:mac]} -j DROP" end # IP-spofing if nic[:filter_ip_spoofing] == "YES" - commands << :iptables "-A #{chain_out} ! --source #{nic[:ip]} -j DROP" + commands.add :iptables, "-A #{chain_out} ! --source #{nic[:ip]} -j DROP" end # Related, Established - commands << :iptables "-A #{chain_in} -m state --state ESTABLISHED,RELATED -j ACCEPT" - commands << :iptables "-A #{chain_out} -m state --state ESTABLISHED,RELATED -j ACCEPT" + commands.add :iptables, "-A #{chain_in} -m state --state ESTABLISHED,RELATED -j ACCEPT" + commands.add :iptables, "-A #{chain_out} -m state --state ESTABLISHED,RELATED -j ACCEPT" commands.run! end @@ -286,8 +286,8 @@ module SGIPTables chain_out = vars[:chain_out] commands = VNMNetwork::Commands.new - commands << :iptables "-A #{chain_in} -j DROP" - commands << :iptables "-A #{chain_out} -j DROP" + commands.add :iptables, "-A #{chain_in} -j DROP" + commands.add :iptables, "-A #{chain_out} -j DROP" commands.run! end @@ -310,7 +310,7 @@ module SGIPTables fields = line.split if [chain_in, chain_out].include?(fields[1]) n = fields[0] - commands << :iptables "-D #{GLOBAL_CHAIN} #{n}" + commands.add :iptables, "-D #{GLOBAL_CHAIN} #{n}" end end @@ -320,13 +320,13 @@ module SGIPTables remove_chains << line.split[1] end end - remove_chains.each {|c| commands << :iptables "-F #{c}" } - remove_chains.each {|c| commands << :iptables "-X #{c}" } + remove_chains.each {|c| commands.add :iptables, "-F #{c}" } + remove_chains.each {|c| commands.add :iptables, "-X #{c}" } ipset_list.lines.each do |line| if line.match(/^#{chain}/) set = line.strip - commands << :ipset "destroy #{set}" + commands.add :ipset, "destroy #{set}" end end diff --git a/src/vnm_mad/remotes/lib/vm.rb b/src/vnm_mad/remotes/lib/vm.rb index 8dfa9f5f18..a09511389d 100644 --- a/src/vnm_mad/remotes/lib/vm.rb +++ b/src/vnm_mad/remotes/lib/vm.rb @@ -77,7 +77,7 @@ module VNMNetwork nil end - private: + private # Method to build the associated Hash from a NIC # @param nic_element [REXML] for the NIC diff --git a/src/vnm_mad/remotes/lib/vnmmad.rb b/src/vnm_mad/remotes/lib/vnmmad.rb new file mode 100644 index 0000000000..7698c2adb3 --- /dev/null +++ b/src/vnm_mad/remotes/lib/vnmmad.rb @@ -0,0 +1,49 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs # +# # +# 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. # +#--------------------------------------------------------------------------- # + +$: << File.dirname(__FILE__) +$: << File.join(File.dirname(__FILE__), '..') + +require 'rexml/document' +require 'base64' +require 'yaml' + +require 'command' +require 'vm' +require 'nic' +require 'address' +require 'security_groups' +require 'security_groups_iptables' +require 'opennebula_network' +require 'one_firewall' +require 'one_sg' + +require 'scripts_common' + +include OpenNebula + +begin + CONF = YAML.load_file( + File.join(File.dirname(__FILE__), "OpenNebulaNetwork.conf") + ) +rescue + CONF = { + :start_vlan => 2 + } +end + +# Set PATH +ENV['PATH'] = "#{ENV['PATH']}:/bin:/sbin:/usr/bin"