class Wire::Resource::NetworkInjection
Network Injection Resource
controls the allocation of host and container network resources for all containers of an application group
Attributes
ids of containers of appgroup
executables
[Hash] of binaries needed to control the resource
names of networks to attach appgroup containers to
filename of state file
Public Class Methods
initialize the object with given appgroup name
and container
ids params: name
: Application group name networks
: [Array] of network objects to attach containers to containers
: [Array] of container ids (i.e. from fig ps -q) statefile
: Optional name of (network) statefile
# File lib/wire/resource/network_injection.rb, line 37 def initialize(name, networks, containers, statefile = nil) super(name) self.containers = containers self.networks = networks self.statefile = statefile begin # try to locate the gem base path and find shell script gem 'dewiring' gem_base_dir = Gem.datadir('dewiring').split('/')[0..-3].join('/') @executables = { :network => File.join(gem_base_dir, 'lib/wire-network-container.sh') } rescue LoadError # use fallback @executables = { :network => '/usr/local/bin/wire-network-container.sh' } end $log.debug "Using network injection script #{@executables[:network]}" end
Public Instance Methods
for the network helper script, construct an array of container devices names and bridge names, i.e. eht1:br0 meaning container will be attached to bridge br0 with eth1. if a network defines a short name, it will be used for the container interface. will check if a network does not have dhcp enable and add a NODHCP flag.
# File lib/wire/resource/network_injection.rb, line 85 def construct_helper_params res = [] networks.each do |network_name, network_data| name = (network_data[:shortname]) ? network_data[:shortname] : network_name line = "#{name}:#{network_name}" (network_data[:dhcp]) || line << ':NODHCP' res << line end res.join(' ') end
detaches network interfaces form containers and bridges
# File lib/wire/resource/network_injection.rb, line 130 def down updown_command :detach end
checks if the bridge is down
# File lib/wire/resource/network_injection.rb, line 125 def down? !(up?) end
calls the verify action to see if container has been networked correctly
# File lib/wire/resource/network_injection.rb, line 73 def exist? up? end
Returns a string representation
# File lib/wire/resource/network_injection.rb, line 135 def to_s "NetworkInjection:[#{name},containers=#{containers.join('/')}," \ "network_args=#{construct_helper_params}]" end
attaches containers to networks
# File lib/wire/resource/network_injection.rb, line 120 def up updown_command :attach end
same as exist?
# File lib/wire/resource/network_injection.rb, line 98 def up? with_helper('verify', [construct_helper_params, containers.join(' ')], '--quiet') do |exec_obj| exec_obj.run return (exec_obj.exitstatus == 0 && count_errors(exec_obj) == 0) end end
Params: cmd
: One of :attach, :detach
# File lib/wire/resource/network_injection.rb, line 109 def updown_command(cmd) $log.debug "About to #{cmd.to_s.capitalize} containers to networks ..." statefile_param = (@statefile) ? "-s #{@statefile}" : '' with_helper(cmd.to_s, [construct_helper_params, containers.join(' ')], statefile_param) do |exec_obj| exec_obj.run return (exec_obj.exitstatus == 0 && count_errors(exec_obj) == 0) end end
calls helper executable with correct action
and given command_arr
array
# File lib/wire/resource/network_injection.rb, line 61 def with_helper(action, params, options = '') # puts "#{@executables[:network]} #{action} --debug -- #{params.join(' ')}" dbg_param = ($log.level == Logger::DEBUG ? '--debug' : '') LocalExecution.with(@executables[:network], [action, dbg_param, options, '--', params].flatten, { :b_sudo => false, :b_shell => false }) do |exec_obj| yield exec_obj end end
Private Instance Methods
counts errors in a text output from given exec_obj
# File lib/wire/resource/network_injection.rb, line 144 def count_errors(exec_obj) num_errors = 0 re = Regexp.new '^ERROR.*' exec_obj.stdout.split("\n").each do |line| $log.debug line next unless line.match(re) $log.debug "Matching error output: #{line}" num_errors += 1 end num_errors end