Ruby on rails Chef deploy_资源私有repo、ssh部署密钥和ssh_包装器

Ruby on rails Chef deploy_资源私有repo、ssh部署密钥和ssh_包装器,ruby-on-rails,chef-infra,Ruby On Rails,Chef Infra,我很难让我的厨师食谱复制私人回购。嗯,我昨天让它工作了,但在我的流浪汉盒子“cheffin”了半打之后,我把它打碎了。你可能猜到我是个厨师新手 按照这里的deploy_资源指南,我创建了我的deploy.rb配方(缩写): 在默认情况下,我有以下内容来创建dir等 directory "/tmp/.ssh" do action :create owner node[:base][:username] group node[:base][:username] recursive t

我很难让我的厨师食谱复制私人回购。嗯,我昨天让它工作了,但在我的流浪汉盒子“cheffin”了半打之后,我把它打碎了。你可能猜到我是个厨师新手

按照这里的deploy_资源指南,我创建了我的deploy.rb配方(缩写):

在默认情况下,我有以下内容来创建dir等

directory "/tmp/.ssh" do
  action :create
  owner node[:base][:username]
  group node[:base][:username]
  recursive true
end

template "/tmp/.ssh/chef_ssh_deploy_wrapper.sh" do
  source "chef_ssh_deploy_wrapper.sh.erb"
  owner node[:base][:username]
  mode 0770
end

# Put SSH private key to be used with SSH wrapper
template "/tmp/.ssh/id_deploy" do
  source "id_rsa.pub.erb"
  owner node[:base][:username]
  mode 0600
end
在包装中:

#!/bin/sh
exec ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "/tmp/.ssh/id_deploy" "$@"
我已经创建了一个公钥并将其上传到github

当我部署配方时,它会给我一个错误:

 deploy_branch[/var/www/html/ps] action deployEnter passphrase for key '/tmp/.ssh/id_deploy': 
Obvs我没有设置密码。。。因此,私钥必须丢失

碰巧,我从配方中删除了id_部署键,删除了文件夹,然后再次运行。瞧,它开始工作了。。。原因是当我手动生成要测试的id_rsa.pub&&id_rsa文件时,它们位于/root/.ssh中

我不明白我做错了什么。因此,我的问题是:

  • 在部署到的每个节点上是否需要私钥和公钥?医生没有提到这一点
  • 这不应该作为非根用户部署吗?我已在我的角色文件中设置了一个用户
  • 为什么ssh_包装器没有做它应该做的事情

    • 花了好几天的时间才正确地解决了这个问题

      我只是想澄清一下,这就是我为解决这个问题所做的。我不知道它是否正确,但它对我有效

      • 生成一组

      • 将公钥添加到要克隆的Github repo

      • 在我的默认配方中创建一个包含公钥和私钥的模板。见下文

      • 为发布密钥和私钥创建了相关模板

      • 创建了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\u ssh\u deploy\u wrapper.erb

      (请确保在此处使用私钥,否则它将失败)

      最后是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路径

      我从中获得了很多灵感


      祝你好运,我希望这对某人有所帮助。

      你的问题没有指向
      deploy\u资源的链接,因此我不能确定这是否适用,但如果它在下面使用
      git
      资源,以下内容可能会有所帮助

      如中所述,通过将SSH命令添加为存储库URL的“外部传输”部分,可以避免为每个SSH密钥创建额外的脚本文件:

      git "/path/to/destination" do
        repository "ext::ssh -i /path/to/.ssh/deployment_key -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no git@github.com %S /my_name/some_repo.git"
        branch "master"
        ...
      end
      

      该解决方案的唯一问题是,您在回购协议中存储的是未加密的私钥。最好使用加密数据包来存储私钥,然后在ruby_块中打开文件,写入文件(加密数据包中的值),关闭文件。效果相同,但存储库中没有私钥。看看这个:当我写这篇文章的时候,这是一个有效的观点和标志。我当时不知道有这样的事情
      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
      
      git "/path/to/destination" do
        repository "ext::ssh -i /path/to/.ssh/deployment_key -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no git@github.com %S /my_name/some_repo.git"
        branch "master"
        ...
      end