class Trema::Command

trema command rubocop:disable ClassLength

Attributes

controller[R]

Public Class Methods

unix_domain_socket(name, check = false) click to toggle source
# File lib/trema/command.rb, line 7
def self.unix_domain_socket(name, check = false)
  path = File.expand_path(File.join(Phut.socket_dir, "#{name}.ctl"))
  if check && !FileTest.socket?(path)
    raise "Socket file #{path} does not exist."
  end
  'drbunix:' + path
end

Public Instance Methods

fetch(name) click to toggle source
# File lib/trema/command.rb, line 81
def fetch(name)
  @phut.fetch(name)
rescue KeyError
  raise "Host not found: #{name}"
end
kill(name) click to toggle source

rubocop:enable MethodLength rubocop:enable AbcSize

# File lib/trema/command.rb, line 41
def kill(name)
  @phut.fetch(name).stop
end
killall() click to toggle source

rubocop:disable CyclomaticComplexity

# File lib/trema/command.rb, line 55
def killall
  @controller.logger.debug 'Shutting down...' if @controller
  @controller.stop
  @controller_thread.kill if @controller_thread
  @phut_run_thread.kill if @phut_run_thread
  @phut.stop if @phut
  FileUtils.rm pid_file if FileTest.exists?(pid_file)
  DRb.stop_service
  exit 0 if @daemon
end
port_down(switch_name, port) click to toggle source
# File lib/trema/command.rb, line 76
def port_down(switch_name, port)
  switch = @phut.fetch(switch_name)
  switch.bring_port_down(port)
end
port_up(switch_name, port) click to toggle source
# File lib/trema/command.rb, line 71
def port_up(switch_name, port)
  switch = @phut.fetch(switch_name)
  switch.bring_port_up(port)
end
run(args, options) click to toggle source

rubocop:disable AbcSize rubocop:disable MethodLength

# File lib/trema/command.rb, line 19
def run(args, options)
  @args = args
  @daemon = options[:daemonize]
  $LOAD_PATH.unshift File.expand_path(File.dirname(@args.first))
  load @args.first
  port_number = (options[:port] || Controller::DEFAULT_TCP_PORT).to_i
  @controller =
    Controller.create(port_number, options.fetch(:logging_level))

  trap_signals
  create_pid_file
  start_phut(options[:conf])

  if options[:daemonize]
    run_as_daemon { start_controller_and_drb_threads }
  else
    start_controller_and_drb_threads
  end
end
up(name) click to toggle source

rubocop:enable CyclomaticComplexity

# File lib/trema/command.rb, line 67
def up(name)
  @phut.fetch(name).run
end

Private Instance Methods

create_pid_file() click to toggle source

rubocop:enable MethodLength

# File lib/trema/command.rb, line 152
def create_pid_file
  raise "#{name} is already running." if running?
  update_pid_file
end
name() click to toggle source
# File lib/trema/command.rb, line 169
def name
  @controller.name
end
pid_file() click to toggle source
# File lib/trema/command.rb, line 161
def pid_file
  File.join Phut.pid_dir, "#{name}.pid"
end
redirect_stdio_to_devnull() click to toggle source
# File lib/trema/command.rb, line 123
def redirect_stdio_to_devnull
  open('/dev/null', 'r+') do |devnull|
    $stdin.reopen devnull
    $stdout.reopen devnull
    $stderr.reopen devnull
  end
end
run_as_daemon() { || ... } click to toggle source
# File lib/trema/command.rb, line 115
def run_as_daemon
  fork do
    redirect_stdio_to_devnull
    update_pid_file
    yield
  end
end
running?() click to toggle source
# File lib/trema/command.rb, line 165
def running?
  FileTest.exists? pid_file
end
start_controller_and_drb_threads() click to toggle source

rubocop:enable MethodLength

# File lib/trema/command.rb, line 105
def start_controller_and_drb_threads
  DRb.start_service Command.unix_domain_socket(@controller.name), self
  @controller_thread = Thread.new { @controller.run @args[1..-1] }
  @controller_thread.abort_on_exception = true
  DRb.thread.join
rescue
  killall
  raise $ERROR_INFO
end
start_phut(config_file) click to toggle source

rubocop:disable MethodLength

# File lib/trema/command.rb, line 90
def start_phut(config_file)
  return unless config_file
  system 'sudo -v'
  @phut = Phut::Parser.new(@controller.logger).parse(config_file)
  @phut_run_thread = Thread.start { @phut.run }
  @phut_run_thread.join
  Thread.start { start_sudo_credential_update }
rescue ScriptError, NameError
  killall
  raise $ERROR_INFO
rescue StandardError
  raise $ERROR_INFO unless @stop
end
start_sudo_credential_update() click to toggle source
# File lib/trema/command.rb, line 173
def start_sudo_credential_update
  loop do
    system 'sudo -v'
    sleep 60
  end
end
stop() click to toggle source
# File lib/trema/command.rb, line 131
def stop
  @stop = true
end
trap_signals() click to toggle source

rubocop:disable MethodLength

# File lib/trema/command.rb, line 136
def trap_signals
  @killall_thread = Thread.start do
    loop do
      if @stop
        killall
        break
      end
      sleep 1
    end
  end
  @killall_thread.abort_on_exception = true
  Signal.trap(:TERM) { stop }
  Signal.trap(:INT) { stop }
end
update_pid_file() click to toggle source
# File lib/trema/command.rb, line 157
def update_pid_file
  File.open(pid_file, 'w') { |file| file << Process.pid }
end