#!/usr/bin/env ruby $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'gli' require 'phut' module Phut class VhostDaemon # /bin/vhost command module App extend GLI::App version Phut::VERSION desc 'Runs a vhost process' command :run do |c| desc 'Host name' c.flag [:n, :name] desc 'Network interface' c.flag [:I, :interface] desc 'IP address' c.flag [:i, :ip_address] desc 'MAC address' c.flag [:m, :mac_address] c.desc 'ARP entries' c.flag [:a, :arp_entries], negatable: false c.desc 'Promisc mode' c.switch [:p, :promisc], negatable: false c.desc 'Location to put pid files' c.flag [:P, :pid_dir], default_value: Phut.pid_dir c.desc 'Location to put log files' c.flag [:L, :log_dir], default_value: Phut.log_dir c.desc 'Location to put socket files' c.flag [:S, :socket_dir], default_value: Phut.socket_dir c.action do |_global_options, options, _args| fail '--name option is mandatory' if options[:name].nil? fail '--interface option is mandatory' if options[:interface].nil? fail '--ip_address option is mandatory' if options[:ip_address].nil? fail '--mac_address option is mandatory' if options[:mac_address].nil? options[:arp_table] = options[:arp_entries].split(','). each_with_object({}) do |each, table| ip_address, mac_address = each.split('/') table[ip_address] = mac_address end VhostDaemon.new(options).run end end desc 'Terminates a vhost process' command :stop do |c| desc 'Host name' c.flag [:n, :name] c.desc 'Location to put socket files' c.flag [:S, :socket_dir], default_value: Phut.socket_dir c.action do |_global_options, options, _args| fail '--name option is mandatory' if options[:name].nil? begin VhostDaemon.process(options[:name], options[:socket_dir]).stop rescue DRb::DRbConnError true # OK end end end desc 'Sends UDP packets to destination host' command :send_packets do |c| desc 'Source host name' c.flag [:s, :source] c.action do |_global_options, options, _args| fail '--source option is mandatory' if options[:source].nil? VhostDaemon.process(options[:source], options[:socket_dir]). send_packets end end default_command :help exit run(ARGV) end end end