Skip to content

Commit 0dedaa6

Browse files
committed
Allow running local commands using on(:local)
This works as an alias for `run_locally`. The advantage of this feature is that Capistrano can be easily configured to perform actions on the local host or on a server, depending on user input, without having to replace all `on` calls with `run_locally`. For example (in Capistrano): if ENV['RUN_LOCALLY'] server :local, :roles => [:app] else server "example.com", :roles => [:app] end on roles(:app) do execute :any_command end
1 parent 08a213b commit 0dedaa6

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

EXAMPLES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,10 @@ Replace `on` with `run_locally`
344344
end
345345
end
346346

347+
You can achieve the same thing with `on(:local)`
348+
349+
on(:local) do
350+
within '/tmp' do
351+
execute :whoami
352+
end
353+
end

lib/sshkit/backends/local.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ module Backend
66
class Local < Printer
77

88
def initialize(&block)
9-
@host = Host.new(hostname: 'localhost') # just for logging
9+
@host = Host.new(:local) # just for logging
1010
@block = block
1111
end
1212

1313
def run
14-
instance_exec(&@block)
14+
instance_exec(@host, &@block)
1515
end
1616

1717
def test(*args)

lib/sshkit/host.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'ostruct'
2+
require 'etc'
23

34
module SSHKit
45

@@ -22,7 +23,11 @@ def keys
2223

2324
def initialize(host_string_or_options_hash)
2425

25-
unless host_string_or_options_hash.is_a?(Hash)
26+
if host_string_or_options_hash == :local
27+
@local = true
28+
@hostname = "localhost"
29+
@user = Etc.getpwuid.name
30+
elsif !host_string_or_options_hash.is_a?(Hash)
2631
suitable_parsers = [
2732
SimpleHostParser,
2833
HostWithPortParser,
@@ -51,6 +56,10 @@ def initialize(host_string_or_options_hash)
5156
end
5257
end
5358

59+
def local?
60+
@local
61+
end
62+
5463
def hash
5564
user.hash ^ hostname.hash ^ port.hash
5665
end

lib/sshkit/runners/abstract.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ def initialize(hosts, options = nil, &block)
1515
private
1616

1717
def backend(host, &block)
18-
SSHKit.config.backend.new(host, &block)
18+
if host.local?
19+
SSHKit::Backend::Local.new(&block)
20+
else
21+
SSHKit.config.backend.new(host, &block)
22+
end
1923
end
2024

2125
end

test/unit/test_host.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'helper'
2+
require 'etc'
23

34
module SSHKit
45

@@ -41,6 +42,14 @@ def test_host_with_username_and_port
4142
assert_equal 'example.com', h.hostname
4243
end
4344

45+
def test_host_local
46+
h = Host.new :local
47+
assert h.local?
48+
assert_nil h.port
49+
assert_equal Etc.getpwuid.name, h.username
50+
assert_equal 'localhost', h.hostname
51+
end
52+
4453
def test_does_not_confuse_ipv6_hosts_with_port_specification
4554
h = Host.new '[1fff:0:a88:85a3::ac1f]:8001'
4655
assert_equal 8001, h.port

0 commit comments

Comments
 (0)