sshkit_addon 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/addon/cap_base.rb +33 -0
- data/lib/addon/shell_command_constructor.rb +16 -0
- data/lib/addon/sshkit_monkey_patch.rb +31 -0
- data/lib/addon/sshkit_my_formatter.rb +58 -0
- data/lib/sshkit_addon.rb +9 -0
- data/readme.md +29 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 646bed166f7237a05479be5c16db53ef4b7d4544
|
4
|
+
data.tar.gz: a2247967ece7259e48510e37c3fdc4dfc6b71cc2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aa77e0c45d9303a9d2a61819371c2bf7c7631f37c505f0af345d7d4ed63657651c1395745327de3664418de56408f99adb73fff04c9bf5048cb46ba24b05885b
|
7
|
+
data.tar.gz: 8175e2d4c4e13689efad3a605f06dd1eb49e0cbb4c31727a9b86b18919da46da5e7c898cabe6ffc52dc5b9de8a26632f37ba66ebbd55b95e160852d00d8a4879
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "sshkit"
|
2
|
+
require "sshkit/dsl"
|
3
|
+
include SSHKit::DSL
|
4
|
+
|
5
|
+
require File.dirname(__FILE__) + "/sshkit_monkey_patch"
|
6
|
+
|
7
|
+
class CapBase
|
8
|
+
attr_reader :servers, :servers_by_hash
|
9
|
+
def initialize server_hash
|
10
|
+
@servers = []
|
11
|
+
@servers_by_hash = {}
|
12
|
+
|
13
|
+
server_hash.each do |name, hash|
|
14
|
+
args = [:host,:user,:port,:password].each_with_object({}) do |key, h|
|
15
|
+
if key == :host
|
16
|
+
h[:hostname] = hash[key]
|
17
|
+
else
|
18
|
+
h[key] = hash[key] if hash.has_key? key
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# host = SSHKit::Host.new :hostname => hash[:host], :user => hash[:user], :password => hash[:password]
|
23
|
+
host = SSHKit::Host.new args
|
24
|
+
host.key = hash[:key] if hash.has_key? :key #use ssh private key file
|
25
|
+
|
26
|
+
host.properties.options = hash
|
27
|
+
|
28
|
+
@servers.push host
|
29
|
+
|
30
|
+
@servers_by_hash[name] = host
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ShellCommandConstructor
|
2
|
+
def self.construct_command strings_or_list, connector = " && "
|
3
|
+
list = case strings_or_list
|
4
|
+
when Array
|
5
|
+
strings_or_list
|
6
|
+
when String
|
7
|
+
strings_or_list.split(/\n/)
|
8
|
+
end
|
9
|
+
list.each_with_object([]) do |line, obj|
|
10
|
+
line.strip!
|
11
|
+
next if line.empty?
|
12
|
+
next if line =~ /^#/
|
13
|
+
obj.push line
|
14
|
+
end.join connector
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#take out the support of ssh gateway as the upload/download is not really supported
|
2
|
+
require File.dirname(__FILE__) + "/sshkit_my_formatter"
|
3
|
+
SSHKit.config.use_format :myformatter
|
4
|
+
|
5
|
+
module SSHKit; module Backend; class Netssh
|
6
|
+
def put strings, remote, options = {}
|
7
|
+
local = StringIO.new strings
|
8
|
+
upload! local, remote, options
|
9
|
+
end
|
10
|
+
|
11
|
+
def su_run user, cmd
|
12
|
+
execute %Q(su - #{user} -c "#{cmd}")
|
13
|
+
end
|
14
|
+
alias_method :su_execute, :su_run
|
15
|
+
|
16
|
+
def sudo cmd
|
17
|
+
SSHKit::Backend::Netssh.config.pty = true
|
18
|
+
# p host
|
19
|
+
password = host.password
|
20
|
+
execute %Q( echo #{password} | sudo -kS bash -c "#{cmd}" )
|
21
|
+
|
22
|
+
# execute %Q(sudo bash -c "#{cmd}"), interaction_handler: {
|
23
|
+
# /\(yes\/no\)/i => "yes\n",
|
24
|
+
# /[sudo] password/i => "#{password}\n",
|
25
|
+
# }
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end;end;end
|
30
|
+
|
31
|
+
SSHKit.config.output_verbosity = SSHKit::Logger::DEBUG
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module SSHKit
|
2
|
+
module Formatter
|
3
|
+
class MyFormatter < Abstract
|
4
|
+
LEVEL_NAMES = %w{ DEBUG INFO WARN ERROR FATAL }.freeze
|
5
|
+
LEVEL_COLORS = [:black, :blue, :yellow, :red, :red].freeze
|
6
|
+
|
7
|
+
def write(obj)
|
8
|
+
if obj.kind_of?(SSHKit::LogMessage)
|
9
|
+
write_message(obj.verbosity, obj.to_s)
|
10
|
+
else
|
11
|
+
raise "write only supports formatting SSHKit::LogMessage, called with #{obj.class}: #{obj.inspect}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def log_command_start(command)
|
16
|
+
host_prefix = command.host.user ? "as #{colorize(command.host.user, :blue)}@" : 'on '
|
17
|
+
message = "Running #{colorize(command, :yellow, :bold)} #{host_prefix}#{colorize(command.host, :blue)}"
|
18
|
+
write_message(command.verbosity, message, command.uuid)
|
19
|
+
write_message(Logger::DEBUG, "Command: #{colorize(command.to_command, :blue)}", command.uuid)
|
20
|
+
end
|
21
|
+
|
22
|
+
def log_command_data(command, stream_type, stream_data)
|
23
|
+
color = \
|
24
|
+
case stream_type
|
25
|
+
when :stdout then :green
|
26
|
+
when :stderr then :red
|
27
|
+
else raise "Unrecognised stream_type #{stream_type}, expected :stdout or :stderr"
|
28
|
+
end
|
29
|
+
uuid = "#{command.host}: (#{command.uuid})"
|
30
|
+
write_message(Logger::DEBUG, colorize("\t#{stream_data}".chomp, color), uuid)
|
31
|
+
end
|
32
|
+
|
33
|
+
def log_command_exit(command)
|
34
|
+
runtime = sprintf('%5.3f seconds', command.runtime)
|
35
|
+
successful_or_failed = command.failure? ? colorize('failed', :red, :bold) : colorize('successful', :green, :bold)
|
36
|
+
message = "Finished in #{runtime} with exit status #{command.exit_status} (#{successful_or_failed})."
|
37
|
+
write_message(command.verbosity, message, command.uuid)
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def format_message(verbosity, message, uuid=nil)
|
43
|
+
message = "[#{colorize(uuid, :green)}] #{message}" unless uuid.nil?
|
44
|
+
level = colorize(LEVEL_NAMES[verbosity].rjust(6), LEVEL_COLORS[verbosity])
|
45
|
+
"#{level} #{message}"
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def write_message(verbosity, message, uuid=nil)
|
51
|
+
original_output << "#{format_message(verbosity, message, uuid)}\n" if verbosity >= SSHKit.config.output_verbosity
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
data/lib/sshkit_addon.rb
ADDED
data/readme.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# An addon for sshkit
|
2
|
+
|
3
|
+
define the sshkit host with hash structure. then define tasks by choosing the host
|
4
|
+
```
|
5
|
+
cap3 = CapBase.new({
|
6
|
+
:core1 => { :host => "192.168.5.21", :user => "core", :key => %q(mykey.openssh) },
|
7
|
+
:core2 => { :host => "192.168.5.22", :user => "core", :password => 'xxxxx' },
|
8
|
+
:core3 => { :host => "192.168.5.22", :user => "core", :password => 'xxxxx' },
|
9
|
+
})
|
10
|
+
servers = cap3.servers
|
11
|
+
|
12
|
+
desc "run command"
|
13
|
+
task :run_command, [:cmd] do |t, args|
|
14
|
+
cmd = args.cmd
|
15
|
+
on servers do |host|
|
16
|
+
execute cmd
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "run command on a specific host"
|
21
|
+
task :run_command_on, [:host_key, :cmd] do |t, args|
|
22
|
+
cmd = args.cmd
|
23
|
+
on cap3.servers_by_hash[args.host_key] do |host|
|
24
|
+
execute cmd
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
```
|
29
|
+
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sshkit_addon
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Zhimin Wen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2010-04-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sshkit
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
description: Addon of the sshkit
|
28
|
+
email: zhimin.wen@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/addon/cap_base.rb
|
34
|
+
- lib/addon/shell_command_constructor.rb
|
35
|
+
- lib/addon/sshkit_monkey_patch.rb
|
36
|
+
- lib/addon/sshkit_my_formatter.rb
|
37
|
+
- lib/sshkit_addon.rb
|
38
|
+
- readme.md
|
39
|
+
homepage: http://github.com/zhiminwen/sshkit_addon
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.6.11
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: sshkit addon
|
63
|
+
test_files: []
|