Sha256: 218b739fd6d2bd5c992927a032a46ebe882b302ab4dcebe98c44ddad9e0bad82

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 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 :network_device

    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 #{@network_device}",
       "-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.6.2 lib/phut/vhost.rb
phut-0.6.1 lib/phut/vhost.rb
phut-0.6.0 lib/phut/vhost.rb
phut-0.5.0 lib/phut/vhost.rb
phut-0.4.0 lib/phut/vhost.rb