rest_connection 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +2 -2
- data/lib/rest_connection/ssh_hax.rb +20 -96
- data/lib/rest_connection/version.rb +1 -1
- metadata +4 -4
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rest_connection (1.0.
|
4
|
+
rest_connection (1.0.5)
|
5
5
|
activesupport (= 2.3.10)
|
6
6
|
highline
|
7
7
|
json
|
@@ -20,7 +20,7 @@ GEM
|
|
20
20
|
rbx-require-relative (> 0.0.4)
|
21
21
|
mime-types (1.19)
|
22
22
|
net-ssh (2.1.4)
|
23
|
-
nokogiri (1.5.
|
23
|
+
nokogiri (1.5.6)
|
24
24
|
rake (0.8.7)
|
25
25
|
rbx-require-relative (0.0.9)
|
26
26
|
rest-client (1.6.7)
|
@@ -44,114 +44,29 @@ module SshHax
|
|
44
44
|
ssh_keys
|
45
45
|
end
|
46
46
|
|
47
|
-
def run_and_tail(run_this, tail_command, expect, ssh_key=nil, host_dns=self.reachable_ip)
|
48
|
-
status = nil
|
49
|
-
result = nil
|
50
|
-
output = ""
|
51
|
-
connection.logger("Running: #{run_this}")
|
52
|
-
Net::SSH.start(host_dns, 'root', :keys => ssh_key_config(ssh_key), :user_known_hosts_file => "/dev/null") do |ssh|
|
53
|
-
cmd_channel = ssh.open_channel do |ch1|
|
54
|
-
ch1.on_request('exit-status') do |ch, data|
|
55
|
-
status = data.read_long
|
56
|
-
end
|
57
|
-
ch1.exec(run_this) do |ch2, success|
|
58
|
-
unless success
|
59
|
-
output = "ERROR: SSH cmd failed to exec"
|
60
|
-
status = 1
|
61
|
-
end
|
62
|
-
ch2.on_data do |ch, data|
|
63
|
-
output += data
|
64
|
-
end
|
65
|
-
ch2.on_extended_data do |ch, type, data|
|
66
|
-
output += data
|
67
|
-
end
|
68
47
|
|
69
|
-
end
|
70
|
-
end
|
71
|
-
log_channel = ssh.open_channel do |ch2|
|
72
|
-
ch2.exec tail_command do |ch, success|
|
73
|
-
raise "could not execute command" unless success
|
74
|
-
# "on_data" is called when the process writes something to stdout
|
75
|
-
ch.on_data do |c, data|
|
76
|
-
output += data
|
77
|
-
if data =~ expect
|
78
|
-
result = $1
|
79
|
-
end
|
80
|
-
end
|
81
|
-
# "on_extended_data" is called when the process writes something to stderr
|
82
|
-
ch.on_extended_data do |c, type, data|
|
83
|
-
#STDERR.print data
|
84
|
-
end
|
85
|
-
ch.on_close do
|
86
|
-
end
|
87
|
-
ch.on_process do |c|
|
88
|
-
if result
|
89
|
-
ch.close
|
90
|
-
ssh.exec("killall tail")
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
95
|
-
cmd_channel.wait
|
96
|
-
log_channel.wait
|
97
|
-
end
|
98
|
-
connection.logger output
|
99
|
-
success = result.include?('completed')
|
100
|
-
connection.logger "Converge failed. See server audit: #{self.audit_link}" unless success
|
101
|
-
return {:status => success, :output => output}
|
102
|
-
end
|
103
|
-
|
104
|
-
# script is an Executable object with minimally nick or id set
|
105
|
-
def run_executable_with_ssh(script, options={}, ssh_key=nil)
|
106
|
-
raise "FATAL: run_executable called on a server with no reachable_ip. You need to run .settings on the server to populate this attribute." unless self.reachable_ip
|
107
|
-
if script.is_a?(Executable)
|
108
|
-
script = script.right_script
|
109
|
-
end
|
110
48
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
run_this = "rs_run_right_script -n #{script.name}"
|
116
|
-
end
|
117
|
-
tail_command ="tail -f -n1 /var/log/messages"
|
118
|
-
expect = /RightLink.*RS> ([completed|failed]+:)/
|
119
|
-
options.each do |key, value|
|
120
|
-
run_this += " -p #{key}=#{value}"
|
121
|
-
end
|
122
|
-
AuditEntry.new(run_and_tail(run_this, tail_command, expect))
|
49
|
+
def spot_check(command, ssh_key=nil, host_dns=self.reachable_ip, &block)
|
50
|
+
puts "SshHax::Probe method #{__method__}() entered..."
|
51
|
+
results = spot_check_command(command, ssh_key, host_dns)
|
52
|
+
yield results[:output]
|
123
53
|
end
|
124
54
|
|
125
|
-
# recipe can be either a String, or an Executable
|
126
|
-
# host_dns is optional and will default to objects self.reachable_ip
|
127
|
-
def run_recipe_with_ssh(recipe, ssh_key=nil, host_dns=self.reachable_ip)
|
128
|
-
raise "FATAL: run_script called on a server with no reachable_ip. You need to run .settings on the server to populate this attribute." unless self.reachable_ip
|
129
|
-
if recipe.is_a?(Executable)
|
130
|
-
recipe = recipe.recipe
|
131
|
-
end
|
132
|
-
tail_command ="tail -f -n1 /var/log/messages"
|
133
|
-
expect = /RightLink.*RS> ([completed|failed]+: < #{recipe} >)/
|
134
|
-
run_this = "rs_run_recipe -n '#{recipe}'"
|
135
|
-
run_and_tail(run_this, tail_command, expect, ssh_key)
|
136
|
-
end
|
137
55
|
|
138
|
-
def spot_check(command, ssh_key=nil, host_dns=self.reachable_ip, &block)
|
139
|
-
connection.logger "SSHing to #{host_dns}"
|
140
|
-
Net::SSH.start(host_dns, 'root', :keys => ssh_key_config(ssh_key)) do |ssh|
|
141
|
-
result = ssh.exec!(command)
|
142
|
-
yield result
|
143
|
-
end
|
144
|
-
end
|
145
56
|
|
146
57
|
# returns true or false based on command success
|
147
58
|
def spot_check_command?(command, ssh_key=nil, host_dns=self.reachable_ip)
|
59
|
+
puts "SshHax::Probe method #{__method__}() entered..."
|
148
60
|
results = spot_check_command(command, ssh_key, host_dns)
|
149
61
|
return results[:status] == 0
|
150
62
|
end
|
151
63
|
|
152
64
|
|
65
|
+
|
153
66
|
# returns hash of exit_status and output from command
|
67
|
+
# Note that "sudo" is prepended to <command> and the 'rightscale' user is used.
|
154
68
|
def spot_check_command(command, ssh_key=nil, host_dns=self.reachable_ip, do_not_log_result=false)
|
69
|
+
puts "SshHax::Probe method #{__method__}() entered..."
|
155
70
|
raise "FATAL: spot_check_command called on a server with no reachable_ip. You need to run .settings on the server to populate this attribute." unless host_dns
|
156
71
|
connection.logger "SSHing to #{host_dns} using key(s) #{ssh_key_config(ssh_key).inspect}"
|
157
72
|
status = nil
|
@@ -163,17 +78,25 @@ module SshHax
|
|
163
78
|
# Test for ability to connect; Net::SSH.start sometimes hangs under certain server-side sshd configs
|
164
79
|
test_ssh = ""
|
165
80
|
[5, 15, 60].each { |timeout_max|
|
166
|
-
test_ssh = `ssh -o \"BatchMode=yes\" -o \"StrictHostKeyChecking=no\" -o \"ConnectTimeout #{timeout_max}\"
|
81
|
+
test_ssh = `ssh -o \"BatchMode=yes\" -o \"StrictHostKeyChecking=no\" -o \"ConnectTimeout #{timeout_max}\" rightscale@#{host_dns} -C \"exit\" 2>&1`.chomp
|
167
82
|
break if test_ssh =~ /permission denied/i or test_ssh.empty?
|
168
83
|
}
|
169
84
|
raise test_ssh unless test_ssh =~ /permission denied/i or test_ssh.empty?
|
170
85
|
|
171
|
-
Net::SSH.start(host_dns, '
|
86
|
+
Net::SSH.start(host_dns, 'rightscale', :keys => ssh_key_config(ssh_key), :user_known_hosts_file => "/dev/null") do |ssh|
|
172
87
|
cmd_channel = ssh.open_channel do |ch1|
|
173
88
|
ch1.on_request('exit-status') do |ch, data|
|
174
89
|
status = data.read_long
|
175
90
|
end
|
176
|
-
|
91
|
+
# Request a pseudo-tty, this is needed as all calls use sudo to support RightLink 5.8
|
92
|
+
ch1.request_pty do |ch, success|
|
93
|
+
raise "Could not obtain a pseudo-tty!" if !success
|
94
|
+
end
|
95
|
+
# Now execute the command with "sudo" prepended to it.
|
96
|
+
# NOTE: The use of single quotes is required to keep Ruby from interpretting the command string passed in and messing up regex's
|
97
|
+
sudo_command = 'sudo ' + command
|
98
|
+
puts 'SshHax::Probe executing ' + sudo_command + '...'
|
99
|
+
ch1.exec(sudo_command) do |ch2, success|
|
177
100
|
unless success
|
178
101
|
status = 1
|
179
102
|
end
|
@@ -194,6 +117,7 @@ module SshHax
|
|
194
117
|
end
|
195
118
|
end
|
196
119
|
connection.logger "SSH Run: #{command} on #{host_dns}. Retry was #{retry_count}. Exit status was #{status}. Output below ---\n#{output}\n---" unless do_not_log_result
|
120
|
+
puts "SshHax::Probe method #{__method__}() exiting..."
|
197
121
|
return {:status => status, :output => output}
|
198
122
|
end
|
199
123
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_connection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 5
|
10
|
+
version: 1.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- RightScale, Inc.
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2013-01-03 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: activesupport
|