sshkit-interactive 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2908a10617b2c1d702b525f2540b89aaf9e6d6d3
4
+ data.tar.gz: 97199152d548f65b98d6658044e56f52f7ffc4ff
5
+ SHA512:
6
+ metadata.gz: cb3dbb43f59ee5a5b41fce160adb41a77754265ca938c9fd5960a1f1082f86e0a2ea6d1bb99568cfcdba4cf4571a45b3e87b01e1352349e2a6225ee37c383802
7
+ data.tar.gz: 4590d048ab511003cb780828173e4a79d13b501c65f66099897f914ae9f9a2e3112353b145c4c8b7f7ebaaf67dce2e693792baa6e74554a5810453f59247960b
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sshkit-interactive.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Aidan Feldman
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # SSHKit::Interactive
2
+
3
+ An [SSHKit](https://github.com/capistrano/sshkit) [backend](https://github.com/capistrano/sshkit/tree/master/test/unit/backends) that allows you to execute interactive commands on your servers. Remote commands that you might want to use this for:
4
+
5
+ * A Rails console
6
+ * A text editor
7
+ * `less`
8
+ * *etc.*
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'sshkit-interactive'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ If you're using [Capistrano](http://capistranorb.com/), add the following to your Capfile:
23
+
24
+ ```ruby
25
+ require 'sshkit/interactive'
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ From SSHKit, use the [interactive backend](lib/sshkit/interactive/backend.rb) (which makes a system call to `ssh` under the hood), then execute commands as normal.
31
+
32
+ ```ruby
33
+ SSHKit.config.backend = SSHKit::Interactive::Backend
34
+ hosts = %w{my.server.com}
35
+ on hosts do |host|
36
+ execute(:vim)
37
+ end
38
+ ```
39
+
40
+ Note that you will probably only want to execute on a single host. In Capistrano, it might look something like this:
41
+
42
+ ```ruby
43
+ namespace :rails do
44
+ desc "Run Rails console"
45
+ task :console do
46
+ SSHKit.config.backend = SSHKit::Interactive::Backend
47
+ on primary(:app) do |host|
48
+ execute(:rails, :console)
49
+ end
50
+ end
51
+ end
52
+ ```
53
+
54
+ ## Contributing
55
+
56
+ 1. [Fork it](https://github.com/afeld/sshkit-interactive/fork)
57
+ 1. Clone it
58
+ 1. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 1. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 1. Push to the branch (`git push origin my-new-feature`)
61
+ 1. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,11 @@
1
+ require 'sshkit'
2
+
3
+ require_relative 'interactive/version'
4
+ require_relative 'interactive/command'
5
+ require_relative 'interactive/backend'
6
+
7
+ module SSHKit
8
+ module Interactive
9
+ # Your code goes here...
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ # based on https://github.com/jetthoughts/j-cap-recipes/blob/be9dffe279b7bee816c9bafcb3633109096b20d5/lib/sshkit/backends/ssh_command.rb
2
+ module SSHKit
3
+ module Interactive
4
+ class Backend < SSHKit::Backend::Printer
5
+ def run
6
+ instance_exec(host, &@block)
7
+ end
8
+
9
+ def within(directory, &block)
10
+ (@pwd ||= []).push directory.to_s
11
+ yield
12
+ ensure
13
+ @pwd.pop
14
+ end
15
+
16
+ def execute(*args, &block)
17
+ remote_command = command(*args)
18
+ output << remote_command
19
+ Command.new(host, remote_command).execute
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,69 @@
1
+ module SSHKit
2
+ module Interactive
3
+ class Command
4
+ attr_reader :host, :remote_command
5
+
6
+ # remote_command can be an SSHKit::Command or a String
7
+ def initialize(host, remote_command=nil)
8
+ @host = host
9
+ @remote_command = remote_command
10
+ end
11
+
12
+ def netssh_options
13
+ self.host.netssh_options
14
+ end
15
+
16
+ def user
17
+ self.host.user
18
+ end
19
+
20
+ def hostname
21
+ self.host.hostname
22
+ end
23
+
24
+ def options
25
+ opts = []
26
+ opts << '-A' if netssh_options[:forward_agent]
27
+ if netssh_options[:keys]
28
+ netssh_options[:keys].each do |k|
29
+ opts << "-i #{k}"
30
+ end
31
+ end
32
+ opts << "-l #{user}" if user
33
+ opts << %{-o "PreferredAuthentications #{netssh_options[:auth_methods].join(',')}"} if netssh_options[:auth_methods]
34
+ opts << %{-o "ProxyCommand #{netssh_options[:proxy].command_line_template}"} if netssh_options[:proxy]
35
+ opts << "-p #{netssh_options[:port]}" if netssh_options[:port]
36
+ opts << '-t' if self.remote_command
37
+
38
+ opts
39
+ end
40
+
41
+ def options_str
42
+ self.options.join(' ')
43
+ end
44
+
45
+ def remote_command_str
46
+ if self.remote_command
47
+ %{"#{self.remote_command}"}
48
+ else
49
+ ''
50
+ end
51
+ end
52
+
53
+ def to_s
54
+ parts = [
55
+ 'ssh',
56
+ self.options_str,
57
+ self.hostname,
58
+ self.remote_command_str
59
+ ]
60
+
61
+ parts.reject(&:empty?).join(' ')
62
+ end
63
+
64
+ def execute
65
+ system(self.to_s)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,5 @@
1
+ module SSHKit
2
+ module Interactive
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ describe SSHKit::Interactive::Backend do
2
+ describe '#execute' do
3
+ it "does a system call with the SSH command" do
4
+ host = SSHKit::Host.new('example.com')
5
+ backend = SSHKit::Interactive::Backend.new(host)
6
+ expect_any_instance_of(SSHKit::Interactive::Command).to receive(:system).with('ssh -A -t example.com "/usr/bin/env ls"')
7
+ backend.execute('ls')
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,72 @@
1
+ describe SSHKit::Interactive::Command do
2
+ describe '#options_str' do
3
+ def command_options_str(host)
4
+ command = SSHKit::Interactive::Command.new(host)
5
+ command.options_str
6
+ end
7
+
8
+ it "handles a simple hostname" do
9
+ host = SSHKit::Host.new('example.com')
10
+ expect(command_options_str(host)).to eq('-A')
11
+ end
12
+
13
+ it "handles a username and port" do
14
+ host = SSHKit::Host.new('someuser@example.com:2222')
15
+ expect(command_options_str(host)).to eq('-A -l someuser -p 2222')
16
+ end
17
+
18
+ it "handles a proxy" do
19
+ host = SSHKit::Host.new('someuser@example.com:2222')
20
+ host.ssh_options = {
21
+ proxy: Net::SSH::Proxy::Command.new('ssh mygateway.com -W %h:%p')
22
+ }
23
+
24
+ expect(command_options_str(host)).to eq('-A -l someuser -o "ProxyCommand ssh mygateway.com -W %h:%p" -p 2222')
25
+ end
26
+
27
+ it "handles keys option" do
28
+ host = SSHKit::Host.new('example.com')
29
+ host.ssh_options = { keys: %w(/home/user/.ssh/id_rsa) }
30
+
31
+ expect(command_options_str(host)).to eq('-A -i /home/user/.ssh/id_rsa')
32
+ end
33
+
34
+ # TODO split into separate tests
35
+ it "handles extra options" do
36
+ host = SSHKit::Host.new('someuser@example.com:2222')
37
+ host.keys = ["~/.ssh/some_key_here"]
38
+ host.ssh_options = {
39
+ port: 3232,
40
+ keys: %w(/home/user/.ssh/id_rsa),
41
+ forward_agent: false,
42
+ auth_methods: %w(publickey password)
43
+ }
44
+
45
+ expect(command_options_str(host)).to eq('-i /home/user/.ssh/id_rsa -l someuser -o "PreferredAuthentications publickey,password" -p 3232')
46
+ end
47
+
48
+ it "handles a password"
49
+ end
50
+
51
+ describe '#to_s' do
52
+ it "includes options" do
53
+ host = SSHKit::Host.new('example.com')
54
+ command = SSHKit::Interactive::Command.new(host)
55
+ expect(command).to receive(:options_str).and_return('-A -B -C')
56
+ expect(command.to_s).to eq('ssh -A -B -C example.com')
57
+ end
58
+
59
+ it "excludes options if they're blank" do
60
+ host = SSHKit::Host.new('example.com')
61
+ command = SSHKit::Interactive::Command.new(host)
62
+ expect(command).to receive(:options_str).and_return('')
63
+ expect(command.to_s).to eq('ssh example.com')
64
+ end
65
+
66
+ it "accepts a remote command" do
67
+ host = SSHKit::Host.new('example.com')
68
+ command = SSHKit::Interactive::Command.new(host, 'ls')
69
+ expect(command.to_s).to eq('ssh -A -t example.com "ls"')
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,95 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+
18
+ require_relative '../lib/sshkit/interactive'
19
+
20
+ require 'net/ssh/proxy/command'
21
+
22
+
23
+ RSpec.configure do |config|
24
+ # rspec-expectations config goes here. You can use an alternate
25
+ # assertion/expectation library such as wrong or the stdlib/minitest
26
+ # assertions if you prefer.
27
+ config.expect_with :rspec do |expectations|
28
+ # This option will default to `true` in RSpec 4. It makes the `description`
29
+ # and `failure_message` of custom matchers include text for helper methods
30
+ # defined using `chain`, e.g.:
31
+ # be_bigger_than(2).and_smaller_than(4).description
32
+ # # => "be bigger than 2 and smaller than 4"
33
+ # ...rather than:
34
+ # # => "be bigger than 2"
35
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
36
+ end
37
+
38
+ # rspec-mocks config goes here. You can use an alternate test double
39
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
40
+ config.mock_with :rspec do |mocks|
41
+ # Prevents you from mocking or stubbing a method that does not exist on
42
+ # a real object. This is generally recommended, and will default to
43
+ # `true` in RSpec 4.
44
+ mocks.verify_partial_doubles = true
45
+ end
46
+
47
+ # The settings below are suggested to provide a good initial experience
48
+ # with RSpec, but feel free to customize to your heart's content.
49
+ =begin
50
+ # These two settings work together to allow you to limit a spec run
51
+ # to individual examples or groups you care about by tagging them with
52
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
53
+ # get run.
54
+ config.filter_run :focus
55
+ config.run_all_when_everything_filtered = true
56
+
57
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
58
+ # For more details, see:
59
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
60
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
61
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
62
+ config.disable_monkey_patching!
63
+
64
+ # This setting enables warnings. It's recommended, but in some cases may
65
+ # be too noisy due to issues in dependencies.
66
+ config.warnings = true
67
+
68
+ # Many RSpec users commonly either run the entire suite or an individual
69
+ # file, and it's useful to allow more verbose output when running an
70
+ # individual spec file.
71
+ if config.files_to_run.one?
72
+ # Use the documentation formatter for detailed output,
73
+ # unless a formatter has already been configured
74
+ # (e.g. via a command-line flag).
75
+ config.default_formatter = 'doc'
76
+ end
77
+
78
+ # Print the 10 slowest examples and example groups at the
79
+ # end of the spec run, to help surface which specs are running
80
+ # particularly slow.
81
+ config.profile_examples = 10
82
+
83
+ # Run specs in random order to surface order dependencies. If you find an
84
+ # order dependency and want to debug it, you can fix the order by providing
85
+ # the seed, which is printed after each run.
86
+ # --seed 1234
87
+ config.order = :random
88
+
89
+ # Seed global randomization in this process using the `--seed` CLI option.
90
+ # Setting this allows you to use `--seed` to deterministically reproduce
91
+ # test failures related to randomization by passing the same `--seed` value
92
+ # as the one that triggered the failure.
93
+ Kernel.srand config.seed
94
+ =end
95
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sshkit/interactive/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sshkit-interactive"
8
+ spec.version = SSHKit::Interactive::VERSION
9
+ spec.authors = ["Aidan Feldman"]
10
+ spec.email = ["aidan.feldman@gmail.com"]
11
+ spec.summary = %q{An SSHKit backend that allows you to execute interactive commands on your servers. }
12
+ spec.homepage = "https://github.com/afeld/sshkit-interactive"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "sshkit", "~> 1.6"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.1"
25
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sshkit-interactive
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aidan Feldman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sshkit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ description:
70
+ email:
71
+ - aidan.feldman@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/sshkit/interactive.rb
83
+ - lib/sshkit/interactive/backend.rb
84
+ - lib/sshkit/interactive/command.rb
85
+ - lib/sshkit/interactive/version.rb
86
+ - spec/backend_spec.rb
87
+ - spec/command_spec.rb
88
+ - spec/spec_helper.rb
89
+ - sshkit-interactive.gemspec
90
+ homepage: https://github.com/afeld/sshkit-interactive
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.5
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: An SSHKit backend that allows you to execute interactive commands on your
114
+ servers.
115
+ test_files:
116
+ - spec/backend_spec.rb
117
+ - spec/command_spec.rb
118
+ - spec/spec_helper.rb