shexy 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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ #
9
+ gem "net-ssh"
10
+ gem "net-scp"
11
+
12
+ group :development do
13
+ gem "shoulda", ">= 0"
14
+ gem "rdoc", ">= 3.12"
15
+ gem "bundler", ">= 1.0.0"
16
+ gem "jeweler", ">= 1.8.4"
17
+ gem "simplecov", ">= 0"
18
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Sergio Rubio
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ # Shexy
2
+
3
+ SSH, the way I like it.
4
+
5
+ ## Do it
6
+
7
+ require 'shexy'
8
+
9
+ Shexy.user = 'test'
10
+ Shexy.password = 'test'
11
+ Shexy.host = 'test-host'
12
+ out, err = Shexy.exe 'ls -la'
13
+ out.empty? # no output
14
+ err.empty? # no error output
15
+
16
+ Shexy.exe 'la -la' do |out,err|
17
+ puts out
18
+ puts err
19
+ end
20
+
21
+ Another way, assuming you are using SSH keys
22
+ and added them to the SSH agent (ssh-add ~/.ssh/my-priv-key):
23
+
24
+ Shexy.exe 'test@test-host', 'ls -la' do |out, err|
25
+ puts out
26
+ end
27
+ Shexy.exe 'echo hello' # no need to add host/user again
28
+
29
+ Copying files (local -> remote):
30
+
31
+ Shexy.copy_to 'test@test-host', '/home/rubiojr/my-uber-file', '/tmp/'
32
+
33
+ ## Caution
34
+
35
+ I was bored writing net-ssh boilerplate, so I created this highly
36
+ experimental sh*t (a small amount of it, but experimental).
37
+
38
+ ## Copyright
39
+
40
+ Copyright (c) 2012 Sergio Rubio. See LICENSE.txt for
41
+ further details.
42
+
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ require './lib/shexy.rb'
6
+
7
+ begin
8
+ Bundler.setup(:default, :development)
9
+ rescue Bundler::BundlerError => e
10
+ $stderr.puts e.message
11
+ $stderr.puts "Run `bundle install` to install missing gems"
12
+ exit e.status_code
13
+ end
14
+ require 'rake'
15
+
16
+ require 'jeweler'
17
+ Jeweler::Tasks.new do |gem|
18
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
19
+ gem.version = Shexy::VERSION
20
+ gem.name = "shexy"
21
+ gem.homepage = "http://github.com/rubiojr/shexy"
22
+ gem.license = "MIT"
23
+ gem.summary = %Q{SSH, the way I like it}
24
+ gem.description = %Q{[extremely] thin wrapper around net-ssh and net-scp}
25
+ gem.email = "rubiojr@frameos.org"
26
+ gem.authors = ["Sergio Rubio"]
27
+ # dependencies defined in Gemfile
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs << 'lib' << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+
38
+ task :default => :build
39
+
40
+ require 'rdoc/task'
41
+ Rake::RDocTask.new do |rdoc|
42
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "shexy #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
data/lib/shexy.rb ADDED
@@ -0,0 +1,154 @@
1
+ require 'net/ssh'
2
+ require 'net/scp'
3
+
4
+ #
5
+ # Execute commands in remote servers (SSH)
6
+ #
7
+ # require 'shexy'
8
+ #
9
+ # Shexy.user = 'test'
10
+ # Shexy.password = 'test'
11
+ # Shexy.host = 'test-host'
12
+ # out, err = Shexy.exe 'ls -la'
13
+ # out.empty? # no output
14
+ # err.empty? # no error output
15
+ #
16
+ # Shexy.exe 'la -la' do |out,err|
17
+ # puts out
18
+ # puts err
19
+ # end
20
+ #
21
+ # Another way, assuming you are using SSH keys
22
+ # and added them to the SSH agent (ssh-add ~/.ssh/my-priv-key):
23
+ #
24
+ # Shexy.exe 'test@test-host', 'ls -la' do |out, err|
25
+ # puts out
26
+ # end
27
+ # Shexy.exe 'echo hello' # no need to add host/user again
28
+ #
29
+ # Copying files (local -> remote):
30
+ #
31
+ # Shexy.copy_to 'test@test-host', '/home/rubiojr/my-uber-file', '/tmp/'
32
+ #
33
+ module Shexy
34
+
35
+ VERSION = '0.1'
36
+
37
+ @flags = {}
38
+
39
+ def self.password=(password); flags[:password] = password; end
40
+ def self.password; flags[:password]; end
41
+ def self.key=(key); flags[:keys] = [File.expand_path(key)]; end
42
+ def self.key; flags[:keys]; end
43
+ def self.use_sudo; @sudo = true; end
44
+ def self.sudo?; @sudo ||= false; end
45
+
46
+ class << self
47
+ attr_accessor :host, :user, :flags
48
+ attr_reader :cmd
49
+ end
50
+
51
+ def self.exe(*args)
52
+ args.flatten!
53
+ if args.size > 1
54
+ @host = args[0]
55
+ if @host =~ /@/
56
+ @user, @host = @host.split '@'
57
+ end
58
+ @cmd = args[1]
59
+ else
60
+ @cmd = args[0]
61
+ end
62
+ Net::SSH.start(host, user, flags) do |sh|
63
+ sh.open_channel do |ch|
64
+ if sudo?
65
+ ch.request_pty
66
+ @cmd = "sudo #{@cmd}"
67
+ end
68
+ ch.exec cmd do
69
+ # FIXME: I don't think it's a good idea
70
+ # to implement access to stdout,stderr this way
71
+ stdout = ""
72
+ stderr = ""
73
+ ch.on_extended_data do |c2, type, data|
74
+ # ERROR output here
75
+ stderr << data
76
+ yield stdout, stderr if block_given?
77
+ end
78
+ ch.on_data do |c2, data|
79
+ stdout << data
80
+ yield stdout, stderr if block_given?
81
+ end
82
+ ch.on_close do |c2|
83
+ return stdout, stderr
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ #
91
+ # Shexy.copy_to 'root@foobar.com', 'source_file', 'dest_file'
92
+ #
93
+ # or
94
+ #
95
+ # Shexy.host = 'foobar.com'
96
+ # Shexy.user = 'root'
97
+ # Shexy.copy_to 'source_file', 'dest_file'
98
+ #
99
+ def self.copy_to(*args)
100
+ if args.size > 2
101
+ # First arg assumed to be foo@host.net
102
+ @host = args[0]
103
+ if @host =~ /@/
104
+ @user, @host = @host.split '@'
105
+ end
106
+ from = args[1]
107
+ to = args[2]
108
+ else
109
+ # user, host already set via Shexy.host and
110
+ # Shexy.user
111
+ from = args[0]
112
+ to = args[1]
113
+ end
114
+ from = File.expand_path from
115
+ Net::SCP.start(host, user, flags) do |scp|
116
+ scp.upload! from, to
117
+ end
118
+ end
119
+
120
+ #
121
+ # Detect distro version
122
+ #
123
+ def self.distro
124
+ issue.match(/fedora|centos|frameos|debian|ubuntu|scientific linux/i)[0].gsub(" ", "_").downcase.to_sym
125
+ end
126
+
127
+ #
128
+ # Detect distro release
129
+ #
130
+ def self.distro_release
131
+ case distro
132
+ when :redhat,:centos,:frameos
133
+ issue.split[2]
134
+ when :fedora
135
+ issue.split[2]
136
+ when :ubuntu
137
+ issue.split[1]
138
+ when :debian
139
+ issue.split[2]
140
+ else
141
+ '0'
142
+ end
143
+ end
144
+
145
+ #
146
+ # Lame but short, and it works (most of the times)
147
+ #
148
+ def self.issue
149
+ issue, err = ((exe 'cat /etc/issue')[0].strip.chomp || Shexy.exe('lsb_release -i')[0].strip.chomp).lines.first
150
+ raise Exception.new "Error reading release info." unless (err.nil? or err.empty?)
151
+ issue
152
+ end
153
+
154
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'shexy'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestShexy < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shexy
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sergio Rubio
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: net-ssh
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
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: net-scp
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: shoulda
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rdoc
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '3.12'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '3.12'
78
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 1.0.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 1.0.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: jeweler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 1.8.4
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: 1.8.4
110
+ - !ruby/object:Gem::Dependency
111
+ name: simplecov
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: ! '[extremely] thin wrapper around net-ssh and net-scp'
127
+ email: rubiojr@frameos.org
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files:
131
+ - LICENSE.txt
132
+ - README.rdoc
133
+ files:
134
+ - .document
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.rdoc
138
+ - Rakefile
139
+ - lib/shexy.rb
140
+ - test/helper.rb
141
+ - test/test_shexy.rb
142
+ homepage: http://github.com/rubiojr/shexy
143
+ licenses:
144
+ - MIT
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ segments:
156
+ - 0
157
+ hash: 518133985048816115
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ none: false
160
+ requirements:
161
+ - - ! '>='
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 1.8.24
167
+ signing_key:
168
+ specification_version: 3
169
+ summary: SSH, the way I like it
170
+ test_files: []