Sha256: d1fbe3dc32ca03b3ca349768028d6aaef4e20a4fb16fb2676a3c44b9413cc158

Contents?: true

Size: 1.56 KB

Versions: 8

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 :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 = [])
      @all_hosts ||= all_hosts
      sh "rvmsudo vhost run #{run_options}"
    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
      ["-n #{name}",
       "-I #{@network_device}",
       "-i #{@ip_address}",
       "-m #{@mac_address}",
       "-a #{arp_entries}",
       @promisc ? '--promisc' : nil,
       "-P #{Phut.pid_dir}",
       "-L #{Phut.log_dir}",
       "-S #{Phut.socket_dir}"].compact.join(' ')
    end

    def arp_entries
      @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

8 entries across 8 versions & 1 rubygems

Version Path
phut-0.6.10 lib/phut/vhost.rb
phut-0.6.9 lib/phut/vhost.rb
phut-0.6.8 lib/phut/vhost.rb
phut-0.6.7 lib/phut/vhost.rb
phut-0.6.6 lib/phut/vhost.rb
phut-0.6.5 lib/phut/vhost.rb
phut-0.6.4 lib/phut/vhost.rb
phut-0.6.3 lib/phut/vhost.rb