Class: Odania::Config::GlobalConfig

Inherits:
PluginConfig show all
Defined in:
lib/odania/config/global_config.rb

Instance Attribute Summary

Attributes inherited from PluginConfig

#config, #domains, #plugin_config

Instance Method Summary collapse

Methods inherited from PluginConfig

#[], #initialize, #load

Constructor Details

This class inherits a constructor from Odania::Config::PluginConfig

Instance Method Details

#add_backend(backend_config) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/odania/config/global_config.rb', line 110

def add_backend(backend_config)
	return if backend_config.ServiceAddress.nil? or backend_config.ServiceAddress.empty?

	backend = Backend.new(backend_config.ServiceName, backend_config.ServiceID, backend_config.ServiceAddress, backend_config.ServicePort)
	@backend_groups[backend_config.ServiceName].add_backend backend
	@backend_groups[backend_config.ServiceName].check_core_backend backend_config.ServiceTags
end

#add_plugin_config(plugin_cfg) ⇒ Object

Add the configuration from the plugin



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/odania/config/global_config.rb', line 73

def add_plugin_config(plugin_cfg)
	config_section = plugin_cfg['config']
	group_name = plugin_cfg['plugin-config']['name']

	if $debug
		puts 'Loading configuration'
		puts JSON.pretty_generate plugin_cfg
	end

	@plugin_config[group_name] = plugin_cfg['plugin-config']
	@default_subdomains.deep_merge(plugin_cfg['default_subdomains']) unless plugin_cfg['default_subdomains'].nil?

	# Add this service as a default backend if specified
	@default_backend_groups << group_name if plugin_cfg['plugin-config']['default']

	# Add config
	unless config_section.nil?
		config_section.each_pair do |key, _val|
			unless @config[key].nil?
				@duplicates.add :config, {key => 'already defined'}, group_name
			end
		end
		@config.deep_merge! config_section
	end

	# Add Domain Information
	unless plugin_cfg['domains'].nil?
		plugin_cfg['domains'].each_pair do |name, data|
			@domains[name].add(data, group_name).each do |duplicate_key, duplicate_data|
				@duplicates.add duplicate_key, duplicate_data, group_name
			end
		end
	end

	true
end

#backend_groupsObject



133
134
135
# File 'lib/odania/config/global_config.rb', line 133

def backend_groups
	@backend_groups
end

#default_backendObject



137
138
139
140
141
142
# File 'lib/odania/config/global_config.rb', line 137

def default_backend
	group_name = @default_backend_groups.first
	group_name = @backend_groups.keys.first if group_name.nil?
	raise 'No backend found' if @backend_groups.empty?
	@backend_groups[group_name].backends.first
end

#default_redirectsObject



148
149
150
151
152
# File 'lib/odania/config/global_config.rb', line 148

def default_redirects
	general = @domains['_general'].subdomain('_general')
	return {} if general.nil?
	general.redirects
end

#default_subdomainsObject



144
145
146
# File 'lib/odania/config/global_config.rb', line 144

def default_subdomains
	@default_subdomains
end

#dumpObject



154
155
156
157
158
# File 'lib/odania/config/global_config.rb', line 154

def dump
	cfg = super
	cfg['default_backend_groups'] = @default_backend_groups
	cfg
end

#duplicatesObject



129
130
131
# File 'lib/odania/config/global_config.rb', line 129

def duplicates
	@duplicates.duplicates
end

#generate_global_configObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/odania/config/global_config.rb', line 61

def generate_global_config
	puts 'Loading plugin configs from consul'
	load_from_consul

	puts 'Generating global config'
	config = self.dump
	puts JSON.pretty_generate config if $debug
	Odania.plugin.set_global_config config
	config
end

#load_from_consulObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/odania/config/global_config.rb', line 31

def load_from_consul
	Odania.plugin.get_all.each_pair do |plugin_name, instances|
		puts "PLUGIN NAME #{plugin_name} - #{instances.count}" if $debug
		instances.each do |instance|
			add_backend(instance)
		end

		config = Odania.plugin.config_for plugin_name
		next if config.nil?

		begin
			puts JSON.pretty_generate config if $debug
			self.add_plugin_config(config)
		rescue => e
			puts 'Error loading configuration'
			puts 'Config start ' + '+' * 50
			puts JSON.pretty_generate config
			puts 'Config end ' + '+' * 50
			puts 'Error start ' + '+' * 50
			puts e.inspect
			puts 'Error end ' + '+' * 50
			puts 'Error backtrace start ' + '+' * 50
			e.backtrace.each do |line|
				puts line
			end
			puts 'Error backtrace end ' + '+' * 50
		end
	end
end

#load_global_config(config) ⇒ Object

Load the global configuration



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/odania/config/global_config.rb', line 5

def load_global_config(config)
	throw 'Invalid global config!' if config.nil?

	reset
	@default_backend_groups = config['default_backend_groups'] unless config['default_backend_groups'].nil?

	unless config['domains'].nil?
		config['domains'].each_pair do |name, data|
			@domains[name].load(data)
		end
	end

	# load backends from consul
	Odania.plugin.get_all.each_pair do |_name, instances|
		instances.each do |instance|
			add_backend(instance)
		end
	end

	#puts config.inspect
	#puts @backend_groups.inspect
	#puts @default_backend_groups.inspect

	self
end

#resetObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/odania/config/global_config.rb', line 118

def reset
	super

	@default_backend_groups = []
	@duplicates = Duplicates.new
	@backend_groups = Hash.new { |hash, key| hash[key] = BackendGroup.new(key) }
	@default_subdomains = {}

	@domains['_general'].add_subdomain('_general')
end