Class: IptablesWeb::Configuration

Inherits:
Hash
  • Object
show all
Defined in:
lib/iptables_web/configuration.rb

Constant Summary collapse

CONFIG_FILES =
%W(#{ENV['HOME']}/.iptables-web/config.yml /etc/iptables-web/config.yml)
STATIC_RULES_FILES =
%W(#{ENV['HOME']}/.iptables-web/static_rules /etc/iptables-web/static_rules)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



8
9
10
11
12
13
14
15
# File 'lib/iptables_web/configuration.rb', line 8

def initialize
  CONFIG_FILES.each do |config|
    if load(config)
      @loaded = true
      break
    end
  end
end

Instance Attribute Details

#loadedObject

Returns the value of attribute loaded.



4
5
6
# File 'lib/iptables_web/configuration.rb', line 4

def loaded
  @loaded
end

Class Method Details

.config_dirObject



38
39
40
# File 'lib/iptables_web/configuration.rb', line 38

def self.config_dir
  File.join(ENV['HOME'], '.iptables-web')
end

.static_rulesObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/iptables_web/configuration.rb', line 22

def self.static_rules
  rules = STATIC_RULES_FILES.map do |file|
    File.exist?(file) ? File.read(file) : nil
  end.compact.join("\n").strip
  chains = rules.scan(/\*([a-z]+)(.*?)COMMIT/m)
  if chains && chains.size > 0
    chains.each_with_object({}) do |r, obj|
      chain = r[0]
      obj[chain] ||= []
      obj[chain] = obj[chain] | r[1].split("\n")
    end
  else
    { 'filter' => rules.split("\n") }
  end
end

Instance Method Details

#load(config) ⇒ Object



17
18
19
20
# File 'lib/iptables_web/configuration.rb', line 17

def load(config)
  clear
  merge! YAML.load File.read(config) if File.exist?(config)
end