1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-28 17:57:22 +03:00

F #2451: Add configurble hooks to the network drivers in the form of

pre.d, post.d and clean.d scripts

Co-authored-by: Daniel Clavijo Coca <dclavijo@opennebula.systems>
This commit is contained in:
Ruben S. Montero 2019-04-22 11:27:20 +02:00
parent 99ded0f884
commit 1e15db5ac0
No known key found for this signature in database
GPG Key ID: A0CEA6FA880A1D87
53 changed files with 205 additions and 49 deletions

View File

@ -289,14 +289,41 @@ VAR_DIRS="$VAR_LOCATION/remotes \
$VAR_LOCATION/remotes/vmm/packet \
$VAR_LOCATION/remotes/vnm \
$VAR_LOCATION/remotes/vnm/802.1Q \
$VAR_LOCATION/remotes/vnm/802.1Q/pre.d \
$VAR_LOCATION/remotes/vnm/802.1Q/post.d \
$VAR_LOCATION/remotes/vnm/802.1Q/clean.d \
$VAR_LOCATION/remotes/vnm/vxlan \
$VAR_LOCATION/remotes/vnm/vxlan/pre.d \
$VAR_LOCATION/remotes/vnm/vxlan/post.d \
$VAR_LOCATION/remotes/vnm/vxlan/clean.d \
$VAR_LOCATION/remotes/vnm/dummy \
$VAR_LOCATION/remotes/vnm/dummy/pre.d \
$VAR_LOCATION/remotes/vnm/dummy/post.d \
$VAR_LOCATION/remotes/vnm/dummy/clean.d \
$VAR_LOCATION/remotes/vnm/bridge \
$VAR_LOCATION/remotes/vnm/bridge/pre.d \
$VAR_LOCATION/remotes/vnm/bridge/post.d \
$VAR_LOCATION/remotes/vnm/bridge/clean.d \
$VAR_LOCATION/remotes/vnm/ebtables \
$VAR_LOCATION/remotes/vnm/ebtables/pre.d \
$VAR_LOCATION/remotes/vnm/ebtables/post.d \
$VAR_LOCATION/remotes/vnm/ebtables/clean.d \
$VAR_LOCATION/remotes/vnm/fw \
$VAR_LOCATION/remotes/vnm/fw/pre.d \
$VAR_LOCATION/remotes/vnm/fw/post.d \
$VAR_LOCATION/remotes/vnm/fw/clean.d \
$VAR_LOCATION/remotes/vnm/ovswitch \
$VAR_LOCATION/remotes/vnm/ovswitch/pre.d \
$VAR_LOCATION/remotes/vnm/ovswitch/post.d \
$VAR_LOCATION/remotes/vnm/ovswitch/clean.d \
$VAR_LOCATION/remotes/vnm/ovswitch_vxlan \
$VAR_LOCATION/remotes/vnm/ovswitch_vxlan/pre.d \
$VAR_LOCATION/remotes/vnm/ovswitch_vxlan/post.d \
$VAR_LOCATION/remotes/vnm/ovswitch_vxlan/clean.d \
$VAR_LOCATION/remotes/vnm/vcenter \
$VAR_LOCATION/remotes/vnm/vcenter/pre.d \
$VAR_LOCATION/remotes/vnm/vcenter/post.d \
$VAR_LOCATION/remotes/vnm/vcenter/clean.d \
$VAR_LOCATION/remotes/tm/ \
$VAR_LOCATION/remotes/tm/dummy \
$VAR_LOCATION/remotes/tm/shared \
@ -1091,9 +1118,9 @@ NETWORK_EBTABLES_FILES="src/vnm_mad/remotes/ebtables/clean \
src/vnm_mad/remotes/ebtables/Ebtables.rb"
NETWORK_FW_FILES="src/vnm_mad/remotes/fw/post \
src/vnm_mad/remotes/fw/pre \
src/vnm_mad/remotes/fw/update_sg \
src/vnm_mad/remotes/fw/clean"
src/vnm_mad/remotes/fw/pre \
src/vnm_mad/remotes/fw/update_sg \
src/vnm_mad/remotes/fw/clean"
NETWORK_OVSWITCH_FILES="src/vnm_mad/remotes/ovswitch/clean \
src/vnm_mad/remotes/ovswitch/post \

View File

