Class: HolePunch::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/holepunch/dsl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ DSL

Returns a new instance of DSL.



31
32
33
34
35
# File 'lib/holepunch/dsl.rb', line 31

def initialize(env)
  @definition = Definition.new(env)
  @group = nil
  @groups = {}
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



25
26
27
# File 'lib/holepunch/dsl.rb', line 25

def groups
  @groups
end

Class Method Details

.evaluate(filename, env = nil) ⇒ Object



27
28
29
# File 'lib/holepunch/dsl.rb', line 27

def self.evaluate(filename, env = nil)
  DSL.new(env).eval_dsl(filename)
end

Instance Method Details

#depends(id) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/holepunch/dsl.rb', line 50

def depends(id)
  id = id.to_s
  raise GroupError, "duplicate group id #{id}" if @definition.groups.include?(id)
  raise HolePunchSyntaxError, "dependency group #{id} cannot have a block" if block_given?
  @group            = SecurityGroup.new(id, dependency: true)
  @definition.add_group(@group)
  yield if block_given?
ensure
  @group = nil
end

#desc(str) ⇒ Object

Raises:

  • (HolePunchSyntaxError)


71
72
73
74
75
# File 'lib/holepunch/dsl.rb', line 71

def desc(str)
  raise HolePunchSyntaxError, 'desc must be used inside a group' if @group.nil?
  raise HolePunchSyntaxError, 'desc cannot be used in a dependency group (the group is expected to be already defined elsewhere)' if @group.dependency
  @group.desc = str
end

#envObject

Raises:



45
46
47
48
# File 'lib/holepunch/dsl.rb', line 45

def env
  raise EnvNotDefinedError, 'env not defined' if @definition.env.nil?
  @definition.env
end

#eval_dsl(filename) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/holepunch/dsl.rb', line 37

def eval_dsl(filename)
  instance_eval(HolePunch.read_file(filename.to_s), filename.to_s, 1)
  @definition.validate!
  @definition
rescue SyntaxError => e
  raise SecurityGroupsFileError, "SecurityGroups syntax error #{e.message.gsub("#{filename.to_s}:", 'on line ')}"
end

#group(id, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/holepunch/dsl.rb', line 61

def group(id, &block)
  id = id.to_s
  raise GroupError, "duplicate group id #{id}" if @definition.groups.include?(id)
  @group            = SecurityGroup.new(id, dependency: false)
  @definition.add_group(@group)
  yield if block_given?
ensure
  @group = nil
end

#icmp(*sources) ⇒ Object Also known as: ping

Raises:

  • (HolePunchSyntaxError)


77
78
79
80
81
82
# File 'lib/holepunch/dsl.rb', line 77

def icmp(*sources)
  raise HolePunchSyntaxError, 'ping/icmp must be used inside a group' if @group.nil?
  raise HolePunchSyntaxError, 'ping/icmp cannot be used in a dependency group (the group is expected to be already defined elsewhere)' if @group.dependency
  sources << '0.0.0.0/0' if sources.empty?
  @group.ingresses << Permission.new(:icmp, nil, sources.flatten)
end

#tcp(ports, *sources) ⇒ Object

Raises:

  • (HolePunchSyntaxError)


85
86
87
88
89
90
# File 'lib/holepunch/dsl.rb', line 85

def tcp(ports, *sources)
  raise HolePunchSyntaxError, 'tcp must be used inside a group' if @group.nil?
  raise HolePunchSyntaxError, 'tcp cannot be used in a dependency group (the group is expected to be already defined elsewhere)' if @group.dependency
  sources << '0.0.0.0/0' if sources.empty?
  @group.ingresses << Permission.new(:tcp, ports, sources.flatten)
end

#udp(ports, *sources) ⇒ Object

Raises:

  • (HolePunchSyntaxError)


92
93
94
95
96
97
# File 'lib/holepunch/dsl.rb', line 92

def udp(ports, *sources)
  raise HolePunchSyntaxError, 'udp must be used inside a group' if @group.nil?
  raise HolePunchSyntaxError, 'udp cannot be used in a dependency group (the group is expected to be already defined elsewhere)' if @group.dependency
  sources << '0.0.0.0/0' if sources.empty?
  @group.ingresses << Permission.new(:udp, ports, sources.flatten)
end