【问题标题】:How to connect via defined ssh connection and run shell commands on server with capistrano?如何通过已定义的 ssh 连接进行连接并使用 capistrano 在服务器上运行 shell 命令?
【发布时间】:2014-01-02 10:06:30
【问题描述】:

我有我的 deploy.rb:

require 'net/ssh/proxy/command'

set :application, 'my app name'
set :repo_url,    'my github url'
set :scm,         :git
set :scm_verbose, true
set :use_sudo,    false
set :port,        ENV['SSH_PORT'] # will give the port number, for example: 3029

set :ssh_options, proxy: Net::SSH::Proxy::Command.new("ssh -i ~/.ssh/nh -p #{fetch(:port)} #{roles(:app)}") # I just wanna make use of my public key, for example: nh in .ssh directory to connect to server(s).

task :execute_on_server do
  on roles(:app) do
    puts "Running onto server from hello!"
    execute "#{sudo} cp ~/hello /home/username/hello"
  end
end

然后部署/production.rb:

set :stage, :production
set :branch,'production'
# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary
# server in each group is considered to be the first
# unless any hosts have the primary property set.
set :production_app_ip, "#{ENV['CAP_PRODUCTION_USER']}@#{ENV['CAP_PRODUCTION_IP']}"
set :production_web_ip, "#{ENV['CAP_PRODUCTION_USER']}@#{ENV['CAP_PRODUCTION_IP']}"
set :production_db_ip, "#{ENV['PRODUCTION_USERNAME']}@#{ENV['PRODUCTION_HOST']}"

role :app, [fetch(:production_app_ip)]
role :web, [fetch(:production_web_ip)]
role :db,  [fetch(:production_db_ip)]

# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a hash can be used to set
# extended properties on the server.
server ENV['CAP_PRODUCTION_IP'], user: ENV['CAP_PRODUCTION_USER'], roles: %w{web app}, my_property: :my_value

当我运行:$ cap production execute_on_server 时,它会抛出此错误:

[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Running onto server from hello!
 INFO [6d724595] Running /usr/bin/env sudo on 198.343.661.910
DEBUG [6d724595] Command: /usr/bin/env sudo
Pseudo-terminal will not be allocated because stdin is not a terminal.
ssh: Could not resolve hostname []: Name or service not known
cap aborted!
connection closed by remote host
/home/surya/.rvm/gems/jruby-1.7.6/gems/net-ssh-2.7.0/lib/net/ssh/transport/server_version.rb:50:in `negotiate!'
org/jruby/RubyKernel.java:1517:in `loop'
/home/surya/.rvm/gems/jruby-1.7.6/gems/net-ssh-2.7.0/lib/net/ssh/transport/server_version.rb:45:in `negotiate!'
org/jruby/RubyKernel.java:1517:in `loop'
/home/surya/.rvm/gems/jruby-1.7.6/gems/net-ssh-2.7.0/lib/net/ssh/transport/server_version.rb:43:in `negotiate!'
/home/surya/.rvm/gems/jruby-1.7.6/gems/net-ssh-2.7.0/lib/net/ssh/transport/server_version.rb:32:in `initialize'
/home/surya/.rvm/gems/jruby-1.7.6/gems/net-ssh-2.7.0/lib/net/ssh/transport/session.rb:78:in `initialize'
org/jruby/ext/timeout/Timeout.java:105:in `timeout'
/home/surya/.rvm/gems/jruby-1.7.6/gems/net-ssh-2.7.0/lib/net/ssh/transport/session.rb:78:in `initialize'
/home/surya/.rvm/gems/jruby-1.7.6/gems/net-ssh-2.7.0/lib/net/ssh.rb:200:in `start'
/home/surya/.rvm/gems/jruby-1.7.6/gems/sshkit-1.3.0/lib/sshkit/backends/connection_pool.rb:25:in `create_or_reuse_connection'
/home/surya/.rvm/gems/jruby-1.7.6/gems/sshkit-1.3.0/lib/sshkit/backends/netssh.rb:173:in `ssh'
/home/surya/.rvm/gems/jruby-1.7.6/gems/sshkit-1.3.0/lib/sshkit/backends/netssh.rb:126:in `_execute'
org/jruby/RubyKernel.java:1889:in `tap'
/home/surya/.rvm/gems/jruby-1.7.6/gems/sshkit-1.3.0/lib/sshkit/backends/netssh.rb:123:in `_execute'
/home/surya/.rvm/gems/jruby-1.7.6/gems/sshkit-1.3.0/lib/sshkit/backends/netssh.rb:66:in `execute'
/home/surya/.rvm/gems/jruby-1.7.6/gems/capistrano-3.0.1/lib/capistrano/dsl.rb:26:in `sudo'
/home/surya/Desktop/surya/work/codes/projects/job/resolutions/config/deploy.rb:56:in `(root)'
org/jruby/RubyBasicObject.java:1565:in `instance_exec'
/home/surya/.rvm/gems/jruby-1.7.6/gems/sshkit-1.3.0/lib/sshkit/backends/netssh.rb:54:in `run'
/home/surya/.rvm/gems/jruby-1.7.6/gems/sshkit-1.3.0/lib/sshkit/runners/parallel.rb:12:in `execute'
Tasks: TOP => hello
(See full trace by running task with --trace)

请注意,我使用的是 Ruby:jruby 1.7.6 (1.9.3p392) 2013-10-22 6004147 on OpenJDK 64-Bit Server VM 1.6.0_27-b27 [linux-amd64],capistrano 版本:3.0.1,rails 版本:4.0.2

【问题讨论】:

  • "ssh: 无法解析主机名 []: 名称或服务未知"
  • 是的,这正是我想不通的。从那以后,我给了一个正确的主机名。
  • 不,你没有!它是空白或[],您作为主机名提供!
  • 我发现了这个问题,我不得不提到我定义服务器的 ssh 端口并且它工作正常。谢谢你的时间,@phoet。

标签: ruby-on-rails ssh ruby-on-rails-3.2 ruby-on-rails-4 capistrano3


【解决方案1】:

问题出在我的 deploy/production.rb 中,我在定义服务器时不得不提及 ssh 端口。因此,这将如下所示:

set :stage,  :production
set :branch, 'production'
set :user,   ENV['CAP_PRODUCTION_USER']
# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary
# server in each group is considered to be the first
# unless any hosts have the primary property set.
set :production_app_ip, "#{ENV['CAP_PRODUCTION_USER']}@#{ENV['CAP_PRODUCTION_IP']}"
set :production_web_ip, "#{ENV['CAP_PRODUCTION_USER']}@#{ENV['CAP_PRODUCTION_IP']}"
set :production_db_ip, "#{ENV['PRODUCTION_USERNAME']}@#{ENV['PRODUCTION_HOST']}"

role :app, [fetch(:production_app_ip)]
role :web, [fetch(:production_web_ip)]
role :db,  [fetch(:production_db_ip)]

# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a hash can be used to set
# extended properties on the server.
server ENV['CAP_PRODUCTION_IP'], user: ENV['CAP_PRODUCTION_USER'], roles: %w{web app}, port: ENV['SSH_PORT'], primary: true

【讨论】:

    猜你喜欢
    • 2019-01-21
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    相关资源
    最近更新 更多