2014-05-09 30 views
0

我的問題很簡單,但我無法在Google上找到答案。Ruby On Rails:如何在使用Capistrano 3進行部署時如何在文件中編寫yml配置?

我想通過Capistrano任務將yml配置插入到我的服務器上的遠程文件中。

雖然這是很容易與Capistrano的2和「放」的命令,我只是無法找到Capistrano的3

比如做正確的方式,這是我的陽明配置:

set(:database_username, "db_user") 

    ask(:database_password, "Database Password: ") 

    db_config = <<-EOF 
     base: &base 
     adapter: postgresql 
     encoding: unicode 
     reconnect: false 
     pool: 10 
     username: #{fetch(:database_username)} 
     password: #{fetch(:database_password)} 

     staging: 
     database: #{fetch(:application)}_staging 
     <<: *base 

     production: 
     database: #{fetch(:application)}_production 
     <<: *base 
    EOF 

我想將我的「db_config」變量插入到database.yml文件中。使用Capistrano 2我會這樣做:

put db_config, "#{shared_path}/config/database.yml" 

但與Capistrano 3它不再起作用。 我想是這樣的:

execute "echo '#{db_config}' > #{shared_path}/config/database.yml" 

但是行尾不被保存,它們被替換爲「;」因爲'echo'命令。

有沒有人曾經用Capistrano 3做過這樣的事情?

否則,我將只使用「database.yml.example」並在部署後直接對其進行修改。

謝謝!

[更新]這裏是我的整個Capistrano的3任務代碼:

namespace :db do 
    desc "Create database yaml in shared path" 
    task :configure do 
    on roles(:db) do 
     if capture("cat #{shared_path}/config/database.yml").length > 0 
     puts "### INFO: Database.yml already exists" 
     else 
     set(:database_username, "db_user")  
     ask(:database_password, "Database Password: ") 

     db_config = <<-EOF 
      base: &base 
      adapter: postgresql 
      encoding: unicode 
      reconnect: false 
      pool: 10 
      username: #{fetch(:database_username)} 
      password: #{fetch(:database_password)} 

      staging: 
      database: #{fetch(:application)}_staging 
      <<: *base 

      production: 
      database: #{fetch(:application)}_production 
      <<: *base 
     EOF 

     execute "mkdir -p #{shared_path}/config" 
     execute "touch #{shared_path}/config/database.yml" 

     # Everything works so far. This is the buggy part: 
     #execute "echo '#{db_config}' > #{shared_path}/config/database.yml" 
     execute "cat '#{db_config}' > #{shared_path}/config/database.yml" 
     end 
    end 
    end 
end 

回答

0

這應該工作。

execute "cat '#{db_config}' > #{shared_path}/config/database.yml" 
+0

NOP ...我已經嘗試過,並有錯誤:SSHKit ::命令::失敗:貓/project/shared/config/database.yml標準輸出:沒有書面 貓/project/shared/config/database.yml stderr:沒有寫入 – Kulgar

+0

可能原因是你有第一個路徑錯誤。 –

+0

你的意思是,對於db_config?這是在capistrano任務中創建的變量。如果我用回聲代替貓,它可以工作,但是每行的末尾都被替換爲「;」。 (我只是用/ project替換了我的正常路徑......我不認爲路徑出現問題,就好像我通過在服務器上覆制/粘貼整個路徑來測試貓,它的工作原理)。我將用整個Capistrano任務編輯我的問題。 – Kulgar

0

我在解決這個問題時遇到了麻煩,因爲capistrano 3用分號取代換行符。因此,這裏是我的代碼:

namespace :setup do 
    task :setup_database do 
    ask(:db_user, 'db_user') 
    ask(:db_pass, 'db_pass') 
    ask(:db_name, 'db_name') 
    db_config = <<-EOF 
production: 
    adapter: mysql2 
    database: #{fetch(:db_name)} 
    username: #{fetch(:db_user)} 
    password: #{fetch(:db_pass)} 
    EOF 

    on roles(:app) do 
     execute "mkdir -p #{shared_path}/config" 
     upload! StringIO.new(db_config), "#{shared_path}/config/database.yml" 
    end 
    end 
end 
+0

這似乎是一個非常好的解決方案。儘管我沒有時間去測試它,但是隻要我測試一下,我就會回到這裏。謝謝! :) – Kulgar

相關問題