@ -32,7 +32,7 @@ begin
filter_driver = VNMMAD::VNMDriver.filter_driver(template64,
xpath_filter,
deploy_id)
filter_driver.deactivate
filter_driver.run_hooks(ARGV, template64) if filter_driver.deactivate == 0
rescue Exception => e
OpenNebula.log_error(e.message)
OpenNebula.log_error(e.backtrace)

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -29,7 +29,7 @@ begin
filter_driver = VNMMAD::VNMDriver.filter_driver(template64,
xpath_filter,
deploy_id)
filter_driver.activate
filter_driver.run_hooks(ARGV, template64) if filter_driver.activate == 0
rescue Exception => e
OpenNebula.log_error(e.message)
OpenNebula.log_error(e.backtrace)

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -26,4 +26,4 @@ deploy_id = ARGV[0]
xpath_filter = VLANTagDriver::XPATH_FILTER
hm = VLANTagDriver.from_base64(template64, xpath_filter, deploy_id)
exit hm.activate
exit hm.run_hooks(ARGV, template64) if hm.activate == 0

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -27,9 +27,10 @@ xpath_filter = "TEMPLATE/NIC[VN_MAD='bridge']"
begin
hm = VNMMAD::NoVLANDriver.from_base64(template64, xpath_filter, deploy_id)
hm.deactivate
hm.run_hooks(ARGV, template64) if hm.deactivate == 0
rescue Exception => e
OpenNebula.log_error(e.message)
OpenNebula.log_error(e.backtrace)
exit 1
end

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2019, OpenNebula Project, OpenNebula Systems #
@ -16,4 +16,12 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
exit 0
$LOAD_PATH << File.dirname(__FILE__)
$LOAD_PATH << File.join(File.dirname(__FILE__), '..')
require 'vnmmad'
template64 = STDIN.read
hm = VNMMAD::VNMDriver.from_base64(template64)
exit hm.run_hooks(ARGV, template64)

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -26,4 +26,4 @@ deploy_id = ARGV[0]
xpath_filter = "TEMPLATE/NIC[VN_MAD='bridge']"
hm = VNMMAD::NoVLANDriver.from_base64(template64, xpath_filter, deploy_id)
exit hm.activate
exit hm.run_hooks(ARGV, template64) if hm.activate == 0

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -95,7 +95,7 @@ class EbtablesVLAN < VNMMAD::NoVLANDriver
super
return 0
0
end
def rules

View File

@ -32,7 +32,7 @@ begin
filter_driver = VNMMAD::VNMDriver.filter_driver(template64,
xpath_filter,
deploy_id)
filter_driver.deactivate
filter_driver.run_hooks(ARGV, template64) if filter_driver.deactivate == 0
rescue Exception => e
OpenNebula.log_error(e.message)
OpenNebula.log_error(e.backtrace)

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -32,7 +32,7 @@ begin
filter_driver = VNMMAD::VNMDriver.filter_driver(template64,
xpath_filter,
deploy_id)
filter_driver.activate
filter_driver.run_hooks(ARGV, template64) if filter_driver.activate == 0
rescue Exception => e
OpenNebula.log_error(e.message)
OpenNebula.log_error(e.backtrace)

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -26,4 +26,4 @@ deploy_id = ARGV[0]
xpath_filter = EbtablesVLAN::XPATH_FILTER
onevlan = EbtablesVLAN.from_base64(template64, xpath_filter, deploy_id)
onevlan.activate
onevlan.run_hooks(ARGV, template64) if onevlan.activate == 0

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -32,7 +32,7 @@ begin
filter_driver = VNMMAD::VNMDriver.filter_driver(template64,
xpath_filter,
deploy_id)
filter_driver.deactivate
filter_driver.run_hooks(ARGV, template64) if filter_driver.deactivate == 0
rescue Exception => e
OpenNebula.log_error(e.message)
OpenNebula.log_error(e.backtrace)

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -29,7 +29,7 @@ begin
filter_driver = VNMMAD::VNMDriver.filter_driver(template64,
xpath_filter,
deploy_id)
filter_driver.activate
filter_driver.run_hooks(ARGV, template64) if filter_driver.activate == 0
rescue Exception => e
OpenNebula.log_error(e.message)
OpenNebula.log_error(e.backtrace)

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -26,4 +26,4 @@ deploy_id = ARGV[0]
xpath_filter = "TEMPLATE/NIC[VN_MAD='fw']"
hm = VNMMAD::NoVLANDriver.from_base64(template64, xpath_filter, deploy_id)
exit hm.activate
exit hm.run_hooks(ARGV, template64) if hm.activate == 0

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -64,7 +64,7 @@ module VNMMAD
unlock
return 0
0
end
# This function needs to be implemented by any VLAN driver to
@ -116,6 +116,8 @@ module VNMMAD
end if @bridges
unlock
0
end
private

