Class: Wire::Resource::NetworkInjection
- Inherits:
-
ResourceBase
- Object
- ResourceBase
- Wire::Resource::NetworkInjection
- Defined in:
- lib/wire/resource/network_injection.rb
Overview
Network Injection Resource controls the allocation of host and container network resources for all containers of an application group
Instance Attribute Summary collapse
-
#containers ⇒ Object
ids of containers of appgroup.
-
#executables ⇒ Object
executables[Hash] of binaries needed to control the resource. -
#networks ⇒ Object
names of networks to attach appgroup containers to.
-
#statefile ⇒ Object
filename of state file.
Attributes inherited from ResourceBase
Instance Method Summary collapse
-
#construct_helper_params ⇒ Object
for the network helper script, construct an array of container devices names and bridge names, i.e.
-
#down ⇒ Object
detaches network interfaces form containers and bridges.
-
#down? ⇒ Boolean
checks if the bridge is down.
-
#exist? ⇒ Boolean
calls the verify action to see if container has been networked correctly.
-
#initialize(name, networks, containers, statefile = nil) ⇒ NetworkInjection
constructor
initialize the object with given appgroup
nameandcontainerids params:name: Application group namenetworks: [Array] of network objects to attach containers tocontainers: [Array] of container ids (i.e. from fig ps -q)statefile: Optional name of (network) statefile. -
#to_s ⇒ Object
Returns a string representation.
-
#up ⇒ Object
attaches containers to networks.
-
#up? ⇒ Boolean
same as exist?.
-
#updown_command(cmd) ⇒ Object
Params:
cmd: One of :attach, :detach. -
#with_helper(action, params, options = '') ⇒ Object
calls helper executable with correct
actionand givencommand_arrarray.
Constructor Details
#initialize(name, networks, containers, statefile = nil) ⇒ NetworkInjection
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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# 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 |
Instance Attribute Details
#containers ⇒ Object
ids of containers of appgroup
22 23 24 |
# File 'lib/wire/resource/network_injection.rb', line 22 def containers @containers end |
#executables ⇒ Object
executables [Hash] of binaries needed to control the resource
19 20 21 |
# File 'lib/wire/resource/network_injection.rb', line 19 def executables @executables end |
#networks ⇒ Object
names of networks to attach appgroup containers to
25 26 27 |
# File 'lib/wire/resource/network_injection.rb', line 25 def networks @networks end |
#statefile ⇒ Object
filename of state file
28 29 30 |
# File 'lib/wire/resource/network_injection.rb', line 28 def statefile @statefile end |
Instance Method Details
#construct_helper_params ⇒ Object
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.
85 86 87 88 89 90 91 92 93 94 95 |
# 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 |
#down ⇒ Object
detaches network interfaces form containers and bridges
130 131 132 |
# File 'lib/wire/resource/network_injection.rb', line 130 def down updown_command :detach end |
#down? ⇒ Boolean
checks if the bridge is down
125 126 127 |
# File 'lib/wire/resource/network_injection.rb', line 125 def down? !(up?) end |
#exist? ⇒ Boolean
calls the verify action to see if container has been networked correctly
73 74 75 |
# File 'lib/wire/resource/network_injection.rb', line 73 def exist? up? end |
#to_s ⇒ Object
Returns a string representation
135 136 137 138 |
# File 'lib/wire/resource/network_injection.rb', line 135 def to_s "NetworkInjection:[#{name},containers=#{containers.join('/')}," \ "network_args=#{construct_helper_params}]" end |
#up ⇒ Object
attaches containers to networks
120 121 122 |
# File 'lib/wire/resource/network_injection.rb', line 120 def up updown_command :attach end |
#up? ⇒ Boolean
same as exist?
98 99 100 101 102 103 104 105 |
# 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 |
#updown_command(cmd) ⇒ Object
Params: cmd: One of :attach, :detach
109 110 111 112 113 114 115 116 117 |
# 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 |
#with_helper(action, params, options = '') ⇒ Object
calls helper executable with correct action and given command_arr array
61 62 63 64 65 66 67 68 69 |
# File 'lib/wire/resource/network_injection.rb', line 61 def with_helper(action, params, = '') # puts "#{@executables[:network]} #{action} --debug -- #{params.join(' ')}" dbg_param = ($log.level == Logger::DEBUG ? '--debug' : '') LocalExecution.with(@executables[:network], [action, dbg_param, , '--', params].flatten, { :b_sudo => false, :b_shell => false }) do |exec_obj| yield exec_obj end end |