class OnetableTerminator::Iptables::Parser

Constants

COLUMN_COUNT
NIC_COLUMN
NUMBER_COLUMN
TARGET_COLUMN
TARGET_COLUMN_COUNT
TARGET_IO_COLUMN
TARGET_VM_ID_COLUMN

Public Instance Methods

parse_rules(output) click to toggle source
# File lib/onetable_terminator/iptables/parser.rb, line 12
def parse_rules(output)
  nics = {}
  lines = output.lines.to_a
  normalize_output!(lines)

  lines.each do |line|
    parsed_line = parse_line line
    nic_name = parsed_line[:nic]

    nic = nics[nic_name]
    unless nic
      nic = OnetableTerminator::Structures::Nic.new nic_name
      nics[nic_name] = nic
    end
    nic.add_rule OnetableTerminator::Structures::Rule.new parsed_line
  end

  nics.values
end

Private Instance Methods

normalize_output!(lines) click to toggle source
# File lib/onetable_terminator/iptables/parser.rb, line 34
def normalize_output!(lines)
  lines.shift 2

  unless lines.empty?
    accept_line = lines.pop
    raise OnetableTerminator::Errors::ParsingError, "Missing last ACCEPT rule" unless accept_line =~ /ACCEPT/
  end
end
parse_line(line) click to toggle source
# File lib/onetable_terminator/iptables/parser.rb, line 43
def parse_line(line)
  logger.debug "Parsing line #{line.inspect}"
  splitted_line = line.split
  raise OnetableTerminator::Errors::ParsingError, "Cannot parse line #{line.inspect}" unless splitted_line.size == COLUMN_COUNT

  target = parse_target splitted_line[TARGET_COLUMN]
  number = splitted_line[NUMBER_COLUMN]

  raise OnetableTerminator::Errors::ParsingError, "Cannot parse line number for line #{line.inspect}" unless number =~ /\A\d+\z/

  parsed_line = { number: number.to_i, nic: splitted_line[NIC_COLUMN], raw_line: line, target: splitted_line[TARGET_COLUMN] }.merge target
  logger.debug "Line parsed: #{parsed_line.inspect}"

  parsed_line
end
parse_target(target) click to toggle source
# File lib/onetable_terminator/iptables/parser.rb, line 59
def parse_target(target)
  slices = target.split('-', TARGET_COLUMN_COUNT)

  raise OnetableTerminator::Errors::ParsingError, "Cannot parse target #{target.inspect}" unless slices.size == TARGET_COLUMN_COUNT

  vm_id = slices[TARGET_VM_ID_COLUMN]
  io = slices[TARGET_IO_COLUMN]

  raise OnetableTerminator::Errors::ParsingError, "Cannot parse VM ID for chain #{target.inspect}" unless vm_id =~ /\A\d+\z/
  raise OnetableTerminator::Errors::ParsingError, "Cannot detect input/output for chain #{target.inspect}" unless io == 'i' || io == 'o'

  { vm_id: vm_id.to_i, io: io }
end