puppet-whats 0.0.2

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/whats +136 -0
  3. data/lib/whats.rb +79 -0
  4. metadata +88 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f7a7411f919a392627ed06e3f114ffe7da1bf40
4
+ data.tar.gz: 8d6f155ef1ea1def68cc8f15c83acbb48db66701
5
+ SHA512:
6
+ metadata.gz: c31f9fa45a2834882ab088dca0bee8f96e600f6192651395cc4284e10327515f5b0e8e67d61c7df4d8a7a4d51edc5d4a1e22efb6afe0ce93cd6f54e25e7b85fc
7
+ data.tar.gz: 00ad94c6bab5902a9c103bd4c6e8e9b83d936589fe9715ecd648e866c897ad3a8045ddd1f0514a396f8c7f3e790662bc6540ea4a0cdc34295688a94fb6e492e7
data/bin/whats ADDED
@@ -0,0 +1,136 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'yaml'
5
+ require 'pp'
6
+ require 'json'
7
+ require 'yaml'
8
+ require 'net/http'
9
+ require 'uri'
10
+ require 'resolv'
11
+ require 'ipaddr'
12
+ require 'terminal-table'
13
+
14
+ require 'whats'
15
+
16
+ #SETUP
17
+ default_options={
18
+ :puppetdb_host => 'puppetdb',
19
+ :puppetdb_port => '8081',
20
+ :domains => ['.local'],
21
+ :basic_info => ["hostname","manufacturer","productname","serialnumber","operatingsystem","operatingsystemrelease","architecture","processor0","processorcount","memorysize","kernelrelease","ipaddress","macaddress","virtual","uptime"],
22
+ :verbose => false,
23
+ :yaml => false,
24
+ :json => false,
25
+ :sort => false,
26
+ :all => false,
27
+ :config => '',
28
+ :quiet => false,
29
+
30
+ }
31
+
32
+ options = default_options
33
+
34
+ OptionParser.new do |opts|
35
+ opts.banner = "Usage: whats [options] <hostname>"
36
+
37
+ opts.on("-j","--json","JSON output") do |j|
38
+ options[:json] = j
39
+ end
40
+ opts.on("-y","--yaml","YAML output") do |y|
41
+ options[:yaml] = true
42
+ end
43
+ opts.on("-p","--pp","Pretty Print output") do |p|
44
+ options[:pp] = true
45
+ end
46
+ opts.on("-a","--all","Use all facts") do |a|
47
+ options[:all] = true
48
+ end
49
+ opts.on("-c","--config CONFIG","Config file to use") do |c|
50
+ options.merge!(Hash[YAML::load(open(c)).map { |k, v| [k.to_sym, v] }])
51
+ options[:config] = c
52
+ end
53
+ opts.on("-v","--verbose", "Be verbose") do |v|
54
+ options[:verbose] = v
55
+ end
56
+ opts.on("-q","--quiet", "Be extra quiet, only shown") do |q|
57
+ options[:quiet] = q
58
+ end
59
+ opts.parse!
60
+ end.parse
61
+
62
+ if ARGV.length != 1
63
+ puts "Please pass a hostname, see --help"
64
+ exit
65
+ else
66
+ #see if we were given an ip instead, and if so, get hostname
67
+ if !(IPAddr.new(ARGV[0]) rescue nil).nil?
68
+ #got an ip, convert
69
+ host = Resolv.new.getname ARGV[0]
70
+ else
71
+ host = ARGV[0].downcase
72
+ end
73
+ end
74
+
75
+
76
+
77
+
78
+ #load config
79
+ if options[:config] == ""
80
+ config=Whats.config_find()
81
+ options[:config] = config
82
+ options.merge!(Hash[YAML::load(open(File.expand_path(config))).map { |k, v| [k.to_sym, v] }])
83
+ if options[:verbose]
84
+ pp options
85
+ end
86
+ else
87
+ options.merge!(Hash[YAML::load(open(File.expand_path(options[:config]))).map { |k, v| [k.to_sym, v] }])
88
+ if options[:verbose]
89
+ pp options
90
+ end
91
+ end
92
+ #try the name as given first, some hosts have short certnames(sigh)
93
+
94
+ if !options[:quiet]
95
+ puts "Searching for #{host}..."
96
+ end
97
+ #detect if we got a short hostname or a fqdn.
98
+ if host.include?('.')
99
+ fqdn=host
100
+ if Whats.node_test(options[:puppetdb_host],options[:puppetdb_port],fqdn)
101
+ facts=Whats.get_facts(options[:puppetdb_host],options[:puppetdb_port],fqdn)
102
+ else
103
+ raise ("Node #{fqdn} not found in puppetdb")
104
+ end
105
+ else
106
+ #we will need to itterate the available search domains...
107
+ options[:domains].each do |dom|
108
+ fqdn="#{host}.#{dom}"
109
+ if !options[:quiet]
110
+ puts "Searching for #{fqdn}"
111
+ end
112
+ if Whats.node_test(options[:puppetdb_host],options[:puppetdb_port],fqdn)
113
+ facts=Whats.get_facts(options[:puppetdb_host],options[:puppetdb_port],fqdn)
114
+ break
115
+ else
116
+ next
117
+ end
118
+ end
119
+ if facts == nil
120
+ raise ("node #{host} not found after serching all domains")
121
+ end
122
+ end
123
+
124
+
125
+ node_facts=Whats.facts_to_hash(facts)
126
+
127
+ facts2 = Hash.new
128
+ if options[:all] == true
129
+ facts2 = node_facts
130
+ else
131
+ options[:basic_info].each do |val|
132
+ facts2["#{val}"] = node_facts[val]
133
+ end
134
+ end
135
+
136
+ Whats.output(options,facts2)
data/lib/whats.rb ADDED
@@ -0,0 +1,79 @@
1
+ class Whats
2
+
3
+ def self.config_find()
4
+ config_search_paths=['~/.whats.yaml','/etc/whats.yaml']
5
+ config_search_paths.each do |path|
6
+ #If #{path} is a file and re can read it, return it.
7
+ if File.exists?(File.expand_path(path))
8
+ return (path)
9
+ end
10
+ end
11
+ #no config found
12
+ raise ("No config found!")
13
+ end
14
+
15
+
16
+
17
+ #if we passed shortname, get fqdn
18
+ def self.domain_fix(host,domain,match)
19
+ #Fix domain if needed
20
+ if host.match(match)
21
+ fqdn = host.to_s
22
+ else
23
+ fqdn = host.to_s + domain
24
+ end
25
+ return(fqdn)
26
+ end
27
+
28
+ #Query to see if the node we're looking for exists
29
+ def self.node_test(puppetdb_host,puppetdb_port,fqdn)
30
+ test_uri=URI.parse("http://#{puppetdb_host}:#{puppetdb_port}/pdb/query/v4/nodes/#{fqdn}")
31
+ test_response = Net::HTTP.get_response(test_uri)
32
+ test_body=JSON.parse(test_response.body)
33
+ if test_body.key?("error")
34
+ return false
35
+ else
36
+ return true
37
+ end
38
+ end
39
+
40
+ #get all facts for the given node
41
+ def self.get_facts(puppetdb_host,puppetdb_port,fqdn)
42
+ uri=URI.parse("http://#{puppetdb_host}:#{puppetdb_port}/pdb/query/v4/nodes/#{fqdn}/facts")
43
+ response = Net::HTTP.get_response(uri)
44
+ facts=JSON.parse(response.body)
45
+ return(facts)
46
+ end
47
+
48
+ #Parse the fact output from puppetdb into a simpler hash
49
+ #
50
+ # v4 apit returns facts as an array, with each element a hash like
51
+ #{"certname"=>"some_host",
52
+ # "environment"=>"some_env",
53
+ # "name"=>"memorysize",
54
+ # "value"=>"3.70 GiB"},
55
+ #
56
+ def self.facts_to_hash(facts)
57
+ node_facts=Hash.new
58
+ facts.each do |fact|
59
+ node_facts[fact["name"]] = fact["value"]
60
+ end
61
+ return node_facts
62
+ end
63
+
64
+ #do output
65
+ def self.output(options,facts2)
66
+ #output time
67
+ values=options[:basic_info]
68
+ if options[:json] == true
69
+ puts facts2.to_json
70
+ elsif options[:yaml] == true
71
+ puts facts2.to_yaml
72
+ elsif options[:pp] == true
73
+ pp facts2
74
+ else
75
+ table=Terminal::Table.new :headings => ['Fact', 'Value'], :rows => facts2
76
+ puts table
77
+ end
78
+ end
79
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-whats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Nicholson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: terminal-table
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ description: Whats asks pupetdb about systems for you
56
+ email: matthew.a.nicholson@gmail.com
57
+ executables:
58
+ - whats
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - bin/whats
63
+ - lib/whats.rb
64
+ homepage: http://github.com/sjoeboo/whats
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.6
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Whats?
88
+ test_files: []