Class: Hosum::Dsl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Dsl

Returns a new instance of Dsl.



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

def initialize(options = {}, &block)
  self.groups = []
  self.containers = []
  self.ips_list_scopes = []
  self.context = options[:context] || []

  instance_eval(&block) if block
end

Instance Attribute Details

#containersObject

Returns the value of attribute containers.



5
6
7
# File 'lib/hosum/dsl.rb', line 5

def containers
  @containers
end

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#groupsObject

Returns the value of attribute groups.



7
8
9
# File 'lib/hosum/dsl.rb', line 7

def groups
  @groups
end

#ips_list_scopesObject

Returns the value of attribute ips_list_scopes.



5
6
7
# File 'lib/hosum/dsl.rb', line 5

def ips_list_scopes
  @ips_list_scopes
end

Instance Method Details

#containers_eval(object, &block) ⇒ Object



18
19
20
21
22
# File 'lib/hosum/dsl.rb', line 18

def containers_eval(object, &block)
  self.containers.push(object)
  instance_eval(&block)
  self.containers.pop
end

#group(name, &block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/hosum/dsl.rb', line 24

def group(name, &block)
  group = Group.new
  group.name = name
  self.groups << group
  
  containers_eval(group, &block) if block
end

#hosts(*args, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hosum/dsl.rb', line 59

def hosts(*args, &block)
  options = args.extract_options!
  
  container = self.containers.last
  hosts = Hosts.new
  hosts.options = options
  hosts.domains = [ args ].flatten
  hosts.parent = container
  container.children << hosts
  
  if !self.ips_list_scopes.empty?
    ips_list = self.ips_list_scopes.last
    hosts.ips_list = ips_list
  end
  
  containers_eval(hosts, &block) if block
end

#ip(*args, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hosum/dsl.rb', line 32

def ip(*args, &block)
  options = args.extract_options!
  
  ips = Ips.new
  ips.options = options
  ips.addresses = [ args ].flatten
  
  if block
    self.ips_list_scopes << [ ips ]
    instance_eval(&block)
    self.ips_list_scopes.pop
  else
    self.containers.last.ips_list << ips
  end
end

#subject(name, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/hosum/dsl.rb', line 48

def subject(name, &block)
  subject = Subject.new
  subject.name = name
  
  parent = self.containers.last
  parent.children << subject
  subject.parent = parent
  
  containers_eval(subject, &block) if block
end

#to_dnsmasq(options = {}) ⇒ Object



83
84
85
86
87
# File 'lib/hosum/dsl.rb', line 83

def to_dnsmasq(options = {})
  self.groups.map do |group|
    group.to_dnsmasq(options)
  end.join("\n\n")
end

#to_hosts(options = {}) ⇒ Object



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

def to_hosts(options = {})
  self.groups.map do |group|
    group.to_hosts(options)
  end.join("\n\n")
end