sshkit 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,3 +1,4 @@
1
+ before_install: echo 'yes' | gem uninstall ffi -a -I || true
1
2
  language: ruby
2
3
  rvm:
3
4
  - 1.9.3
data/CHANGELOG.md CHANGED
@@ -3,6 +3,16 @@
3
3
  This file is written in reverse chronological order, newer releases will
4
4
  appear at the top.
5
5
 
6
+ ## 0.0.11
7
+
8
+ * Implementing confuguration objects on the backends (WIP, undocumented)
9
+ * Implement `SSHKit.config.default_env`, a hash which can be modified and
10
+ will act as a global `with`.
11
+ * Fixed #9 (with(a: 'b', c: 'c') being parsed as `A=bC=d`. Now properly space
12
+ separated.
13
+ * Fixed #10 (overly aggressive shell escaping), one can now do:
14
+ `with(path: 'foo:$PATH') without the $ being escaped too early.
15
+
6
16
  ## 0.0.10
7
17
 
8
18
  * Include more attributes in `Command#to_hash`.
data/EXAMPLES.md CHANGED
@@ -8,6 +8,13 @@
8
8
  end
9
9
  end
10
10
 
11
+ ## Run with default environemntal variables
12
+
13
+ SSHKit.config.default_env = { path: '/usr/local/libexec/bin:$PATH' }
14
+ on hosts do |host|
15
+ puts capture(:env)
16
+ end
17
+
11
18
  ## Run a command in a different directory
12
19
 
13
20
  on hosts do |host|
data/README.md CHANGED
@@ -194,10 +194,10 @@ should be printed.
194
194
  not, perhaps a family of similarly named bang methods should be the ones to
195
195
  raise. (Perhaps `test()` should be a way to `execute()` without raising, and
196
196
  `execute()` and friends should always raise)~~
197
- * It would be nice to be able to say `SSHKit.config.formatter = :pretty` and
197
+ * ~~It would be nice to be able to say `SSHKit.config.formatter = :pretty` and
198
198
  have that method setter do the legwork of updating `SSHKit.config.output` to
199
199
  be an instance of the correct formatter class wrapping the existing output
200
- stream.
200
+ stream.~~
201
201
  * No closing of connections, the abstract backend class should include a
202
202
  cleanup method which is empty but can be overriden by
203
203
  * No conncetion pooling, the `connection` method of the NetSSH backend could
@@ -207,3 +207,7 @@ should be printed.
207
207
  * Wrap all commands in a known shell, that is that `execute('uptime')` should
208
208
  be converted into `sh -c 'uptime'` to ensure that we have a consistent shell
209
209
  experience.
210
+ * There's no suitable host parser that accepts `Host.new('user@ip:port')`, it
211
+ will decode a `user@hostname:port`, but IP addresses don't work.
212
+ * If Net::SSH raises `IOError` (as it does when authentication fails) this
213
+ needs to be caught, and re-raised as some kind of ConnectionFailed error.
data/lib/sshkit.rb CHANGED
@@ -23,6 +23,10 @@ module SSHKit
23
23
  @@config ||= Configuration.new
24
24
  end
25
25
 
26
+ def self.reset_configuration!
27
+ @@config = nil
28
+ end
29
+
26
30
  end
27
31
 
28
32
  require_relative 'sshkit/all'
@@ -5,6 +5,10 @@ module SSHKit
5
5
 
6
6
  class Netssh < Printer
7
7
 
8
+ class Configuration
9
+ attr_accessor :connection_timeout, :pty
10
+ end
11
+
8
12
  include SSHKit::CommandHelper
9
13
 
10
14
  def run
@@ -29,6 +33,14 @@ module SSHKit
29
33
  _execute(*args).stdout.strip
30
34
  end
31
35
 
36
+ def configure
37
+ yield config
38
+ end
39
+
40
+ def config
41
+ @config ||= Configuration.new
42
+ end
43
+
32
44
  private
33
45
 
34
46
  def _execute(*args)
@@ -36,6 +48,7 @@ module SSHKit
36
48
  output << cmd
37
49
  cmd.started = true
38
50
  ssh.open_channel do |chan|
51
+ chan.request_pty if config.pty
39
52
  chan.exec cmd.to_s do |ch, success|
40
53
  chan.on_data do |ch, data|
41
54
  cmd.stdout += data
@@ -124,14 +124,19 @@ module SSHKit
124
124
  if options[:in]
125
125
  cs << sprintf("cd %s && ", options[:in])
126
126
  end
127
+ unless SSHKit.config.default_env.empty?
128
+ if options[:env].is_a? Hash
129
+ options[:env] = SSHKit.config.default_env.merge(options[:env])
130
+ end
131
+ end
127
132
  if options[:env]
128
133
  cs << '( '
129
134
  options[:env].each do |k,v|
130
135
  cs << k.to_s.upcase
131
136
  cs << "="
132
- cs << v.to_s.shellescape
137
+ cs << v.to_s
138
+ cs << ' '
133
139
  end
134
- cs << ' '
135
140
  end
136
141
  if options[:user]
137
142
  cs << "sudo su #{options[:user]} -c "
@@ -2,12 +2,23 @@ module SSHKit
2
2
 