View File

@ -15,6 +15,7 @@
#--------------------------------------------------------------------------- #
require 'shellwords'
require 'open3'
################################################################################
# The VNMMAD module provides the basic abstraction to implement custom
@ -29,6 +30,7 @@ module VNMMAD
# drivers FirewallDriver and SGDriver.
############################################################################
class VNMDriver
attr_reader :vm
# Creates new driver using:
@ -39,16 +41,16 @@ module VNMMAD
@locking ||= false
@vm = VNMNetwork::VM.new(REXML::Document.new(vm_tpl).root,
xpath_filter, deploy_id)
xpath_filter, deploy_id)
end
# Creates a new VNMDriver using:
# @param vm_64 [String] Base64 encoded XML String from oned
# @param deploy_id [String]
def self.from_base64(vm_64, xpath_filter = nil, deploy_id = nil)
vm_xml = Base64::decode64(vm_64)
vm_xml = Base64.decode64(vm_64)
self.new(vm_xml, xpath_filter, deploy_id)
new(vm_xml, xpath_filter, deploy_id)
end
# Locking function to serialized driver operations if needed. Similar
@ -56,7 +58,7 @@ module VNMMAD
def lock
if @locking
driver_name = self.class.name.downcase
@locking_file = File.open("/tmp/onevnm-#{driver_name}-lock","w")
@locking_file = File.open("/tmp/onevnm-#{driver_name}-lock", 'w')
@locking_file.flock(File::LOCK_EX)
end
end
@ -64,19 +66,19 @@ module VNMMAD
# Unlock driver execution mutex
def unlock
if @locking
@locking_file.close
@locking_file.close
end
end
# Executes the given block on each NIC
def process(&block)
def process
blk = lambda do |nic|
add_nic_conf(nic)
add_bridge_conf(nic)
add_ovs_bridge_conf(nic)
add_ip_link_conf(nic)
block.call(nic)
yield(nic)
end
@vm.each_nic(blk)
@ -84,7 +86,7 @@ module VNMMAD
# Parse network configuration and add it to the nic
def add_nic_conf(nic)
return if nic[:conf] and nic[:conf].instance_of? Hash
return if nic[:conf] && nic[:conf].instance_of?(Hash)
default_conf = CONF || {}
nic_conf = {}
@ -118,16 +120,16 @@ module VNMMAD
end
def add_command_conf(nic, conf_name)
return if nic[conf_name] and nic[conf_name].instance_of? Hash
return if nic[conf_name] && nic[conf_name].instance_of?(Hash)
default_conf = CONF[conf_name] || {}
nic_conf = {}
# sanitize
default_conf.each do |key, value|
option = Shellwords.escape(key.to_s.strip.downcase)
option = Shellwords.escape(key.to_s.strip.downcase)
if value.class == String
value = Shellwords.escape(value.strip)
value = Shellwords.escape(value.strip)
end
nic_conf[option] = value
@ -138,7 +140,7 @@ module VNMMAD
if value == '__delete__'
nic_conf.delete(option.strip.downcase)
else
option = Shellwords.escape(option.strip.downcase)
option = Shellwords.escape(option.strip.downcase)
if value == ''
value = nil
else
@ -163,13 +165,13 @@ module VNMMAD
# Returns the associated command including sudo and other configuration
# attributes
def command(cmd)
if VNMNetwork::COMMANDS.keys.include?(cmd.to_sym)
cmd_str = "#{VNMNetwork::COMMANDS[cmd.to_sym]}"
if VNMNetwork::COMMANDS.key?(cmd.to_sym)
cmd_str = (VNMNetwork::COMMANDS[cmd.to_sym]).to_s
else
cmd_str = "#{cmd}"
cmd_str = cmd.to_s
end
return cmd_str
cmd_str
end
def parse_options(string)
@ -178,7 +180,7 @@ module VNMMAD
def self.parse_options(string)
options = {}
return options if !string
return options unless string
string.split(',').each do |op|
m = op.match(/^\s*(?<option>[^=]+)\s*=\s*(?<value>.*?)\s*$/)
@ -189,6 +191,43 @@ module VNMMAD
options
end
# Runs hooks in action.d directory inside the particular vnm driver
# Params:
# +args+:: +array+ Arguments passed to each script
# +stdin+:: +string+ Variable passed as Standard Input to each script
def run_hooks(args, stdin)
dir = "#{$PROGRAM_NAME}.d"
return 0 unless Dir.exist? dir
return 0 if Dir["#{dir}/*"].empty?
programs(dir).each do |file|
OpenNebula.log "Running #{file}"
cmd = "#{file} #{args.join(' ')}"
_o, e, s = Open3.capture3(cmd, :stdin_data => stdin.to_s)
raise "Error running #{file}\n#{e}" unless s.exitstatus.zero?
end
0
end
private
# returns files sorted alphabetically
# if executable by the user running this method
def programs(dir)
files = []
Dir["#{dir}/*"].each do |file|
files << file if File.executable?(file)
end
files.sort
end
end
# Returns true if the driver is executing action pre

