Sha256: 252de4184f4b3d707815cf593656e93d65e657d4d81e58522dfc2017a59aad33

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents

require 'phut/null_logger'
require 'phut/setting'
require 'phut/shell_runner'
require 'pio/mac'

module Phut
  # An interface class to vhost emulation utility program.
  class Vhost
    include ShellRunner

    attr_reader :ip_address
    attr_reader :mac_address
    attr_accessor :interface

    def initialize(ip_address, mac_address, promisc,
                   name = nil, logger = NullLogger.new)
      @ip_address = ip_address
      @promisc = promisc
      @name = name
      @mac_address = mac_address
      @logger = logger
    end

    def name
      @name || @ip_address
    end

    def to_s
      "vhost (name = #{name}, IP address = #{@ip_address})"
    end

    def run(all_hosts = [])
      sh "rvmsudo vhost run #{run_options all_hosts}"
    end

    def stop
      fail "vhost (name = #{name}) is not running!" unless running?
      sh "vhost stop -n #{name} -s #{Phut.socket_dir}"
    end

    def maybe_stop
      return unless running?
      stop
    end

    def running?
      FileTest.exists?(pid_file)
    end

    private

    def run_options(all_hosts)
      ["-n #{name}",
       "-I #{interface}",
       "-i #{@ip_address}",
       "-m #{@mac_address}",
       "-a #{arp_entries all_hosts}",
       @promisc ? '--promisc' : nil,
       "-P #{Phut.pid_dir}",
       "-L #{Phut.log_dir}",
       "-S #{Phut.socket_dir}"].compact.join(' ')
    end

    def arp_entries(all_hosts)
      all_hosts.map do |each|
        "#{each.ip_address}/#{each.mac_address}"
      end.join(',')
    end

    def pid_file
      "#{Phut.pid_dir}/vhost.#{name}.pid"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
phut-0.3.1 lib/phut/vhost.rb
phut-0.3.0 lib/phut/vhost.rb
phut-0.2.4 lib/phut/vhost.rb
phut-0.2.3 lib/phut/vhost.rb
phut-0.2.2 lib/phut/vhost.rb