odania 0.0.18 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,242 +0,0 @@
1
- # Generate a joined configuration for all available plugins
2
-
3
- require_relative 'duplicates'
4
- require_relative 'models/backend'
5
- require_relative 'models/backend_group'
6
- require_relative 'models/domain'
7
- require_relative 'models/layout'
8
- require_relative 'models/page'
9
- require_relative 'models/partial'
10
- require_relative 'models/redirect'
11
- require_relative 'models/sub_domain'
12
-
13
- module Odania
14
- module PluginConfig
15
- class Base22
16
- def initialize
17
- reset
18
- end
19
-
20
- def load_from_consul
21
- Odania.plugin.get_all.each_pair do |plugin_name, instances|
22
- puts "PLUGIN NAME #{plugin_name} - #{instances.count}" if $debug
23
- instances.each do |instance|
24
- add_backend(instance)
25
- end
26
-
27
- config = Odania.plugin.config_for plugin_name
28
- next if config.nil?
29
-
30
- puts JSON.pretty_generate config if $debug
31
- self.load(config)
32
- end
33
- end
34
-
35
- def load_global_config(config)
36
- throw 'Invalid global config!' if config.nil?
37
-
38
- reset
39
- @default_backend_groups = config['default_backend_groups'] unless config['default_backend_groups'].nil?
40
- @default_subdomains = config['default_subdomains'] unless config['default_subdomains'].nil?
41
-
42
- unless config['backends'].nil?
43
- config['backends'].each_pair do |group_name, group|
44
- group['backends'].each do |data|
45
- backend = Backend.new(data['service_name'], data['instance_name'], data['host'], data['port'])
46
- @backend_groups[group_name].add_backend backend
47
- end
48
- end
49
- end
50
-
51
-
52
- unless config['redirects'].nil?
53
- config['redirects'].each_pair do |src, target|
54
- @redirects[src].target = target
55
- end
56
- end
57
-
58
- unless config['domains'].nil?
59
- config['domains'].each_pair do |name, data|
60
- @domains[name].load(data)
61
- end
62
- end
63
-
64
- unless config['layouts'].nil?
65
- config['layouts'].each_pair do |name, data|
66
- @layouts[name].load(data)
67
- end
68
- end
69
-
70
- unless config['partials'].nil?
71
- config['partials'].each_pair do |name, data|
72
- @partials[name].load(data)
73
- end
74
- end
75
-
76
- self
77
- end
78
-
79
- def add_backend(backend_config)
80
- return if backend_config.ServiceAddress.nil? or backend_config.ServiceAddress.empty?
81
-
82
- backend = Backend.new(backend_config.ServiceName, backend_config.ServiceID, backend_config.ServiceAddress, backend_config.ServicePort)
83
- @backend_groups[backend_config.ServiceName].add_backend backend
84
- end
85
-
86
- def load(config)
87
- group_name = config['name']
88
-
89
- if $debug
90
- puts 'Loading configuration'
91
- puts JSON.pretty_generate config
92
- end
93
-
94
- # Add this service as a default backend if specified
95
- @default_backend_groups << group_name if config['default']
96
-
97
- # Add redirect info
98
- unless config['redirects'].nil?
99
- config['redirects'].each_pair do |idx, val|
100
- @duplicates.add(group_name, :redirect, idx, @redirects[idx].plugins) unless @redirects[idx].add(val, group_name)
101
- end
102
- end
103
-
104
- # Add Domain Information
105
- unless config['domains'].nil?
106
- config['domains'].each_pair do |domain_name, domain_data|
107
- uri = URI.parse("http://#{domain_name}")
108
- domain = PublicSuffix.parse(uri.host)
109
- base_domain = domain.domain
110
- subdomain_name = domain.trd
111
-
112
- # Add subdomain with config
113
- subdomain = @domains[base_domain].add_subdomain(subdomain_name)
114
- errors = subdomain.set_config(domain_data['config'], group_name)
115
- errors.each do |error|
116
- @duplicates.add(group_name, error[:type], error[:key], error[:plugins])
117
- end
118
-
119
- unless domain_data['public_pages'].nil?
120
- domain_data['public_pages'].each_pair do |language, language_contents|
121
- language_contents.each_pair do |web_url, plugin_url|
122
- unless subdomain.add_page(web_url, group_name, plugin_url, group_name)
123
- @duplicates.add(group_name, :page, web_url, subdomain.plugins(:page, web_url))
124
- end
125
- end
126
- end
127
- end
128
-
129
- domain_data['assets'].each_pair do |web_url, plugin_url|
130
- unless subdomain.add_asset(web_url, group_name, plugin_url, group_name)
131
- @duplicates.add(group_name, :asset, web_url, subdomain.plugins(:asset, web_url))
132
- end
133
- end
134
- end
135
- end
136
-
137
- unless config['layouts'].nil?
138
- config['layouts'].each_pair do |name, layout_config|
139
- unless @layouts[name].set_config(layout_config, group_name)
140
- @duplicates.add(group_name, :layout, name, @layouts[name].plugins)
141
- end
142
- end
143
- end
144
-
145
- unless config['partials'].nil?
146
- config['partials'].each_pair do |name, partial_file|
147
- unless @partials[name].load(name, partial_file, group_name)
148
- @duplicates.add(group_name, :partial, name, @partials[name].plugins)
149
- end
150
- end
151
- end
152
-
153
- @default_subdomains = config['default_subdomains'] unless config['default_subdomains'].nil?
154
- end
155
-
156
- def generate
157
- if $debug
158
- puts 'Default Backend Groups ----------------------------------'
159
- puts JSON.pretty_generate @default_backend_groups
160
- puts 'Backend Groups ----------------------------------'
161
- puts JSON.pretty_generate @backend_groups
162
- puts 'Redirects ----------------------------------'
163
- puts JSON.pretty_generate @redirects
164
- puts 'Domains ----------------------------------'
165
- puts JSON.pretty_generate @domains
166
- puts 'Default SubDomains ----------------------------------'
167
- puts JSON.pretty_generate @default_subdomains
168
- puts 'Generate ----------------------------------'
169
- end
170
- config = {
171
- 'default_backend_groups' => @default_backend_groups,
172
- 'default_subdomains' => @default_subdomains,
173
- 'backends' => {},
174
- 'redirects' => {},
175
- 'domains' => {},
176
- 'layouts' => {},
177
- 'partials' => {}
178
- }
179
-
180
- @backend_groups.each_pair do |group_name, instance|
181
- config['backends'][group_name] = instance.dump
182
- end
183
-
184
- @redirects.each_pair do |src, redirect|
185
- config['redirects'][src] = redirect.target
186
- end
187
-
188
- @domains.each_pair do |domain_name, domain|
189
- config['domains'][domain_name] = domain.dump
190
- end
191
-
192
- @layouts.each_pair do |name, layout|
193
- config['layouts'][name] = layout.dump
194
- end
195
-
196
- @partials.each_pair do |name, partial|
197
- config['partials'][name] = partial.dump
198
- end
199
-
200
- puts JSON.pretty_generate config if $debug
201
- Odania.plugin.set_global_config config
202
- end
203
-
204
- def backend_groups
205
- @backend_groups
206
- end
207
-
208
- def default_backend
209
- group_name = @default_backend_groups.first
210
- group_name = @backend_groups.keys.first if group_name.nil?
211
-
212
- @backend_groups[group_name].backends.first
213
- end
214
-
215
- def redirects
216
- @redirects
217
- end
218
-
219
- def domains
220
- @domains
221
- end
222
-
223
- def default_subdomains
224
- @default_subdomains
225
- end
226
-
227
- private
228
-
229
- def reset
230
- @default_backend_groups = []
231
- @backend_groups = Hash.new { |hash, key| hash[key] = BackendGroup.new(key) }
232
- @redirects = Hash.new { |hash, key| hash[key] = Redirect.new(key) }
233
- @domains = Hash.new { |hash, key| hash[key] = Domain.new(key) }
234
- @default_subdomains = {}
235
- @layouts = Hash.new { |hash, key| hash[key] = Layout.new }
236
- @partials = Hash.new { |hash, key| hash[key] = Partial.new }
237
-
238
- @duplicates = Duplicates.new
239
- end
240
- end
241
- end
242
- end