cncflora_commons 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cncflora_commons.rb +47 -14
- metadata +1 -1
data/lib/cncflora_commons.rb
CHANGED
@@ -92,13 +92,6 @@ def etcd2config(server,prefix="")
|
|
92
92
|
config
|
93
93
|
end
|
94
94
|
|
95
|
-
def etcd2settings(server,prefix="")
|
96
|
-
config = etcd2config(server,prefix)
|
97
|
-
config.keys.each { |key| set key, config[key] }
|
98
|
-
set :config, config
|
99
|
-
config
|
100
|
-
end
|
101
|
-
|
102
95
|
def setup(file)
|
103
96
|
config_file file
|
104
97
|
|
@@ -108,10 +101,14 @@ def setup(file)
|
|
108
101
|
|
109
102
|
if ENV["DB"] then
|
110
103
|
set :db, ENV["DB"]
|
104
|
+
else
|
105
|
+
set :db, "cncflora"
|
111
106
|
end
|
112
107
|
|
113
108
|
if ENV["CONTEXT"] then
|
114
109
|
set :context, ENV["CONTEXT"]
|
110
|
+
else
|
111
|
+
set :context, "connect"
|
115
112
|
end
|
116
113
|
|
117
114
|
if ENV["PREFIX"] then
|
@@ -122,28 +119,48 @@ def setup(file)
|
|
122
119
|
|
123
120
|
if ENV["BASE"] then
|
124
121
|
set :base, ENV["BASE"]
|
122
|
+
else
|
123
|
+
set :base, ""
|
125
124
|
end
|
126
125
|
|
127
126
|
if settings.prefix.length >= 1 then
|
128
127
|
set :prefix, "#{settings.prefix}_"
|
129
128
|
end
|
130
129
|
|
131
|
-
|
130
|
+
if ENV["ETCD_PORT_4001_TCP_ADDR"] then
|
131
|
+
set :etcd, "http://#{ENV["ETCD_PORT_4001_TCP_ADDR"]}:#{ENV["ETCD_PORT_4001_TCP_PORT"]}"
|
132
|
+
elsif ENV["ETCD"] then
|
133
|
+
set :etcd, ENV["ETCD"]
|
134
|
+
else
|
135
|
+
set :etcd, "http://localhost:4001"
|
136
|
+
end
|
137
|
+
|
138
|
+
@config = etcd2config(settings.etcd,settings.prefix)
|
139
|
+
|
140
|
+
onchange(settings.etcd,settings.prefix) do |newconfig|
|
141
|
+
@config = newconfig
|
142
|
+
setup! newconfig
|
143
|
+
end
|
144
|
+
|
145
|
+
setup! @config
|
146
|
+
end
|
147
|
+
|
148
|
+
def setup!(config)
|
132
149
|
|
133
150
|
if settings.lang then
|
134
151
|
config[:strings] = JSON.parse(File.read("src/locales/#{settings.lang}.json", :encoding => "BINARY"))
|
135
152
|
end
|
136
153
|
|
137
|
-
if
|
138
|
-
config[:elasticsearch] = "#{config[:datahub]}/#{settings.db}"
|
139
|
-
else
|
154
|
+
if config.has_key? :elasticsearch then
|
140
155
|
config[:elasticsearch] = "#{config[:elasticsearch]}/#{settings.db}"
|
156
|
+
else
|
157
|
+
config[:elasticsearch] = "#{config[:datahub]}/#{settings.db}"
|
141
158
|
end
|
142
159
|
|
143
|
-
if
|
144
|
-
config[:couchdb] = "#{config[:datahub]}/#{settings.db}"
|
145
|
-
else
|
160
|
+
if config.has_key? :couchdb then
|
146
161
|
config[:couchdb] = "#{config[:couchdb]}/#{settings.db}"
|
162
|
+
else
|
163
|
+
config[:couchdb] = "#{config[:datahub]}/#{settings.db}"
|
147
164
|
end
|
148
165
|
|
149
166
|
if settings.context then
|
@@ -155,5 +172,21 @@ def setup(file)
|
|
155
172
|
end
|
156
173
|
|
157
174
|
config.keys.each { |key| set key, config[key] }
|
175
|
+
|
176
|
+
config
|
177
|
+
end
|
178
|
+
|
179
|
+
def onchange(etcd,prefix="")
|
180
|
+
@last = ""
|
181
|
+
Thread.new do
|
182
|
+
while true do
|
183
|
+
actual = Net::HTTP.get(URI("#{ etcd }/v2/keys/?recursive=true"))
|
184
|
+
if actual != @last then
|
185
|
+
yield etcd2config(etcd,prefix)
|
186
|
+
@last = actual
|
187
|
+
end
|
188
|
+
sleep 4
|
189
|
+
end
|
190
|
+
end
|
158
191
|
end
|
159
192
|
|