Class: Simp::Cli::Commands::Config

Inherits:
Simp::Cli
  • Object
show all
Defined in:
lib/simp/cli/commands/config.rb

Overview

Handle CLI interactions for “simp config”

Constant Summary

Constants inherited from Simp::Cli

VERSION

Class Method Summary collapse

Methods inherited from Simp::Cli

help, menu

Class Method Details

.read_answers_file(file) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/simp/cli/commands/config.rb', line 165

def self.read_answers_file file
  answers_hash = {}    # Read the input file

  if file
    unless File.exist?(file)
      raise "Could not access the file '#{file}'!"
    end
  else
    file = @options[:system_file]
  end

  begin
    answers_hash = YAML.load(File.read(file))
    answers_hash.empty?
  rescue Errno::EACCES
    error = "WARNING: Could not access the answers file '#{file}'!"
    say "<%= color(%q{#{error}}, YELLOW) %>\n"
  rescue
    # If the file existed, but ingest failed, then there's a problem
    raise "System Configuration File: '#{file}' is corrupted.\nReview the file and either fix or remove it before trying again."
  end

  answers_hash
end

.remove_saved_sessionObject



157
158
159
160
161
162
# File 'lib/simp/cli/commands/config.rb', line 157

def self.remove_saved_session
  if file = @options.fetch( :output_file )
    _file = File.join( File.dirname( file ), ".#{File.basename( file )}" )
    FileUtils.rm_f( _file, :verbose => false ) if File.file?( _file )
  end
end

.run(args = []) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/simp/cli/commands/config.rb', line 190

def self.run(args = [])
  begin
    super # parse @options
  rescue OptionParser::InvalidOption=> e
    error = "ERROR: #{e.message}"
    say "\n<%= color(%q{#{error}}, RED) %>\n"
    puts @opt_parser
    exit 1
  end

  # Ensure that custom facts are available before the first pluginsync
  %x{puppet config print modulepath}.strip.split(':').each do |dir|
    next unless File.directory?(dir)
    Find.find(dir) do |mod_path|
      fact_path = File.expand_path('lib/facter', mod_path)
      Facter.search(fact_path) if File.directory?(fact_path)
      Find.prune unless mod_path == dir
    end
  end

  # read in answers file
  answers_hash = {}
  if file = @options.fetch( :input_file )
    answers_hash = read_answers_file( file )
  end

  # NOTE: answers from an interrupted session take precedence over input file
  answers_hash = saved_session.merge( answers_hash )

  # NOTE: answers provided from the cli take precedence over everything else
  cli_answers  = Hash[ ARGV[1..-1].map{ |x| x.split '=' } ]
  answers_hash = answers_hash.merge( cli_answers )

  # get the list of items
  #  - applies any known answers at this point
  item_list          = Simp::Cli::Config::ItemListFactory.new( @options ).process( nil, answers_hash )

  # process items:
  #  - get any remaining answers
  #  - apply changes as needed
  questionnaire      = Simp::Cli::Config::Questionnaire.new( @options )
  answers            = questionnaire.process( item_list, {} )

  remove_saved_session
end

.saved_sessionObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/simp/cli/commands/config.rb', line 111

def self.saved_session
  result = {}
  if @options.fetch( :use_safety_save, false ) && file = @options.fetch( :output_file )
    _file = File.join( File.dirname( file ), ".#{File.basename( file )}" )
    if File.file?( _file )
      lines      = File.open( _file, 'r' ).readlines
      saved_hash = read_answers_file _file
      last_item  = nil
      if saved_hash.keys.size > 0
        last_item = {saved_hash.keys.last =>
                     saved_hash[ saved_hash.keys.last ]}.to_yaml.gsub( /^---/, '' ).strip
      end

      message = %Q{WARNING: interrupted session detected!}
      say "<%= color(%q{*** #{message} ***}, YELLOW, BOLD) %> \n\n"
      say "<%= color(%q{An automatic safety-save file from a previous session has been found at:}, YELLOW) %> \n\n"
      say "      <%= color( %q{#{_file}}, BOLD ) %>\n\n"
      if last_item
        say "<%= color(%q{The most recent answer from this session was:}, YELLOW) %> \n\n"
        say "<%= color( %q{#{last_item.gsub( /^/, "      \0" )}} ) %>\n\n"
      end

      if @options.fetch( :autoaccept_safety_save, false )
        color = 'YELLOW'
        say "<%= color(%q{Automatically resuming these answers because }, #{color}) %>" +
            "<%= color(%q{--accept-safety-save}, BOLD, #{color}) %>" +
            "<%= color(%q{ is active.}, #{color}) %>\n\n"
        result = saved_hash
      else
        say "<%= color(%q{You can resume these answers or delete the file.}, YELLOW) %>\n\n"

        if agree( "resume the session? (no = deletes file)" ){ |q| q.default = 'yes' }
          say "\n<%= color( %q{applying answers from '#{_file}'}, GREEN )%>\n"
          result = saved_hash
        else
          say "\n<%= color( %q{removing file '#{_file}'}, RED )%>\n"
          FileUtils.rm_f _file, :verbose => true
        end
      end
      sleep 1
    end
  end
  result
end