relay 0.0.3 → 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.
- data/LICENSE +1 -1
- data/README.markdown +18 -18
- data/Rakefile +3 -1
- data/bin/relay +59 -16
- data/lib/relay.rb +22 -35
- data/relay.gemspec +3 -3
- data/test/commands.rb +1 -1
- data/test/relay_test.rb +8 -23
- data/test/test_helper.rb +14 -23
- metadata +24 -12
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c)
|
1
|
+
Copyright (c) 2009, 2010 Michel Martens and Damian Janowski
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
4
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.markdown
CHANGED
@@ -10,13 +10,22 @@ Relay is a simple library that allows you to send commands over SSH.
|
|
10
10
|
It uses your own SSH, not a Ruby version, so you can profit from your
|
11
11
|
settings and public/private keys.
|
12
12
|
|
13
|
+
The following options are available:
|
14
|
+
|
15
|
+
-c command
|
16
|
+
Executes the command on the passed servers.
|
17
|
+
|
18
|
+
-f recipe
|
19
|
+
Reads commands from a file and executes them in the passed servers.
|
20
|
+
|
13
21
|
Usage
|
14
22
|
-----
|
15
23
|
|
16
|
-
To send a command to
|
24
|
+
To send a command to two servers called `server1` and `server2`:
|
17
25
|
|
18
|
-
$ relay
|
19
|
-
$ relay
|
26
|
+
$ relay -c "ls -al" server1 server2
|
27
|
+
$ relay -c "cd foo; ls" server1 server2
|
28
|
+
$ relay -c "cd foo" -c "ls" server1 server2
|
20
29
|
|
21
30
|
If you want to send more commands, you can write a shell script:
|
22
31
|
|
@@ -25,33 +34,24 @@ If you want to send more commands, you can write a shell script:
|
|
25
34
|
ls
|
26
35
|
mkdir -p bar/baz
|
27
36
|
|
28
|
-
$ relay recipe.sh
|
29
|
-
|
30
|
-
It will execute those commands on `myserver` and show the output.
|
31
|
-
|
32
|
-
This last form accepts one file as the recipe and one or many servers:
|
33
|
-
|
34
|
-
$ relay recipe.sh server1 server2 server3
|
37
|
+
$ relay -f recipe.sh server1 server2
|
35
38
|
|
36
|
-
|
39
|
+
It will execute those commands on both servers and show the output.
|
37
40
|
|
38
|
-
$ relay identify myserver
|
39
41
|
|
40
|
-
|
42
|
+
You can use each flag many times, so this is possible:
|
41
43
|
|
42
|
-
|
43
|
-
>> Relay.execute "echo foo", "myserver"
|
44
|
-
=> ["foo\n"]
|
44
|
+
$ relay -f recipe1.sh -f recipe2.sh server1 server2 server3
|
45
45
|
|
46
46
|
Installation
|
47
47
|
------------
|
48
48
|
|
49
|
-
$
|
49
|
+
$ gem install relay
|
50
50
|
|
51
51
|
License
|
52
52
|
-------
|
53
53
|
|
54
|
-
Copyright (c) 2009 Michel Martens and Damian Janowski
|
54
|
+
Copyright (c) 2009, 2010 Michel Martens and Damian Janowski
|
55
55
|
|
56
56
|
Permission is hereby granted, free of charge, to any person
|
57
57
|
obtaining a copy of this software and associated documentation
|
data/Rakefile
CHANGED
data/bin/relay
CHANGED
@@ -1,21 +1,64 @@
|
|
1
|
-
#! /usr/bin/env ruby
|
1
|
+
#! /usr/bin/env ruby
|
2
2
|
|
3
|
-
|
3
|
+
help = <<-EOS
|
4
|
+
RELAY(1)
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
NAME
|
7
|
+
relay -- Relay commands over SSH.
|
8
|
+
|
9
|
+
SYNOPSIS
|
10
|
+
relay [-c command] [-f recipe] server1 [server2 ...]
|
11
|
+
|
12
|
+
DESCRIPTION
|
13
|
+
Relay is a simple library that allows you to send commands over SSH. It uses
|
14
|
+
your own SSH, not a Ruby version, so you can profit from your settings and
|
15
|
+
public/private keys.
|
16
|
+
|
17
|
+
The following options are available:
|
18
|
+
|
19
|
+
-c command
|
20
|
+
Executes the command on the passed servers.
|
21
|
+
|
22
|
+
-f recipe
|
23
|
+
Reads commands from a file and executes them in the passed servers.
|
24
|
+
|
25
|
+
USAGE
|
26
|
+
To send a command to two servers called `server1` and `server2`:
|
27
|
+
|
28
|
+
$ relay -c "ls -al" server1 server2
|
29
|
+
$ relay -c "cd foo; ls" server1 server2
|
30
|
+
$ relay -c "cd foo" -c "ls" server1 server2
|
31
|
+
|
32
|
+
If you want to send more commands, you can write a shell script:
|
33
|
+
|
34
|
+
$ cat recipe.sh
|
35
|
+
cd foo
|
36
|
+
ls
|
37
|
+
mkdir -p bar/baz
|
11
38
|
|
12
|
-
|
13
|
-
if ARGV[0] && File.exists?(ARGV[0])
|
14
|
-
file = ARGV.shift
|
39
|
+
$ relay -f recipe.sh server1 server2
|
15
40
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
41
|
+
It will execute those commands on both servers and show the output.
|
42
|
+
|
43
|
+
|
44
|
+
You can use each flag many times, so this is possible:
|
45
|
+
|
46
|
+
$ relay -f recipe1.sh -f recipe2.sh server1 server2 server3
|
47
|
+
EOS
|
48
|
+
|
49
|
+
if ARGV.empty?
|
50
|
+
puts help
|
51
|
+
exit
|
21
52
|
end
|
53
|
+
|
54
|
+
require "clap"
|
55
|
+
|
56
|
+
require File.expand_path("../lib/relay", File.dirname(__FILE__))
|
57
|
+
|
58
|
+
relay = Relay.new
|
59
|
+
|
60
|
+
servers = Clap.run ARGV,
|
61
|
+
"-f" => relay.method(:recipe),
|
62
|
+
"-c" => relay.method(:command)
|
63
|
+
|
64
|
+
relay.run(servers)
|
data/lib/relay.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
#! /usr/bin/env ruby
|
2
2
|
|
3
|
-
require "thor"
|
4
3
|
require "open3"
|
5
4
|
|
6
5
|
class Ssh
|
@@ -42,47 +41,35 @@ class Ssh
|
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
45
|
-
class Relay
|
44
|
+
class Relay
|
45
|
+
VERSION = "0.1.0"
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
# @example
|
51
|
-
#
|
52
|
-
# >> require "relay"
|
53
|
-
# >> Relay.execute "echo foo", "myserver"
|
54
|
-
# => ["foo\n"]
|
55
|
-
def self.execute(*args)
|
56
|
-
start args.unshift("execute").push("--quiet")
|
47
|
+
def initialize
|
48
|
+
@recipes = []
|
49
|
+
@commands = []
|
57
50
|
end
|
58
51
|
|
59
|
-
|
60
|
-
|
61
|
-
method_option :path, :type => :string, :aliases => "-p"
|
62
|
-
def identify(server)
|
63
|
-
path = options[:path] || "~/.ssh/authorized_keys"
|
64
|
-
keys = [options[:key], "~/.ssh/id_rsa.pub", "~/.ssh/id_dsa.pub"].compact
|
65
|
-
key = keys.find { |k| File.exists?(File.expand_path(k)) }
|
66
|
-
|
67
|
-
if system %Q{cat #{key} | ssh #{server} "cat >> #{path}"}
|
68
|
-
say_status :copied, "#{key} to #{server}:#{path}"
|
69
|
-
end
|
52
|
+
def recipe(recipe)
|
53
|
+
@recipes << recipe
|
70
54
|
end
|
71
55
|
|
72
|
-
|
73
|
-
|
74
|
-
Ssh.new(server).start do |session|
|
75
|
-
File.readlines(recipe).each do |command|
|
76
|
-
session.run(command)
|
77
|
-
end
|
78
|
-
end
|
56
|
+
def command(command)
|
57
|
+
@commands << command
|
79
58
|
end
|
80
59
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
60
|
+
def run(servers)
|
61
|
+
servers.each do |server|
|
62
|
+
Ssh.new(server).start do |session|
|
63
|
+
@commands.each do |command|
|
64
|
+
session.run(command)
|
65
|
+
end
|
66
|
+
|
67
|
+
@recipes.each do |recipe|
|
68
|
+
File.readlines(recipe).each do |command|
|
69
|
+
session.run(command)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
86
73
|
end
|
87
74
|
end
|
88
75
|
end
|
data/relay.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "relay"
|
3
|
-
s.version = "0.0
|
3
|
+
s.version = "0.1.0"
|
4
4
|
s.summary = "Relay commands over SSH"
|
5
5
|
s.description = "Relay allows you to execute remote commands via SSH with ease."
|
6
6
|
s.authors = ["Damian Janowski", "Michel Martens"]
|
@@ -9,9 +9,9 @@ Gem::Specification.new do |s|
|
|
9
9
|
|
10
10
|
s.rubyforge_project = "relay"
|
11
11
|
|
12
|
-
s.executables
|
12
|
+
s.executables.push("relay")
|
13
13
|
|
14
|
-
s.add_dependency("
|
14
|
+
s.add_dependency("clap")
|
15
15
|
|
16
16
|
s.files = ["LICENSE", "README.markdown", "Rakefile", "bin/relay", "lib/relay.rb", "relay.gemspec", "test/commands.rb", "test/relay_test.rb", "test/test_helper.rb"]
|
17
17
|
end
|
data/test/commands.rb
CHANGED
data/test/relay_test.rb
CHANGED
@@ -1,28 +1,18 @@
|
|
1
|
-
require File.
|
1
|
+
require File.expand_path("test_helper", File.dirname(__FILE__))
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
Dir.chdir(root("test", "tmp")) do
|
7
|
-
out, err = relay("identify localhost --path #{root("test", "tmp", "authorized_keys")}")
|
8
|
-
|
9
|
-
assert_match /copied/, out
|
10
|
-
assert File.exists?("authorized_keys")
|
11
|
-
assert File.read("authorized_keys")[/^ssh-/]
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
should "send a command to SERVER" do
|
3
|
+
scope do
|
4
|
+
scope do
|
5
|
+
test "send a command to SERVER" do
|
16
6
|
Dir.chdir(root("test", "tmp")) do
|
17
7
|
FileUtils.touch("foobar")
|
18
8
|
|
19
|
-
out, err = relay("
|
9
|
+
out, err = relay("-c \"ls #{root("test", "tmp")}\" localhost")
|
20
10
|
|
21
|
-
|
11
|
+
assert out[/foobar/]
|
22
12
|
end
|
23
13
|
end
|
24
14
|
|
25
|
-
|
15
|
+
test "relay a recipe of commands to SERVER" do
|
26
16
|
Dir.chdir(root("test", "tmp")) do
|
27
17
|
File.open("recipe.sh", "w") do |file|
|
28
18
|
file.puts "cd #{root("test", "tmp")}"
|
@@ -30,7 +20,7 @@ class TestRelay < Test::Unit::TestCase
|
|
30
20
|
file.puts "cat list"
|
31
21
|
end
|
32
22
|
|
33
|
-
out, err = relay("recipe.sh localhost")
|
23
|
+
out, err = relay("-f recipe.sh localhost")
|
34
24
|
|
35
25
|
assert out["$ cd #{root("test", "tmp")}"]
|
36
26
|
assert out["$ ls -al > list"]
|
@@ -38,10 +28,5 @@ class TestRelay < Test::Unit::TestCase
|
|
38
28
|
assert out["recipe.sh"]
|
39
29
|
end
|
40
30
|
end
|
41
|
-
|
42
|
-
should "return the output when used programmatically" do
|
43
|
-
assert_equal ["foo\n"], Relay.execute("echo foo", "localhost")
|
44
|
-
assert_equal ["foo\n", "bar\n", "baz\n"], Relay.execute("echo foo; echo bar; echo baz", "localhost")
|
45
|
-
end
|
46
31
|
end
|
47
32
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,34 +1,25 @@
|
|
1
1
|
require "rubygems"
|
2
|
-
require "contest"
|
3
2
|
require "fileutils"
|
4
|
-
require File.join(File.dirname(__FILE__), "..", "lib", "relay")
|
5
3
|
|
6
|
-
|
7
|
-
|
8
|
-
$:.unshift ROOT
|
4
|
+
require File.expand_path("../lib/relay", File.dirname(__FILE__))
|
5
|
+
require File.expand_path("commands", File.dirname(__FILE__))
|
9
6
|
|
10
|
-
|
7
|
+
include Commands
|
11
8
|
|
12
|
-
|
13
|
-
include Test::Commands
|
9
|
+
ROOT = File.expand_path(File.join(File.dirname(__FILE__), ".."))
|
14
10
|
|
15
|
-
|
16
|
-
File.join(ROOT, *args)
|
17
|
-
end
|
11
|
+
$:.unshift ROOT
|
18
12
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
13
|
+
def root(*args)
|
14
|
+
File.join(ROOT, *args)
|
15
|
+
end
|
24
16
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
17
|
+
prepare do
|
18
|
+
Dir[root("test", "tmp", "*")].each do |file|
|
19
|
+
FileUtils.rm(file)
|
29
20
|
end
|
21
|
+
end
|
30
22
|
|
31
|
-
|
32
|
-
|
33
|
-
end
|
23
|
+
def relay(args = nil)
|
24
|
+
sh("ruby -rubygems #{root "bin/relay"} #{args}")
|
34
25
|
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: relay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Damian Janowski
|
@@ -10,19 +15,22 @@ autorequire:
|
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date:
|
18
|
+
date: 2010-11-06 00:00:00 -03:00
|
14
19
|
default_executable:
|
15
20
|
dependencies:
|
16
21
|
- !ruby/object:Gem::Dependency
|
17
|
-
name:
|
18
|
-
|
19
|
-
|
20
|
-
|
22
|
+
name: clap
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
21
26
|
requirements:
|
22
|
-
- -
|
27
|
+
- - ">="
|
23
28
|
- !ruby/object:Gem::Version
|
24
|
-
|
25
|
-
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
26
34
|
description: Relay allows you to execute remote commands via SSH with ease.
|
27
35
|
email:
|
28
36
|
- djanowski@dimaion.com
|
@@ -53,21 +61,25 @@ rdoc_options: []
|
|
53
61
|
require_paths:
|
54
62
|
- lib
|
55
63
|
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
56
65
|
requirements:
|
57
66
|
- - ">="
|
58
67
|
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 0
|
59
70
|
version: "0"
|
60
|
-
version:
|
61
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
62
73
|
requirements:
|
63
74
|
- - ">="
|
64
75
|
- !ruby/object:Gem::Version
|
76
|
+
segments:
|
77
|
+
- 0
|
65
78
|
version: "0"
|
66
|
-
version:
|
67
79
|
requirements: []
|
68
80
|
|
69
81
|
rubyforge_project: relay
|
70
|
-
rubygems_version: 1.3.
|
82
|
+
rubygems_version: 1.3.7
|
71
83
|
signing_key:
|
72
84
|
specification_version: 3
|
73
85
|
summary: Relay commands over SSH
|