Class: Mybot::Node
- Inherits:
-
Object
show all
- Includes:
- Fmt
- Defined in:
- lib/mybot/node.rb
Constant Summary
Constants included
from Fmt
Fmt::WIDTH
Instance Method Summary
collapse
Methods included from Fmt
#asterisks, #colored, #print_cmd, #print_cmd!, #print_progress, #print_stderr, #print_stdout, #spaces
Constructor Details
#initialize(host, user, options = {}) ⇒ Node
Returns a new instance of Node.
8
9
10
|
# File 'lib/mybot/node.rb', line 8
def initialize(host, user, options = {})
@host, @user, @options = host, user, options
end
|
Instance Method Details
#download(from, to, options = {}) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/mybot/node.rb', line 88
def download(from, to, options = {})
print_cmd! "download", "#{from} -> #{to}", :green, :bold
sftp.download!(from, to) do |event, uploader, *args|
case event
when :get
size = 0
if args[0].size
size = args[0].size
else
size = sftp.stat!(from).size
end
n = (args[1].to_f * 100 / size.to_f).to_i
print_progress(n)
when :finish
print_progress(100)
end
end
end
|
#exists?(file) ⇒ Boolean
67
68
69
70
71
72
73
|
# File 'lib/mybot/node.rb', line 67
def exists?(file)
sftp.stat!(file) do |resp|
return resp.ok?
end
rescue Net::SFTP::StatusException
false
end
|
#run(cmd, options = {}) {|command| ... } ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/mybot/node.rb', line 12
def run(cmd, options = {})
command = Command.new
yield command if block_given?
ssh.open_channel do |ch|
ch.request_pty do |ch, ok|
abort "cannot request pty" unless ok
end
command.channel = ch
options[:env] ||= {}
options[:sudo] ||= false
options[:cwd] ||= ""
unless options[:env].empty?
values = options[:env].map { |k, v| "#{k}='#{v}'" }.join " "
cmd = "#{values} #{cmd}"
end
if options[:sudo]
cmd = "sudo #{cmd}"
end
if options[:cwd] != ""
cmd = "cd #{options[:cwd]} && #{cmd}"
end
print_cmd! "run", cmd, :green, :bold
ch.exec cmd do |ch, ok|
abort "cannot exec command" unless ok
ch.on_data do |ch, data|
command.handle_stdout data
end
ch.on_extended_data do |ch, type, data|
command.handle_stderr data if type == 1
end
ch.on_request("exit-status") do |ch, data|
command.exit = data.read_long
end
ch.on_close do |ch|
command.handle_close
end
end
end
ssh.loop
return command.result
end
|
#upload(from, to, options = {}) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/mybot/node.rb', line 75
def upload(from, to, options = {})
print_cmd! "upload", "#{from} -> #{to}", :green, :bold
sftp.upload!(from, to) do |event, uploader, *args|
case event
when :put
n = (args[1].to_f * 100 / args[0].size.to_f).to_i
print_progress(n)
when :finish
print_progress(100)
end
end
end
|