我正在使用Octopus来分离我的Rails应用程序的读写。我在JSON文件和包装类中有整个应用程序的配置,以便于访问。
现在我想通过生成shards.yml
文件来配置章鱼的从属连接,这样我就可以在我的JSON配置文件中设置已配置的从属数量。
这有效:
<%
# See config.json file for MyApp-specific configuration of web services
require 'myapp/config.rb'
%>
octopus:
environments:
- <%= MyApp::Config::deployment %>
replicated: true
fully_replicated: true
development:
slave1:
host: <%= MyApp::Config::DatabaseSlaves.host(0) %>
adapter: <%= MyApp::Config::DatabaseSlaves.adapter(0) %>
database: <%= MyApp::Config::DatabaseSlaves.database(0) %>
username: <%= MyApp::Config::DatabaseSlaves.username(0) %>
password: <%= MyApp::Config::DatabaseSlaves.password(0) %>
reconnect: <%= MyApp::Config::DatabaseSlaves.reconnect(0) %>
这不是:
<%
# See config.json file for MyApp-specific configuration of web services
require 'myapp/config.rb'
%>
octopus:
environments:
- <%= MyApp::Config::deployment %>
replicated: true
fully_replicated: true
<%= MyApp::Config::deployment %>:
<% counter = 1
while counter <= MyApp::Config::DatabaseSlaves.count do %>
slave<%= counter.to_s %>:
host: <%= MyApp::Config::DatabaseSlaves.host(counter - 1) %>
adapter: <%= MyApp::Config::DatabaseSlaves.adapter(counter - 1) %>
database: <%= MyApp::Config::DatabaseSlaves.database(counter - 1) %>
username: <%= MyApp::Config::DatabaseSlaves.username(counter - 1) %>
password: <%= MyApp::Config::DatabaseSlaves.password(counter - 1) %>
reconnect: <%= MyApp::Config::DatabaseSlaves.reconnect(counter - 1) %>
<%
counter++
end
%>
当我在数据库上运行Webrick或Rake任务时给我一个错误:
SyntaxError ((erb):23: syntax error, unexpected keyword_end
(erb):25: syntax error, unexpected $end, expecting keyword_end
; _erbout.force_encoding(__ENCODING__)
^):
/usr/lib/ruby/1.9.1/erb.rb:838:in `eval'
/usr/lib/ruby/1.9.1/erb.rb:838:in `result'
/home/jriepshoff/.bundler/ruby/1.9.1/octopus-4435a53054ee/lib/octopus.rb:22:in `config'
/home/jriepshoff/.bundler/ruby/1.9.1/octopus-4435a53054ee/lib/octopus.rb:64:in `environments'
/home/jriepshoff/.bundler/ruby/1.9.1/octopus-4435a53054ee/lib/octopus.rb:38:in `enabled?'
/home/jriepshoff/.bundler/ruby/1.9.1/octopus-4435a53054ee/lib/octopus/model.rb:51:in `should_use_normal_connection?'
/home/jriepshoff/.bundler/ruby/1.9.1/octopus-4435a53054ee/lib/octopus/model.rb:59:in `connection_with_octopus'
activerecord (4.0.1) lib/active_record/migration.rb:792:in `current_version'
activerecord (4.0.1) lib/active_record/migration.rb:800:in `needs_migration?'
activerecord (4.0.1) lib/active_record/migration.rb:379:in `check_pending!'
activerecord (4.0.1) lib/active_record/migration.rb:366:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.1) lib/active_support/callbacks.rb:373:in `_run__2591721780440848137__call__callbacks'
activesupport (4.0.1) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.1) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.1) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.1) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.1) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.0.1) lib/rails/engine.rb:511:in `call'
railties (4.0.1) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
如果有人能提供一些关于如何生成YAML的提示,我会很高兴。我不熟悉语法,做了很多研究,包括我自己的测试但没有任何成功。
答案 0 :(得分:2)
不要使用ERB,而是使用直接Ruby。当我们在工作中创建复杂的YAML文件时,我从Ruby开始,让它发出样板:
require 'yaml'
# octopus:
# environments:
# - <%= MyApp::Config::deployment %>
# replicated: true
# fully_replicated: true
# development:
# slave1:
# host: <%= MyApp::Config::DatabaseSlaves.host(0) %>
# adapter: <%= MyApp::Config::DatabaseSlaves.adapter(0) %>
# database: <%= MyApp::Config::DatabaseSlaves.database(0) %>
# username: <%= MyApp::Config::DatabaseSlaves.username(0) %>
# password: <%= MyApp::Config::DatabaseSlaves.password(0) %>
# reconnect: <%= MyApp::Config::DatabaseSlaves.reconnect(0) %>
config = {
'octopus' => {
'environments' => 'MyApp::Config.deployment',
'replicated' => true,
'fully_replicated' => true,
'development' => {
'slave1' => {
'host' => 'MyApp::Config::DatabaseSlaves.host(0)',
'adapter' => 'MyApp::Config::DatabaseSlaves.adapter(0)',
'database' => 'MyApp::Config::DatabaseSlaves.database(0)',
'username' => 'MyApp::Config::DatabaseSlaves.username(0)',
'password' => 'MyApp::Config::DatabaseSlaves.password(0)',
'reconnect' => 'MyApp::Config::DatabaseSlaves.reconnect(0)',
}
}
}
}
puts config.to_yaml
生成:
---
octopus:
environments: MyApp::Config.deployment
replicated: true
fully_replicated: true
development:
slave1:
host: MyApp::Config::DatabaseSlaves.host(0)
adapter: MyApp::Config::DatabaseSlaves.adapter(0)
database: MyApp::Config::DatabaseSlaves.database(0)
username: MyApp::Config::DatabaseSlaves.username(0)
password: MyApp::Config::DatabaseSlaves.password(0)
reconnect: MyApp::Config::DatabaseSlaves.reconnect(0)
一旦我拥有基本模板,我可以手动修改它或充实代码以实际生成我需要的一切。我这样做是因为一些应用程序有很多配置参数,并且必须手动生成该配置太容易出错。相反,YAML文件生成器可以在一秒钟内完成,然后我们可以根据需要进行调整。