繁体   English   中英

无法在vps上运行rake或rails控制台

[英]can't run rake or rails console on vps

感谢Ryan Bates在Railscasts上将其部署到VPS情节中,因此我在VPS上启动了演示Rails应用程序并在VPS上运行。 在我的服务器上,我创建了一个目录结构,例如/home/username/apps然后将应用程序部署到了/home/username/apps/appname. 在app name文件夹中,还有Ryan的脚本创建的另外三个文件夹(我认为是)

current  releases  shared

current文件夹内是通常的Rails目录。 如果我进入current文件夹并运行rails crake db:seed收到错误消息

 configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)

从共享目录触发

 apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/connection_adapters

我发现这是一个不寻常的错误,因为演示应用程序(正在搭建)正在保存新创建的条目并列出它们,所以我假设我尝试运行rake和rails控制台命令的方式有问题,而不是我对适配器的声明有问题。

你能告诉我可能是什么吗?

注意,已经有一个问题, 数据库配置未指定用于处理该一般主题的适配器 ,但是,该问题的答案似乎是解决了数据库根本无法工作的情况,但是,在我看来,这并不是案子。 因此,我将这个问题与该线程区分开。

这是我的database.yml文件

production:
  adapter:  postgresql
  encoding:  unicode
  database:  dodeploy_production
  pool:  5
  host:  localhost
  username:  michael
  password:  secretpassword

这是Ryan用来部署的脚本。 我之所以加入它,是因为其中可能包含一些信息,可以帮助您了解我所缺少的东西。

require "bundler/capistrano"

server "198XXXX", :web, :app, :db, primary: true

set :application, "dodeploy"
set :user, "michael"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false

set :scm, "git"
set :repository, "git@github.com:Username/appname.git"
set :branch, "master"

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

after "deploy", "deploy:cleanup" # keep only the last 5 releases

namespace :deploy do
  %w[start stop restart].each do |command|
    desc "#{command} unicorn server"
    task command, roles: :app, except: {no_release: true} do
      run "/etc/init.d/unicorn_#{application} #{command}"
    end
  end

  task :setup_config, roles: :app do
    sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
    sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
    run "mkdir -p #{shared_path}/config"
    put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
    puts "Now edit the config files in #{shared_path}."
  end
  after "deploy:setup", "deploy:setup_config"

  task :symlink_config, roles: :app do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  end
  after "deploy:finalize_update", "deploy:symlink_config"

  desc "Make sure local git is in sync with remote."
  task :check_revision, roles: :web do
    unless `git rev-parse HEAD` == `git rev-parse origin/master`
      puts "WARNING: HEAD is not the same as origin/master"
      puts "Run `git push` to sync changes."
      exit
    end
  end
  before "deploy", "deploy:check_revision"
end

错误

当我运行rails c (或rake db:seed )时,我得到了

home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/connection_adapters/connection_specification.rb:52:in `resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/connection_adapters/connection_specification.rb:46:in `resolve_string_connection'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/connection_adapters/connection_specification.rb:30:in `spec'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/connection_handling.rb:39:in `establish_connection'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/railtie.rb:170:in `block (2 levels) in <class:Railtie>'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/lazy_load_hooks.rb:44:in `each'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/base.rb:322:in `<module:ActiveRecord>'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/base.rb:22:in `<top (required)>'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/dependencies.rb:228:in `require'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/dependencies.rb:228:in `block in require'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/dependencies.rb:213:in `load_dependency'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activesupport-4.0.0.rc2/lib/active_support/dependencies.rb:228:in `require'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/activerecord-4.0.0.rc2/lib/active_record/railtie.rb:61:in `block in <class:Railtie>'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/railtie.rb:188:in `call'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/railtie.rb:188:in `block in run_console_blocks'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/railtie.rb:188:in `each'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/railtie.rb:188:in `run_console_blocks'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/application.rb:264:in `block in run_console_blocks'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/engine/railties.rb:17:in `each'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/engine/railties.rb:17:in `each'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/application.rb:264:in `run_console_blocks'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/engine.rb:431:in `load_console'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/commands/console.rb:51:in `initialize'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/commands/console.rb:9:in `new'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/commands/console.rb:9:in `start'
    from /home/michael/apps/dodeploy/shared/bundle/ruby/1.9.1/gems/railties-4.0.0.rc2/lib/rails/commands.rb:66:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

更新

我不确定为什么Ryan在Railscast中创建了database.example.yml。 他没有配置它,在添加用户名和密码之前,我只是将其保留为原始database.yml文件的副本。

database.example.yml

development:
  adapter: postgresql
  encoding: unicode
  database: dodeploy_development
  pool: 5
  password:
test:
  adapter: postgresql
  encoding: unicode
  database: dodeploy_test
  pool: 5
  password:

production:
  adapter: postgresql
  encoding: unicode
  database: dodeploy_production
  pool: 5
  password:

这是nginx.conf文件

upstream unicorn {
  server unix:/tmp/unicorn.dodeploy.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  # server_name example.com;
  root /home/michael/apps/dodeploy/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

更新。

这是来自connection_specification.rb的方法

     def resolve_hash_connection(spec) # :nodoc:
          spec = spec.symbolize_keys

          raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)

          path_to_adapter = "active_record/connection_adapters/#{spec[:adapter]}_adapter"
          begin
            require path_to_adapter
          rescue Gem::LoadError => e
            raise Gem::LoadError, "Specified '#{spec[:adapter]}' for database adapter, but the gem is not loaded. Add `$
          rescue LoadError => e
 raise LoadError, "Could not load '#{path_to_adapter}'. Make sure that the adapter in config/database.yml is$
          end

          adapter_method = "#{spec[:adapter]}_connection"

          ConnectionSpecification.new(spec, adapter_method)
        end

我在运行命令时通过指定生产环境来使其正常工作

RAILS_ENV=production rails console

RAILS_ENV=production rake db:seed

我发现这很不正常,因为该应用程序正在运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM