lib/phut/netns.rb in phut-0.7.4 vs lib/phut/netns.rb in phut-0.7.5

- old
+ new

@@ -1,17 +1,28 @@ +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 = NullLogger.new) + def initialize(options, name, logger) @name = name @options = options @logger = logger end @@ -24,10 +35,10 @@ " 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 + def stop sh "sudo ip netns delete #{name}" end def method_missing(message, *_args) @options.__send__ :[], message