Class: IptablesWeb::Configuration
- Inherits:
-
Hash
- Object
- Hash
- IptablesWeb::Configuration
- 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)- CHECKSUM_FILE =
"#{ENV['HOME']}/.iptables-web/checksum"
Instance Attribute Summary collapse
-
#loaded ⇒ Object
Returns the value of attribute loaded.
Class Method Summary collapse
- .checksum=(checksum) ⇒ Object
- .checksum?(checksum) ⇒ Boolean
- .config_dir ⇒ Object
- .static_rules ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #load(config) ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
9 10 11 12 13 14 15 16 17 |
# File 'lib/iptables_web/configuration.rb', line 9 def initialize CONFIG_FILES.each do |config| puts "Load configuration from #{config}" if load(config) @loaded = true break end end end |
Instance Attribute Details
#loaded ⇒ Object
Returns the value of attribute loaded.
4 5 6 |
# File 'lib/iptables_web/configuration.rb', line 4 def loaded @loaded end |
Class Method Details
.checksum=(checksum) ⇒ Object
44 45 46 |
# File 'lib/iptables_web/configuration.rb', line 44 def self.checksum=(checksum) File.write(CHECKSUM_FILE, checksum) end |
.checksum?(checksum) ⇒ Boolean
40 41 42 |
# File 'lib/iptables_web/configuration.rb', line 40 def self.checksum?(checksum) File.exists?(CHECKSUM_FILE) && File.read(CHECKSUM_FILE) == checksum end |
.config_dir ⇒ Object
48 49 50 |
# File 'lib/iptables_web/configuration.rb', line 48 def self.config_dir File.join(ENV['HOME'], '.iptables-web') end |
.static_rules ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/iptables_web/configuration.rb', line 24 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
19 20 21 22 |
# File 'lib/iptables_web/configuration.rb', line 19 def load(config) clear merge! YAML.load File.read(config) if File.exist?(config) end |