Skip to content

Commit

Permalink
Refactor Phut::Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jun 12, 2016
1 parent a604dac commit b139a17
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 96 deletions.
90 changes: 0 additions & 90 deletions lib/phut/configuration.rb

This file was deleted.

47 changes: 44 additions & 3 deletions lib/phut/parser.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,61 @@
# frozen_string_literal: true
require 'phut/configuration'
require 'phut/link'
require 'phut/null_logger'
require 'phut/syntax'
require 'phut/vswitch'

module Phut
# Configuration DSL parser.
class Parser
def initialize(file, logger = NullLogger.new)
@file = file
@netns = []
@logger = logger
end

def parse
Configuration.new do |config|
Syntax.new(config).instance_eval IO.read(@file), @file
Syntax.new(@netns).instance_eval IO.read(@file), @file
update_vswitch_ports
update_vhost_interfaces
update_netns_interfaces
end

private

def update_vswitch_ports
Link.each do |each|
maybe_connect_link_to_vswitch each
end
end

def maybe_connect_link_to_vswitch(link)
Vswitch.select { |each| link.connect_to?(each) }.each do |each|
each.add_port link.device(each.name)
end
end

def update_vhost_interfaces
Vhost.each do |each|
each.device = find_network_device(each)
end
end

def update_netns_interfaces
@netns.each do |each|
netns =
Netns.create(name: each[:name],
ip_address: each[:ip], netmask: each[:netmask],
route: { net: each[:net], gateway: each[:gateway] })
netns.device = find_network_device(each.name)
end
end

def find_network_device(name)
Link.each do |each|
device = each.device(name)
return device if device
end
nil
end
end
end
6 changes: 3 additions & 3 deletions lib/phut/syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
module Phut
# DSL syntax definitions.
class Syntax
def initialize(config)
@config = config
def initialize(netns)
@netns = netns
end

def vswitch(alias_name = nil, &block)
Expand All @@ -33,7 +33,7 @@ def link(name_a, name_b)
end

def netns(name, &block)
@config.netns << NetnsDirective.new(name, &block)
@netns << NetnsDirective.new(name, &block)
end
end
end

0 comments on commit b139a17

Please sign in to comment.