View File

@ -116,7 +116,7 @@ class OpenvSwitchVLAN < VNMMAD::VNMDriver
unlock
return 0
0
end
def deactivate
@ -162,6 +162,8 @@ class OpenvSwitchVLAN < VNMMAD::VNMDriver
end
unlock
0
end
def vlan

View File

@ -26,4 +26,4 @@ deploy_id = nil
xpath_filter = OpenvSwitchVLAN::XPATH_FILTER
ovs = OpenvSwitchVLAN.from_base64(template64, xpath_filter, deploy_id)
ovs.deactivate
ovs.run_hooks(ARGV, template64) if ovs.deactivate == 0

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -26,5 +26,4 @@ deploy_id = ARGV[0]
xpath_filter = OpenvSwitchVLAN::XPATH_FILTER
ovs = OpenvSwitchVLAN.from_base64(template64, xpath_filter, deploy_id)
ovs.activate
ovs.run_hooks(ARGV, template64) if ovs.activate == 0

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -26,5 +26,4 @@ deploy_id = ARGV[0]
xpath_filter = OpenvSwitchVLAN::XPATH_FILTER
ovs = OpenvSwitchVLAN.from_base64(template64, xpath_filter, deploy_id)
ovs.activate
ovs.run_hooks(ARGV, template64) if ovs.activate == 0

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -26,4 +26,4 @@ deploy_id = nil
xpath_filter = OpenvSwitchVXLAN::XPATH_FILTER
ovs = OpenvSwitchVXLAN.from_base64(template64, xpath_filter, deploy_id)
ovs.deactivate
ovs.run_hooks(ARGV, template64) if ovs.deactivate == 0

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -26,5 +26,4 @@ deploy_id = ARGV[0]
xpath_filter = OpenvSwitchVXLAN::XPATH_FILTER
ovs = OpenvSwitchVXLAN.from_base64(template64, xpath_filter, deploy_id)
ovs.activate
ovs.run_hooks(ARGV, template64) if ovs.activate == 0

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -26,5 +26,4 @@ deploy_id = ARGV[0]
xpath_filter = OpenvSwitchVXLAN::XPATH_FILTER
ovs = OpenvSwitchVXLAN.from_base64(template64, xpath_filter, deploy_id)
ovs.activate
ovs.run_hooks(ARGV, template64) if ovs.activate == 0

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -33,6 +33,7 @@ begin
xpath_filter,
deploy_id)
filter_driver.deactivate
filter_driver.run_hooks(ARGV, template64) if filter_driver.deactivate == 0
rescue Exception => e
OpenNebula.log_error(e.message)
OpenNebula.log_error(e.backtrace)

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -29,7 +29,7 @@ begin
filter_driver = VNMMAD::VNMDriver.filter_driver(template64,
xpath_filter,
deploy_id)
filter_driver.activate
filter_driver.run_hooks(ARGV, template64) if filter_driver.activate == 0
rescue Exception => e
OpenNebula.log_error(e.message)
OpenNebula.log_error(e.backtrace)

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore

View File

@ -25,6 +25,5 @@ template64 = STDIN.read
deploy_id = ARGV[0]
xpath_filter = VXLANDriver::XPATH_FILTER
hm = VXLANDriver.from_base64(template64, xpath_filter, deploy_id)
exit hm.activate
exit hm.run_hooks(ARGV, template64) if hm.activate == 0

View File

@ -0,0 +1,3 @@
# Do not track files in this directory except for .gitignore file
*
!.gitignore