Ruby on rails 以编程方式创建shards.yml?
我正在使用八达通来分离Rails应用程序的读写操作。我将整个应用程序的配置放在一个JSON文件和一个包装器类中,以便于访问 现在我想通过生成Ruby on rails 以编程方式创建shards.yml?,ruby-on-rails,ruby,config,octopus,Ruby On Rails,Ruby,Config,Octopus,我正在使用八达通来分离Rails应用程序的读写操作。我将整个应用程序的配置放在一个JSON文件和一个包装器类中,以便于访问 现在我想通过生成shards.yml文件来配置octopus的从属连接,这样我就可以在JSON配置文件中设置配置的从属连接的数量 这项工作: <% # See config.json file for MyApp-specific configuration of web services require 'myapp/config.rb' %> octop
shards.yml
文件来配置octopus的从属连接,这样我就可以在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) %>
如果有人能就如何生成YAML给出一些提示,我将非常高兴。我不熟悉语法,做了很多研究,包括自己的测试,但没有成功。不要使用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
一旦我有了基本的模板,我就可以手工修改它,或者充实代码来实际生成我需要的一切。我这样做是因为有些应用程序有很多配置参数,手工生成配置太容易出错。相反,YAML文件生成器可以在一秒钟内完成这项工作,然后我们可以根据需要进行调整。我非常喜欢这种方法,现在它非常适合我。
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'
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)