jossh 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +41 -2
- data/bin/jossh +32 -0
- data/lib/jossh/api.rb +68 -8
- data/lib/jossh/command_runner.rb +12 -1
- data/lib/jossh/output_handler.rb +18 -4
- data/lib/jossh/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae6b17ea42779bad1f8ba891969a8ff6aba3b4a3
|
4
|
+
data.tar.gz: 5550c9b4bb4a1b26396befaddb9baab2aaecfb31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7aca02debcfdf336894b3efe1c1178aea44ec455ebce3332208018ea7fdde7be8d9445cf0159c7c9457d006fac51acccc680f9d057a1d2235c554d414de5a371
|
7
|
+
data.tar.gz: 98dd31bb03b543c68368a64f740855ab74e3988d0210e163d4de7bd214107ed6348cb2bb09d49bbe3dfa2bfa3b0cf3c92f3bbe858b98b0a64a180100bf54c069
|
data/README.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
Jossh - Your SSH Buddy
|
2
2
|
======================
|
3
3
|
|
4
|
+
[](http://badge.fury.io/rb/jossh)
|
5
|
+
[](https://codeclimate.com/github/DannyBen/jossh)
|
6
|
+
[](https://gemnasium.com/DannyBen/jossh)
|
7
|
+
|
8
|
+
|
4
9
|
Ruby SSH functions for easier and prettier remote deployment and automation.
|
5
10
|
|
6
11
|
## Install
|
@@ -24,8 +29,29 @@ Or install manually
|
|
24
29
|
6. Allows storing host specifications in a YAML file.
|
25
30
|
7. Supports all options available in `Net::SSH#start`.
|
26
31
|
8. Prints output Heroku-style.
|
32
|
+
9. Provides a command line interface - `jossh <host> <script>`
|
33
|
+
|
34
|
+
## Command Line Usage
|
27
35
|
|
28
|
-
|
36
|
+
After installing, you can call `jossh` from the command line to run arbitrary
|
37
|
+
commands or a local script over SSH.
|
38
|
+
|
39
|
+
```
|
40
|
+
$ jossh
|
41
|
+
|
42
|
+
Usage: jossh <host> <script>
|
43
|
+
|
44
|
+
<host> - any key available in ./ssh_hosts.yml
|
45
|
+
<script> - can be either a filename or one or more direct command
|
46
|
+
quotes are only needed if you include multiple commands
|
47
|
+
with && or semicolor (;)
|
48
|
+
|
49
|
+
Examples: jossh localhost git status
|
50
|
+
jossh localhost "cd ~ && ls -l"
|
51
|
+
jossh localhost deploy
|
52
|
+
```
|
53
|
+
|
54
|
+
## Library Usage
|
29
55
|
|
30
56
|
### Example 1: Host specifications in a YAML file
|
31
57
|
|
@@ -78,4 +104,17 @@ touch 'tmp/restart.txt'
|
|
78
104
|
echo "-> Done"
|
79
105
|
```
|
80
106
|
|
81
|
-
See also: The examples folder
|
107
|
+
See also: The [examples folder](https://github.com/DannyBen/jossh/tree/master/examples)
|
108
|
+
|
109
|
+
## Host specification file
|
110
|
+
|
111
|
+
Host specifications should be configured in `ssh_hosts.yml` by default.
|
112
|
+
|
113
|
+
If you wish to use a different location, use the `ssh_hostfile` method:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
ssh_hostfile "my_hosts.yml"
|
117
|
+
ssh! :myhost, 'ls'
|
118
|
+
```
|
119
|
+
|
120
|
+
See [ssh_hosts.example.yml](https://github.com/DannyBen/jossh/blob/master/ssh_hosts.example.yml) as an example.
|
data/bin/jossh
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'jossh'
|
4
|
+
|
5
|
+
# for dev (not needed in most cases)
|
6
|
+
# require File.dirname(__FILE__) + "/../lib/jossh"
|
7
|
+
|
8
|
+
trap(:INT) { abort "\r\nGoodbye" }
|
9
|
+
|
10
|
+
def usage
|
11
|
+
"\nUsage: jossh <host> <script>\n\n" +
|
12
|
+
" <host> - any key available in ./ssh_hosts.yml\n" +
|
13
|
+
" <script> - can be either a filename or one or more direct command\n" +
|
14
|
+
" quotes are only needed if you include multiple commands\n" +
|
15
|
+
" with && or semicolor (;)\n\n" +
|
16
|
+
"Examples: jossh localhost git status\n" +
|
17
|
+
" jossh localhost \"cd ~ && ls -l\"\n" +
|
18
|
+
" jossh localhost deploy"
|
19
|
+
end
|
20
|
+
|
21
|
+
def run
|
22
|
+
host = ARGV.shift
|
23
|
+
script = ARGV.join ' '
|
24
|
+
host and script or abort usage
|
25
|
+
if File.exist? script
|
26
|
+
ssh_script! host.to_sym, script
|
27
|
+
else
|
28
|
+
ssh host.to_sym, script
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
run
|
data/lib/jossh/api.rb
CHANGED
@@ -1,21 +1,81 @@
|
|
1
|
+
# = API Methods
|
2
|
+
# These are all the exposed commands.
|
3
|
+
|
1
4
|
module Jossh
|
2
5
|
|
3
|
-
|
4
|
-
|
6
|
+
# Execute one or more commands via SSH
|
7
|
+
#
|
8
|
+
# ==== Params:
|
9
|
+
#
|
10
|
+
# +hostspec+::
|
11
|
+
# A hash of SSH host parameters or a symbol pointing to a record in
|
12
|
+
# +ssh_hosts.yml+. If a hash is provided, it must include at
|
13
|
+
# least +:host+ and +:user+. It can also include any or the options
|
14
|
+
# supported by Net::SSH#start.
|
15
|
+
#
|
16
|
+
# +script+::
|
17
|
+
# A string or array of commands
|
18
|
+
#
|
19
|
+
# +callback+::
|
20
|
+
# (optional) A method to be called on each block of data received from
|
21
|
+
# the SSH execution. If none provided, we will simply use `puts`.
|
22
|
+
#
|
23
|
+
# ==== Examples:
|
24
|
+
#
|
25
|
+
# ssh :localhost, ["cd /opt/app", "git pull"]
|
26
|
+
#
|
27
|
+
# ssh { host: 'localhost', user: 'vagrant' }, "ls -l"
|
28
|
+
#
|
29
|
+
# def my_puts(data)
|
30
|
+
# puts "> #{data}"
|
31
|
+
# end
|
32
|
+
# ssh :localhost, "ls -l", method: my_puts
|
33
|
+
#
|
34
|
+
def ssh(hostspec, script, callback: nil)
|
35
|
+
CommandRunner.instance.ssh hostspec, script, callback: callback
|
5
36
|
end
|
6
37
|
|
7
|
-
|
8
|
-
|
38
|
+
# Same as +ssh+, only this will print a pretty output.
|
39
|
+
#
|
40
|
+
# This method accepts only +hostspec+ and +script+ (no +callback+).
|
41
|
+
#
|
42
|
+
def ssh!(hostspec, script)
|
43
|
+
CommandRunner.instance.ssh! hostspec, script
|
9
44
|
end
|
10
45
|
|
11
|
-
|
12
|
-
|
46
|
+
# Same as +ssh+, only load commands from a file.
|
47
|
+
#
|
48
|
+
# ==== Params:
|
49
|
+
#
|
50
|
+
# +hostspec+::
|
51
|
+
# See +ssh+
|
52
|
+
#
|
53
|
+
# +script+::
|
54
|
+
# A path to the script to run. The script file should be a file with a
|
55
|
+
# list of shell commands.
|
56
|
+
#
|
57
|
+
# +callback+::
|
58
|
+
# (optional) See +ssh+
|
59
|
+
#
|
60
|
+
# ==== Examples:
|
61
|
+
#
|
62
|
+
# ssh_script :localhost, "deploy"
|
63
|
+
#
|
64
|
+
def ssh_script(hostspec, script, callback: nil)
|
65
|
+
CommandRunner.instance.ssh_script hostspec, script, callback: callback
|
13
66
|
end
|
14
67
|
|
15
|
-
|
16
|
-
|
68
|
+
# Same as +ssh_script+, only this will print a pretty output.
|
69
|
+
#
|
70
|
+
# This method accepts only +hostspec+ and +script+ (no +callback+).
|
71
|
+
#
|
72
|
+
def ssh_script!(hostspec, script)
|
73
|
+
CommandRunner.instance.ssh_script! hostspec, script
|
17
74
|
end
|
18
75
|
|
76
|
+
def ssh_hostfile(file)
|
77
|
+
CommandRunner.instance.ssh_hostfile file
|
78
|
+
end
|
19
79
|
|
20
80
|
end
|
21
81
|
|
data/lib/jossh/command_runner.rb
CHANGED
@@ -3,8 +3,15 @@ require "yaml"
|
|
3
3
|
|
4
4
|
module Jossh
|
5
5
|
|
6
|
+
# CommandRunner is the primary class in the Jossh module. It is responsible
|
7
|
+
# for providing the backend for all api methods.
|
8
|
+
#
|
6
9
|
class CommandRunner
|
7
10
|
|
11
|
+
def self.instance
|
12
|
+
@@instance ||= self.new
|
13
|
+
end
|
14
|
+
|
8
15
|
def ssh!(hostspec, script)
|
9
16
|
ssh hostspec, script, callback: OutputHandler.new.method(:pretty_puts)
|
10
17
|
end
|
@@ -17,7 +24,7 @@ module Jossh
|
|
17
24
|
user = hostspec.delete :user
|
18
25
|
|
19
26
|
Net::SSH.start( host, user, hostspec ) do |ssh|
|
20
|
-
ssh.exec! script do |
|
27
|
+
ssh.exec! script do |_ch, _stream, data|
|
21
28
|
callback.call data
|
22
29
|
end
|
23
30
|
end
|
@@ -31,6 +38,10 @@ module Jossh
|
|
31
38
|
ssh hostspec, load_script(script), callback: callback
|
32
39
|
end
|
33
40
|
|
41
|
+
def ssh_hostfile(file)
|
42
|
+
@hostfile = file
|
43
|
+
end
|
44
|
+
|
34
45
|
private
|
35
46
|
|
36
47
|
def load_spec(key)
|
data/lib/jossh/output_handler.rb
CHANGED
@@ -1,17 +1,31 @@
|
|
1
1
|
require 'colsole'
|
2
2
|
|
3
|
-
# Print functions inspired by Heroku and based on code from Mina
|
4
|
-
# https://github.com/mina-deploy/mina/blob/master/lib/mina/output_helpers.rb
|
5
|
-
|
6
3
|
module Jossh
|
7
4
|
|
5
|
+
# OutputHandler is responsible for generating the pretty output created by
|
6
|
+
# +ssh!+ and +ssh_script!+ commands.
|
7
|
+
#
|
8
8
|
class OutputHandler
|
9
9
|
|
10
10
|
include Colsole
|
11
11
|
|
12
|
+
# Print indented output while giving special treatment to strings that
|
13
|
+
# start with certain markers.
|
14
|
+
#
|
15
|
+
# This method is used as the callback function by +ssh!+ and +ssh_script!+
|
16
|
+
#
|
17
|
+
# ==== Params:
|
18
|
+
# +lines+::
|
19
|
+
# A newline delimited string or array to pretty-print
|
20
|
+
#
|
21
|
+
#
|
22
|
+
# ==== Credits:
|
23
|
+
# This function was inspired by Heroku and based on code from Mina[https://github.com/mina-deploy/mina/blob/master/lib/mina/output_helpers.rb]
|
24
|
+
#
|
12
25
|
def pretty_puts(lines)
|
13
26
|
lines = lines.split("\n") if lines.is_a? String
|
14
27
|
lines.each do |line|
|
28
|
+
line = line.rstrip.gsub /\t/, " "
|
15
29
|
if line =~ /^\-+> (.*?)$/
|
16
30
|
puts_status $1
|
17
31
|
elsif line =~ /^! (.*?)$/
|
@@ -43,7 +57,7 @@ module Jossh
|
|
43
57
|
end
|
44
58
|
|
45
59
|
def puts_stdout(msg)
|
46
|
-
|
60
|
+
puts " #{msg}"
|
47
61
|
end
|
48
62
|
|
49
63
|
end
|
data/lib/jossh/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jossh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
@@ -83,11 +83,13 @@ dependencies:
|
|
83
83
|
description: Jossh is a wrapper around Ruby Net::SSH with a simpler interface and
|
84
84
|
prettier output
|
85
85
|
email: db@dannyben.com
|
86
|
-
executables:
|
86
|
+
executables:
|
87
|
+
- jossh
|
87
88
|
extensions: []
|
88
89
|
extra_rdoc_files: []
|
89
90
|
files:
|
90
91
|
- README.md
|
92
|
+
- bin/jossh
|
91
93
|
- lib/jossh.rb
|
92
94
|
- lib/jossh/api.rb
|
93
95
|
- lib/jossh/command_runner.rb
|