construqt 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/construqt/addresses.rb +204 -0
- data/lib/construqt/bgps.rb +164 -0
- data/lib/construqt/cables.rb +47 -0
- data/lib/construqt/firewalls.rb +247 -0
- data/lib/construqt/flavour/ciscian/ciscian.rb +687 -0
- data/lib/construqt/flavour/ciscian/dialect_dlink-dgs15xx.rb +235 -0
- data/lib/construqt/flavour/ciscian/dialect_hp-2510g.rb +114 -0
- data/lib/construqt/flavour/delegates.rb +448 -0
- data/lib/construqt/flavour/flavour.rb +97 -0
- data/lib/construqt/flavour/mikrotik/flavour_mikrotik.rb +417 -0
- data/lib/construqt/flavour/mikrotik/flavour_mikrotik_bgp.rb +134 -0
- data/lib/construqt/flavour/mikrotik/flavour_mikrotik_interface.rb +79 -0
- data/lib/construqt/flavour/mikrotik/flavour_mikrotik_ipsec.rb +65 -0
- data/lib/construqt/flavour/mikrotik/flavour_mikrotik_result.rb +182 -0
- data/lib/construqt/flavour/mikrotik/flavour_mikrotik_schema.rb +355 -0
- data/lib/construqt/flavour/plantuml/plantuml.rb +462 -0
- data/lib/construqt/flavour/ubuntu/flavour_ubuntu.rb +381 -0
- data/lib/construqt/flavour/ubuntu/flavour_ubuntu_bgp.rb +117 -0
- data/lib/construqt/flavour/ubuntu/flavour_ubuntu_dns.rb +97 -0
- data/lib/construqt/flavour/ubuntu/flavour_ubuntu_firewall.rb +300 -0
- data/lib/construqt/flavour/ubuntu/flavour_ubuntu_ipsec.rb +144 -0
- data/lib/construqt/flavour/ubuntu/flavour_ubuntu_opvn.rb +60 -0
- data/lib/construqt/flavour/ubuntu/flavour_ubuntu_result.rb +537 -0
- data/lib/construqt/flavour/ubuntu/flavour_ubuntu_services.rb +115 -0
- data/lib/construqt/flavour/ubuntu/flavour_ubuntu_vrrp.rb +52 -0
- data/lib/construqt/flavour/unknown/unknown.rb +175 -0
- data/lib/construqt/hostid.rb +42 -0
- data/lib/construqt/hosts.rb +98 -0
- data/lib/construqt/interfaces.rb +166 -0
- data/lib/construqt/ipsecs.rb +64 -0
- data/lib/construqt/networks.rb +81 -0
- data/lib/construqt/regions.rb +32 -0
- data/lib/construqt/resource.rb +42 -0
- data/lib/construqt/services.rb +53 -0
- data/lib/construqt/tags.rb +61 -0
- data/lib/construqt/templates.rb +37 -0
- data/lib/construqt/tests/test_addresses.rb +50 -0
- data/lib/construqt/tests/test_bgps.rb +24 -0
- data/lib/construqt/tests/test_hostid.rb +32 -0
- data/lib/construqt/tests/test_hosts.rb +23 -0
- data/lib/construqt/tests/test_utils.rb +76 -0
- data/lib/construqt/users.rb +19 -0
- data/lib/construqt/util.rb +163 -0
- data/lib/construqt/version.rb +3 -0
- data/lib/construqt/vlans.rb +51 -0
- data/lib/construqt.rb +92 -0
- metadata +105 -0
@@ -0,0 +1,175 @@
|
|
1
|
+
|
2
|
+
module Construqt
|
3
|
+
module Flavour
|
4
|
+
module Unknown
|
5
|
+
def self.name
|
6
|
+
'unknown'
|
7
|
+
end
|
8
|
+
|
9
|
+
Construqt::Flavour.add(self)
|
10
|
+
|
11
|
+
class Device < OpenStruct
|
12
|
+
def initialize(cfg)
|
13
|
+
super(cfg)
|
14
|
+
end
|
15
|
+
|
16
|
+
def build_config(host, iface)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Vrrp < OpenStruct
|
21
|
+
def initialize(cfg)
|
22
|
+
super(cfg)
|
23
|
+
end
|
24
|
+
|
25
|
+
def build_config(host, iface)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Bond < OpenStruct
|
30
|
+
def initialize(cfg)
|
31
|
+
super(cfg)
|
32
|
+
end
|
33
|
+
|
34
|
+
def build_config(host, iface)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class Vlan < OpenStruct
|
39
|
+
def initialize(cfg)
|
40
|
+
super(cfg)
|
41
|
+
end
|
42
|
+
|
43
|
+
def build_config(host, iface)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Bridge < OpenStruct
|
48
|
+
def initialize(cfg)
|
49
|
+
super(cfg)
|
50
|
+
end
|
51
|
+
|
52
|
+
def build_config(host, iface)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Host < OpenStruct
|
57
|
+
def initialize(cfg)
|
58
|
+
super(cfg)
|
59
|
+
end
|
60
|
+
|
61
|
+
def header(host)
|
62
|
+
end
|
63
|
+
|
64
|
+
def footer(host)
|
65
|
+
end
|
66
|
+
|
67
|
+
def build_config(host, unused)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class Gre < OpenStruct
|
72
|
+
def initialize(cfg)
|
73
|
+
super(cfg)
|
74
|
+
end
|
75
|
+
|
76
|
+
def build_config(host, iface)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class Opvn < OpenStruct
|
81
|
+
def initialize(cfg)
|
82
|
+
super(cfg)
|
83
|
+
end
|
84
|
+
|
85
|
+
def build_config(host, iface)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
class Template
|
90
|
+
def initialize(cfg)
|
91
|
+
super(cfg)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
class Result
|
96
|
+
def initialize(host)
|
97
|
+
end
|
98
|
+
|
99
|
+
def commit
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# class Interface < OpenStruct
|
104
|
+
# def initialize(cfg)
|
105
|
+
# super(cfg)
|
106
|
+
# end
|
107
|
+
|
108
|
+
# def build_config(host, iface)
|
109
|
+
# self.clazz.build_config(host, iface||self)
|
110
|
+
# end
|
111
|
+
|
112
|
+
# end
|
113
|
+
#
|
114
|
+
def self.clazzes
|
115
|
+
{
|
116
|
+
"opvn" => Opvn,
|
117
|
+
"gre" => Gre,
|
118
|
+
"host" => Host,
|
119
|
+
"device"=> Device,
|
120
|
+
"vrrp" => Vrrp,
|
121
|
+
"bridge" => Bridge,
|
122
|
+
"template" => Template,
|
123
|
+
"bond" => Bond,
|
124
|
+
"vlan" => Vlan,
|
125
|
+
"result" => Result
|
126
|
+
}
|
127
|
+
end
|
128
|
+
|
129
|
+
def self.clazz(name)
|
130
|
+
ret = self.clazzes[name]
|
131
|
+
throw "class not found #{name}" unless ret
|
132
|
+
ret
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.create_host(name, cfg)
|
136
|
+
cfg['name'] = name
|
137
|
+
cfg['result'] = nil
|
138
|
+
host = Host.new(cfg)
|
139
|
+
host.result = Result.new(host)
|
140
|
+
host
|
141
|
+
end
|
142
|
+
|
143
|
+
def self.create_interface(name, cfg)
|
144
|
+
cfg['name'] = name
|
145
|
+
clazz(cfg['clazz']).new(cfg)
|
146
|
+
end
|
147
|
+
|
148
|
+
class Bgp < OpenStruct
|
149
|
+
def initialize(cfg)
|
150
|
+
super(cfg)
|
151
|
+
end
|
152
|
+
|
153
|
+
def build_config(unused, unused1)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def self.create_bgp(cfg)
|
158
|
+
Bgp.new(cfg)
|
159
|
+
end
|
160
|
+
|
161
|
+
class Ipsec < OpenStruct
|
162
|
+
def initialize(cfg)
|
163
|
+
super(cfg)
|
164
|
+
end
|
165
|
+
|
166
|
+
def build_config(unused, unused1)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def self.create_ipsec(cfg)
|
171
|
+
Ipsec.new(cfg)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Construqt
|
2
|
+
|
3
|
+
class HostId
|
4
|
+
attr_accessor :interfaces
|
5
|
+
def self.create(&block)
|
6
|
+
a = HostId.new()
|
7
|
+
a.interfaces=[]
|
8
|
+
block.call(a)
|
9
|
+
return a
|
10
|
+
end
|
11
|
+
|
12
|
+
def first_ipv6!
|
13
|
+
self.interfaces.each do |i|
|
14
|
+
next unless i.address
|
15
|
+
return i.address if i.address.first_ipv6
|
16
|
+
end
|
17
|
+
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def first_ipv6
|
22
|
+
ret = first_ipv6!
|
23
|
+
throw "first_ipv6 failed #{self.interfaces.first.host.name}" unless ret
|
24
|
+
ret
|
25
|
+
end
|
26
|
+
|
27
|
+
def first_ipv4!
|
28
|
+
self.interfaces.each do |i|
|
29
|
+
next unless i.address
|
30
|
+
return i.address if i.address.first_ipv4
|
31
|
+
end
|
32
|
+
|
33
|
+
nil
|
34
|
+
end
|
35
|
+
|
36
|
+
def first_ipv4
|
37
|
+
ret = first_ipv4!
|
38
|
+
throw "first_ipv4 failed #{self.interfaces.first.host.name}" unless ret
|
39
|
+
ret
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
|
2
|
+
module Construqt
|
3
|
+
|
4
|
+
class Hosts
|
5
|
+
|
6
|
+
def initialize(region)
|
7
|
+
@region = region
|
8
|
+
@hosts = {}
|
9
|
+
@default_pwd = SecureRandom.urlsafe_base64(24)
|
10
|
+
end
|
11
|
+
|
12
|
+
def region
|
13
|
+
@region
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_default_password(pwd)
|
17
|
+
@default_pwd = pwd
|
18
|
+
end
|
19
|
+
|
20
|
+
def default_password
|
21
|
+
@default_pwd
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_hosts()
|
25
|
+
@hosts.values
|
26
|
+
end
|
27
|
+
|
28
|
+
def del(name)
|
29
|
+
host = @hosts[name]
|
30
|
+
return nil unless host
|
31
|
+
@hosts.delete(name)
|
32
|
+
host
|
33
|
+
end
|
34
|
+
|
35
|
+
def add(host_name, cfg, &block)
|
36
|
+
(host_name, host) = Construqt::Tags.add(host_name) { |name| add_internal(name, cfg) { |h| block.call(h) } }
|
37
|
+
host
|
38
|
+
end
|
39
|
+
|
40
|
+
def add_internal(name, cfg, &block)
|
41
|
+
#binding.pry
|
42
|
+
throw "id is not allowed" if cfg['id']
|
43
|
+
throw "configip is not allowed" if cfg['configip']
|
44
|
+
throw "Host with the name #{name} exisits" if @hosts[name]
|
45
|
+
cfg['interfaces'] = {}
|
46
|
+
cfg['id'] ||=nil
|
47
|
+
cfg['configip'] ||=nil
|
48
|
+
|
49
|
+
cfg['name'] = name
|
50
|
+
cfg['dns_server'] ||= false
|
51
|
+
cfg['result'] = nil
|
52
|
+
cfg['shadow'] ||= nil
|
53
|
+
cfg['flavour'] = Flavour.find(cfg['flavour'] || 'ubuntu')
|
54
|
+
# cfg['clazz'] = cfg['flavour'].clazz("host")
|
55
|
+
throw "flavour #{cfg['flavour']} for host #{name} not found" unless cfg['flavour']
|
56
|
+
cfg['region'] = @region
|
57
|
+
host = cfg['flavour'].create_host(name, cfg)
|
58
|
+
block.call(host)
|
59
|
+
throw "host attribute id is required" unless host.id.kind_of? HostId
|
60
|
+
throw "host attribute configip is required" unless host.configip.kind_of? HostId
|
61
|
+
|
62
|
+
if (host.id.first_ipv4! && !host.id.first_ipv4!.dhcpv4?) ||
|
63
|
+
(host.id.first_ipv6! && !host.id.first_ipv6!.dhcpv6?)
|
64
|
+
adr = nil
|
65
|
+
if host.id.first_ipv4!
|
66
|
+
adr = (adr || region.network.addresses.create).add_ip(host.id.first_ipv4.first_ipv4.to_s).set_name(host.name)
|
67
|
+
end
|
68
|
+
|
69
|
+
if host.id.first_ipv6!
|
70
|
+
adr = (adr || region.network.addresses.create).add_ip(host.id.first_ipv6.first_ipv6.to_s).set_name(host.name)
|
71
|
+
end
|
72
|
+
|
73
|
+
adr = region.network.addresses.create unless adr
|
74
|
+
adr.host = host if adr
|
75
|
+
end
|
76
|
+
|
77
|
+
@hosts[name] = host
|
78
|
+
end
|
79
|
+
|
80
|
+
def find(name)
|
81
|
+
ret = @hosts[name]
|
82
|
+
throw "host not found #{name}" unless ret
|
83
|
+
ret
|
84
|
+
end
|
85
|
+
|
86
|
+
def build_config(hosts = nil)
|
87
|
+
(hosts || @hosts.values).each do |host|
|
88
|
+
host.build_config(host, nil)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def commit(hosts = nil)
|
93
|
+
(hosts || @hosts.values).each { |h| h.commit }
|
94
|
+
Flavour.call_aspects("completed", nil, nil)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
module Construqt
|
2
|
+
class Interfaces
|
3
|
+
def initialize(region)
|
4
|
+
@region = region
|
5
|
+
end
|
6
|
+
|
7
|
+
def setup_template(iface)
|
8
|
+
iface.template.vlans.each do |vlan|
|
9
|
+
|
10
|
+
vname = vlan.description
|
11
|
+
to_add_iface = iface.host.interfaces[vname]
|
12
|
+
unless to_add_iface
|
13
|
+
to_add_iface = add_vlan(iface.host, vname, vlan.to_h.inject({}){|r,(k,v)| r[k.to_s]=v; r })
|
14
|
+
end
|
15
|
+
|
16
|
+
#puts ">>>>>#{iface.name}"
|
17
|
+
to_add_iface.interfaces << iface
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_device(host, dev_name, cfg)
|
22
|
+
throw "Host not found:#{dev_name}" unless host
|
23
|
+
throw "Interface is duplicated:#{host.name}:#{dev_name}" if host.interfaces[dev_name]
|
24
|
+
throw "invalid name #{dev_name}" unless dev_name.match(/^[A-Za-z0-9\-\.]+$/)
|
25
|
+
if match=/^.*[^\d](\d+)$/.match(dev_name)
|
26
|
+
cfg['number'] ||= match[1].to_i
|
27
|
+
end
|
28
|
+
|
29
|
+
cfg['host'] = host
|
30
|
+
cfg['mtu'] ||= 1500
|
31
|
+
#binding.pry if host && host.name == "ct-iar1-ham"
|
32
|
+
# binding.pry
|
33
|
+
cfg['clazz'] ||= "device"
|
34
|
+
cfg['address'] ||= nil
|
35
|
+
(dev_name, iface) = Construqt::Tags.add(dev_name) { |name| host.flavour.create_interface(name, cfg) }
|
36
|
+
# iface.clazz.attach = iface
|
37
|
+
host.interfaces[dev_name] = iface
|
38
|
+
host.interfaces[dev_name].address.interface = host.interfaces[dev_name] if host.interfaces[dev_name].address
|
39
|
+
setup_template(iface) if iface.template
|
40
|
+
host.interfaces[dev_name]
|
41
|
+
end
|
42
|
+
|
43
|
+
# def add_template(host, name, cfg)
|
44
|
+
# cfg['clazz'] = "template"
|
45
|
+
# cfg['host'] = host
|
46
|
+
# cfg['name'] = name
|
47
|
+
# self.add_device(host,name, cfg)
|
48
|
+
# end
|
49
|
+
|
50
|
+
def add_openvpn(host, name, cfg)
|
51
|
+
cfg['clazz'] = "opvn"
|
52
|
+
cfg['ipv6'] ||= nil
|
53
|
+
cfg['ipv4'] ||= nil
|
54
|
+
dev = add_device(host, name, cfg)
|
55
|
+
dev.address.interface = host.interfaces[name] if dev.address
|
56
|
+
dev.network.name = "#{name}-#{host.name}"
|
57
|
+
dev
|
58
|
+
end
|
59
|
+
|
60
|
+
def add_gre(host, name, cfg)
|
61
|
+
throw "we need an address on this cfg #{cfg.inspect}" unless cfg['address']
|
62
|
+
cfg['clazz'] = "gre"
|
63
|
+
cfg['local'] ||= nil
|
64
|
+
cfg['remote'] ||= nil
|
65
|
+
dev = add_device(host, name, cfg)
|
66
|
+
dev.address.interface = host.interfaces[name] if dev.address
|
67
|
+
dev
|
68
|
+
end
|
69
|
+
|
70
|
+
def add_vlan(host, name, cfg)
|
71
|
+
unless cfg["vlan_id"].to_s.match(/^[0-9]+$/) && 1 <= cfg["vlan_id"].to_i && cfg["vlan_id"].to_i < 4096
|
72
|
+
throw "vlan_id must be set on vlan with name #{name}"
|
73
|
+
end
|
74
|
+
cfg = cfg.clone
|
75
|
+
interfaces = cfg['interfaces'] || []
|
76
|
+
interfaces << cfg['interface'] if cfg['interface']
|
77
|
+
cfg.delete('interface')
|
78
|
+
cfg['interfaces'] = interfaces
|
79
|
+
# throw "we need an interface #{cfg['interfaces']}" if cfg['interfaces'].empty?
|
80
|
+
cfg['clazz'] = "vlan"
|
81
|
+
dev = add_device(host, name, cfg)
|
82
|
+
dev.address.interface = host.interfaces[name] if dev.address
|
83
|
+
dev
|
84
|
+
end
|
85
|
+
|
86
|
+
def add_bond(host, name, cfg)
|
87
|
+
cfg['interfaces'].each do |interface|
|
88
|
+
throw "interface not one same host:#{interface.host.name}:#{host.name}" unless host.name == interface.host.name
|
89
|
+
end
|
90
|
+
|
91
|
+
cfg['clazz'] = "bond"
|
92
|
+
dev = add_device(host, name, cfg)
|
93
|
+
dev.address.interface = host.interfaces[name] if dev.address
|
94
|
+
dev
|
95
|
+
end
|
96
|
+
|
97
|
+
def add_vrrp(name, cfg)
|
98
|
+
nets = {}
|
99
|
+
cfg['address'].ips.each do |adr|
|
100
|
+
throw "only host ip's are allowed #{adr.to_s}" if adr.ipv4? && adr.prefix != 32
|
101
|
+
throw "only host ip's are allowed #{adr.to_s}" if adr.ipv6? && adr.prefix != 128
|
102
|
+
nets[adr.network.to_s] = true
|
103
|
+
end
|
104
|
+
|
105
|
+
cfg['interfaces'].each do |interface|
|
106
|
+
throw "interface need priority #{interface}" unless interface.priority
|
107
|
+
throw "interface not found:#{name}" unless interface
|
108
|
+
cfg['clazz'] = "vrrp"
|
109
|
+
cfg['interface'] = interface
|
110
|
+
throw "vrrp interface does not have within the same network" if nets.length == interface.address.ips.select { |adr| nets[adr.network.to_s] }.length
|
111
|
+
dev = add_device(interface.host, name, cfg)
|
112
|
+
dev.address.interface = nil
|
113
|
+
dev.address.host = nil
|
114
|
+
dev.address.name = name
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def add_bridge(host, name, cfg)
|
119
|
+
#cfg['interfaces'] = []
|
120
|
+
cfg['interfaces'].each do |interface|
|
121
|
+
throw "interface not one same host:#{interface.host.name}:#{host.name}" unless host.name == interface.host.name
|
122
|
+
end
|
123
|
+
|
124
|
+
cfg['clazz'] = "bridge"
|
125
|
+
dev = add_device(host, name, cfg)
|
126
|
+
dev.address.interface = host.interfaces[name] if dev.address
|
127
|
+
dev
|
128
|
+
end
|
129
|
+
|
130
|
+
def find(host_or_name, iface_name)
|
131
|
+
if host_or_name.kind_of?(String)
|
132
|
+
host = @region.hosts.find(host_or_name)
|
133
|
+
throw "host not found #{host_or_name}" unless host
|
134
|
+
else
|
135
|
+
host = host_or_name
|
136
|
+
end
|
137
|
+
|
138
|
+
iface = host.interfaces[iface_name]
|
139
|
+
throw "interface not found for #{iface_name}:#{host.name}" unless iface
|
140
|
+
iface
|
141
|
+
end
|
142
|
+
|
143
|
+
def build_config(hosts = nil)
|
144
|
+
(hosts||Hosts.get_hosts).each do |host|
|
145
|
+
by_clazz = {}
|
146
|
+
host.interfaces.values.each do |interface|
|
147
|
+
#throw "class less interface #{interface.inspect}" unless interface.clazz
|
148
|
+
#throw "no clazz defined in interface #{interface.clazz}" unless interface.clazz.name
|
149
|
+
name = interface.clazz # .name[interface.clazz.name.rindex(':')+1..-1].downcase
|
150
|
+
#puts "<<<<<<< #{name}"
|
151
|
+
by_clazz[name] ||= []
|
152
|
+
by_clazz[name] << interface
|
153
|
+
end
|
154
|
+
|
155
|
+
#binding.pry
|
156
|
+
["host", "device", "vlan", "bond", "bridge", "vrrp", "gre", "bgp", "opvn", "ipsec"].each do |key|
|
157
|
+
next unless by_clazz[key]
|
158
|
+
by_clazz[key].each do |interface|
|
159
|
+
#Construqt.logger.debug "Interface:build_config:#{interface.name}:#{interface.class.name}:#{interface.ident}"
|
160
|
+
interface.build_config(host, interface)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
|
2
|
+
module Construqt
|
3
|
+
module Ipsecs
|
4
|
+
class Ipsec < OpenStruct
|
5
|
+
def initialize(cfg)
|
6
|
+
super(cfg)
|
7
|
+
end
|
8
|
+
|
9
|
+
def build_config()
|
10
|
+
self.left.build_config(nil, nil)
|
11
|
+
self.right.build_config(nil, nil)
|
12
|
+
end
|
13
|
+
|
14
|
+
def ident
|
15
|
+
self.left.ident
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
@ipsecs = {}
|
20
|
+
def self.add_connection(cfg, id, to_id, iname)
|
21
|
+
throw "my not found #{cfg[id].inspect}" unless cfg[id]['my']
|
22
|
+
throw "host not found #{cfg[id].inspect}" unless cfg[id]['host']
|
23
|
+
throw "remote not found #{cfg[id].inspect}" unless cfg[id]['remote']
|
24
|
+
cfg[id]['other'] = nil
|
25
|
+
cfg[id]['cfg'] = nil
|
26
|
+
cfg[id]['my'].host = cfg[id]['host']
|
27
|
+
cfg[id]['my'].name = "#{iname}-#{cfg[id]['host'].name}"
|
28
|
+
cfg[id]['interface'] = nil
|
29
|
+
cfg[id] = cfg[id]['host'].flavour.create_ipsec(cfg[id])
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.connection(name, cfg)
|
33
|
+
# binding.pry
|
34
|
+
add_connection(cfg, 'left', 'right', Util.add_gre_prefix(cfg['right']['host'].name))
|
35
|
+
add_connection(cfg, 'right', 'left', Util.add_gre_prefix(cfg['left'].host.name))
|
36
|
+
cfg['name'] = name
|
37
|
+
cfg = @ipsecs[name] = Ipsec.new(cfg)
|
38
|
+
cfg.left.other = cfg.right
|
39
|
+
cfg.left.cfg = cfg
|
40
|
+
cfg.right.other = cfg.left
|
41
|
+
cfg.right.cfg = cfg
|
42
|
+
|
43
|
+
#puts "-------- #{cfg.left.my.host.name} - #{cfg.right.my.host.name}"
|
44
|
+
cfg.left.interface = cfg.left.my.host.region.interfaces.add_gre(cfg.left.my.host, cfg.left.other.host.name,
|
45
|
+
"address" => cfg.left.my,
|
46
|
+
"local" => cfg.left.remote,
|
47
|
+
"remote" => cfg.right.remote
|
48
|
+
)
|
49
|
+
cfg.right.interface = cfg.left.my.host.region.interfaces.add_gre(cfg.right.my.host, cfg.right.other.host.name,
|
50
|
+
"address" => cfg.right.my,
|
51
|
+
"local" => cfg.right.remote,
|
52
|
+
"remote" => cfg.left.remote
|
53
|
+
)
|
54
|
+
#binding.pry
|
55
|
+
cfg
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.build_config()
|
59
|
+
@ipsecs.each do |name, ipsec|
|
60
|
+
ipsec.build_config()
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module Construqt
|
2
|
+
module Networks
|
3
|
+
|
4
|
+
class Network
|
5
|
+
def initialize(name)
|
6
|
+
@name = name
|
7
|
+
@networks = []
|
8
|
+
@domain = "construqt.org"
|
9
|
+
@contact = "soa@construqt.org"
|
10
|
+
@addresses = Construqt::Addresses.new(self)
|
11
|
+
@services = Construqt::Services.new
|
12
|
+
@dns_resolver = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def services
|
16
|
+
@services
|
17
|
+
end
|
18
|
+
|
19
|
+
def addresses
|
20
|
+
@addresses
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_blocks(*nets)
|
24
|
+
nets.each do |net|
|
25
|
+
@networks << IPAddress.parse(net)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def networks
|
30
|
+
@networks
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_network(ip)
|
34
|
+
ret = (@networks.find{ |my| (ip.ipv6? == my.ipv6? && ip.ipv4? == my.ipv4?) && my.include?(ip) } || ip.network)
|
35
|
+
ret
|
36
|
+
end
|
37
|
+
|
38
|
+
def set_dns_resolver(nameservers, search)
|
39
|
+
@dns_resolver = OpenStruct.new :nameservers => nameservers, :search => search
|
40
|
+
end
|
41
|
+
|
42
|
+
def dns_resolver
|
43
|
+
@dns_resolver
|
44
|
+
end
|
45
|
+
|
46
|
+
def set_domain(domain)
|
47
|
+
@domain = domain
|
48
|
+
end
|
49
|
+
|
50
|
+
def domain
|
51
|
+
@domain
|
52
|
+
end
|
53
|
+
|
54
|
+
def set_contact(contact)
|
55
|
+
@contact = contact
|
56
|
+
end
|
57
|
+
|
58
|
+
def contact
|
59
|
+
@contact
|
60
|
+
end
|
61
|
+
|
62
|
+
# def domain(name)
|
63
|
+
# _fqdn = self.fqdn(name)
|
64
|
+
# _fqdn[_fqdn.index('.')+1..-1]
|
65
|
+
# end
|
66
|
+
|
67
|
+
def fqdn(name)
|
68
|
+
throw "name must set" unless name
|
69
|
+
_name = name.gsub(/[\s_]+/, '-')
|
70
|
+
return "#{_name}.#{self.domain}" unless _name.include?('.')
|
71
|
+
return _name
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
@networks = {}
|
76
|
+
def self.add(name)
|
77
|
+
throw "network with name #{name} exists" if @networks[name]
|
78
|
+
@networks[name] = Network.new(name)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
module Construqt
|
3
|
+
module Regions
|
4
|
+
@regions = {}
|
5
|
+
class Region
|
6
|
+
attr_reader :name, :cables, :hosts, :interfaces, :users, :vlans, :network, :templates, :resources
|
7
|
+
def initialize(name, network)
|
8
|
+
@name = name
|
9
|
+
@network = network
|
10
|
+
@vlans = Construqt::Vlans.new(self)
|
11
|
+
@hosts = Construqt::Hosts.new(self)
|
12
|
+
@interfaces = Construqt::Interfaces.new(self)
|
13
|
+
@templates = Construqt::Templates.new(self)
|
14
|
+
@users = Construqt::Users.new(self)
|
15
|
+
@cables = Construqt::Cables.new(self)
|
16
|
+
@resources = Construqt::Resources.new(self)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.add(name, network)
|
21
|
+
throw "region names #{name} has to be unique" if @regions[name]
|
22
|
+
ret = Region.new(name, network)
|
23
|
+
@regions[name] = ret
|
24
|
+
ret
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.find(name)
|
28
|
+
throw "region with name #{name} not found" unless @regions[name]
|
29
|
+
@regions[name]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Construqt
|
2
|
+
class Resources
|
3
|
+
module Rights
|
4
|
+
ROOT_0600 = OpenStruct.new :right => "0600", :owner => 'root'
|
5
|
+
ROOT_0644 = OpenStruct.new :right => "0644", :owner => 'root'
|
6
|
+
ROOT_0755 = OpenStruct.new :right => "0755", :owner => 'root'
|
7
|
+
end
|
8
|
+
|
9
|
+
class Resource
|
10
|
+
attr_accessor :path
|
11
|
+
attr_accessor :right
|
12
|
+
attr_accessor :data
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(region)
|
16
|
+
@region = region
|
17
|
+
@files = {}
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_from_file(src_fname, right, key, *path)
|
21
|
+
add_file(IO.read(src_fname), right, key, *path)
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_file(data, right, key, *path)
|
25
|
+
throw "need a key" unless key
|
26
|
+
throw "need a path #{key}" if path.empty?
|
27
|
+
throw "resource exists with key #{key}" if @files[key]
|
28
|
+
resource = Resource.new
|
29
|
+
resource.path = *path
|
30
|
+
resource.right = right
|
31
|
+
resource.data = data
|
32
|
+
@files[key] = resource
|
33
|
+
resource
|
34
|
+
end
|
35
|
+
|
36
|
+
def find(key)
|
37
|
+
ret = @files[key]
|
38
|
+
throw "resource with key #{key} not found" unless ret
|
39
|
+
ret
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|