Skip to content

Commit

Permalink
Update phut
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Oct 24, 2016
1 parent f830361 commit 3711a30
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 42 deletions.
6 changes: 6 additions & 0 deletions Gemfile
Expand Up @@ -2,4 +2,10 @@ source 'https://rubygems.org'

gemspec development_group: :test

git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
"https://github.com/#{repo_name}.git"
end

gem 'pio', github: 'trema/pio', branch: 'develop'
gem 'phut', github: 'trema/phut', branch: 'develop'
36 changes: 32 additions & 4 deletions bin/trema
Expand Up @@ -72,7 +72,10 @@ module Trema
c.flag [:S, :socket_dir], default_value: Trema::DEFAULT_SOCKET_DIR

c.action do |_global_options, options, args|
puts Trema.fetch(args.first, options.fetch(:socket_dir)).dump_flows
vswitch = Trema.vswitch(options[:socket_dir]).map do |each|
each.find_by(name: args[0])
end.compact
vswitch.each { |each| puts each.dump_flows }
end
end

Expand Down Expand Up @@ -195,7 +198,17 @@ module Trema

c.action do |_global_options, options, args|
help_now! if args.size != 1
Trema.fetch(args[0], options[:socket_dir]).stop

vswitch = Trema.vswitch(options[:socket_dir]).map do |each|
each.find_by(name: args[0])
end.compact
vhost = Trema.vhost(options[:socket_dir]).map do |each|
each.find_by(name: args[0])
end.compact

raise %("#{args[0]}" does not exist) if (vswitch + vhost).empty?
vswitch.each(&:stop)
vhost.each(&:kill)
end
end

Expand All @@ -207,7 +220,11 @@ module Trema

c.action do |_global_options, options, args|
help_now! if args.size != 2
Trema.fetch(args, options[:socket_dir]).stop

vlink = Trema.vlink(options[:socket_dir]).map do |each|
each.find(args)
end
vlink.each(&:destroy)
end
end

Expand All @@ -219,7 +236,18 @@ module Trema

c.action do |_global_options, options, args|
help_now! if args.size != 1
Trema.fetch(args[0], options[:socket_dir]).run

vswitch = Trema.vswitch(options[:socket_dir]).map do |each|
each.find_by(name: args[0])
end.compact
vhost = Trema.vhost(options[:socket_dir]).map do |each|
each.find_by(name: args[0])
end.compact

raise %("#{args[0]}" does not exist) if (vswitch + vhost).empty?

vswitch.each { |each| each.run(Controller::DEFAULT_TCP_PORT) }
vhost.each(&:run)
end
end

Expand Down
3 changes: 2 additions & 1 deletion features/trema_netns/netns.feature
Expand Up @@ -48,8 +48,9 @@ Feature: netns
Then the stdout should contain "<message>"
Examples:
| command | message |
| trema netns host1 ip add show host1 | 192.168.1.2 |
| trema netns host1 -- ping -c1 192.168.1.3 | 1 packets transmitted, 1 received, |
| trema netns host2 -- ping -c1 192.168.1.2 | 1 packets transmitted, 1 received, |
| trema netns host1 "ping -c1 192.168.1.3" | 1 packets transmitted, 1 received, |
| trema netns host1 "ip addr show lo" | 127.0.0.1 |
| trema netns host1 ls $PWD | simple_hub.conf |

