odania 0.0.16 → 0.0.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +63 -21
- data/Guardfile +31 -0
- data/Rakefile +1 -0
- data/config/varnish_config.json +4 -2
- data/lib/odania/{plugin_config/models → config}/backend.rb +1 -1
- data/lib/odania/{plugin_config/models → config}/backend_group.rb +7 -2
- data/lib/odania/config/domain.rb +55 -0
- data/lib/odania/config/duplicates.rb +28 -0
- data/lib/odania/config/global_config.rb +154 -0
- data/lib/odania/config/internal.rb +48 -0
- data/lib/odania/config/layout.rb +35 -0
- data/lib/odania/config/page.rb +28 -0
- data/lib/odania/config/plugin_config.rb +51 -0
- data/lib/odania/config/style.rb +60 -0
- data/lib/odania/config/sub_domain.rb +125 -0
- data/lib/odania/config.rb +16 -0
- data/lib/odania/consul.rb +6 -2
- data/lib/odania/erb.rb +154 -0
- data/lib/odania/plugin.rb +12 -5
- data/lib/odania/plugin_config/{plugin_config.rb → base.rb} +26 -31
- data/lib/odania/varnish/generators/generate_backend_vcl.rb +12 -0
- data/lib/odania/varnish/generators/generate_site_vcl.rb +16 -0
- data/lib/odania/varnish/generators/generate_sites_vcl.rb +0 -4
- data/lib/odania/varnish.rb +11 -12
- data/lib/odania/version.rb +1 -1
- data/lib/odania.rb +7 -1
- data/odania.gemspec +3 -0
- data/spec/fixtures/global_config.json +142 -0
- data/spec/fixtures/plugin_config_1.json +93 -0
- data/spec/lib/odania/config/global_config_spec.rb +67 -0
- data/spec/lib/odania/config/plugin_config_spec.rb +28 -0
- data/spec/lib/odania/erb_spec.rb +16 -0
- data/spec/lib/odania/plugin_spec.rb +25 -0
- data/spec/lib/odania/varnish_spec.rb +37 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/consul_mock.rb +116 -0
- data/tasks/odania.rake +1 -5
- data/templates/varnish/backend.vcl.erb +7 -1
- data/templates/varnish/catch_all.vcl.erb +1 -0
- data/templates/varnish/default.vcl.erb +1 -0
- data/templates/varnish/general.vcl.erb +29 -13
- data/templates/varnish/general_site.vcl.erb +33 -0
- data/templates/varnish/redirects.vcl.erb +1 -0
- data/templates/varnish/site.vcl.erb +18 -8
- data/templates/varnish/sites.vcl.erb +1 -1
- data/templates/varnish/websocker.vcl.erb +1 -0
- metadata +79 -19
- data/lib/odania/plugin_config/models/domain.rb +0 -38
- data/lib/odania/plugin_config/models/layout.rb +0 -31
- data/lib/odania/plugin_config/models/page.rb +0 -23
- data/lib/odania/plugin_config/models/redirect.rb +0 -20
- data/lib/odania/plugin_config/models/sub_domain.rb +0 -99
- data/lib/odania/template/asset.rb +0 -15
- data/lib/odania/template/config.rb +0 -15
- data/lib/odania/template/page.rb +0 -16
- data/lib/odania/template.rb +0 -8
- data/lib/odania/varnish/generators/generate_site_assets_vcl.rb +0 -27
- data/templates/varnish/site_assets.vcl.erb +0 -24
- /data/spec/{odania → lib}/odania_spec.rb +0 -0
@@ -0,0 +1,125 @@
|
|
1
|
+
module Odania
|
2
|
+
module Config
|
3
|
+
class SubDomain
|
4
|
+
attr_accessor :name, :config, :direct, :dynamic, :internal, :from_plugin, :redirects
|
5
|
+
|
6
|
+
def initialize(name)
|
7
|
+
self.name = name
|
8
|
+
reset
|
9
|
+
end
|
10
|
+
|
11
|
+
def set_config(config_data, group_name)
|
12
|
+
errors = []
|
13
|
+
return errors if config_data.nil?
|
14
|
+
|
15
|
+
config_data.each_pair do |key, val|
|
16
|
+
from_plugin[:config][key] << group_name
|
17
|
+
errors << {:type => :config, :plugins => from_plugin[:config][key], :key => key} unless config[key].nil?
|
18
|
+
config[key] = val
|
19
|
+
end
|
20
|
+
errors
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_page(type, web_url, group_name, url_data, plugin_name)
|
24
|
+
result = true
|
25
|
+
result = false unless self.pages[type].key? web_url
|
26
|
+
self.pages[type][web_url].group_name = group_name
|
27
|
+
self.pages[type][web_url].plugin_url = url_data['plugin_url']
|
28
|
+
self.pages[type][web_url].cacheable = url_data['cacheable'] unless url_data['cacheable'].nil?
|
29
|
+
self.pages[type][web_url].expires = url_data['expires'] unless url_data['expires'].nil?
|
30
|
+
@plugins[:page]["#{type}-#{web_url}"] << plugin_name
|
31
|
+
result
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_internal(web_url, group_name, plugin_url, plugin_name)
|
35
|
+
result = true
|
36
|
+
result = false unless self.assets.key? web_url
|
37
|
+
self.assets[web_url].group_name = group_name
|
38
|
+
self.assets[web_url].plugin_url = plugin_url
|
39
|
+
@plugins[:asset][web_url] << plugin_name
|
40
|
+
result
|
41
|
+
end
|
42
|
+
|
43
|
+
def get_redirects
|
44
|
+
return {} if self.config.nil?
|
45
|
+
return {} if self.config['redirects'].nil?
|
46
|
+
self.config['redirects']
|
47
|
+
end
|
48
|
+
|
49
|
+
def plugins(type, key)
|
50
|
+
@plugins[type][key]
|
51
|
+
end
|
52
|
+
|
53
|
+
def dump
|
54
|
+
direct_data = {}
|
55
|
+
direct.each_pair do |web_url, page|
|
56
|
+
direct_data[web_url] = page.dump
|
57
|
+
end
|
58
|
+
|
59
|
+
dynamic_data = {}
|
60
|
+
dynamic.each_pair do |web_url, page|
|
61
|
+
dynamic_data[web_url] = page.dump
|
62
|
+
end
|
63
|
+
|
64
|
+
{
|
65
|
+
'redirects' => self.redirects,
|
66
|
+
'config' => self.config,
|
67
|
+
'direct' => direct_data,
|
68
|
+
'dynamic' => dynamic_data,
|
69
|
+
'internal' => self.internal.dump
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
def load(data)
|
74
|
+
reset
|
75
|
+
self.add(data)
|
76
|
+
end
|
77
|
+
|
78
|
+
def add(data, group_name=nil)
|
79
|
+
self.config = data['config'] unless data['config'].nil?
|
80
|
+
duplicates = Hash.new { |hash, key| hash[key] = [] }
|
81
|
+
|
82
|
+
unless data['direct'].nil?
|
83
|
+
data['direct'].each_pair do |name, data|
|
84
|
+
duplicates[:direct] << name if self.direct.key? name
|
85
|
+
self.direct[name].load(data, group_name)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
unless data['dynamic'].nil?
|
90
|
+
data['dynamic'].each_pair do |name, data|
|
91
|
+
duplicates[:dynamic] << name if self.direct.key? name
|
92
|
+
self.dynamic[name].load(data, group_name)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
self.internal.load(data['internal']) unless data['internal'].nil?
|
97
|
+
unless data['redirects'].nil?
|
98
|
+
data['redirects'].each_pair do |src_url, target_url|
|
99
|
+
duplicates[:redirect] << src_url if self.redirects.key? src_url
|
100
|
+
self.redirects[src_url] = target_url
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
duplicates
|
105
|
+
end
|
106
|
+
|
107
|
+
def [](type)
|
108
|
+
return self.direct if 'direct'.eql? type.to_s
|
109
|
+
self.dynamic
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
def reset
|
115
|
+
self.config = {}
|
116
|
+
self.from_plugin = {:config => Hash.new { |hash, key| hash[key] = [] }}
|
117
|
+
self.direct = Hash.new { |hash, key| hash[key] = Page.new }
|
118
|
+
self.dynamic = Hash.new { |hash, key| hash[key] = Page.new }
|
119
|
+
self.redirects = {}
|
120
|
+
self.internal = Internal.new
|
121
|
+
@plugins = {:direct => Hash.new { |hash, key| hash[key] = [] }, :dynamic => Hash.new { |hash, key| hash[key] = [] }}
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative 'config/internal'
|
2
|
+
require_relative 'config/domain'
|
3
|
+
require_relative 'config/duplicates'
|
4
|
+
require_relative 'config/layout'
|
5
|
+
require_relative 'config/page'
|
6
|
+
require_relative 'config/plugin_config'
|
7
|
+
require_relative 'config/global_config'
|
8
|
+
require_relative 'config/sub_domain'
|
9
|
+
require_relative 'config/style'
|
10
|
+
require_relative 'config/backend'
|
11
|
+
require_relative 'config/backend_group'
|
12
|
+
|
13
|
+
module Odania
|
14
|
+
module Config
|
15
|
+
end
|
16
|
+
end
|
data/lib/odania/consul.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Odania
|
2
2
|
class Consul
|
3
|
-
|
3
|
+
attr_reader :service, :config, :event, :health
|
4
4
|
|
5
5
|
def initialize(consul_url)
|
6
6
|
consul_url = "http://#{ENV['CONSUL_PORT_8500_TCP_ADDR']}:#{ENV['CONSUL_PORT_8500_TCP_PORT']}" if consul_url.nil?
|
@@ -63,7 +63,11 @@ module Odania
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def get_all_for(plugin_name)
|
66
|
-
|
66
|
+
begin
|
67
|
+
instances = get(plugin_name, :all)
|
68
|
+
rescue
|
69
|
+
instances = []
|
70
|
+
end
|
67
71
|
instances.is_a?(Array) ? instances : [instances]
|
68
72
|
end
|
69
73
|
|
data/lib/odania/erb.rb
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
module Odania
|
2
|
+
class Erb
|
3
|
+
attr_accessor :variables, :config, :page, :asset
|
4
|
+
|
5
|
+
def initialize(template, domain, partials, group_name, req_host)
|
6
|
+
@template = template
|
7
|
+
|
8
|
+
self.variables = Variables.new(template, domain, partials, group_name, req_host)
|
9
|
+
self.config = Config.new self.variables
|
10
|
+
self.page = Page.new self.variables
|
11
|
+
self.asset = Asset.new self.variables
|
12
|
+
|
13
|
+
if LOCAL_TEST_MODE
|
14
|
+
data = "\n<!-- Domain: #{domain} -->"
|
15
|
+
data += "\n<!-- Group Name: #{group_name} -->"
|
16
|
+
data += "\n<!-- Request Host: #{req_host} -->"
|
17
|
+
data += "\n<!-- Base Domain: #{self.variables.base_domain} -->"
|
18
|
+
data += "\n<!-- Partials: #{JSON.pretty_generate(self.variables.partials)} -->"
|
19
|
+
data += "\n<!-- Config: #{JSON.pretty_generate(self.variables.config)} -->"
|
20
|
+
data += "\n<!-- SubDomain Config: #{JSON.pretty_generate(self.variables.subdomain_config)} -->"
|
21
|
+
#data += "\n<!-- Global Config: #{JSON.pretty_generate(self.variables.global_config)} -->"
|
22
|
+
@template += data.html_safe
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def render
|
27
|
+
ERB.new(@template).result(binding)
|
28
|
+
end
|
29
|
+
|
30
|
+
class Variables
|
31
|
+
attr_accessor :template, :config, :subdomain_config, :global_config, :domain
|
32
|
+
attr_accessor :base_domain, :partials, :group_name, :req_host
|
33
|
+
|
34
|
+
def initialize(template, domain, partials, group_name, req_host)
|
35
|
+
self.template = template
|
36
|
+
self.global_config = Odania.plugin.get_global_config
|
37
|
+
|
38
|
+
domain_info = PublicSuffix.parse(domain)
|
39
|
+
self.config, self.base_domain = Odania.plugin.get_domain_config_for domain_info.domain, self.global_config
|
40
|
+
self.subdomain_config = self.config[domain_info.trd] unless domain_info.trd.nil?
|
41
|
+
self.subdomain_config = self.config['_general'] if self.subdomain_config.nil?
|
42
|
+
self.subdomain_config = {} if self.subdomain_config.nil?
|
43
|
+
|
44
|
+
self.domain = domain
|
45
|
+
self.partials = partials
|
46
|
+
self.group_name = group_name
|
47
|
+
self.req_host = req_host
|
48
|
+
end
|
49
|
+
|
50
|
+
def get_partial(page)
|
51
|
+
get_specific %w(internal partials), page
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_config_key(key)
|
55
|
+
get_specific %w(config), key
|
56
|
+
end
|
57
|
+
|
58
|
+
def notify_error_async(type, key, data)
|
59
|
+
data = {
|
60
|
+
domain: self.domain,
|
61
|
+
base_domain: self.base_domain,
|
62
|
+
group_name: self.group_name,
|
63
|
+
req_host: self.req_host,
|
64
|
+
type: type,
|
65
|
+
key: key,
|
66
|
+
data: data
|
67
|
+
}
|
68
|
+
ProcessErrorJob.perform_later data
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def get_specific(part, page)
|
74
|
+
subdomain = req_host.gsub(".#{domain}", '')
|
75
|
+
|
76
|
+
# subdomain specific layouts
|
77
|
+
result = retrieve_hash_path global_config, ['domains', domain, subdomain] + part + [page]
|
78
|
+
return result unless result.nil?
|
79
|
+
|
80
|
+
# domain specific layouts
|
81
|
+
result = retrieve_hash_path global_config, ['domains', domain, '_general'] + part + [page]
|
82
|
+
return result unless result.nil?
|
83
|
+
|
84
|
+
# general layouts
|
85
|
+
result = retrieve_hash_path global_config, %w(domains _general _general) + part + [page]
|
86
|
+
return result unless result.nil?
|
87
|
+
|
88
|
+
nil
|
89
|
+
end
|
90
|
+
|
91
|
+
def retrieve_hash_path(hash, path)
|
92
|
+
key = path.shift
|
93
|
+
|
94
|
+
return nil until hash.has_key? key
|
95
|
+
return hash[key] if path.empty?
|
96
|
+
retrieve_hash_path hash[key], path
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class Config
|
101
|
+
def initialize(variables)
|
102
|
+
@variables = variables
|
103
|
+
end
|
104
|
+
|
105
|
+
def get(key)
|
106
|
+
val = @variables.get_config_key(key)
|
107
|
+
return val unless val.nil?
|
108
|
+
"--- Config Key Not found: #{key} ---"
|
109
|
+
|
110
|
+
@variables.notify_error_async :config, 'Key not found', key
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class Page
|
115
|
+
def initialize(variables)
|
116
|
+
@variables = variables
|
117
|
+
end
|
118
|
+
|
119
|
+
def get(page)
|
120
|
+
if not @variables.partials[page].nil?
|
121
|
+
"<!-- Page: #{page} -->\n<esi:include src=\"#{@variables.partials[page]}\"/>\n<!-- End Page: #{page} -->"
|
122
|
+
else
|
123
|
+
partial = @variables.get_partial(page)
|
124
|
+
|
125
|
+
if partial.nil?
|
126
|
+
#"<!-- Page: #{page} -->\n<esi:include src=\"http://internal.core/template/partial/#{page}?domain=#{@variables.domain}&group_name=#{@variables.group_name}\"/>\n<!-- End Page: #{page} -->"
|
127
|
+
"\n\n\n<pre>UNHANDLED PAGE: #{page} !!!!!!!!!!!!!!!!!!!!</pre>\n\n\n"
|
128
|
+
else
|
129
|
+
"<!-- Page: #{page} -->\n<esi:include src=\"http://internal.core/template/partial/#{page}?domain=#{@variables.domain}&group_name=#{@variables.group_name}&plugin_url=#{partial['plugin_url']}&req_host=#{@variables.req_host}\"/>\n<!-- End Page: #{page} -->"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
class Asset
|
136
|
+
def initialize(variables)
|
137
|
+
@variables = variables
|
138
|
+
end
|
139
|
+
|
140
|
+
def get(asset)
|
141
|
+
asset_url = get_asset_url(@variables.domain)
|
142
|
+
asset_url = "//#{asset_url}" unless asset_url.include? '//'
|
143
|
+
"#{asset_url}/#{asset}"
|
144
|
+
end
|
145
|
+
|
146
|
+
private
|
147
|
+
|
148
|
+
def get_asset_url(domain)
|
149
|
+
return @variables.subdomain_config['asset_url'] unless @variables.subdomain_config['asset_url'].nil?
|
150
|
+
domain
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
data/lib/odania/plugin.rb
CHANGED
@@ -1,26 +1,25 @@
|
|
1
|
-
require_relative 'plugin_config/plugin_config'
|
2
|
-
|
3
1
|
module Odania
|
4
2
|
class Plugin
|
5
3
|
INSTANCE_FILES_PATH = '/tmp/plugin_instance_name_'
|
6
4
|
|
7
5
|
def initialize(consul)
|
8
6
|
@consul = consul
|
9
|
-
@plugin_config =
|
7
|
+
@plugin_config = Config::GlobalConfig.new
|
10
8
|
end
|
11
9
|
|
12
10
|
def register(plugin_instance_name, plugin_config)
|
13
|
-
plugin_name = plugin_config['name']
|
11
|
+
plugin_name = plugin_config['plugin-config']['name']
|
14
12
|
|
15
13
|
# Write configuration of the plugin
|
16
14
|
@consul.config.set(get_plugin_config_path_for(plugin_name), plugin_config)
|
17
15
|
|
18
16
|
# Register service
|
19
|
-
consul_config = @consul.service.build_config(plugin_name, plugin_instance_name, plugin_config['ip'], plugin_config['tags'], plugin_config['port'])
|
17
|
+
consul_config = @consul.service.build_config(plugin_name, plugin_instance_name, plugin_config['plugin-config']['ip'], plugin_config['plugin-config']['tags'], plugin_config['plugin-config']['port'])
|
20
18
|
@consul.service.register(consul_config)
|
21
19
|
|
22
20
|
# Fire event
|
23
21
|
@consul.event.fire 'service-registered', "#{plugin_name}|#{plugin_instance_name}"
|
22
|
+
"#{plugin_name}|#{plugin_instance_name}"
|
24
23
|
end
|
25
24
|
|
26
25
|
def deregister(plugin_instance_name)
|
@@ -61,6 +60,14 @@ module Odania
|
|
61
60
|
@consul.config.get get_global_plugin_config_path
|
62
61
|
end
|
63
62
|
|
63
|
+
def get_domain_config_for(domain, global_config=nil)
|
64
|
+
global_config = get_global_config if global_config.nil?
|
65
|
+
|
66
|
+
domain_info = PublicSuffix.parse(domain)
|
67
|
+
return global_config['domains'][domain_info.domain], domain unless global_config['domains'][domain_info.domain].nil?
|
68
|
+
return false, nil
|
69
|
+
end
|
70
|
+
|
64
71
|
def health
|
65
72
|
@consul.health
|
66
73
|
end
|
@@ -1,25 +1,20 @@
|
|
1
1
|
# Generate a joined configuration for all available plugins
|
2
2
|
|
3
|
+
require_relative 'duplicates'
|
3
4
|
require_relative 'models/backend'
|
4
5
|
require_relative 'models/backend_group'
|
5
6
|
require_relative 'models/domain'
|
6
7
|
require_relative 'models/layout'
|
7
8
|
require_relative 'models/page'
|
9
|
+
require_relative 'models/partial'
|
8
10
|
require_relative 'models/redirect'
|
9
11
|
require_relative 'models/sub_domain'
|
10
12
|
|
11
13
|
module Odania
|
12
14
|
module PluginConfig
|
13
|
-
class
|
15
|
+
class Base22
|
14
16
|
def initialize
|
15
|
-
|
16
|
-
@backend_groups = Hash.new { |hash, key| hash[key] = BackendGroup.new(key) }
|
17
|
-
@redirects = Hash.new { |hash, key| hash[key] = Redirect.new(key) }
|
18
|
-
@domains = Hash.new { |hash, key| hash[key] = Domain.new(key) }
|
19
|
-
@default_subdomains = {}
|
20
|
-
@layouts = Hash.new { |hash, key| hash[key] = Layout.new }
|
21
|
-
|
22
|
-
@duplicates = Duplicates.new
|
17
|
+
reset
|
23
18
|
end
|
24
19
|
|
25
20
|
def load_from_consul
|
@@ -53,6 +48,7 @@ module Odania
|
|
53
48
|
end
|
54
49
|
end
|
55
50
|
|
51
|
+
|
56
52
|
unless config['redirects'].nil?
|
57
53
|
config['redirects'].each_pair do |src, target|
|
58
54
|
@redirects[src].target = target
|
@@ -71,6 +67,12 @@ module Odania
|
|
71
67
|
end
|
72
68
|
end
|
73
69
|
|
70
|
+
unless config['partials'].nil?
|
71
|
+
config['partials'].each_pair do |name, data|
|
72
|
+
@partials[name].load(data)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
74
76
|
self
|
75
77
|
end
|
76
78
|
|
@@ -140,6 +142,14 @@ module Odania
|
|
140
142
|
end
|
141
143
|
end
|
142
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
|
+
|
143
153
|
@default_subdomains = config['default_subdomains'] unless config['default_subdomains'].nil?
|
144
154
|
end
|
145
155
|
|
@@ -163,7 +173,8 @@ module Odania
|
|
163
173
|
'backends' => {},
|
164
174
|
'redirects' => {},
|
165
175
|
'domains' => {},
|
166
|
-
'layouts' => {}
|
176
|
+
'layouts' => {},
|
177
|
+
'partials' => {}
|
167
178
|
}
|
168
179
|
|
169
180
|
@backend_groups.each_pair do |group_name, instance|
|
@@ -182,6 +193,10 @@ module Odania
|
|
182
193
|
config['layouts'][name] = layout.dump
|
183
194
|
end
|
184
195
|
|
196
|
+
@partials.each_pair do |name, partial|
|
197
|
+
config['partials'][name] = partial.dump
|
198
|
+
end
|
199
|
+
|
185
200
|
puts JSON.pretty_generate config if $debug
|
186
201
|
Odania.plugin.set_global_config config
|
187
202
|
end
|
@@ -218,30 +233,10 @@ module Odania
|
|
218
233
|
@domains = Hash.new { |hash, key| hash[key] = Domain.new(key) }
|
219
234
|
@default_subdomains = {}
|
220
235
|
@layouts = Hash.new { |hash, key| hash[key] = Layout.new }
|
236
|
+
@partials = Hash.new { |hash, key| hash[key] = Partial.new }
|
221
237
|
|
222
238
|
@duplicates = Duplicates.new
|
223
239
|
end
|
224
240
|
end
|
225
|
-
|
226
|
-
class Duplicates
|
227
|
-
def initialize
|
228
|
-
@duplicates = Hash.new { |hash, key| hash[key] = Hash.new { |hash, key| hash[key] = Duplicate.new } }
|
229
|
-
end
|
230
|
-
|
231
|
-
def add(group, type, key, plugin_info)
|
232
|
-
@duplicates[group][type].add(key, plugin_info)
|
233
|
-
end
|
234
|
-
|
235
|
-
class Duplicate
|
236
|
-
def initialize
|
237
|
-
@duplicate = {}
|
238
|
-
end
|
239
|
-
|
240
|
-
def add(key, plugin_info)
|
241
|
-
@duplicate[key] = [] if @duplicate[key].nil?
|
242
|
-
@duplicate[key] += plugin_info
|
243
|
-
end
|
244
|
-
end
|
245
|
-
end
|
246
241
|
end
|
247
242
|
end
|
@@ -8,6 +8,18 @@ module Odania
|
|
8
8
|
self.template = File.new("#{BASE_DIR}/templates/varnish/backend.vcl.erb").read
|
9
9
|
end
|
10
10
|
|
11
|
+
def core_backends
|
12
|
+
core_backends = []
|
13
|
+
self.backend_groups.each_pair do |group_name, backend_group|
|
14
|
+
if backend_group.core_backend
|
15
|
+
backend_group.backends.each do |backend|
|
16
|
+
core_backends << "#{Odania.varnish_sanitize(group_name)}_#{Odania.varnish_sanitize(backend.instance_name)}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
core_backends
|
21
|
+
end
|
22
|
+
|
11
23
|
def render
|
12
24
|
Erubis::Eruby.new(self.template).result(binding)
|
13
25
|
end
|
@@ -8,10 +8,26 @@ module Odania
|
|
8
8
|
'www'
|
9
9
|
end
|
10
10
|
|
11
|
+
def template_url_for(domain, page)
|
12
|
+
"&domain=#{domain.name}"+
|
13
|
+
"&plugin_url=#{page.plugin_url.nil? ? '/' : page.plugin_url}"+
|
14
|
+
"&group_name=#{Odania.varnish_sanitize(page.group_name)}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def prepare_url(url)
|
18
|
+
return "/#{url}" unless '/'.eql? url[0]
|
19
|
+
url
|
20
|
+
end
|
21
|
+
|
22
|
+
def general_subdomain
|
23
|
+
self.domain['_general']
|
24
|
+
end
|
25
|
+
|
11
26
|
def initialize(domain, default_subdomains)
|
12
27
|
self.domain = domain
|
13
28
|
self.default_subdomains = default_subdomains
|
14
29
|
self.template = File.new("#{BASE_DIR}/templates/varnish/site.vcl.erb").read
|
30
|
+
self.template = File.new("#{BASE_DIR}/templates/varnish/general_site.vcl.erb").read if '_general'.eql? domain.name
|
15
31
|
end
|
16
32
|
|
17
33
|
def render
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require_relative 'generate_site_vcl'
|
2
|
-
require_relative 'generate_site_assets_vcl'
|
3
2
|
|
4
3
|
module Odania
|
5
4
|
class GenerateSitesVcl
|
@@ -22,9 +21,6 @@ module Odania
|
|
22
21
|
self.domains.each_pair do |domain_name, domain|
|
23
22
|
out = GenerateSiteVcl.new(domain, default_subdomains)
|
24
23
|
out.write out_dir
|
25
|
-
|
26
|
-
out = GenerateSiteAssetsVcl.new(domain)
|
27
|
-
out.write out_dir
|
28
24
|
end
|
29
25
|
end
|
30
26
|
end
|
data/lib/odania/varnish.rb
CHANGED
@@ -9,16 +9,16 @@ module Odania
|
|
9
9
|
class Varnish
|
10
10
|
def generate(out_dir='/etc/varnish')
|
11
11
|
FileUtils.mkdir_p out_dir unless File.directory? out_dir
|
12
|
-
|
13
|
-
|
12
|
+
global_config_json = Odania.plugin.get_global_config
|
13
|
+
global_config = Odania.plugin.plugin_config.load_global_config global_config_json
|
14
14
|
|
15
15
|
# Backend config
|
16
|
-
backend_groups =
|
17
|
-
default_backend =
|
16
|
+
backend_groups = global_config.backend_groups
|
17
|
+
default_backend = global_config.default_backend
|
18
18
|
|
19
19
|
# Domain information config
|
20
|
-
domains =
|
21
|
-
default_subdomains =
|
20
|
+
domains = global_config.domains
|
21
|
+
default_subdomains = global_config.default_subdomains
|
22
22
|
|
23
23
|
# Generate catch all vcl
|
24
24
|
gen = GenerateCatchAllVcl.new
|
@@ -37,7 +37,7 @@ module Odania
|
|
37
37
|
gen.write(out_dir)
|
38
38
|
|
39
39
|
# Generate global redirects
|
40
|
-
gen = GenerateRedirectsVcl.new(
|
40
|
+
gen = GenerateRedirectsVcl.new(global_config.default_redirects)
|
41
41
|
gen.write(out_dir)
|
42
42
|
|
43
43
|
# Generate main vcl
|
@@ -47,7 +47,6 @@ module Odania
|
|
47
47
|
puts
|
48
48
|
puts 'Registering internal varnish plugin'
|
49
49
|
register_plugin
|
50
|
-
puts
|
51
50
|
end
|
52
51
|
|
53
52
|
def reload_config
|
@@ -71,10 +70,10 @@ module Odania
|
|
71
70
|
plugin_config = JSON.parse File.read "#{BASE_DIR}/config/varnish_config.json"
|
72
71
|
|
73
72
|
ips = Odania.ips
|
74
|
-
plugin_config['ips'] = ips
|
75
|
-
plugin_config['ip'] = ips.first
|
76
|
-
plugin_config['port'] = 80
|
77
|
-
plugin_config['tags'] = ["plugin-#{get_plugin_name}"]
|
73
|
+
plugin_config['plugin-config']['ips'] = ips
|
74
|
+
plugin_config['plugin-config']['ip'] = ips.first
|
75
|
+
plugin_config['plugin-config']['port'] = 80
|
76
|
+
plugin_config['plugin-config']['tags'] = ["plugin-#{get_plugin_name}"]
|
78
77
|
puts JSON.pretty_generate plugin_config if $debug
|
79
78
|
|
80
79
|
plugin_instance_name = Odania.plugin.get_plugin_instance_name get_plugin_name
|
data/lib/odania/version.rb
CHANGED
data/lib/odania.rb
CHANGED
@@ -4,16 +4,21 @@ require 'erubis'
|
|
4
4
|
require 'fileutils'
|
5
5
|
require 'uri/http'
|
6
6
|
require 'public_suffix'
|
7
|
+
require 'deep_merge'
|
8
|
+
require 'json'
|
9
|
+
require 'socket'
|
7
10
|
|
8
11
|
BASE_DIR = File.absolute_path File.join File.dirname(__FILE__), '..'
|
9
12
|
ENVIRONMENT = ENV['ENVIRONMENT'].nil? ? 'development' : ENV['ENVIRONMENT']
|
13
|
+
LOCAL_TEST_MODE = 'development'.eql?(ENVIRONMENT)
|
10
14
|
|
11
15
|
module Odania
|
12
16
|
CORE_PLUGIN_NAME = 'odania-core'
|
13
17
|
|
18
|
+
autoload :Config, 'odania/config'
|
14
19
|
autoload :Consul, 'odania/consul'
|
20
|
+
autoload :Erb, 'odania/erb'
|
15
21
|
autoload :Plugin, 'odania/plugin'
|
16
|
-
autoload :Template, 'odania/template'
|
17
22
|
autoload :Varnish, 'odania/varnish'
|
18
23
|
|
19
24
|
def self.plugin
|
@@ -42,6 +47,7 @@ module Odania
|
|
42
47
|
end
|
43
48
|
|
44
49
|
def self.varnish_sanitize(name)
|
50
|
+
raise 'Could not sanitize varnish name!!' if name.nil?
|
45
51
|
name.gsub(/[^0-9a-zA-Z_]/, '_')
|
46
52
|
end
|
47
53
|
end
|
data/odania.gemspec
CHANGED
@@ -22,8 +22,11 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency 'rake'
|
23
23
|
spec.add_development_dependency 'rspec'
|
24
24
|
spec.add_development_dependency 'cucumber'
|
25
|
+
spec.add_development_dependency 'guard'
|
26
|
+
spec.add_development_dependency 'guard-rspec'
|
25
27
|
|
26
28
|
spec.add_dependency 'diplomat'
|
27
29
|
spec.add_dependency 'erubis'
|
28
30
|
spec.add_dependency 'public_suffix'
|
31
|
+
spec.add_dependency 'deep_merge'
|
29
32
|
end
|