jossh 0.0.1
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/README.md +81 -0
- data/lib/jossh/api.rb +21 -0
- data/lib/jossh/command_runner.rb +64 -0
- data/lib/jossh/output_handler.rb +52 -0
- data/lib/jossh/version.rb +3 -0
- data/lib/jossh.rb +6 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 89bbdbdf3826fe36d90cb9c883b272dc2a61b480
|
4
|
+
data.tar.gz: 8f3ee16da118802914ec86221606a4abf99cdb89
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 474aa547a0e00146e1a0bc0b477e358e2d210939639f58f8e854b42c530c8a24d20f04d711de205a6eb2fa538e7a2793dcc9dfa2eb186ee2634627c9db0be2c2
|
7
|
+
data.tar.gz: 28e579729179e79b391eb2ce7e443d3dd5087bba59d98e67269f7f18c0ee85ca310f49fec4cb50f2b9c571b6bfa1316898cbc0b3936d8188e3b16471d6bcfdc1
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
Jossh - Your SSH Buddy
|
2
|
+
======================
|
3
|
+
|
4
|
+
Ruby SSH functions for easier and prettier remote deployment and automation.
|
5
|
+
|
6
|
+
## Install
|
7
|
+
|
8
|
+
Add to your Gemfile
|
9
|
+
|
10
|
+
gem 'jossh'
|
11
|
+
|
12
|
+
Or install manually
|
13
|
+
|
14
|
+
gem install jossh
|
15
|
+
|
16
|
+
|
17
|
+
## Features
|
18
|
+
|
19
|
+
1. Allows running one or more commands over SSH.
|
20
|
+
2. Allows running external local scripts remotely
|
21
|
+
3. Has four commands: `ssh`, `ssh!`, `ssh_script` and `ssh_script!`. The 'bang' versions generate pretty and indented output.
|
22
|
+
4. Uses a single SSH connection.
|
23
|
+
5. Uses a simple hash for defining hosts.
|
24
|
+
6. Allows storing host specifications in a YAML file.
|
25
|
+
7. Supports all options available in `Net::SSH#start`.
|
26
|
+
8. Prints output Heroku-style.
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
### Example 1: Host specifications in a YAML file
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# example.rb
|
34
|
+
require 'jossh'
|
35
|
+
|
36
|
+
ssh! :localhost, ["cd /opt/app", "git pull"]
|
37
|
+
```
|
38
|
+
|
39
|
+
```yaml
|
40
|
+
# ssh_hosts.yml
|
41
|
+
:localhost:
|
42
|
+
:host: 'localhost'
|
43
|
+
:user: 'vagrant'
|
44
|
+
```
|
45
|
+
|
46
|
+
### Example 2: Host specifications directly in the code
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
# example.rb
|
50
|
+
require 'jossh'
|
51
|
+
|
52
|
+
localhost = {
|
53
|
+
host: 'localhost',
|
54
|
+
user: 'vagrant',
|
55
|
+
forward_agent: true,
|
56
|
+
}
|
57
|
+
|
58
|
+
ssh! localhost, ["cd /opt/app", "git pull"]
|
59
|
+
```
|
60
|
+
|
61
|
+
|
62
|
+
### Example 3: Run an external local script remotely
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
# example.rb
|
66
|
+
require 'jossh'
|
67
|
+
|
68
|
+
ssh_script! :production, deploy
|
69
|
+
```
|
70
|
+
|
71
|
+
```bash
|
72
|
+
# deploy
|
73
|
+
cd /opt/app
|
74
|
+
echo "-> Pulling source from origin"
|
75
|
+
git pull
|
76
|
+
echo "-> Restarting server"
|
77
|
+
touch 'tmp/restart.txt'
|
78
|
+
echo "-> Done"
|
79
|
+
```
|
80
|
+
|
81
|
+
See also: The examples folder
|
data/lib/jossh/api.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Jossh
|
2
|
+
|
3
|
+
def ssh_script!(hostspec, script)
|
4
|
+
CommandRunner.new.ssh_script! hostspec, script
|
5
|
+
end
|
6
|
+
|
7
|
+
def ssh_script(hostspec, script, callback: nil)
|
8
|
+
CommandRunner.new.ssh_script hostspec, script, callback: callback
|
9
|
+
end
|
10
|
+
|
11
|
+
def ssh!(hostspec, script)
|
12
|
+
CommandRunner.new.ssh! hostspec, script
|
13
|
+
end
|
14
|
+
|
15
|
+
def ssh(hostspec, script, callback: nil)
|
16
|
+
CommandRunner.new.ssh hostspec, script, callback: callback
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "net/ssh"
|
2
|
+
require "yaml"
|
3
|
+
|
4
|
+
module Jossh
|
5
|
+
|
6
|
+
class CommandRunner
|
7
|
+
|
8
|
+
def ssh!(hostspec, script)
|
9
|
+
ssh hostspec, script, callback: OutputHandler.new.method(:pretty_puts)
|
10
|
+
end
|
11
|
+
|
12
|
+
def ssh(hostspec, script, callback: nil)
|
13
|
+
callback ||= method :puts
|
14
|
+
script = script.join "\n" if script.is_a? Array
|
15
|
+
hostspec = load_spec(hostspec) if hostspec.is_a? Symbol
|
16
|
+
host = hostspec.delete :host
|
17
|
+
user = hostspec.delete :user
|
18
|
+
|
19
|
+
Net::SSH.start( host, user, hostspec ) do |ssh|
|
20
|
+
ssh.exec! script do |ch, stream, data|
|
21
|
+
callback.call data
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def ssh_script!(hostspec, script)
|
27
|
+
ssh! hostspec, load_script(script)
|
28
|
+
end
|
29
|
+
|
30
|
+
def ssh_script(hostspec, script, callback: nil)
|
31
|
+
ssh hostspec, load_script(script), callback: callback
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def load_spec(key)
|
37
|
+
ssh_hosts[key] or abort "Cannot find :#{key} in #{hostfile}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def ssh_hosts
|
41
|
+
@ssh_hosts ||= load_ssh_hosts
|
42
|
+
end
|
43
|
+
|
44
|
+
def load_ssh_hosts
|
45
|
+
File.exist? hostfile or abort "Cannot find #{hostfile}"
|
46
|
+
YAML.load_file hostfile
|
47
|
+
end
|
48
|
+
|
49
|
+
def hostfile
|
50
|
+
@hostfile ||= 'ssh_hosts.yml'
|
51
|
+
end
|
52
|
+
|
53
|
+
def hostfile=(file)
|
54
|
+
@hostfile = file
|
55
|
+
end
|
56
|
+
|
57
|
+
def load_script(script)
|
58
|
+
File.read script
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'colsole'
|
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
|
+
module Jossh
|
7
|
+
|
8
|
+
class OutputHandler
|
9
|
+
|
10
|
+
include Colsole
|
11
|
+
|
12
|
+
def pretty_puts(lines)
|
13
|
+
lines = lines.split("\n") if lines.is_a? String
|
14
|
+
lines.each do |line|
|
15
|
+
if line =~ /^\-+> (.*?)$/
|
16
|
+
puts_status $1
|
17
|
+
elsif line =~ /^! (.*?)$/
|
18
|
+
puts_error $1
|
19
|
+
elsif line =~ /^\$ (.*?)$/
|
20
|
+
puts_command $1
|
21
|
+
else
|
22
|
+
puts_stdout line
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def puts_status(msg)
|
30
|
+
say "!txtgrn!----->!txtrst! #{msg}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def puts_error(msg)
|
34
|
+
say " !txtylw!!!txtrst! !txtred!#{msg}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def puts_stderr(msg)
|
38
|
+
say " !txtred!#{msg}"
|
39
|
+
end
|
40
|
+
|
41
|
+
def puts_command(msg)
|
42
|
+
say " !txtpur!$ #{msg}"
|
43
|
+
end
|
44
|
+
|
45
|
+
def puts_stdout(msg)
|
46
|
+
say " #{msg}"
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
data/lib/jossh.rb
ADDED
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jossh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Danny Ben Shitrit
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colsole
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: net-ssh
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.8'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.8'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-reporters
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.1'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: run-gem-dev
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.2'
|
83
|
+
description: Jossh is a wrapper around Ruby Net::SSH with a simpler interface and
|
84
|
+
prettier output
|
85
|
+
email: db@dannyben.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- README.md
|
91
|
+
- lib/jossh.rb
|
92
|
+
- lib/jossh/api.rb
|
93
|
+
- lib/jossh/command_runner.rb
|
94
|
+
- lib/jossh/output_handler.rb
|
95
|
+
- lib/jossh/version.rb
|
96
|
+
homepage: https://github.com/DannyBen/jossh
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.4.6
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Easier and Prettier SSH for Ruby
|
120
|
+
test_files: []
|