Class: IPTables::TableComparison

Inherits:
Object
  • Object
show all
Defined in:
lib/iptables/tables.rb

Instance Method Summary collapse

Constructor Details

#initialize(table1, table2) ⇒ TableComparison

Returns a new instance of TableComparison.



328
329
330
331
332
333
334
335
336
# File 'lib/iptables/tables.rb', line 328

def initialize(table1, table2)
  raise "must provide two tables" unless (table1.class == IPTables::Table) and (table2.class == IPTables::Table)
  raise "table names should match" unless table1.name == table2.name
  @table1 = table1
  @table2 = table2

  @including_comments = true
  @compared = false
end

Instance Method Details

#as_arrayObject



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/iptables/tables.rb', line 390

def as_array
  self.compare
  array = []
  return array if self.equal?
  array << "Changed table: #{@table1.name}"
  if self.missing.any?
    self.missing.each{ |chain_name|
      array << 'Missing chain:'
      array.concat @table1.chains[chain_name].all_as_array
    }
  end
  if self.new.any?
    self.new.each{ |chain_name|
      array << 'New chain:'
      array.concat @table2.chains[chain_name].all_as_array
    }
  end
  if self.changed.any?
    self.changed.each{ |chain_comparison|
      array.concat chain_comparison.as_array
    }
  end
  return array
end

#changedObject



385
386
387
388
# File 'lib/iptables/tables.rb', line 385

def changed
  self.compare
  return @chain_diffs
end

#compareObject



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/iptables/tables.rb', line 338

def compare
  return if @compared
  @equal = true

  table1_chains = @table1.chains.keys.sort
  table2_chains = @table2.chains.keys.sort
  @only_in_current = table1_chains - table2_chains
  @only_in_new = table2_chains - table1_chains
  @equal = false if @only_in_current.any? or @only_in_new.any?

  @chain_diffs = []
  (table1_chains - @only_in_current - @only_in_new).each{ |chain|
    chain_comparison = IPTables::ChainComparison.new(@table1.chains[chain], @table2.chains[chain])
    if @including_comments
      chain_comparison.include_comments
    else
      chain_comparison.ignore_comments
    end
    next if chain_comparison.equal?

    @equal = false
    @chain_diffs << chain_comparison
  }

  return nil
end

#equal?Boolean

Returns:



415
416
417
418
# File 'lib/iptables/tables.rb', line 415

def equal?
  self.compare
  return @equal
end

#ignore_commentsObject



365
366
367
368
# File 'lib/iptables/tables.rb', line 365

def ignore_comments
  @including_comments = false
  @compared = false
end

#include_commentsObject



370
371
372
373
# File 'lib/iptables/tables.rb', line 370

def include_comments
  @including_comments = true
  @compared = false
end

#missingObject



375
376
377
378
# File 'lib/iptables/tables.rb', line 375

def missing
  self.compare
  return @only_in_current
end

#newObject



380
381
382
383
# File 'lib/iptables/tables.rb', line 380

def new
  self.compare
  return @only_in_new
end