rhe*_*nik 24 ruby ruby-on-rails amazon-web-services amazon-elastic-beanstalk
通过最近使用Git设置用于Ruby部署的AWS Elastic Beanstalk的教程,我只是从我的CI服务器设置了一个Elastic Beanstalk环境.但是,应用程序无法启动.我浏览了日志,发现bundle install
失败并显示错误消息.
获取git@github.com:example/private-repository.git主机密钥验证失败.致命:远程端意外挂断[31mGit错误:
git clone 'git@github.com:example/private-repository.git' "/var/app/ondeck/vendor/cache/ruby/1.9.1/cache/bundler/git/private-repository-e4bbe6c2b13bb62664e39e345c1b01d80017934c" --bare --no-hardlinks
目录/ var/app/ondeck中的命令失败.[0m
Gemfile
我的Rails应用程序包含对Github上我拥有的几个私有存储库上托管的gemified插件的引用.就像是
gem'partgemname',: git =>'git@github.com:example/private-repository.git'
我遇到过与Capistrano部署类似的问题,这些问题通过设置解决了ssh_options[:forward_agent] = true
.
AWS Elastic Beanstalk Ruby容器通过.config
放置在其下的自定义文件支持自定义配置.ebextensions
.在这种情况下,设置SSH转发代理会有所帮助吗?在启动Elastic Beanstalk环境时,还有其他替代方法可以访问私有Github存储库吗?
更新1:我刚刚检查了bundle install
启动了a的用户.发现脚本以用户身份/opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh
启动.我尝试在其下创建一个SSH密钥,并将其pub-key添加到该存储库的Github Deploy密钥.到目前为止没有运气.现在尝试在Github上为我的用户帐户添加SSH pub-key,以便它适用于通过我的Github帐户可访问的所有私有存储库.bundle install
root
/root/.ssh
mar*_*say 34
经过一天的努力,我终于通过使用.config
文件启用了我的组织的私有GitHub repos和Elastic Beanstalk .我正在使用Python pip
,但它也适用于EB上的其他软件包安装程序.
rhetonik的ssh-agent
+ ssh-add
方法对我来说根本不起作用,所以我选择设置一个ssh配置文件.
这是我的.ebextensions/3-pip-install-from-github.config
档案:
files:
"/root/.ssh/config":
owner: root
group: root
mode: "000600"
content: |
Host github.com
User git
Hostname github.com
IdentityFile /root/.ssh/github
"/root/.ssh/known_hosts":
owner: root
group: root
mode: "000644"
content: |
#
# paste output of `ssh-keyscan -H github.com` here
#
commands:
01-command:
command: sudo aws s3 cp s3://bucket-with-your-github-ssh-key/github /root/.ssh
02-command:
command: sudo chmod 600 /root/.ssh/github
Run Code Online (Sandbox Code Playgroud)
粗略的说明:
设置EB实例可访问的S3存储桶.里面那个桶,存储SSH密钥允许访问要通过访问GitHub的仓库pip
,npm
,bundle
等使用sudo aws s3 cp
,以复制钥匙到您的EB实例上部署.sudo
是必要的,因为EB脚本使用root
而不是ec2-user
.
此ebextensions配置文件还会在EB实例上创建2个文件./root/.ssh/config
告诉ssh
(由pip
和调用git
)使用从S3复制的密钥.粘贴ssh-keyscan -H github.com
into 的输出/root/.ssh/known_hosts
将预先验证ssh
您的EB实例上是否实际与GitHub通信以避免MITM攻击.这比禁止更StrictHostKeyChecking
在/root/.ssh/config
.
这是我的requirements.txt
文件pip
:
Beaker==1.7.0
Flask==0.10.1
Jinja2==2.7.3
MarkupSafe==0.23
# [...]
git+ssh://git@github.com/myorganization/myprivaterepo.git@0.0.142
Run Code Online (Sandbox Code Playgroud)
在运行时eb-deploy
,您可以tail -f /var/log/eb-activity.log
确保一切顺利进行.
rhe*_*nik 10
这是我最终做到的.这就是为负责bundle install
阶段的用户设置SSH密钥.
root
用户
创建SSH密钥$ sudo su - root
$ ssh-keygen -t rsa -C"some-email@yourdomain.com"
编辑.bash_profile
以显式启动ssh-agent
并添加新生成的SSH密钥.添加以下行(这可能看起来没必要,我只是为了确定)
eval`ssh-agent
EVAL
ssh-add ~/.ssh/id_rsa
请注意SSH公钥Eg:~/.ssh/id_rsa.pub
并将其添加到可以访问私有存储库的Github帐户的SSH密钥集
此时,您的实例可以访问您的私有Github存储库.您可以通过git clone
以root
用户身份登录来发布这些存储库来测试此问题.
使用标准方法从此实例创建AMI
回到您的AWS Elastic Beanstalk仪表板,Edit Configuration
在您的应用程序环境中查找选项.在Server
选项卡中,查找允许您指定a的选项Custom AMI
.使用新创建的AMI ID更新此字段例如:ami-4324fd4
.
通过点击保存配置Apply Changes
.AWS Elastic Beanstalk将开始在您的环境中部署新实例并终止旧实例.这是为了确保所有自动扩展的实例都具有私有Github访问所需的白名单SSH密钥.
完成上述步骤后,您可以继续部署Rails应用程序 git aws.push
希望这有助于其他被困的人.我很高兴看到比这个更优雅的解决方案.