Skip to content

Commit 7596075

Browse files
committed
Merge remote-tracking branch 'EdgeJ/add_multiple_node_option' into 1-5-4
2 parents e04026f + 395ba13 commit 7596075

File tree

6 files changed

+55
-15
lines changed

6 files changed

+55
-15
lines changed

doc/optionsref.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
```
1111
Usage: octocatalog-diff [command line options]
12-
-n, --hostname HOSTNAME Use PuppetDB facts from last run of hostname
12+
-n HOSTNAME1[,HOSTNAME2[,...]], Use PuppetDB facts from last run of a hostname or a comma separated list of multiple hostnames
13+
--hostname
1314
--basedir DIRNAME Use an alternate base directory (git checkout of puppet repository)
1415
-f, --from FROM_BRANCH Branch you are coming from
1516
-t, --to TO_BRANCH Branch you are going to
@@ -856,14 +857,17 @@ Puppet control repo template, the value of this should be 'hieradata', which is
856857

857858
<tr>
858859
<td valign=top>
859-
<pre><code>-n HOSTNAME
860-
--hostname HOSTNAME</code></pre>
860+
<pre><code>-n HOSTNAME1[,HOSTNAME2[,...]]
861+
--hostname HOSTNAME1[,HOSTNAME2[,...]]</code></pre>
861862
</td>
862863
<td valign=top>
863-
Use PuppetDB facts from last run of hostname
864+
Use PuppetDB facts from last run of a hostname or a comma separated list of multiple hostnames
864865
</td>
865866
<td valign=top>
866-
Set hostname, which is used to look up facts in PuppetDB, and in the header of diff display. (<a href="../lib/octocatalog-diff/cli/options/hostname.rb">hostname.rb</a>)
867+
Set hostname, which is used to look up facts in PuppetDB, and in the header of diff display.
868+
This option can recieve a single hostname, or a comma separated list of
869+
multiple hostnames, which are split into an Array. Multiple hostnames do not
870+
work with the `catalog-only` or `bootstrap-then-exit` options. (<a href="../lib/octocatalog-diff/cli/options/hostname.rb">hostname.rb</a>)
867871
</td>
868872
</tr>
869873

lib/octocatalog-diff/cli.rb

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require_relative 'version'
1212

1313
require 'logger'
14+
require 'parallel'
1415
require 'socket'
1516

1617
module OctocatalogDiff
@@ -116,16 +117,34 @@ def self.cli(argv = ARGV, logger = Logger.new(STDERR), opts = {})
116117
end
117118

118119
# Compile catalogs and do catalog-diff
119-
catalog_diff = OctocatalogDiff::API::V1.catalog_diff(options.merge(logger: logger))
120-
diffs = catalog_diff.diffs
121-
122-
# Display diffs
123-
printer_obj = OctocatalogDiff::Cli::Printer.new(options, logger)
124-
printer_obj.printer(diffs, catalog_diff.from.compilation_dir, catalog_diff.to.compilation_dir)
120+
node_set = options.delete(:node)
121+
node_set = [node_set] unless node_set.is_a?(Array)
122+
catalog_diff = nil
123+
all_diffs = []
124+
125+
# run multiple node diffs in parallel
126+
Parallel.map(node_set, in_threads: 4) do |node|
127+
options[:node] = node
128+
catalog_diff = OctocatalogDiff::API::V1.catalog_diff(options.merge(logger: logger))
129+
diffs = catalog_diff.diffs
130+
131+
# Display diffs
132+
printer_obj = OctocatalogDiff::Cli::Printer.new(options, logger)
133+
printer_obj.printer(diffs, catalog_diff.from.compilation_dir, catalog_diff.to.compilation_dir)
134+
135+
# Append any diffs for final exit status
136+
all_diffs << diffs
137+
end
125138

126139
# Return the resulting diff object if requested (generally for testing) or otherwise return exit code
127140
return catalog_diff if opts[:INTEGRATION]
128-
diffs.any? ? EXITCODE_SUCCESS_WITH_DIFFS : EXITCODE_SUCCESS_NO_DIFFS
141+
142+
all_diffs.each do |diff|
143+
next unless diff.any?
144+
return EXITCODE_SUCCESS_WITH_DIFFS
145+
end
146+
147+
EXITCODE_SUCCESS_NO_DIFFS
129148
end
130149

131150
# Parse command line options with 'optparse'. Returns a hash with the parsed arguments.

lib/octocatalog-diff/cli/options.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Cli
1111
# This class contains the option parser. 'parse_options' is the external entry point.
1212
class Options
1313
# The usage banner.
14-
BANNER = 'Usage: catalog-diff -n <hostname> [-f <from environment>] [-t <to environment>]'.freeze
14+
BANNER = 'Usage: catalog-diff -n <hostname>[,<hostname>...] [-f <from environment>] [-t <to environment>]'.freeze
1515

1616
# An error class specifically for passing information to the document build task.
1717
class DocBuildError < RuntimeError; end
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
# frozen_string_literal: true
22

33
# Set hostname, which is used to look up facts in PuppetDB, and in the header of diff display.
4+
# This option can recieve a single hostname, or a comma separated list of
5+
# multiple hostnames, which are split into an Array. Multiple hostnames do not
6+
# work with the `catalog-only` or `bootstrap-then-exit` options.
47
# @param parser [OptionParser object] The OptionParser argument
58
# @param options [Hash] Options hash being constructed; this is modified in this method.
69

710
OctocatalogDiff::Cli::Options::Option.newoption(:hostname) do
811
has_weight 1
912

1013
def parse(parser, options)
11-
parser.on('--hostname HOSTNAME', '-n', 'Use PuppetDB facts from last run of hostname') do |hostname|
12-
options[:node] = hostname
14+
parser.on(
15+
'--hostname HOSTNAME1[,HOSTNAME2[,...]]',
16+
'-n',
17+
'Use PuppetDB facts from last run of a hostname or a comma separated list of multiple hostnames'
18+
) do |hostname|
19+
options[:node] = if hostname.include?(',')
20+
hostname.split(',')
21+
else
22+
hostname
23+
end
1324
end
1425
end
1526
end

octocatalog-diff.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ EOF
2727
s.add_runtime_dependency 'diffy', '>= 3.1.0'
2828
s.add_runtime_dependency 'httparty', '>= 0.11.0'
2929
s.add_runtime_dependency 'hashdiff', '>= 0.3.0'
30+
s.add_runtime_dependency 'parallel', '>= 1.12.0'
3031
s.add_runtime_dependency 'rugged', '>= 0.25.0b2'
3132

3233
s.add_development_dependency 'rspec', '~> 3.4.0'

spec/octocatalog-diff/tests/cli/options/hostname_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@
88
result = run_optparse(['-n', 'octonode.rspec'])
99
expect(result.fetch(:node, 'key-not-defined')).to eq('octonode.rspec')
1010
end
11+
12+
it 'should set multiple nodes when passed a series of nodes' do
13+
result = run_optparse(['-n', 'octonode1.rspec,octonode2.rspec'])
14+
expect(result.fetch(:node, 'key-not-defined')).to eq(%w[octonode1.rspec octonode2.rspec])
15+
end
1116
end
1217
end

0 commit comments

Comments
 (0)