Sha256: a35309b76583d6372d7e66afbcf6b826b85e91fbd1e6a1f2b4e0f04864ba3e89

Contents?: true

Size: 914 Bytes

Versions: 5

Compression:

Stored size: 914 Bytes

Contents

require 'phut/null_logger'
require 'phut/shell_runner'

module Phut
  # `ip netns ...` command runner
  class Netns
    include ShellRunner

    attr_reader :name
    attr_accessor :network_device

    def initialize(options, name, logger = NullLogger.new)
      @name = name
      @options = options
      @logger = logger
    end

    # rubocop:disable AbcSize
    def run
      sh "sudo ip netns add #{name}"
      sh "sudo ip link set dev #{network_device} netns #{name}"
      sh "sudo ip netns exec #{name} ifconfig lo 127.0.0.1"
      sh "sudo ip netns exec #{name}"\
         " ifconfig #{network_device} #{ip} netmask #{netmask}"
      sh "sudo ip netns exec #{name} route add -net #{net} gw #{gateway}"
    end
    # rubocop:enable AbcSize

    def maybe_stop
      sh "sudo ip netns delete #{name}"
    end

    def method_missing(message, *_args)
      @options.__send__ :[], message
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
phut-0.7.4 lib/phut/netns.rb
phut-0.7.3 lib/phut/netns.rb
phut-0.7.2 lib/phut/netns.rb
phut-0.7.1 lib/phut/netns.rb
phut-0.7.0 lib/phut/netns.rb