11 changes: 6 additions & 5 deletions features/trema_start/start.feature
Expand Up @@ -29,22 +29,23 @@ Feature: start
link '0xabc', 'host1'
"""
And I successfully run `trema run switch_ready_controller.rb -c trema.conf -d`
And sleep 10

@sudo
Scenario: stop and start a switch
Given I successfully run `trema stop 0xabc`
And sleep 3
When I successfully run `trema start 0xabc`
And sleep 10
And sleep 3
Then the file "SwitchReadyController.log" should contain:
"""
Switch 0xabc connected again.
"""

@sudo
@sudo @wip
Scenario: stop and start host_name
Given I successfully run `trema stop host1`
And sleep 3
And sleep 10
When I successfully run `trema start host1`
And sleep 10
Then the file named "vhost.host1.pid" should exist
Expand All @@ -55,7 +56,7 @@ Feature: start
Then the exit status should not be 0
And the output should contain:
"""
Phut::OpenVswitch::AlreadyRunning: Open vSwitch (dpid = 2748) is already running!
An Open vSwitch #<Vswitch name: "0xabc", dpid: 0xabc, openflow_version: 1.0, tcp_port: 6653> is already running
"""

@sudo @wip
Expand All @@ -73,5 +74,5 @@ Feature: start
Then the exit status should not be 0
And the output should contain:
"""
"NO_SUCH_NAME" does not exist.
"NO_SUCH_NAME" does not exist
"""
2 changes: 1 addition & 1 deletion features/trema_stop/stop.feature
Expand Up @@ -46,5 +46,5 @@ Feature: stop
Then the exit status should not be 0
And the output should contain:
"""
"NO_SUCH_NAME" does not exist.
"NO_SUCH_NAME" does not exist
"""
58 changes: 28 additions & 30 deletions lib/trema/command.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true
require 'English'
require 'phut'
require 'phut/vswitch'

module Trema
# trema command
Expand Down Expand Up @@ -35,50 +37,47 @@ def run(args, options)
end
# rubocop:enable MethodLength

def kill(name)
@phut.fetch(name).stop
end

def delete_link(endpoint1, endpoint2)
target = @phut.fetch([endpoint1, endpoint2])
begin
target.stop
rescue
true
end
end

# rubocop:disable CyclomaticComplexity
# rubocop:disable MethodLength
# rubocop:disable AbcSize
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
Phut::Vswitch.destroy_all
Phut::Vhost.destroy_all
Phut::Netns.destroy_all
Phut::Link.destroy_all
FileUtils.rm pid_file if FileTest.exist?(pid_file)
DRb.stop_service
exit 0 if @options[:daemonize]
end
# rubocop:enable CyclomaticComplexity

def up(name)
@phut.fetch(name).run
end
# rubocop:enable MethodLength
# rubocop:enable AbcSize

def port_up(switch_name, port)
switch = @phut.fetch(switch_name)
switch.bring_port_up(port)
Phut::Vswitch.find_by(name: switch_name).bring_port_up(port)
end

def port_down(switch_name, port)
switch = @phut.fetch(switch_name)
switch.bring_port_down(port)
Phut::Vswitch.find_by(name: switch_name).bring_port_down(port)
end

def vswitch
Phut::Vswitch
end

def vhost
Phut::Vhost
end

def vlink
Phut::Link
end

# FIXME
def fetch(name)
@phut.fetch(name)
rescue KeyError
raise "Host not found: #{name}"
Phut::Vswitch.find_by(name: name) || Phut::Vhost.find_by(name: name)
end

private
Expand Down Expand Up @@ -115,9 +114,8 @@ def trap_signals
def start_phut
return unless @options[:conf]
system 'sudo -v'
@phut = Phut::Parser.new(@options[:conf])
@phut.parse
@phut_run_thread = Thread.start { @phut.run }
@phut_run_thread =
Thread.start { Phut::Parser.new(@options[:conf]).parse }
@phut_run_thread.join
Thread.start { start_sudo_credential_update }
rescue ScriptError, NameError, Errno::ENOENT
Expand Down
18 changes: 18 additions & 0 deletions lib/trema/drb.rb
Expand Up @@ -26,6 +26,24 @@ def self.vhosts(socket_dir = Phut.socket_dir)
vhosts.map { |each| DRbObject.new_with_uri("drbunix:#{each}") }
end

def self.vswitch(socket_dir)
trema_processes(socket_dir).map do |trema|
trema.try(:vswitch)
end.compact
end

def self.vhost(socket_dir)
trema_processes(socket_dir).map do |trema|
trema.try(:vhost)
end.compact
end

def self.vlink(socket_dir)
trema_processes(socket_dir).map do |trema|
trema.try(:vlink)
end.compact
end

def self.fetch(name, socket_dir)
trema_processes(socket_dir).each do |trema|
begin
Expand Down
2 changes: 1 addition & 1 deletion trema.gemspec
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |gem|
gem.test_files = `git ls-files -- {spec,features}/*`.split("\n")

gem.add_dependency 'bundler', '>= 1.11.2', '< 2.0'
gem.add_dependency 'gli', '~> 2.13.4'
gem.add_dependency 'gli', '~> 2.14.0'
gem.add_dependency 'phut', '~> 0.7.7'
gem.add_dependency 'pio', '~> 0.30.0'

Expand Down

0 comments on commit 3711a30

Please sign in to comment.