2

我正在使用 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 提供一些提示,我将非常高兴。我不熟悉语法并且做了很多研究,包括我自己的测试,但没有任何成功。

4

1 回答 1

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 文件生成器可以在一秒钟内完成,然后我们可以根据需要进行调整。

于 2013-12-02T14:54:57.753 回答