Sha256: 42258f8308e76f36e33f9a920a5f668ff593433f448eeab8f05de1449b44e87b

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

require 'active_support/core_ext/class/attribute_accessors'
require 'phut/null_logger'
require 'phut/shell_runner'

module Phut
  # `ip netns ...` command runner
  class Netns
    cattr_accessor(:all, instance_reader: false) { [] }

    def self.create(options, name, logger = NullLogger.new)
      new(options, name, logger).tap { |netns| all << netns }
    end

    def self.each(&block)
      all.each(&block)
    end

    include ShellRunner

    attr_reader :name
    attr_accessor :network_device

    def initialize(options, name, logger)
      @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 stop
      sh "sudo ip netns delete #{name}"
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
phut-0.7.7 lib/phut/netns.rb
phut-0.7.6 lib/phut/netns.rb
phut-0.7.5 lib/phut/netns.rb