我花了好几天的时间才正确地解决这个问题。
只是为了澄清,这就是我修复它的方法。我不知道这是否正确,但它对我有用。
生成一组遵循本教程的公钥和私钥 https://help.github.com/articles/generating-ssh-keys.
将公钥添加到要克隆的 Github 存储库。
在我的默认配方中创建一个模板,其中包含公钥和私钥。见下文。
为公钥和私钥创建了相关模板。
创建了 Chef_ssh_deploy_wrapper.sh.erb 文件(见下文)
创建了一个deploy.rb配方(见下文)
上传食谱并将其添加到我的角色中。跑厨师兼客户。
嘿,快点!坐下来喝杯啤酒,看看你的回购协议。巧妙地克隆到您的目录中。
模板如下:
创建目录和模板:
template "/tmp/.ssh/chef_ssh_deploy_wrapper.sh" do
source "chef_ssh_deploy_wrapper.sh.erb"
owner node[:base][:username]
mode 0770
end
template "/home/#{node[:base][:username]}/.ssh/id_rsa.pub" do
source "id_rsa.pub.erb"
owner node[:base][:username]
mode 0600
end
template "/home/#{node[:base][:username]}/.ssh/id_rsa" do
source "id_rsa.erb"
owner node[:base][:username]
mode 0600
end
创建 ssh 包装器Chef_ssh_deploy_wrapper.erb
#!/bin/sh
exec ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "/home/#{node[:base][:username]}/.ssh/id_rsa" "$@"
(这里一定要使用私钥,否则会失败)
最后是deploy.rb配方:
deploy_branch node[:my_app][:deploy_to] do
repo node[:base][:repository]
ssh_wrapper "/tmp/.ssh/chef_ssh_deploy_wrapper.sh"
branch "rails4"
user node[:base][:username]
group node[:base][:username]
rollback_on_error true
migrate false
environment "RAILS_ENV" => node[:my_app][:environment]
purge_before_symlink %w{conf data log tmp public/system public/assets}
create_dirs_before_symlink []
symlinks(
"config" => "config",
"data" => "data",
"log" => "log",
"tmp" => "tmp",
"system" => "public/system",
"assets" => "public/assets"
)
scm_provider Chef::Provider::Git # is the default, for svn: Chef::Provider::Subversion
before_restart do
system("su #{node[:base][:username]} -c 'cd #{node[:my_app][:deploy_to]}/current && /usr/bin/bundle install'") or raise "bundle install failed"
system("su #{node[:base][:username]} -c 'RAILS_ENV=production /usr/local/bin/rake assets:precompile'")
end
notifies :restart, "service[my_app]"
notifies :restart, "service[nginx]"
end
重新启动之前的内容已被替换,因为我们最初是从源代码编译 ruby,但最终决定使用 rvm。多用户安装更加容易。
注意:我以 sudo 用户身份进行部署,如果您以 root 身份进行部署(避免这种情况),请改用 /root/.ssh 路径。
我从中得到了很多灵感本文 http://web.archive.org/web/20160515204949/http://tech.hulu.com/blog/2012/07/06/automating-system-provisioning-and-application-deployment-with-chef/.
祝你好运,我希望这对某人有帮助。