我的問題很簡單,但我無法在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
NOP ...我已經嘗試過,並有錯誤:SSHKit ::命令::失敗:貓/project/shared/config/database.yml標準輸出:沒有書面 貓/project/shared/config/database.yml stderr:沒有寫入 – Kulgar
可能原因是你有第一個路徑錯誤。 –
你的意思是,對於db_config?這是在capistrano任務中創建的變量。如果我用回聲代替貓,它可以工作,但是每行的末尾都被替換爲「;」。 (我只是用/ project替換了我的正常路徑......我不認爲路徑出現問題,就好像我通過在服務器上覆制/粘貼整個路徑來測試貓,它的工作原理)。我將用整個Capistrano任務編輯我的問題。 – Kulgar