linux_admin 0.17.0 → 0.18.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.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/linux_admin.rb +2 -1
- data/lib/linux_admin/ssh.rb +88 -0
- data/lib/linux_admin/ssh_agent.rb +61 -0
- data/lib/linux_admin/version.rb +1 -1
- data/spec/ssh_spec.rb +53 -0
- metadata +25 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a90ef3973da3f00152bf552f74cab0baa544af95
|
4
|
+
data.tar.gz: f296bf3c99266062abc2cc1f67d987aa0e974ec2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c010a5739c54682ca620bfb4b0851d28e785e063225230c0e848472dfb1359876e27ea7880881838e6c93cf1de30a31a470e4b605e301e8af3c642023e9e953b
|
7
|
+
data.tar.gz: f1f10c4d2bb146033cb405a35b7f510618b7c6cde0d5226b81ea4daa52d6bd44c6a40642ed504f427cebf655ee6784aa4752304c7fed5ed773420a5002c6c942
|
data/README.md
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
[](https://codeclimate.com/github/ManageIQ/linux_admin)
|
6
6
|
[](https://coveralls.io/r/ManageIQ/linux_admin)
|
7
7
|
[](https://gemnasium.com/ManageIQ/linux_admin)
|
8
|
+
[](https://hakiri.io/github/ManageIQ/linux_admin/master)
|
9
|
+
|
8
10
|
|
9
11
|
LinuxAdmin is a module to simplify management of linux systems.
|
10
12
|
It should be a single place to manage various system level configurations,
|
data/lib/linux_admin.rb
CHANGED
@@ -11,7 +11,8 @@ require 'linux_admin/rpm'
|
|
11
11
|
require 'linux_admin/deb'
|
12
12
|
require 'linux_admin/version'
|
13
13
|
require 'linux_admin/yum'
|
14
|
-
|
14
|
+
require 'linux_admin/ssh'
|
15
|
+
require 'linux_admin/ssh_agent'
|
15
16
|
require 'linux_admin/service'
|
16
17
|
require 'linux_admin/mountable'
|
17
18
|
require 'linux_admin/disk'
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module LinuxAdmin
|
2
|
+
class SSH
|
3
|
+
attr_reader :ip
|
4
|
+
attr_reader :username
|
5
|
+
attr_reader :private_key
|
6
|
+
attr_reader :agent
|
7
|
+
|
8
|
+
def initialize(ip, username, private_key = nil, password = nil)
|
9
|
+
@ip = ip
|
10
|
+
@private_key = private_key
|
11
|
+
@username = username
|
12
|
+
@password = password
|
13
|
+
end
|
14
|
+
|
15
|
+
def perform_commands(commands = [], agent_socket = nil, stdin = nil)
|
16
|
+
require 'net/ssh'
|
17
|
+
if block_given?
|
18
|
+
execute_commands(commands, agent_socket, stdin, &Proc.new)
|
19
|
+
else
|
20
|
+
execute_commands(commands, agent_socket, stdin)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def execute_commands(commands, agent_socket, stdin)
|
27
|
+
result = nil
|
28
|
+
args = {:paranoid => false}
|
29
|
+
if agent_socket
|
30
|
+
args.merge!(:forward_agent => true,
|
31
|
+
:number_of_password_prompts => 0,
|
32
|
+
:agent_socket_factory => -> { UNIXSocket.open(agent_socket) })
|
33
|
+
elsif @private_key
|
34
|
+
args[:key_data] = [@private_key]
|
35
|
+
elsif @password
|
36
|
+
args[:password] = @password
|
37
|
+
end
|
38
|
+
Net::SSH.start(@ip, @username, args) do |ssh|
|
39
|
+
if block_given?
|
40
|
+
result = yield ssh
|
41
|
+
else
|
42
|
+
commands.each do |cmd|
|
43
|
+
result = ssh_exec!(ssh, cmd, stdin)
|
44
|
+
result[:last_command] = cmd
|
45
|
+
break if result[:exit_status] != 0
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
result
|
50
|
+
end
|
51
|
+
|
52
|
+
def ssh_exec!(ssh, command, stdin)
|
53
|
+
stdout_data = ''
|
54
|
+
stderr_data = ''
|
55
|
+
exit_status = nil
|
56
|
+
exit_signal = nil
|
57
|
+
|
58
|
+
ssh.open_channel do |channel|
|
59
|
+
channel.request_pty unless stdin
|
60
|
+
channel.exec(command) do |_, success|
|
61
|
+
channel.send_data(stdin) if stdin
|
62
|
+
channel.eof!
|
63
|
+
raise StandardError, "Command \"#{command}\" was unable to execute" unless success
|
64
|
+
channel.on_data do |_, data|
|
65
|
+
stdout_data << data
|
66
|
+
end
|
67
|
+
channel.on_extended_data do |_, _, data|
|
68
|
+
stderr_data << data
|
69
|
+
end
|
70
|
+
channel.on_request('exit-status') do |_, data|
|
71
|
+
exit_status = data.read_long
|
72
|
+
end
|
73
|
+
|
74
|
+
channel.on_request('exit-signal') do |_, data|
|
75
|
+
exit_signal = data.read_long
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
ssh.loop
|
80
|
+
{
|
81
|
+
:stdout => stdout_data,
|
82
|
+
:stderr => stderr_data,
|
83
|
+
:exit_status => exit_status,
|
84
|
+
:exit_signal => exit_signal
|
85
|
+
}
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
module LinuxAdmin
|
3
|
+
class SSHAgent
|
4
|
+
attr_accessor :pid
|
5
|
+
attr_reader :socket
|
6
|
+
|
7
|
+
def initialize(ssh_private_key, agent_socket = nil)
|
8
|
+
@socket = agent_socket
|
9
|
+
@private_key = ssh_private_key
|
10
|
+
end
|
11
|
+
|
12
|
+
def start
|
13
|
+
if @socket
|
14
|
+
FileUtils.mkdir_p(File.dirname(@socket))
|
15
|
+
agent_details = `ssh-agent -a #{@socket}`
|
16
|
+
else
|
17
|
+
agent_details = `ssh-agent`
|
18
|
+
@socket = parse_ssh_agent_socket(agent_details)
|
19
|
+
end
|
20
|
+
@pid = parse_ssh_agent_pid(agent_details)
|
21
|
+
IO.popen({'SSH_AUTH_SOCK' => @socket, 'SSH_AGENT_PID' => @pid}, ['ssh-add', '-'], :mode => 'w') do |f|
|
22
|
+
f.puts(@private_key)
|
23
|
+
end
|
24
|
+
raise StandardError, "Couldn't add key to agent" if $CHILD_STATUS.to_i != 0
|
25
|
+
end
|
26
|
+
|
27
|
+
def with_service
|
28
|
+
start
|
29
|
+
yield @socket
|
30
|
+
ensure
|
31
|
+
stop
|
32
|
+
end
|
33
|
+
|
34
|
+
def stop
|
35
|
+
system({'SSH_AGENT_PID' => @pid}, '(ssh-agent -k) &> /dev/null') if process_exists?(@pid)
|
36
|
+
File.delete(@socket) if File.exist?(@socket)
|
37
|
+
@socket = nil
|
38
|
+
@pid = nil
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def process_exists?(process_pid)
|
44
|
+
Process.kill(0, process_pid) == 1
|
45
|
+
rescue
|
46
|
+
false
|
47
|
+
end
|
48
|
+
|
49
|
+
def parse_ssh_agent_socket(output)
|
50
|
+
parse_ssh_agent_output(output, 1)
|
51
|
+
end
|
52
|
+
|
53
|
+
def parse_ssh_agent_pid(output)
|
54
|
+
parse_ssh_agent_output(output, 2)
|
55
|
+
end
|
56
|
+
|
57
|
+
def parse_ssh_agent_output(output, index)
|
58
|
+
output.split('=')[index].split(' ')[0].chop
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/linux_admin/version.rb
CHANGED
data/spec/ssh_spec.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
describe LinuxAdmin::SSH do
|
2
|
+
before(:each) do
|
3
|
+
require 'net/ssh'
|
4
|
+
@example_ssh_key = <<-eos
|
5
|
+
-----BEGIN RSA PRIVATE KEY-----
|
6
|
+
MIIEowIBAAKCAQEAasdvkMNteq7hjnqNE61ESnEHnaOtHxZffdsQ33R7BXcu9eCH
|
7
|
+
ncZadHmmfRZgMPQnHGX0NzboVKfdpdF40o+iGQKyy3wKqdgGnTAWqx/hxrsdsdgh
|
8
|
+
f/g7AABNjo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Sj3l2
|
23
|
+
J7eAKxKtybNtptOPF6RVq74dbqFFO77cmPZcTPspxJPdFKBFp18w36G9zeTKq0I9
|
24
|
+
HpqjkZHLShbej3XW/ODO/Qqc29bd0e4xt2aEWGC0cxKwqzRKTk7rg/A+sqsszK9G
|
25
|
+
KgZ9VuH5QyokpDfHB6pkcQKBgChsq8PgGTT0llGT/ue1HgQROqEwNCZC4BcaHT21
|
26
|
+
+oGxr4cktfx3Cjsw9IFXo9o0zQyksUaRrNYpJxDuazWVlFLpPPQIoF5vMWbELwhA
|
27
|
+
L9lbWzG0U1kGHpaFe73/5ioW8tJ7HvXhmNj+W+vSXXwUzT0CkqW9J61Kc9FEKHfb
|
28
|
+
TLVxAoGBAM8WKsTfEMxuv5pabGTo8BC1ruz4c5qLJGLGnH4kWJm5o6ippugFsOlK
|
29
|
+
rDUWive4uLSKi3Fsb2kPw6gHuRGerFN1CBpCENLib3xG5Bd4XhmKZMDxqU2bO0gd
|
30
|
+
sV1Tr/acrE0aWBkD9RYrR2/UwG1zfXuIJeufdWf8c0SY3X6J7jJN
|
31
|
+
-----END RSA PRIVATE KEY-----
|
32
|
+
eos
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should create a call using agent" do
|
36
|
+
expect(Net::SSH).to receive(:start).with("127.0.0.1", "root", :paranoid => false, :forward_agent => true, :number_of_password_prompts => 0, :agent_socket_factory => Proc).and_return(true)
|
37
|
+
ssh_agent = LinuxAdmin::SSHAgent.new(@example_ssh_key)
|
38
|
+
ssh_agent.with_service do |socket|
|
39
|
+
ssh = LinuxAdmin::SSH.new("127.0.0.1", "root")
|
40
|
+
ssh.perform_commands(%w("ls", "pwd"), socket)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should preform command using private key" do
|
45
|
+
expect(Net::SSH).to receive(:start).with("127.0.0.1", "root", :paranoid => false, :key_data => [@example_ssh_key]).and_return(true)
|
46
|
+
LinuxAdmin::SSH.new("127.0.0.1", "root", @example_ssh_key).perform_commands(%w("ls", "pwd"))
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should preform command using password" do
|
50
|
+
expect(Net::SSH).to receive(:start).with("127.0.0.1", "root", :paranoid => false, :password => "password").and_return(true)
|
51
|
+
LinuxAdmin::SSH.new("127.0.0.1", "root", nil, "password").perform_commands(%w("ls", "pwd"))
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linux_admin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.18.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Dunne
|
@@ -13,10 +13,12 @@ authors:
|
|
13
13
|
- Nick Carboni
|
14
14
|
- Martin Hradil
|
15
15
|
- Yuri Rudman
|
16
|
+
- Oleg Barenboim
|
17
|
+
- Alon Goldboim
|
16
18
|
autorequire:
|
17
19
|
bindir: bin
|
18
20
|
cert_chain: []
|
19
|
-
date: 2016-07-
|
21
|
+
date: 2016-07-25 00:00:00.000000000 Z
|
20
22
|
dependencies:
|
21
23
|
- !ruby/object:Gem::Dependency
|
22
24
|
name: bundler
|
@@ -158,6 +160,20 @@ dependencies:
|
|
158
160
|
- - ">="
|
159
161
|
- !ruby/object:Gem::Version
|
160
162
|
version: '0'
|
163
|
+
- !ruby/object:Gem::Dependency
|
164
|
+
name: net-ssh
|
165
|
+
requirement: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - "~>"
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: 3.2.0
|
170
|
+
type: :runtime
|
171
|
+
prerelease: false
|
172
|
+
version_requirements: !ruby/object:Gem::Requirement
|
173
|
+
requirements:
|
174
|
+
- - "~>"
|
175
|
+
- !ruby/object:Gem::Version
|
176
|
+
version: 3.2.0
|
161
177
|
description: |2
|
162
178
|
|
163
179
|
LinuxAdmin is a module to simplify management of linux systems.
|
@@ -173,6 +189,8 @@ email:
|
|
173
189
|
- ncarboni@redhat.com
|
174
190
|
- mhradil@redhat.com
|
175
191
|
- yrudman@redhat.com
|
192
|
+
- chessbyte@gmail.com
|
193
|
+
- alongoldboim@gmail.com
|
176
194
|
executables: []
|
177
195
|
extensions: []
|
178
196
|
extra_rdoc_files: []
|
@@ -210,6 +228,8 @@ files:
|
|
210
228
|
- lib/linux_admin/service.rb
|
211
229
|
- lib/linux_admin/service/sys_v_init_service.rb
|
212
230
|
- lib/linux_admin/service/systemd_service.rb
|
231
|
+
- lib/linux_admin/ssh.rb
|
232
|
+
- lib/linux_admin/ssh_agent.rb
|
213
233
|
- lib/linux_admin/system.rb
|
214
234
|
- lib/linux_admin/time_date.rb
|
215
235
|
- lib/linux_admin/version.rb
|
@@ -261,6 +281,7 @@ files:
|
|
261
281
|
- spec/service/systemd_service_spec.rb
|
262
282
|
- spec/service_spec.rb
|
263
283
|
- spec/spec_helper.rb
|
284
|
+
- spec/ssh_spec.rb
|
264
285
|
- spec/subscription_manager_spec.rb
|
265
286
|
- spec/system_spec.rb
|
266
287
|
- spec/time_date_spec.rb
|
@@ -286,7 +307,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
286
307
|
version: '0'
|
287
308
|
requirements: []
|
288
309
|
rubyforge_project:
|
289
|
-
rubygems_version: 2.
|
310
|
+
rubygems_version: 2.5.1
|
290
311
|
signing_key:
|
291
312
|
specification_version: 4
|
292
313
|
summary: LinuxAdmin is a module to simplify management of linux systems.
|
@@ -335,6 +356,7 @@ test_files:
|
|
335
356
|
- spec/service/systemd_service_spec.rb
|
336
357
|
- spec/service_spec.rb
|
337
358
|
- spec/spec_helper.rb
|
359
|
+
- spec/ssh_spec.rb
|
338
360
|
- spec/subscription_manager_spec.rb
|
339
361
|
- spec/system_spec.rb
|
340
362
|
- spec/time_date_spec.rb
|