Skip to content

Commit fca48eb

Browse files
committed
Add an "--action reindex" for the octofacts-updater CLI
1 parent f3490c2 commit fca48eb

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

lib/octofacts_updater/cli.rb

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def initialize(argv)
1616
end
1717

1818
opts.on("-c", "--config <config_file>", String, "Path to configuration file") do |f|
19-
raise "Invalid configuration file" unless File.file?(f)
20-
@opts[:config] = f
19+
@opts[:config] = File.expand_path(f)
20+
raise "Invalid configuration file #{@opts[:config].inspect}" unless File.file?(@opts[:config])
2121
end
2222

2323
opts.on("-H", "--hostname <hostname>", String, "FQDN of the host whose facts are to be gathered") do |h|
@@ -70,8 +70,9 @@ def usage
7070
puts "Usage: octofacts-updater --action <action> [--config-file /path/to/config.yaml] [other options]"
7171
puts ""
7272
puts "Available actions:"
73-
puts " bulk: Update fixtures and index in bulk"
74-
puts " facts: Obtain facts for one node (requires --hostname <hostname>)"
73+
puts " bulk: Update fixtures and index in bulk"
74+
puts " facts: Obtain facts for one node (requires --hostname <hostname>)"
75+
puts " reindex: Build a new index from the existing fact fixtures"
7576
puts ""
7677
end
7778

@@ -106,6 +107,7 @@ def run
106107

107108
return handle_action_bulk if opts[:action] == "bulk"
108109
return handle_action_facts if opts[:action] == "facts"
110+
return handle_action_bulk if opts[:action] == "reindex"
109111

110112
usage
111113
exit 255
@@ -126,19 +128,39 @@ def substitute_relative_paths!(object_in, basedir)
126128
end
127129
end
128130

129-
def handle_action_bulk
130-
facts_to_index = @config.fetch("index", {})["indexed_facts"]
131-
unless facts_to_index.is_a?(Array)
132-
raise ArgumentError, "Must declare index:indexed_facts in configuration to use bulk update"
133-
end
131+
def nodes_for_bulk
132+
if opts[:action] == "reindex"
133+
@opts[:quick] = true
134+
135+
path = if opts[:path]
136+
File.expand_path(opts[:path])
137+
elsif @config.fetch("index", {})["node_path"]
138+
File.expand_path(@config.fetch("index", {})["node_path"], File.dirname(opts[:config]))
139+
else
140+
raise ArgumentError, "Must set --path, or define index:node_path to a valid directory in configuration"
141+
end
142+
143+
unless File.directory?(path)
144+
raise Errno::ENOENT, "--path must be a directory (#{path.inspect} is not)"
145+
end
134146

135-
nodes = if opts[:host_list]
147+
Dir.glob("#{path}/*.yaml").map { |f| File.basename(f, ".yaml") }
148+
elsif opts[:host_list]
136149
opts[:host_list]
137150
elsif opts[:hostname]
138151
[opts[:hostname]]
139152
else
140153
OctofactsUpdater::FactIndex.load_file(index_file).nodes(true)
141154
end
155+
end
156+
157+
def handle_action_bulk
158+
facts_to_index = @config.fetch("index", {})["indexed_facts"]
159+
unless facts_to_index.is_a?(Array)
160+
raise ArgumentError, "Must declare index:indexed_facts in configuration to use bulk update"
161+
end
162+
163+
nodes = nodes_for_bulk
142164
if nodes.empty?
143165
raise ArgumentError, "Cannot run bulk update with no nodes to check"
144166
end

0 commit comments

Comments
 (0)