Class: Wire::Resource::NetworkInjection

Inherits:
ResourceBase show all
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

Attributes inherited from ResourceBase

#name

Instance Method Summary collapse

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

#containersObject

ids of containers of appgroup



22
23
24
# File 'lib/wire/resource/network_injection.rb', line 22

def containers
  @containers
end

#executablesObject

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

#networksObject

names of networks to attach appgroup containers to



25
26
27
# File 'lib/wire/resource/network_injection.rb', line 25

def networks
  @networks
end

#statefileObject

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_paramsObject

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

#downObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


73
74
75
# File 'lib/wire/resource/network_injection.rb', line 73

def exist?
  up?
end

#to_sObject

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

#upObject

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?

Returns:

  • (Boolean)


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, 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