Class: Policy

Inherits:
Object
  • Object
show all
Includes:
SlicehostSupport
Defined in:
lib/policy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SlicehostSupport

#slicehost_get_ips

Constructor Details

#initialize(dsl_file, logger) ⇒ Policy

Returns a new instance of Policy.



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

def initialize(dsl_file, logger)
  @stack = []
  @top = []
  @stack.push(@top)
  @dirty = false
  @logger = logger

  instance_eval File.open(dsl_file).read, dsl_file
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/policy.rb', line 6

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/policy.rb', line 6

def name
  @name
end

Instance Method Details

#add_rule(rule) ⇒ Object



73
74
75
76
# File 'lib/policy.rb', line 73

def add_rule(rule)
  @dirty = true
  @stack.last << rule
end

#allow_establishedObject



57
58
59
# File 'lib/policy.rb', line 57

def allow_established
  add_rule IptablesGenerator.allow_established
end

#allow_ip(ip) ⇒ Object



39
40
41
# File 'lib/policy.rb', line 39

def allow_ip(ip)
  add_rule IptablesGenerator.allow_ip ip
end

#allow_ips(*hosts) ⇒ Object



43
44
45
46
# File 'lib/policy.rb', line 43

def allow_ips(*hosts)
  hosts = hosts.first if hosts.length == 1 && hosts.first.instance_of?(Array)
  add_rule IptablesGenerator.allow_ips hosts
end

#allow_listen(*ports) ⇒ Object



48
49
50
51
# File 'lib/policy.rb', line 48

def allow_listen(*ports)
  ports = ports.first if ports.length == 1 && ports.first.instance_of?(Array)
  add_rule IptablesGenerator.allow_listen(ports)
end

#allow_pingObject



61
62
63
# File 'lib/policy.rb', line 61

def allow_ping
  add_rule IptablesGenerator.allow_ping
end

#allow_slicehost_slices(key) ⇒ Object



53
54
55
# File 'lib/policy.rb', line 53

def allow_slicehost_slices(key)
  add_rule IptablesGenerator.allow_slicehost_slices(key)
end

#allow_sshObject



65
66
67
# File 'lib/policy.rb', line 65

def allow_ssh
  add_rule IptablesGenerator.allow_ssh
end

#cleanObject



26
27
28
# File 'lib/policy.rb', line 26

def clean
  @dirty = false
end

#configObject



30
31
32
# File 'lib/policy.rb', line 30

def config
  yield
end

#deny_allObject



69
70
71
# File 'lib/policy.rb', line 69

def deny_all
  add_rule IptablesGenerator.deny_all
end

#dirty?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/policy.rb', line 22

def dirty?
  @dirty
end

#policy(name = :unnamed) ⇒ Object



34
35
36
37
# File 'lib/policy.rb', line 34

def policy(name = :unnamed)
  @name = name
  yield
end

#rulesObject



18
19
20
# File 'lib/policy.rb', line 18

def rules
  @top
end

#update(options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/policy.rb', line 78

def update(options = {})
  index = @stack.last.length

  @stack.push([])
  yield
  rules = @stack.pop
  @stack.last[index] = rules

  period = options[:each].to_i
  if period > 0
    EM.add_periodic_timer period do
      rules.clear
      @stack.push(rules)
      yield
      @stack.pop
    end
  end
end