diff --git a/lib/capistrano/configuration/server.rb b/lib/capistrano/configuration/server.rb index d49c6485..bec214a1 100644 --- a/lib/capistrano/configuration/server.rb +++ b/lib/capistrano/configuration/server.rb @@ -35,6 +35,12 @@ module Capistrano @properties ||= Properties.new end + def netssh_options_with_options + @netssh_options ||= netssh_options_without_options.merge( fetch(:ssh_options) || {} ) + end + alias_method :netssh_options_without_options, :netssh_options + alias_method :netssh_options, :netssh_options_with_options + class Properties def initialize diff --git a/lib/capistrano/templates/stage.rb.erb b/lib/capistrano/templates/stage.rb.erb index 8aee667c..f4a1bbe6 100644 --- a/lib/capistrano/templates/stage.rb.erb +++ b/lib/capistrano/templates/stage.rb.erb @@ -17,4 +17,18 @@ role :db, %w{deploy@example.com} # extended properties on the server. server 'example.com', user: 'deploy', roles: %w{web app}, my_property: :my_value +# you can set custom ssh options +# it's possible to pass any option but you need to keep in mind that net/ssh understand limited list of options +# you can see them in [net/ssh documentation](http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start) +# server 'example.com', +# user: 'user_name', +# roles: %w{web app}, +# ssh_options: { +# user: 'user_name', # overrides user setting above +# keys: %w(/home/user_name/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(publickey password) +# # password: 'please use keys' +# } + # set :rails_env, :<%= stage %> diff --git a/spec/lib/capistrano/configuration/server_spec.rb b/spec/lib/capistrano/configuration/server_spec.rb index 80c62a3d..d595f112 100644 --- a/spec/lib/capistrano/configuration/server_spec.rb +++ b/spec/lib/capistrano/configuration/server_spec.rb @@ -134,6 +134,51 @@ module Capistrano end end + describe 'assign ssh_options' do + let(:server) { Server.new('user_name@hostname') } + + # before do + # server + # end + + context 'defaults' do + it 'forward agent' do + expect(server.netssh_options[:forward_agent]).to eq true + end + it 'contains user' do + expect(server.netssh_options[:user]).to eq 'user_name' + end + end + + context 'custom' do + let(:properties) do + { ssh_options: { + user: 'another_user', + keys: %w(/home/another_user/.ssh/id_rsa), + forward_agent: false, + auth_methods: %w(publickey password) } } + end + + before do + server.with(properties) + end + + it 'not forward agent' do + expect(server.netssh_options[:forward_agent]).to eq false + end + it 'contains correct user' do + expect(server.netssh_options[:user]).to eq 'another_user' + end + it 'contains keys' do + expect(server.netssh_options[:keys]).to eq %w(/home/another_user/.ssh/id_rsa) + end + it 'contains auth_methods' do + expect(server.netssh_options[:auth_methods]).to eq %w(publickey password) + end + end + + end + end end end