Class: IPTables::TablesComparison

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

Instance Method Summary collapse

Constructor Details

#initialize(tables1, tables2) ⇒ TablesComparison

Returns a new instance of TablesComparison.



117
118
119
120
121
122
123
124
125
# File 'lib/iptables/tables.rb', line 117

def initialize(tables1, tables2)
  raise "must provide two tables" unless (tables1.class == IPTables::Tables) and (tables2.class == IPTables::Tables)
  @tables1 = tables1
  @tables2 = tables2
  @table_diffs = []

  @including_comments = true
  @compared = false
end

Instance Method Details

#as_arrayObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/iptables/tables.rb', line 177

def as_array
  self.compare
  array = []
  return array if self.equal?
  if @only_in_current.any?
    @only_in_current.each{ |table_name|
      array << "Missing table: #{table_name}"
      array.concat @tables1.tables[table_name].as_array
    }
  end
  if @only_in_new.any?
    @only_in_new.each{ |table_name|
      array << "New table: #{table_name}"
      next if @tables2.tables[table_name].nil?
      array.concat @tables2.tables[table_name].as_array
    }
  end
  if @table_diffs.any?
    @table_diffs.each{ |table_comparison|
      array.concat table_comparison.as_array
    }
  end
  return array
end

#compareObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/iptables/tables.rb', line 127

def compare
  return if @compared
  @equal = true

  tables1_tables = @tables1.tables.keys.sort
  tables2_tables = @tables2.tables.keys.sort
  @only_in_current = (tables1_tables - tables2_tables).reject{ |t| @tables1.tables[t].nil? }
  @only_in_new = (tables2_tables - tables1_tables).reject{ |t| @tables2.tables[t].nil? }
  @equal = false if @only_in_current.any? or @only_in_new.any?

  @table_diffs = []
  (tables1_tables - @only_in_current - @only_in_new).each{ |table|
    table1 = @tables1.tables[table]
    table2 = @tables2.tables[table]

    # nil tables are only created by policy, never parsed
    # they mean "use the parsed policy here"
    # which means "for comparison purposes, they are always equal"
    next if table1.nil? or table2.nil?

    table_comparison = IPTables::TableComparison.new(table1, table2)
    if @including_comments
      table_comparison.include_comments
    else
      table_comparison.ignore_comments
    end
    next if table_comparison.equal?

    @equal = false
    @table_diffs << table_comparison
  }

  return nil
end

#equal?Boolean

Returns:

  • (Boolean)


172
173
174
175
# File 'lib/iptables/tables.rb', line 172

def equal?
  self.compare
  return @equal
end

#ignore_commentsObject



162
163
164
165
# File 'lib/iptables/tables.rb', line 162

def ignore_comments
  @including_comments = false
  @compared = false
end

#include_commentsObject



167
168
169
170
# File 'lib/iptables/tables.rb', line 167

def include_comments
  @including_comments = true
  @compared = false
end