Class: IPScriptables::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/ipscriptables/runtime.rb

Constant Summary collapse

DEFAULT_OPTS =
{ counters: true }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, logger = nil) ⇒ Runtime

Returns a new instance of Runtime.



12
13
14
15
16
17
# File 'lib/ipscriptables/runtime.rb', line 12

def initialize(opts = {}, logger = nil)
  @opts = DEFAULT_OPTS.merge(opts)
  @log = logger || Logger.new($stderr)
  @evaluating = 0
  @rulesets = {}
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



10
11
12
# File 'lib/ipscriptables/runtime.rb', line 10

def log
  @log
end

#optsObject (readonly)

Returns the value of attribute opts.



10
11
12
# File 'lib/ipscriptables/runtime.rb', line 10

def opts
  @opts
end

Instance Method Details

#dsl_eval(&block) ⇒ Object



52
53
54
55
56
57
# File 'lib/ipscriptables/runtime.rb', line 52

def dsl_eval(&block)
  @evaluating += 1
  instance_eval(&block)
ensure
  @evaluating -= 1
end

#execute!Object

rubocop:disable CyclomaticComplexity, MethodLength



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ipscriptables/runtime.rb', line 59

def execute!  # rubocop:disable CyclomaticComplexity, MethodLength
  if @evaluating != 0
    fail "I can't let you do that (DSL eval depth #{@evaluating})"
  end

  ok = true
  @rulesets.sort.each do |family, ruleset|
    if !opts.fetch(family, true)
      log.info "Skipping #{family} as requested"
    else
      diff = ruleset.diff
      if diff.to_s.empty?
        log.info "No changes for #{family}, moving along."
      else
        log.info "Changes found for #{family}"
        format = opts.fetch(:color, $stdout.tty?) ? :color : :text
        puts diff.to_s(format) unless opts[:quiet]
        if opts[:apply]
          log.info "Restoring #{family}"
          begin
            ruleset.restore!
          rescue => e
            log.error "Failure restoring #{family}: #{e}"
            ok = false
            return ok if opts[:fail_fast]
          end
        else
          log.info "Would restore #{family}"
        end
      end
    end
  end

  log.warn 'There were errors' unless ok

  ok
end

#family(*families, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/ipscriptables/runtime.rb', line 25

def family(*families, &block)
  families.each do |family|
    begin
      @evaluating += 1
      ruleset(family).dsl_eval(&block)
    ensure
      @evaluating -= 1
    end
  end
end

#ip6tables(&block) ⇒ Object



40
41
42
# File 'lib/ipscriptables/runtime.rb', line 40

def ip6tables(&block)
  family(:inet6, &block)
end

#iptables(&block) ⇒ Object



36
37
38
# File 'lib/ipscriptables/runtime.rb', line 36

def iptables(&block)
  family(:inet, &block)
end

#load_file(path) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/ipscriptables/runtime.rb', line 44

def load_file(path)
  @evaluating += 1
  log.info "Loading configuration from #{path}"
  instance_eval(File.read(path), path)
ensure
  @evaluating -= 1
end

#ruleset(family) ⇒ Object



19
20
21
22
23
# File 'lib/ipscriptables/runtime.rb', line 19

def ruleset(family)
  family = family.to_sym
  @rulesets[family] ||=
    IPScriptables::Ruleset.from_system(family: family).bud(opts)
end