Class: Spiceweasel::CLI

Inherits:
Object
  • Object
show all
Includes:
Mixlib::CLI
Defined in:
lib/spiceweasel/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ CLI

Returns a new instance of CLI.



197
198
199
200
201
202
203
# File 'lib/spiceweasel/cli.rb', line 197

def initialize(argv=[])
  super()
  parse_and_validate_options
  Config.merge!(@config)
  configure_logging
  Spiceweasel::Log.debug("Validation of the manifest has been turned off.") if Spiceweasel::Config[:novalidation]
end

Instance Method Details

#configure_loggingObject



233
234
235
236
237
238
239
# File 'lib/spiceweasel/cli.rb', line 233

def configure_logging
  [Spiceweasel::Log, Chef::Log].each do |log_klass|
    log_klass.init(Spiceweasel::Config[:log_location])
    log_klass.level = Spiceweasel::Config[:log_level]
    log_klass.level = :debug if Spiceweasel::Config[:debug]
  end
end

#parse_and_validate_input(file) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/spiceweasel/cli.rb', line 241

def parse_and_validate_input(file)
  begin
    Spiceweasel::Log.debug("file: #{file}")
    if !File.file?(file)
      STDERR.puts "ERROR: #{file} is an invalid manifest file, please check your path."
      exit(-1)
    end
    if (file.end_with?(".yml"))
      output = YAML.load_file(file)
    elsif (file.end_with?(".json"))
      output = JSON.parse(File.read(file))
    elsif (file.end_with?(".rb"))
      output = self.instance_eval(IO.read(file), file, 1)
      output = JSON.parse(JSON.dump(output))
    else
      STDERR.puts "ERROR: #{file} is an unknown file type, please use a file ending with '.rb', '.json' or '.yml'."
      exit(-1)
    end
  rescue Psych::SyntaxError => e
    STDERR.puts e.message
    STDERR.puts "ERROR: Parsing error in #{file}."
    exit(-1)
  rescue JSON::ParserError => e
    STDERR.puts e.message
    STDERR.puts "ERROR: Parsing error in #{file}."
    exit(-1)
  rescue Exception => e
    STDERR.puts "ERROR: Invalid or missing  manifest .json, .rb, or .yml file provided."
    if(Spiceweasel::Config[:log_level].to_s == 'debug')
      STDERR.puts "ERROR: #{e}\n#{e.backtrace.join("\n")}"
    end
    exit(-1)
  end
  output
end

#parse_and_validate_optionsObject



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
# File 'lib/spiceweasel/cli.rb', line 205

def parse_and_validate_options
  ARGV << "-h" if ARGV.empty?
  begin
    parse_options
    # Load knife configuration if using knife config
    require 'chef/knife'
    knife = Chef::Knife.new
    # Only log on error during startup
    Chef::Config[:verbosity] = 0
    Chef::Config[:log_level] = :error
    if @config[:knifeconfig]
      knife.read_config_file(@config[:knifeconfig])
      Spiceweasel::Config[:knife_options] = " -c #{@config[:knifeconfig]} "
    else
      knife.configure_chef
    end
    if @config[:serverurl]
      Spiceweasel::Config[:knife_options] += "--server-url #{@config[:serverurl]} "
    end
    # NOTE: Only set cookbook path via config if path unset
    Spiceweasel::Config[:cookbook_dir] ||= Chef::Config[:cookbook_path]
  rescue OptionParser::InvalidOption => e
    STDERR.puts e.message
    puts opt_parser.to_s
    exit(-1)
  end
end

#process_manifest(manifest) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/spiceweasel/cli.rb', line 277

def process_manifest(manifest)
  berksfile = Berksfile.new(manifest['berksfile']) if manifest.include?('berksfile')
  if berksfile
    cookbooks = Cookbooks.new(manifest['cookbooks'], berksfile.cookbook_list)
    create = berksfile.create + cookbooks.create
    delete = berksfile.delete + cookbooks.delete
  else
    cookbooks = Cookbooks.new(manifest['cookbooks'])
    create = cookbooks.create
    delete = cookbooks.delete
  end
  environments = Environments.new(manifest['environments'], cookbooks)
  roles = Roles.new(manifest['roles'], environments, cookbooks)
  data_bags = DataBags.new(manifest['data bags'])
  nodes = Nodes.new(manifest['nodes'], cookbooks, environments, roles)
  clusters = Clusters.new(manifest['clusters'], cookbooks, environments, roles)

  create += environments.create + roles.create + data_bags.create + nodes.create + clusters.create
  delete += environments.delete + roles.delete + data_bags.delete + nodes.delete + clusters.delete
  return create, delete
end

#runObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/spiceweasel/cli.rb', line 154

def run
  if Spiceweasel::Config[:extractlocal] || Spiceweasel::Config[:extractjson] || Spiceweasel::Config[:extractyaml]
    manifest = Spiceweasel::ExtractLocal.parse_objects
  else
    manifest = parse_and_validate_input(ARGV.last)
    if Spiceweasel::Config[:clusterfile]
      # if we have a cluster file, override any nodes or clusters in the original manifest
      manifest['nodes'] = manifest['clusters'] = {}
      manifest.merge!(parse_and_validate_input(Spiceweasel::Config[:clusterfile]))
    end
  end
  Spiceweasel::Log.debug("file manifest: #{manifest}")

  create, delete = process_manifest(manifest)

  if Spiceweasel::Config[:extractjson]
    puts JSON.pretty_generate(manifest)
  elsif Spiceweasel::Config[:extractyaml]
    puts manifest.to_yaml
  elsif Spiceweasel::Config[:delete]
    if Spiceweasel::Config[:execute]
      Execute.new(delete)
    else
      puts delete unless delete.empty?
    end
  elsif Spiceweasel::Config[:rebuild]
    if Spiceweasel::Config[:execute]
      Execute.new(delete)
      Execute.new(create)
    else
      puts delete unless delete.empty?
      puts create unless create.empty?
    end
  else
    if Spiceweasel::Config[:execute]
      Execute.new(create)
    else
      puts create unless create.empty?
    end
  end
  exit 0
end