@@ -16,8 +16,8 @@ def initialize(argv)
16
16
end
17
17
18
18
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 ] )
21
21
end
22
22
23
23
opts . on ( "-H" , "--hostname <hostname>" , String , "FQDN of the host whose facts are to be gathered" ) do |h |
@@ -70,8 +70,9 @@ def usage
70
70
puts "Usage: octofacts-updater --action <action> [--config-file /path/to/config.yaml] [other options]"
71
71
puts ""
72
72
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"
75
76
puts ""
76
77
end
77
78
@@ -106,6 +107,7 @@ def run
106
107
107
108
return handle_action_bulk if opts [ :action ] == "bulk"
108
109
return handle_action_facts if opts [ :action ] == "facts"
110
+ return handle_action_bulk if opts [ :action ] == "reindex"
109
111
110
112
usage
111
113
exit 255
@@ -126,19 +128,39 @@ def substitute_relative_paths!(object_in, basedir)
126
128
end
127
129
end
128
130
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
134
146
135
- nodes = if opts [ :host_list ]
147
+ Dir . glob ( "#{ path } /*.yaml" ) . map { |f | File . basename ( f , ".yaml" ) }
148
+ elsif opts [ :host_list ]
136
149
opts [ :host_list ]
137
150
elsif opts [ :hostname ]
138
151
[ opts [ :hostname ] ]
139
152
else
140
153
OctofactsUpdater ::FactIndex . load_file ( index_file ) . nodes ( true )
141
154
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
142
164
if nodes . empty?
143
165
raise ArgumentError , "Cannot run bulk update with no nodes to check"
144
166
end
0 commit comments