net-ssh-script 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 +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +128 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/net/ssh/script.rb +59 -0
- data/lib/net/ssh/script/command.rb +90 -0
- data/lib/net/ssh/script/version.rb +7 -0
- data/net-ssh-script.gemspec +32 -0
- metadata +156 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bb9f0d3800fed09a51bdfa21221a3500f179a0ed
|
4
|
+
data.tar.gz: 3486bb5af1fef149961419a81bdcff14369ce940
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c62fd8a78022fb1037bae0cadfeff682b443027d0d6cfea851ea3ccfe8a66c05cd237b611c9ebb315710d358d6be96493ab629afc0444e7c099f8184dce5630d
|
7
|
+
data.tar.gz: 4248d36519e163af1bb1382d91a5018bb91a21d74efeaaff099959bf01b62b5abb913e54dfb27dfefb775a227cea46cd7d7c64c586b0d08571250ad357c5eb16
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Nuqz
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
# Net::Ssh::Script
|
2
|
+
|
3
|
+
Helps to run commands & scripts over Net::SSH session on remote machines.
|
4
|
+
It can run a single command and whole script and log status/stdout/stderr of each command.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'net-ssh-script'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install net-ssh-script
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
First, require module and create SSH session:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'net/ssh/script'
|
28
|
+
|
29
|
+
Net::SSH.start( '...' ) do |ssh|
|
30
|
+
# insert code below here...
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
### Single command
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# Define a command
|
38
|
+
cmd = Net::SSH::Script::Command['ls']
|
39
|
+
# it is the same as
|
40
|
+
cmd = Net::SSH::Script::Command.new('ls')
|
41
|
+
|
42
|
+
cmd.to_s
|
43
|
+
# => "ls"
|
44
|
+
|
45
|
+
cmd.inspect
|
46
|
+
# => "pending:ls"
|
47
|
+
|
48
|
+
cmd.done?
|
49
|
+
# => false
|
50
|
+
|
51
|
+
cmd.status
|
52
|
+
# => :pending
|
53
|
+
|
54
|
+
cmd.run ssh
|
55
|
+
# => true/false
|
56
|
+
|
57
|
+
cmd.done?
|
58
|
+
# => true
|
59
|
+
|
60
|
+
cmd.status
|
61
|
+
# => :failed/:error/:ok
|
62
|
+
|
63
|
+
cmd.ok? # => true/false
|
64
|
+
cmd.error? # => true/false
|
65
|
+
cmd.failed? # => true/false
|
66
|
+
cmd.pending? # => true/false
|
67
|
+
|
68
|
+
cmd.inspect
|
69
|
+
# => multiline string:
|
70
|
+
# ok:ls
|
71
|
+
# Output:
|
72
|
+
# <a list of files>
|
73
|
+
|
74
|
+
# when command returns error
|
75
|
+
cmd.inspect
|
76
|
+
# => multiline string:
|
77
|
+
# error:ls
|
78
|
+
# Error:
|
79
|
+
# <error returned by command>
|
80
|
+
```
|
81
|
+
|
82
|
+
### Scripts
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
script = Net::SSH::Script::Script[[
|
86
|
+
'cd /tmp',
|
87
|
+
'ls -l',
|
88
|
+
'cd -'
|
89
|
+
]]
|
90
|
+
|
91
|
+
script.run ssh
|
92
|
+
# => true/false
|
93
|
+
|
94
|
+
cmd.running? # => true/false
|
95
|
+
|
96
|
+
script.done?
|
97
|
+
# => true/false
|
98
|
+
|
99
|
+
script.ok? # => true/false
|
100
|
+
script.error? # => true/false
|
101
|
+
script.failed? # => true/false
|
102
|
+
script.pending? # => true/false
|
103
|
+
|
104
|
+
script.inspect
|
105
|
+
# => multiline string:
|
106
|
+
# ok:cd /tmp
|
107
|
+
# Output:
|
108
|
+
# error:ls -l
|
109
|
+
# Error:
|
110
|
+
# 'an error of ls -l command'
|
111
|
+
# pending:cd -
|
112
|
+
```
|
113
|
+
|
114
|
+
## Development
|
115
|
+
|
116
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
117
|
+
|
118
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
119
|
+
|
120
|
+
## Contributing
|
121
|
+
|
122
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/indiesoft/net-ssh-script.
|
123
|
+
|
124
|
+
|
125
|
+
## License
|
126
|
+
|
127
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
128
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "net/ssh/script"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'net/ssh/script/version'
|
2
|
+
require 'net/ssh/script/command'
|
3
|
+
|
4
|
+
module Net
|
5
|
+
module SSH
|
6
|
+
module Script
|
7
|
+
# Net::SSH::Command wrapper to work with multiple commands.
|
8
|
+
class Script
|
9
|
+
def self.[](commands)
|
10
|
+
new commands
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(commands)
|
14
|
+
if commands.is_a? String
|
15
|
+
@_commands = [Command[commands]]
|
16
|
+
elsif commands.is_a?(Array) &&
|
17
|
+
commands.all? { |cmd| cmd.is_a? String }
|
18
|
+
@_commands = commands.map { |c| Command[c] }
|
19
|
+
elsif commands.is_a?(Array) &&
|
20
|
+
commands.all? { |cmd| cmd.is_a? Command }
|
21
|
+
@_commands = commands
|
22
|
+
else raise 'Invalid script argument.'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def run(ssh)
|
27
|
+
@_commands.each { |cmd| break unless cmd.run(ssh) }
|
28
|
+
ok?
|
29
|
+
end
|
30
|
+
|
31
|
+
def done?
|
32
|
+
@_commands.all?(&:done?)
|
33
|
+
end
|
34
|
+
|
35
|
+
def status
|
36
|
+
return :pending if @_commands.all?(&:pending?)
|
37
|
+
return :failed if @_commands.any?(&:failed?)
|
38
|
+
return :error if @_commands.any?(&:error?)
|
39
|
+
return :running unless done?
|
40
|
+
:ok
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_s
|
44
|
+
@_commands.map(&:to_s).join "\n"
|
45
|
+
end
|
46
|
+
|
47
|
+
def inspect
|
48
|
+
@_commands.map(&:inspect).join "\n"
|
49
|
+
end
|
50
|
+
|
51
|
+
[:pending?, :running?, :failed?, :error?, :ok?].each do |helper|
|
52
|
+
define_method helper do
|
53
|
+
status == helper.to_s.sub('?', '').to_sym
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'net/ssh'
|
2
|
+
|
3
|
+
module Net
|
4
|
+
module SSH
|
5
|
+
module Script
|
6
|
+
# Net::SSH::Connection::Session wrapper to capture
|
7
|
+
# status/stdout/stderr produced by a command.
|
8
|
+
class Command
|
9
|
+
attr_reader :output, :error
|
10
|
+
|
11
|
+
def self.[](command)
|
12
|
+
new command
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(command_string)
|
16
|
+
@_cmd = command_string
|
17
|
+
@_done = @_success = false
|
18
|
+
@output = @error = ''
|
19
|
+
end
|
20
|
+
|
21
|
+
def run(ssh)
|
22
|
+
channel = ssh.open_channel do |ch|
|
23
|
+
# In case when we want rerun a command
|
24
|
+
@_done = @_success = false
|
25
|
+
exec_command @_cmd, ch
|
26
|
+
end
|
27
|
+
|
28
|
+
channel.wait
|
29
|
+
|
30
|
+
ok?
|
31
|
+
end
|
32
|
+
|
33
|
+
def done?
|
34
|
+
@_done
|
35
|
+
end
|
36
|
+
|
37
|
+
def inspect
|
38
|
+
case status
|
39
|
+
when :ok
|
40
|
+
"#{status}: #{@_cmd}\nOutput:\n#{@output}\n"
|
41
|
+
when :error
|
42
|
+
"#{status}: #{@_cmd}\nError:\n#{@error}\n"
|
43
|
+
else
|
44
|
+
"#{status}: #{@_cmd}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def status
|
49
|
+
return :pending unless done?
|
50
|
+
return :failed unless @_success
|
51
|
+
return :error unless @error.empty?
|
52
|
+
:ok
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_s
|
56
|
+
@_cmd
|
57
|
+
end
|
58
|
+
|
59
|
+
[:pending?, :failed?, :error?, :ok?].each do |helper|
|
60
|
+
define_method helper do
|
61
|
+
status == helper.to_s.sub('?', '').to_sym
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def listen_channel(chan)
|
68
|
+
chan.on_close { @_done = true }
|
69
|
+
chan.on_data { |_, data| @output << data }
|
70
|
+
chan.on_extended_data do |_, type, data|
|
71
|
+
@error << "(#{type}):#{data}\n"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def exec_command(command, chan)
|
76
|
+
chan.exec command do |ch, success|
|
77
|
+
@_success = success
|
78
|
+
|
79
|
+
unless success
|
80
|
+
@_done = true
|
81
|
+
return false
|
82
|
+
end
|
83
|
+
|
84
|
+
listen_channel ch
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'net/ssh/script/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "net-ssh-script"
|
8
|
+
spec.version = Net::SSH::Script::VERSION
|
9
|
+
spec.authors = ["Nuqz"]
|
10
|
+
spec.email = ["nuqz@indiesoft.ru"]
|
11
|
+
|
12
|
+
spec.summary = %q{Helps to run commands & scripts over Net::SSH session on remote machines.}
|
13
|
+
spec.description = %q{It can run a single command and whole script and log status/stdout/stderr of each command.}
|
14
|
+
spec.homepage = "https://github.com/indiesoft/net-ssh-script"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency 'net-ssh', '~> 4.0'
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "yard"
|
30
|
+
spec.add_development_dependency "simplecov"
|
31
|
+
spec.add_development_dependency "rubocop"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: net-ssh-script
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nuqz
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: net-ssh
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
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.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.14'
|
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.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: It can run a single command and whole script and log status/stdout/stderr
|
112
|
+
of each command.
|
113
|
+
email:
|
114
|
+
- nuqz@indiesoft.ru
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- ".travis.yml"
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- bin/console
|
127
|
+
- bin/setup
|
128
|
+
- lib/net/ssh/script.rb
|
129
|
+
- lib/net/ssh/script/command.rb
|
130
|
+
- lib/net/ssh/script/version.rb
|
131
|
+
- net-ssh-script.gemspec
|
132
|
+
homepage: https://github.com/indiesoft/net-ssh-script
|
133
|
+
licenses:
|
134
|
+
- MIT
|
135
|
+
metadata: {}
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
requirements: []
|
151
|
+
rubyforge_project:
|
152
|
+
rubygems_version: 2.6.8
|
153
|
+
signing_key:
|
154
|
+
specification_version: 4
|
155
|
+
summary: Helps to run commands & scripts over Net::SSH session on remote machines.
|
156
|
+
test_files: []
|