3
3
  class Configuration
4
4
 
5
- attr_writer :command_map
6
- attr_accessor :output, :backend
5
+ attr_writer :output, :backend, :default_env, :command_map
7
6
 
8
- def initialize
9
- @output = SSHKit::Formatter::Pretty.new($stdout)
10
- @backend = SSHKit::Backend::Netssh
7
+ def output
8
+ @output ||= format=(:pretty)
9
+ end
10
+
11
+ def default_env
12
+ @default_env ||= {}
13
+ end
14
+
15
+ def backend
16
+ @backend ||= SSHKit::Backend::Netssh
17
+ end
18
+
19
+ def format=(format)
20
+ formatter = SSHKit::Formatter.const_get(format.capitalize)
21
+ self.output = formatter.new($stdout)
11
22
  end
12
23
 
13
24
  def command_map
@@ -1,3 +1,3 @@
1
1
  module SSHKit
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
@@ -3,11 +3,19 @@ require 'helper'
3
3
  module SSHKit
4
4
  module Backend
5
5
  class TestNetssh < UnitTest
6
+
6
7
  def backend
7
- Netssh.new(Host.new('example.com'), Proc.new)
8
+ @backend ||= Netssh.new(Host.new('example.com'))
8
9
  end
9
- def test_net_ssh_configuration_timeout
10
- skip "No configuration on the Netssh class yet"
10
+
11
+ def test_net_ssh_configuration_options
12
+ backend.configure do |ssh|
13
+ ssh.pty = true
14
+ ssh.connection_timeout = 30
15
+ end
16
+
17
+ assert_equal 30, backend.config.connection_timeout
18
+ assert_equal true, backend.config.pty
11
19
  end
12
20
  end
13
21
  end
@@ -4,6 +4,10 @@ require 'sshkit'
4
4
  module SSHKit
5
5
  class TestCommand < UnitTest
6
6
 
7
+ def setup
8
+ SSHKit.reset_configuration!
9
+ end
10
+
7
11
  def test_maps_a_command
8
12
  c = Command.new('example')
9
13
  assert_equal '/usr/bin/env example', String(c)
@@ -26,10 +30,36 @@ module SSHKit
26
30
  end
27
31
 
28
32
  def test_including_the_env
33
+ SSHKit.config = nil
29
34
  c = Command.new(:rails, 'server', env: {rails_env: :production})
30
35
  assert_equal "( RAILS_ENV=production /usr/bin/env rails server )", String(c)
31
36
  end
32
37
 
38
+ def test_including_the_env_with_multiple_keys
39
+ SSHKit.config = nil
40
+ c = Command.new(:rails, 'server', env: {rails_env: :production, foo: 'bar'})
41
+ assert_equal "( RAILS_ENV=production FOO=bar /usr/bin/env rails server )", String(c)
42
+ end
43
+
44
+ def test_including_the_env_doesnt_addressively_escape
45
+ SSHKit.config = nil
46
+ c = Command.new(:rails, 'server', env: {path: '/example:$PATH'})
47
+ assert_equal "( PATH=/example:$PATH /usr/bin/env rails server )", String(c)
48
+ end
49
+
50
+ def test_global_env
51
+ SSHKit.config = nil
52
+ SSHKit.config.default_env = { default: 'env' }
53
+ c = Command.new(:rails, 'server', env: {})
54
+ assert_equal "( DEFAULT=env /usr/bin/env rails server )", String(c)
55
+ end
56
+
57
+ def test_default_env_is_overwritten_with_locally_defined
58
+ SSHKit.config.default_env = { foo: 'bar', over: 'under' }
59
+ c = Command.new(:rails, 'server', env: { over: 'write'})
60
+ assert_equal "( FOO=bar OVER=write /usr/bin/env rails server )", String(c)
61
+ end
62
+
33
63
  def test_working_in_a_given_directory
34
64
  c = Command.new(:ls, '-l', in: "/opt/sites")
35
65
  assert_equal "cd /opt/sites && /usr/bin/env ls -l", String(c)
@@ -7,12 +7,16 @@ module SSHKit
7
7
  def setup
8
8
  SSHKit.config = nil
9
9
  SSHKit.config.command_map.clear
10
+ SSHKit.config.output = SSHKit::Formatter::Pretty.new($stdout)
10
11
  end
11
12
 
12
13
  def test_output
13
14
  assert SSHKit.config.output.is_a? SSHKit::Formatter::Pretty
14
15
  assert SSHKit.config.output = $stderr
15
- assert_equal $stderr, SSHKit.config.output
16
+ end
17
+
18
+ def test_default_env
19
+ assert SSHKit.config.default_env
16
20
  end
17
21
 
18
22
  def test_backend
@@ -30,6 +34,10 @@ module SSHKit
30
34
  assert_equal "/opt/sites/example/current/bin ruby", SSHKit.config.command_map[:ruby]
31
35
  end
32
36
 
37
+ def test_setting_formatter
38
+ assert SSHKit.config.format = :dot
39
+ assert SSHKit.config.output.is_a? SSHKit::Formatter::Dot
40
+ end
33
41
  end
34
42
 
35
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sshkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-01-18 00:00:00.000000000 Z
13
+ date: 2013-01-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: net-ssh