puppet-pssh 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/puppet-pssh.rb +81 -3
  2. metadata +2 -2
data/lib/puppet-pssh.rb CHANGED
@@ -9,7 +9,50 @@ require 'pp'
9
9
 
10
10
  module PuppetPSSH
11
11
 
12
- VERSION = "0.3.1"
12
+ VERSION = "0.3.2"
13
+
14
+ class PuppetDB
15
+
16
+ def initialize(host = 'puppet', port = '8080', use_ssl = false)
17
+ @host = host
18
+ @port = port
19
+ @use_ssl = use_ssl
20
+ end
21
+
22
+ def url
23
+ "#{@use_ssl ? 'https' : 'http'}://#{@host}:#{@port}/"
24
+ end
25
+
26
+ def get_nodes_from_query(query)
27
+ target_url = "#{url}nodes?query=#{query}"
28
+ Log.debug "Puppet master host: #{@host}"
29
+ Log.debug "Puppet master url: #{target_url}"
30
+
31
+ nodes = []
32
+ begin
33
+ out = Excon.get target_url
34
+ JSON.parse(out.body).each do |n|
35
+ nodes << n
36
+ end
37
+ rescue TypeError => e
38
+ raise Exception.new "Error retrieving node list from master host: #{@host}"
39
+ rescue Excon::Errors::SocketError => e
40
+ raise Exception.new "Could not connect to the puppet master host: #{@host}"
41
+ end
42
+ nodes
43
+ end
44
+
45
+ def deactivated_nodes
46
+ query = URI.encode '["=", ["node", "active"], false]'
47
+ get_nodes_from_query query
48
+ end
49
+
50
+ def active_nodes
51
+ query = URI.encode '["=", ["node", "active"], true]'
52
+ get_nodes_from_query query
53
+ end
54
+
55
+ end
13
56
 
14
57
  if !defined? Log or Log.nil?
15
58
  Log = Logger.new($stdout)
@@ -147,6 +190,7 @@ module PuppetPSSH
147
190
 
148
191
  parameter "COMMAND ...", "Command to run"
149
192
  option "--nameserver", "DNS_SERVER", "Resolve node name using the given nameserver"
193
+ option "--use-ipaddress-fact", :flag, "Use node's ipaddress fact as the target IP"
150
194
  option "--pssh-path", "PSSH_PATH", "Parallel-ssh command path", :default => '/usr/bin/parallel-ssh'
151
195
  option "--hostlist-path", "HOSTLIST_PATH", "Save host list to path", :default => '/tmp/puppet-pssh-run-hostlist'
152
196
  option ["-H", "--hostlist-path"], "HOSTLIST_PATH", "Save host list to path", :default => '/tmp/puppet-pssh-run-hostlist'
@@ -189,12 +233,14 @@ module PuppetPSSH
189
233
  #
190
234
  # Optionally resolve names using specific DNS server
191
235
  #
192
- unless nameserver.nil?
236
+ if !nameserver.nil?
193
237
  require 'net/dns'
194
238
  Log.info "DNS Server: #{nameserver}"
195
239
  Log.info "Resolving node names... (may take a while)"
196
240
  res = Net::DNS::Resolver.new
197
241
  res.nameservers = nameserver
242
+ elsif use_ipaddress_fact?
243
+ Log.info "Using node ipaddress fact to connect to the node..."
198
244
  end
199
245
  #
200
246
  File.open hostlist_path, 'w' do |f|
@@ -202,8 +248,19 @@ module PuppetPSSH
202
248
  address = i
203
249
  # try to resolve before writing the list
204
250
  Log.debug "Adding #{address}"
205
- unless nameserver.nil?
251
+ if !nameserver.nil?
206
252
  address = res.query(i).answer.first.address rescue next
253
+ elsif use_ipaddress_fact?
254
+ value = JSON.parse(
255
+ Excon.get(master_url + "facts/#{i}").body
256
+ )['facts']['ipaddress']
257
+ if value
258
+ address = value
259
+ else
260
+ Log.warn "Node #{i} does not have ipaddress fact. Using FQDN."
261
+ address = i
262
+ end
263
+ else
207
264
  end
208
265
  f.puts "#{address}"
209
266
  end
@@ -241,10 +298,31 @@ module PuppetPSSH
241
298
  end
242
299
  end
243
300
 
301
+ class CountNodes < Clamp::Command
302
+ option ["-p", "--puppetdb-host"], "PUPPETDB_HOST", "PuppetDB host", :default => 'puppet'
303
+ option ["-P", "--puppetdb-port"], "PUPPETMASTER_PORT", "PuppetDB port", :default => '8080'
304
+ option "--debug", :flag, "Print debugging output", :default => false do |o|
305
+ Log.level = Logger::DEBUG
306
+ end
307
+
308
+ def execute
309
+ pdb = PuppetDB.new puppetdb_host, puppetdb_port
310
+ active = pdb.active_nodes.size
311
+ deactivated = pdb.deactivated_nodes.size
312
+ total = active + deactivated
313
+ Log.info "Node population"
314
+ Log.info "Active nodes: #{active}"
315
+ Log.info "Inactive nodes: #{deactivated}"
316
+ Log.info "Total: #{total}"
317
+ end
318
+
319
+ end
320
+
244
321
  class Driver < Clamp::Command
245
322
 
246
323
  subcommand "run", "Run an arbitrary command against the nodes", Run
247
324
  subcommand "list", "List registered nodes", List
325
+ subcommand "count-nodes", "Node population", CountNodes
248
326
 
249
327
  end
250
328
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet-pssh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-08 00:00:00.000000000 Z
12
+ date: 2012-11-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: colored