【问题标题】:Using Postgresql with Amazon Opsworks - Getting IP address in database.yml将 Postgresql 与 Amazon Opsworks 一起使用 - 在 database.yml 中获取 IP 地址
【发布时间】:2023-04-03 17:22:01
【问题描述】:
我正在尝试使用 Amazon Opsworks 让一个基本的 Rails 应用程序与 Postgres 一起使用。 Opsworks 目前缺乏对 Postgres 的内置支持,但我正在使用一些我发现的似乎写得很好的食谱。我已将它们全部分叉到我的自定义食谱中:https://github.com/tibbon/custom-opsworks-cookbooks
无论如何,我现在卡住的地方是将主 postgres 数据库的 IP 地址放入 database.yml 文件中。 似乎应该指定多个后端,有点像我的 haproxy 服务器如何将所有 rails 服务器视为“后端”。
有人搞定这个吗?
【问题讨论】:
标签:
ruby-on-rails
amazon-web-services
chef-infra
rails-postgresql
【解决方案1】:
我相信您必须定义一个自定义配方来更新 database.yml 并重新启动应用服务器。
在this guide 中以redis 服务器为例进行了同样的操作:
node[:deploy].each do |application, deploy|
if deploy[:application_type] != 'rails'
Chef::Log.debug("Skipping redis::configure as application #{application} as it is not an Rails app")
next
end
execute "restart Rails app #{application}" do
cwd deploy[:current_path]
command "touch tmp/restart.txt"
action :nothing
only_if do
File.exists?(deploy[:current_path])
end
end
redis_server = node[:opsworks][:layers][:redis][:instances].keys.first rescue nil
template "#{deploy[:deploy_to]}/current/config/redis.yml" do
source "redis.yml.erb"
mode "0660"
group deploy[:group]
owner deploy[:user]
variables(:host => (node[:opsworks][:layers][:redis][:instances][redis_server][:private_dns_name] rescue nil))
notifies :run, resources(:execute => "restart Rails app #{application}")
only_if do
File.directory?("#{deploy[:deploy_to]}/current")
end
end
end
我还没有为自己测试过这个,但我相信我很快就会尝试更新这个答案。
【解决方案2】:
我必须向我的 Rails 层添加一些自定义 JSON。
看起来像这样:
{
"deploy": {
"my-app-name": {
"database": {
"adapter":"mysql2",
"host":"xxx.xx.xxx.xx"
}
}
}
}