This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
canon.rb
64 lines (54 loc) · 1.58 KB
/
canon.rb
1
2
3
4
5
6
7
8
9
10
11
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
require 'chef/knife'
module Lnxchk
class Canon < Chef::Knife
deps do
require 'chef/mixin/command'
require 'chef/search/query'
require 'chef/knife/search'
require 'chef/knife/ssh'
require 'net/ssh/multi'
require 'chef/knife/core/ui'
end
banner "knife cannon -C \"good copy\" HOSTS_QUERY COMMAND"
option :goodcopy,
:short => '-C CMD',
:long => '--goodcopy CMD',
:boolean => false,
:description => 'Command output expected'
def run
query = name_args[0]
command = name_args[1]
if config[:goodcopy]
goodcopy = config[:goodcopy]
else
print "Please use -C or --goodcopy to add a master output string"
exit 1
end
knife_ssh = Chef::Knife::Ssh.new
cmd_line = command
knife_ssh.name_args = [query, cmd_line]
stdout_orig = STDOUT.clone
STDOUT.reopen(File.new('file', 'w'))
knife_ssh.run
$stdout.reopen(stdout_orig)
matches = 0
file = File.open('./file', 'r')
file.each_line do |line|
host, data = line.split(" ", 2)
if data !~ /#{goodcopy}/
ui.msg(ui.color("ERROR: #{host} failed to match expected output: #{data}", :red) )
else
matches = matches + 1
end
end
file.close
File.delete('file')
if matches == 1
match_message = "1 host was successful"
else
match_message = "#{matches} hosts were successful"
end
ui.msg(ui.color(match_message, :green))
end # close run
end # close class
end # close module