net-ssh-session 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Dan Sosedoff.
1
+ Copyright (c) 2012-2013 Dan Sosedoff.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
data/README.md CHANGED
@@ -71,6 +71,9 @@ session.run_multiple(
71
71
  'rake test'
72
72
  )
73
73
 
74
+ # Execute as sudo
75
+ session.sudo("whoami")
76
+
74
77
  # Execute with time limit (10s)
75
78
  begin
76
79
  session.with_timeout(10) do
@@ -166,6 +169,12 @@ s.run("bundler install --path .")
166
169
  s.close
167
170
  ```
168
171
 
172
+ You can also disable histroy for the whole session:
173
+
174
+ ```ruby
175
+ Net::SSH::Session.new(host, user, password, :history => false)
176
+ ```
177
+
169
178
  ## Credits
170
179
 
171
180
  Library code was extracted and modified from multiple sources:
@@ -1,7 +1,7 @@
1
1
  module Net
2
2
  module SSH
3
3
  class Session
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
6
6
  end
7
7
  end
@@ -16,18 +16,24 @@ module Net
16
16
  attr_reader :history
17
17
 
18
18
  # Initialize a new ssh session
19
- # @param host [String] remote hostname or ip address
20
- # @param user [String] remote account username
21
- # @param password [String] remote account password
22
- def initialize(host, user, password='')
23
- @host = host
24
- @user = user
25
- @password = password
26
- @history = []
19
+ # @param [String] remote hostname or ip address
20
+ # @param [String] remote account username
21
+ # @param [String] remote account password
22
+ # @param [Hash] options hash
23
+ def initialize(host, user, password='', options={})
24
+ @host = host
25
+ @user = user
26
+ @password = password
27
+ @history = []
28
+ @track_history = true
29
+
30
+ if options[:history] == false
31
+ @track_history = false
32
+ end
27
33
  end
28
34
 
29
35
  # Establish connection with remote server
30
- # @param timeout [Integer] max timeout in seconds
36
+ # @param [Integer] max timeout in seconds
31
37
  # @return [Boolean]
32
38
  def open(timeout=nil)
33
39
  if timeout && timeout > 0
@@ -48,8 +54,8 @@ module Net
48
54
  end
49
55
 
50
56
  # Execute command
51
- # @param command [String] command to execute
52
- # @param on_output [Block] output event block
57
+ # @param [String] command to execute
58
+ # @param [Block] output event block
53
59
  # @return [Integer] command execution exit code
54
60
  def exec(command, &on_output)
55
61
  status = nil
@@ -63,8 +69,8 @@ module Net
63
69
  end
64
70
 
65
71
  # Execute a single command
66
- # @param command [String] comand to execute
67
- # @param options [Hash] execution options
72
+ # @param [String] comand to execute
73
+ # @param [Hash] execution options
68
74
  # @return [SessionCommand]
69
75
  def run(command, options={})
70
76
  output = ''
@@ -82,15 +88,18 @@ module Net
82
88
  t_end - t_start
83
89
  )
84
90
 
85
- history << cmd unless options[:history] == false
91
+ if options[:history] == true || @track_history == true
92
+ history << cmd
93
+ end
94
+
86
95
  logger.info(cmd.to_s) if logger
87
96
 
88
97
  cmd
89
98
  end
90
99
 
91
100
  # Execute multiple commands
92
- # @param commands [Array] set of commands to execute
93
- # @param options [Hash] execution options
101
+ # @param [Array] set of commands to execute
102
+ # @param [Hash] execution options
94
103
  # @return [Array] set of command execution results
95
104
  #
96
105
  # Execution options are the following:
@@ -110,10 +119,17 @@ module Net
110
119
  end
111
120
 
112
121
  # Set a global session logger for commands
122
+ # @param [Logger] logger instance
113
123
  def logger=(log)
114
124
  @logger = log
115
125
  end
116
126
 
127
+ # Get last executed command
128
+ # @return [SessionCommand]
129
+ def last_command
130
+ history.last
131
+ end
132
+
117
133
  private
118
134
 
119
135
  def establish_connection
@@ -1,19 +1,20 @@
1
1
  module Net
2
2
  module SSH
3
3
  class SessionCommand
4
- attr_reader :command, :output, :exit_code
5
- attr_reader :duration
4
+ attr_reader :command, :duration
5
+ attr_accessor :output, :exit_code
6
6
  attr_accessor :start_time, :finish_time
7
7
 
8
8
  # Initialize a new session command
9
- # @param command [String] original command
10
- # @param output [String] command execution output
11
- # @param exit_code [Integer] command execution exit code
12
- # @param duration [Float] execution time in seconds
13
- def initialize(command, output, exit_code, duration=0)
9
+ #
10
+ # @param [String] original command
11
+ # @param [String] command execution output
12
+ # @param [Integer] command execution exit code
13
+ # @param [Float] execution time in seconds
14
+ def initialize(command, output=nil, exit_code=nil, duration=0)
14
15
  @command = command
15
16
  @output = output || ''
16
- @exit_code = Integer(exit_code) rescue 1
17
+ @exit_code = Integer(exit_code) rescue 1 if exit_code != nil
17
18
  @duration = Float(duration)
18
19
  end
19
20
 
@@ -29,6 +30,8 @@ module Net
29
30
  exit_code != 0
30
31
  end
31
32
 
33
+ alias :error? :failure?
34
+
32
35
  # Get command string representation
33
36
  # @return [String]
34
37
  def to_s
@@ -3,6 +3,14 @@ require 'timeout'
3
3
  module Net
4
4
  module SSH
5
5
  module SessionHelpers
6
+ # Execute command with sudo
7
+ # @param [String] command string
8
+ # @param [Hash] execution options
9
+ # @return [SessionCommand]
10
+ def sudo(command, options={})
11
+ run("sudo #{command}", options)
12
+ end
13
+
6
14
  # Swith current directory
7
15
  # @param path [String] directory path
8
16
  # @return [Boolean] execution result
@@ -45,6 +45,11 @@ describe Net::SSH::SessionCommand do
45
45
  cmd = Net::SSH::SessionCommand.new('cmd', 'output', '0')
46
46
  cmd.failure?.should be_false
47
47
  end
48
+
49
+ it 'has a :error? alias method' do
50
+ cmd = Net::SSH::SessionCommand.new('cmd', 'output', '0')
51
+ cmd.should respond_to :error?
52
+ end
48
53
  end
49
54
 
50
55
  describe '#to_s' do
metadata CHANGED
@@ -1,71 +1,87 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: net-ssh-session
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
4
5
  prerelease:
5
- version: 0.1.0
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Dan Sosedoff
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-12-06 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: rake
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
24
22
  type: :development
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: rspec
28
23
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
30
33
  none: false
31
- requirements:
34
+ requirements:
32
35
  - - ~>
33
- - !ruby/object:Gem::Version
34
- version: "2.11"
36
+ - !ruby/object:Gem::Version
37
+ version: '2.11'
35
38
  type: :development
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
38
- name: simplecov
39
39
  prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.11'
46
+ - !ruby/object:Gem::Dependency
47
+ name: simplecov
48
+ requirement: !ruby/object:Gem::Requirement
41
49
  none: false
42
- requirements:
50
+ requirements:
43
51
  - - ~>
44
- - !ruby/object:Gem::Version
45
- version: "0.4"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.4'
46
54
  type: :development
47
- version_requirements: *id003
48
- - !ruby/object:Gem::Dependency
49
- name: net-ssh
50
55
  prerelease: false
51
- requirement: &id004 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.4'
62
+ - !ruby/object:Gem::Dependency
63
+ name: net-ssh
64
+ requirement: !ruby/object:Gem::Requirement
52
65
  none: false
53
- requirements:
66
+ requirements:
54
67
  - - ~>
55
- - !ruby/object:Gem::Version
56
- version: "2.6"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.6'
57
70
  type: :runtime
58
- version_requirements: *id004
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.6'
59
78
  description: Shell interface with helper methods to work with Net::SSH connections
60
- email:
79
+ email:
61
80
  - dan.sosedoff@gmail.com
62
81
  executables: []
63
-
64
82
  extensions: []
65
-
66
83
  extra_rdoc_files: []
67
-
68
- files:
84
+ files:
69
85
  - .gitignore
70
86
  - .rspec
71
87
  - .travis.yml
@@ -89,35 +105,31 @@ files:
89
105
  - spec/spec_helper.rb
90
106
  homepage: https://github.com/sosedoff/net-ssh-session
91
107
  licenses: []
92
-
93
108
  post_install_message:
94
109
  rdoc_options: []
95
-
96
- require_paths:
110
+ require_paths:
97
111
  - lib
98
- required_ruby_version: !ruby/object:Gem::Requirement
112
+ required_ruby_version: !ruby/object:Gem::Requirement
99
113
  none: false
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: "0"
104
- required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
119
  none: false
106
- requirements:
107
- - - ">="
108
- - !ruby/object:Gem::Version
109
- version: "0"
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
110
124
  requirements: []
111
-
112
125
  rubyforge_project:
113
126
  rubygems_version: 1.8.24
114
127
  signing_key:
115
128
  specification_version: 3
116
129
  summary: Shell session for Net::SSH connections
117
- test_files:
130
+ test_files:
118
131
  - spec/fixtures/.gitkeep
119
132
  - spec/session_command_spec.rb
120
133
  - spec/session_helpers_spec.rb
121
134
  - spec/session_spec.rb
122
135
  - spec/spec_helper.rb
123
- has_rdoc: