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 0
/
Copy pathpocket.rb
54 lines (47 loc) · 1.45 KB
/
pocket.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
require 'chef/knife'
module Lnxchk
class Pocket < Chef::Knife
deps do
require 'chef/mixin/command'
require 'chef/search/query'
require 'chef/knife/search'
require 'chef/knife/ssh'
require 'net/ssh/multi'
end
banner "knife pocket -P file --find 'QUERY' or knife pocket -P file --ssh 'COMMAND'"
option :file,
:short => '-P FILE',
:long => '--pocket FILE',
:boolean => false,
:description => 'File to save data to'
def run
what = name_args[0]
@stuff = name_args[1]
if config[:file]
file = config[:file]
else
file = ".pocket"
end
if what == "find"
fuzzier_query = "tags:*#{@stuff}* OR roles:*#{@stuff}* OR fqdn:*#{@stuff}* OR addresses:*#{@stuff}*"
knife_search = Chef::Knife::Search.new
# knife_search.name_args = ['node', fuzzier_query, 'format', 'json']
knife_search.name_args = ['node', fuzzier_query]
save_list = knife_search.run
fh = File.open(file, "w")
save_list.each { |node|
x = node["id"]
fh.write "#{x} ";
}
elsif what == "ssh"
hosts = File.open(file, "r").read
#print "knife ssh -m '#{hosts}' '#{@stuff}'\n";
knife_ssh = Chef::Knife::Ssh.new
cmd_line = @stuff
knife_ssh.config[:manual] = true
knife_ssh.name_args = [hosts, cmd_line]
output = knife_ssh.run
end
end
end
end