bin/trema in trema-0.9.0 vs bin/trema in trema-0.10.0

- old
+ new

@@ -9,10 +9,11 @@ require 'trema' # OpenFlow controller framework. module Trema # trema command. + # rubocop:disable ModuleLength module App extend GLI::App desc 'Displays the current runtime version' program_desc 'Trema command-line tool' @@ -48,26 +49,37 @@ c.action do |global_options, options, args| Phut.pid_dir = options[:pid_dir] Phut.log_dir = options[:log_dir] Phut.socket_dir = options[:socket_dir] Pio::OpenFlow.switch_version('OpenFlow13') if options[:openflow13] + begin + options[:logging_level] = + { debug: ::Logger::DEBUG, + info: ::Logger::INFO, + warn: ::Logger::WARN, + error: ::Logger::ERROR, + fatal: ::Logger::FATAL, + unknown: ::Logger::UNKNOWN + }.fetch(options[:logging_level].to_sym) + options[:logging_level] = ::Logger::DEBUG if global_options[:verbose] + rescue KeyError + raise(ArgumentError, + "Invalid log level: #{options[:logging_level]}") + end require 'trema/switch' Trema::Command.new.run(args, global_options.merge(options)) end end desc 'Print all flow entries' - arg_name 'switches...' + arg_name 'switch' command :dump_flows do |c| - c.desc 'Location to put socket files' + c.desc 'Location to find socket files' c.flag [:S, :socket_dir], default_value: Trema::DEFAULT_SOCKET_DIR c.action do |_global_options, options, args| - Phut.socket_dir = options[:socket_dir] - args.each do |each| - puts Trema.fetch(each, options.fetch(:socket_dir)).dump_flows - end + puts Trema.fetch(args.first, options.fetch(:socket_dir)).dump_flows end end desc 'Sends UDP packets to destination host' command :send_packets do |c| @@ -80,12 +92,12 @@ c.desc 'Location to put socket files' c.flag [:S, :socket_dir], default_value: Trema::DEFAULT_SOCKET_DIR c.action do |_global_options, options, _args| - fail '--source option is mandatory' if options[:source].nil? - fail '--dest option is mandatory' if options[:dest].nil? + raise '--source option is mandatory' if options[:source].nil? + raise '--dest option is mandatory' if options[:dest].nil? dest = Trema.fetch(options.fetch(:dest), options.fetch(:socket_dir)) Phut::VhostDaemon. process(options.fetch(:source), options.fetch(:socket_dir)). send_packets(dest, options.fetch(:npackets).to_i) end @@ -124,22 +136,32 @@ rxstats.each { |each| puts " #{each}" } end end end + desc 'Reset stats of packets' + command :reset_stats do |c| + c.desc 'Location to find socket files' + c.flag [:S, :socket_dir], default_value: Trema::DEFAULT_SOCKET_DIR + + c.action do |_global_options, options, _args| + Trema.vhosts(options[:socket_dir]).each(&:reset_stats) + end + end + desc "Brings a switch's specified port up" command :port_up do |c| c.desc 'switch name' c.flag [:s, :switch] c.desc 'port' c.flag [:p, :port] c.desc 'Location to put socket files' c.flag [:S, :socket_dir], default_value: Trema::DEFAULT_SOCKET_DIR c.action do |_global_options, options, _args| - fail '--switch option is mandatory' if options[:switch].nil? - fail '--port option is mandatory' if options[:port].nil? + raise '--switch option is mandatory' if options[:switch].nil? + raise '--port option is mandatory' if options[:port].nil? Trema.trema_processes(options[:socket_dir]).each do |trema| begin trema.port_up(options[:switch], options[:port].to_i) rescue next @@ -156,12 +178,12 @@ c.flag [:p, :port] c.desc 'Location to put socket files' c.flag [:S, :socket_dir], default_value: Trema::DEFAULT_SOCKET_DIR c.action do |_global_options, options, _args| - fail '--switch option is mandatory' if options[:switch].nil? - fail '--port option is mandatory' if options[:port].nil? + raise '--switch option is mandatory' if options[:switch].nil? + raise '--port option is mandatory' if options[:port].nil? Trema.trema_processes(options[:socket_dir]).each do |trema| begin trema.port_down(options[:switch], options[:port].to_i) rescue next @@ -232,39 +254,42 @@ end end end end + # rubocop:disable LineLength desc 'Opens a new shell or runs a command in the specified network namespace' arg_name 'name [command]' command :netns do |c| - c.action do |global_options, options, args| + c.action do |_global_options, _options, args| command_args = args[1..-1] - if command_args && command_args.size > 0 + if command_args && !command_args.empty? system "sudo ip netns exec #{args[0]} #{command_args.join(' ')}" else system "sudo ip netns exec #{args[0]} #{ENV['SHELL']}" end end end + # rubocop:enable LineLength default_command :help on_error do |e| case e when OptionParser::ParseError, - Trema::NoControllerDefined, - Trema::InvalidLoggingLevel, - Phut::OpenVswitch::AlreadyRunning, - GLI::UnknownCommandArgument + Trema::NoControllerDefined, + Phut::OpenVswitch::AlreadyRunning, + GLI::UnknownCommandArgument, + ArgumentError true when Interrupt exit false else # show backtrace - fail e + raise e end end exit run(ARGV) end + # rubocop:enable ModuleLength end