AWS OpsWorks - 无法使用 AWS OpsWorks 部署我的应用程序,尤其是无法成功迁移

Posted

技术标签:

【中文标题】AWS OpsWorks - 无法使用 AWS OpsWorks 部署我的应用程序,尤其是无法成功迁移【英文标题】:AWS OpsWorks - Cannot deploy my application with AWS OpsWorks, especially cannot succeed in migration 【发布时间】:2015-10-16 14:02:37 【问题描述】:

按照此文档 (http://railsconf2015-aws.s3-website-us-east-1.amazonaws.com/),我正在尝试使用 AWS OpsWorks 部署我的 rails 应用程序。但是,我没有这样做,尤其是当我尝试迁移它时。

这是错误日志。

Your bundle is complete!
Gems in the groups test and development were not installed.
It was installed into /home/deploy/.bundler/sample_app

[2015-07-26T08:21:32+00:00] INFO: No database adapter specified for sample_app, guessing
[2015-07-26T08:21:32+00:00] INFO: Looks like sample_app uses mysql2 in its Gemfile
[2015-07-26T08:21:32+00:00] INFO: Processing template[/srv/www/sample_app/shared/config/database.yml] action create (/var/lib/aws/opsworks/cache.stage2/cookbooks/deploy/definitions/opsworks_deploy.rb line 115)
[2015-07-26T08:21:32+00:00] INFO: deploy[/srv/www/sample_app] queueing checkdeploy hook /srv/www/sample_app/releases/20150726082129/deploy/before_migrate.rb
[2015-07-26T08:21:32+00:00] INFO: Processing template[/srv/www/sample_app/shared/config/database.yml] action create (/var/lib/aws/opsworks/cache.stage2/cookbooks/deploy/definitions/opsworks_deploy.rb line 115)
[2015-07-26T08:21:32+00:00] INFO: deploy[/srv/www/sample_app] made pre-migration symlinks
[2015-07-26T08:21:32+00:00] INFO: deploy[/srv/www/sample_app] set user to deploy
[2015-07-26T08:21:32+00:00] INFO: deploy[/srv/www/sample_app] set group to nginx
[2015-07-26T08:21:32+00:00] INFO: deploy[/srv/www/sample_app] migrating deploy

================================================================================
Error executing action `deploy` on resource 'deploy[/srv/www/sample_app]'
================================================================================


Chef::Exceptions::Exec
----------------------
if [ -f Gemfile ]; then echo 'OpsWorks: Gemfile found - running migration with bundle exec' && /usr/local/bin/bundle exec /usr/local/bin/rake db:migrate; else echo 'OpsWorks: no Gemfile - running plain migrations' && /usr/local/bin/rake db:migrate; fi returned 1, expected 0


Cookbook Trace:
---------------
/var/lib/aws/opsworks/cache.stage2/cookbooks/opsworks_commons/libraries/monkey_patch_deploy_provider.rb:45:in `block in migrate'
/var/lib/aws/opsworks/cache.stage2/cookbooks/opsworks_commons/libraries/monkey_patch_deploy_provider.rb:39:in `migrate'


Resource Declaration:
---------------------
# In /var/lib/aws/opsworks/cache.stage2/cookbooks/deploy/definitions/opsworks_deploy.rb

65:     deploy deploy[:deploy_to] do
66:       provider Chef::Provider::Deploy.const_get(deploy[:chef_provider])
67:       keep_releases deploy[:keep_releases]
68:       repository deploy[:scm][:repository]
69:       user deploy[:user]
70:       group deploy[:group]
71:       revision deploy[:scm][:revision]
72:       migrate deploy[:migrate]
73:       migration_command deploy[:migrate_command]
74:       environment deploy[:environment].to_hash
75:       purge_before_symlink(deploy[:purge_before_symlink]) unless deploy[:purge_before_symlink].nil?
76:       create_dirs_before_symlink(deploy[:create_dirs_before_symlink])
77:       symlink_before_migrate(deploy[:symlink_before_migrate])
78:       symlinks(deploy[:symlinks]) unless deploy[:symlinks].nil?
79:       action deploy[:action]
80: 
81:       if deploy[:application_type] == 'rails' && node[:opsworks][:instance][:layers].include?('rails-app')
82:         restart_command "sleep #deploy[:sleep_before_restart] && #node[:opsworks][:rails_stack][:restart_command]"
83:       end
84: 



Compiled Resource:
------------------
# Declared in /var/lib/aws/opsworks/cache.stage2/cookbooks/deploy/definitions/opsworks_deploy.rb:65:in `block in from_file'

deploy("/srv/www/sample_app") do
params :deploy_data=>"deploy_to"=>"/srv/www/sample_app", "chef_provider"=>"Timestamped", "keep_releases"=>5, "current_path"=>"/srv/www/sample_app/current", "document_root"=>"public", "ignore_bundler_groups"=>["test", "development"], "absolute_document_root"=>"/srv/www/sample_app/current/public/", "rake"=>"/usr/local/bin/rake", "migrate"=>true, "migrate_command"=>"if [ -f Gemfile ]; then echo 'OpsWorks: Gemfile found - running migration with bundle exec' && /usr/local/bin/bundle exec /usr/local/bin/rake db:migrate; else echo 'OpsWorks: no Gemfile - running plain migrations' && /usr/local/bin/rake db:migrate; fi", "rails_env"=>"production", "action"=>"deploy", "user"=>"deploy", "group"=>"nginx", "shell"=>"/bin/bash", "home"=>"/home/deploy", "sleep_before_restart"=>0, "stack"=>"needs_reload"=>true, "enable_submodules"=>true, "shallow_clone"=>false, "delete_cached_copy"=>true, "purge_before_symlink"=>["log", "tmp/pids", "public/system"], "create_dirs_before_symlink"=>["tmp", "public", "config"], "symlink_before_migrate"=>"config/database.yml"=>"config/database.yml", "config/memcached.yml"=>"config/memcached.yml", "symlinks"=>"system"=>"public/system", "pids"=>"tmp/pids", "log"=>"log", "environment"=>"RAILS_ENV"=>"production", "RUBYOPT"=>"", "RACK_ENV"=>"production", "HOME"=>"/home/deploy", "SECRET_KEY_BASE"=>"42426faeafcbeafc1ac73a23d0844b7e3f4a27d8cc5a15b8c9ea9e092bab7f0d2f87c5102411aac919a3425bbe4560fb686e22525871236b9322c4b5573bf4da", "environment_variables"=>"SECRET_KEY_BASE"=>"42426faeafcbeafc1ac73a23d0844b7e3f4a27d8cc5a15b8c9ea9e092bab7f0d2f87c5102411aac919a3425bbe4560fb686e22525871236b9322c4b5573bf4da", "ssl_support"=>false, "auto_npm_install_on_deploy"=>true, "nodejs"=>"restart_command"=>"monit restart node_web_app_sample_app", "stop_command"=>"monit stop node_web_app_sample_app", "port"=>80, "application"=>"sample_app", "application_type"=>"rails", "auto_bundle_on_deploy"=>true, "deploying_user"=>"arn:aws:iam::934616152548:root", "domains"=>["sample_app"], "mounted_at"=>nil, "restart_command"=>nil, "ssl_certificate"=>nil, "ssl_certificate_key"=>nil, "ssl_certificate_ca"=>nil, "scm"=>"scm_type"=>"git", "repository"=>"https://github.com/takuwan0405/sample_app.git", "revision"=>nil, "ssh_key"=>nil, "user"=>nil, "password"=>nil, "database"=>"host"=>"172.31.10.7", "database"=>"sample_app", "port"=>3306, "username"=>"root", "password"=>"k7gs5opqfr", "reconnect"=>true, "data_source_provider"=>"stack", "type"=>"mysql", "memcached"=>"host"=>nil, "port"=>11211, :app=>"sample_app", :name=>nil
provider Chef::Provider::Deploy::Timestamped
action [:deploy]
updated true
updated_by_last_action true
retries 0
retry_delay 2
deploy_to "/srv/www/sample_app"
environment "RAILS_ENV"=>"production", "RUBYOPT"=>"", "RACK_ENV"=>"production", "HOME"=>"/home/deploy", "SECRET_KEY_BASE"=>"42426faeafcbeafc1ac73a23d0844b7e3f4a27d8cc5a15b8c9ea9e092bab7f0d2f87c5102411aac919a3425bbe4560fb686e22525871236b9322c4b5573bf4da", "LC_ALL"=>"C"
repository_cache "cached-copy"
purge_before_symlink ["log", "tmp/pids", "public/system"]
create_dirs_before_symlink ["tmp", "public", "config"]
symlink_before_migrate "config/database.yml"=>"config/database.yml", "config/memcached.yml"=>"config/memcached.yml"
symlinks "system"=>"public/system", "pids"=>"tmp/pids", "log"=>"log"
revision "HEAD"
migrate true
remote "origin"
enable_submodules true
scm_provider Chef::Provider::Git
keep_releases 5
enable_checkout true
checkout_branch "deploy"
cookbook_name "deploy"
recipe_name "rails"
repo "https://github.com/takuwan0405/sample_app.git"
user "deploy"
group "nginx"
migration_command "if [ -f Gemfile ]; then echo 'OpsWorks: Gemfile found - running migration with bundle exec' && /usr/local/bin/bundle exec /usr/local/bin/rake db:migrate; else echo 'OpsWorks: no Gemfile - running plain migrations' && /usr/local/bin/rake db:migrate; fi"
restart_command "sleep 0 && ../../shared/scripts/unicorn clean-restart"
before_migrate #<Proc:0x007f2d074dcbb0@/var/lib/aws/opsworks/cache.stage2/cookbooks/deploy/definitions/opsworks_deploy.rb:100>
shared_path "/srv/www/sample_app/shared"
destination "/srv/www/sample_app/shared/cached-copy"
current_path "/srv/www/sample_app/current"
end



[2015-07-26T08:21:33+00:00] INFO: Running queued delayed notifications before re-raising exception
[2015-07-26T08:21:33+00:00] ERROR: Running exception handlers
[2015-07-26T08:21:33+00:00] ERROR: Exception handlers complete
[2015-07-26T08:21:33+00:00] FATAL: Stacktrace dumped to /var/lib/aws/opsworks/cache.stage2/chef-stacktrace.out
[2015-07-26T08:21:33+00:00] ERROR: deploy[/srv/www/sample_app] (deploy::rails line 65) had an error: Chef::Exceptions::Exec: if [ -f Gemfile ]; then echo 'OpsWorks: Gemfile found - running migration with bundle exec' && /usr/local/bin/bundle exec /usr/local/bin/rake db:migrate; else echo 'OpsWorks: no Gemfile - running plain migrations' && /usr/local/bin/rake db:migrate; fi returned 1, expected 0
[2015-07-26T08:21:33+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

好像“捆绑安装”成功了。

使用 ssh,我去了 /srv/www/sample_app/releases/20150726082129 并输入

rake db:migrate

然后出现这样的错误句。

Could not find i18n-0.7.0 in any of the sources
Run `bundle install` to install missing gems.

接下来,按照这个错误语句,我输入了

sudo bundle install

然后它说

Your bundle is complete!
Gems in the groups test and development were not installed.
It was installed into /home/deploy/.bundler/sample_app

但是,gem 似乎是空的。如果我输入了

gem list

我找不到 Gemfile 中指定的宝石

bigdecimal (1.2.6)
bundler (1.5.3)
io-console (0.4.3)
json (1.8.1)
kgio (2.9.3)
minitest (5.4.3)
power_assert (0.2.2)
psych (2.0.8)
rack (1.6.4)
raindrops (0.15.0)
rake (10.4.2)
rdoc (4.2.0)
test-unit (3.0.8)
unicorn (4.7.0)

我认为这些是解决我的问题的提示。

对不起,我的英语不太好,所以如果你不明白我想让你帮助什么,请告诉我不明白的地方。

【问题讨论】:

【参考方案1】:

Opworks 很难通过他们的日志进行调试。这里有一些调试技巧。

您无法在您可能用于 ssh 的普通 ec2 用户帐户中执行此操作。执行此命令成为部署用户...

sudo su - deploy

这将允许您在 当前 位置访问您的应用...

cd /srv/www/sample_app/current

您现在还可以查看日志、运行 rake 等。特别是,现在您可以运行失败的命令...

if [ -f Gemfile ]; then echo 'OpsWorks: Gemfile found - running migration with bundle exec' && /usr/local/bin/bundle exec /usr/local/bin/rake db:migrate; else echo 'OpsWorks: no Gemfile - running plain migrations' && /usr/local/bin/rake db:migrate; fi

查看的地方是共享区域配置、日志,即...

/srv/www/sample_app/shared/config/unicorn.conf 

【讨论】:

【参考方案2】:

我经常使用 OpsWorks 部署 Rails 应用,这种部署错误很常见。

如果您在 OpsWorks 中部署 Ruby on Rails 应用程序时遇到了迁移错误,只需 SSH 到服务器并转到您的最后一个部署文件夹(例如,对于您的情况: /srv/www/sample_app/releases/20150726082129)。

请注意,“/srv/www/your app name/current”只会指向您上次从 OpsWorks 成功部署,如果部署失败,最好直接访问“/srv”下的最新部署文件夹/www/您的应用名称/releases/部署时间"

代替

rake db:migrate

你应该运行

sudo bundle exec rake db:migrate

然后您将看到更有意义的迁移错误,并且您应该知道该怎么做。

【讨论】:

非常感谢!我成功部署了我的应用程序。我应该通过 ssh 在最后一个部署文件夹中完成“rake db:migrate RAILS_ENV=production”!【参考方案3】:

我发现了问题,至少它对我有用。

迁移失败的原因可能取决于多种原因,OpsWorks 日志无法帮助我们。 因此,正如你们中的许多人所建议的那样,要准确了解发生了什么,我们必须通过 SSH 登录到我们的一个实例并尝试运行导致失败的命令。

通过 ssh 登录,然后: su - deploy $ cd /srv/www/mynextapp/releases/20150401052640 $ /usr/local/bin/bundle exec /usr/local/bin/rake db:migrate

现在这个命令应该会失败,告诉我们原因。

@n1t1nv3rma 收到此错误:

ExecJS::RuntimeUnavailable: Could not find a javascript runtime

他修复了它in this way.

对我来说,错误是: /usr/lib/postgresql/9.5/bin/pg_dump: invalid option -- 'i' Try "pg_dump --help" for more information. rake aborted! Error dumping database

我修复它只是更新到 Rails 4.2.6(之前是 4.2.3 - 有一个删除 --i 选项的提交)。

查看这里了解更多详情:https://github.com/aws/opsworks-cookbooks/issues/258

希望对你有帮助。

【讨论】:

以上是关于AWS OpsWorks - 无法使用 AWS OpsWorks 部署我的应用程序,尤其是无法成功迁移的主要内容,如果未能解决你的问题,请参考以下文章

无法在 AWS Opsworks 上使用 Chef 12 查找 Chef 社区食谱

实例的 AWS OpsWorks setup_failed - 无法部署_branch

AWS opsworks 上的 chef 12 脚本无法安装 jenkins

每当 aws opsworks 上的 gem

AWS Opsworks 未能添加 EC2 实例

是否可以在 AWS::OpsWorks::Instance 资源中使用 AWS::CloudFormation::Init 和元数据?