Class: Launcher

Inherits:
Object
  • Object
show all
Defined in:
lib/satops.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Launcher

Returns a new instance of Launcher.



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/satops.rb', line 334

def initialize(params)
  @command=""
  @config_file=nil
  @file_to_run=nil
  @options=nil
  @log_file=STDOUT
  @sat_file=nil
  @sat_source=nil
  @sat_target=nil
  @ssl=false

  unless (params.include?('-s') && params.include?('-c')) || params.include?('show')
    Launcher.usage
  end

  until params.empty?
    case params[0]
    when '-c', '--config='
      params.shift
      @config_file=params[0]
    when '-d', '--debug'
      $DEBUG=true
    when '-h', '--help'
      Launcher.usage
    when '-t', '--tls'
      @ssl=true
      overwrite_net_http
    when '-l', '--log='
      params.shift
      @log_file=params[0]
    when '-s', '--satfile='
      params.shift
      @sat_file=params[0]
    when '-w', '--warnings'
      $VERBOSE=true
    when 'clone'
      operation_size?(params, 3)
      @command='clone'
      params.shift
      @name=params[0].to_sym
      params.shift
      @new_name=params[0]
    when 'destroy'
      operation_size?(params, 1)
      @command='destroy'
    when 'sync'
      operation_size?(params, 1)
      @command='sync'
    when 'extras'
      operation_size?(params, 1)
      @command='extra'
    when 'export'
      operation_size?(params, 3)
      @command='export'
      params.shift
      @path=params[0]
      params.shift
      @format=params[0].to_sym
    when 'import'
      operation_size?(params, 3)
      @command='import'
      params.shift
      @path=params[0]
      params.shift
      @format=params[0].to_sym
    when 'run'
      operation_size?(params, 2)
      @command='run'
      params.shift
      @file_to_run=params[0]
    when 'show'
      params.shift
      case params[0]
      when 'sat'
        puts satellites_syntax
      when 'config'
        puts config_syntax
      else
        puts "Use 'show sat' or 'show config' commands"
      end
      exit
    else
      Launcher.usage
    end
    params.shift
  end

  begin
    @log = Logger.new(@log_file)
    @log.datetime_format = "%d/%m/%Y %H:%M:%S"
    if $DEBUG
      @log.level = Logger::DEBUG
    else
      @log.level = Logger::INFO
    end
    @log.info("Starting #{@command.upcase} command")

    # Load satellites details file
    File.open(@sat_file) do |f|
      @sat_config = YAML.load(f)
    end

    # Load operations configuration file
    File.open(@config_file) do |f|
      @options = YAML.load(f)
    end

    case @command
    when 'clone'
      init_target
      SatOperator.new(@options, @log).clone(@name, @new_name)
    when 'destroy'
      init_target
      SatOperator.new(@options, @log).destroy(@sat_target)
    when 'export'
      init_source
      SatOperator.new(@options, @log).export(@format, @sat_source, @path)
    when 'extra'
      init_source
      init_target
      SatOperator.new(@options, @log).extra(@sat_source, @sat_target)
    when 'import'
      init_target
      SatOperator.new(@options, @log).import(@format, @sat_target, @path)
    when 'sync'
      init_source
      init_target
      SatOperator.new(@options, @log).sync(@sat_source, @sat_target)
    when 'run'
      def run
        puts "###\nExcuting #@file_to_run\n###"
        yield
      end
      init_source
      init_target
      lines=""
      File.open(@file_to_run).each do |line|
        lines << line
      end
      block=eval(lines)
      run(&block)
    end
  rescue SystemCallError => e
    @log.fatal "#{e}"
    exit
  ensure
    # Clean-up
    if @sat_source
      @sat_source.terminate
    end
    if @sat_target
      @sat_target.terminate
    end
    @log.info("Finished #{@command.upcase} command")
    @log.close
  end
end

Class Method Details

.usageObject



311
312
313
314
# File 'lib/satops.rb', line 311

def self.usage
  puts syntax
  exit
end

Instance Method Details

#init_sourceObject



322
323
324
325
326
# File 'lib/satops.rb', line 322

def init_source
  RHN::Session.running?(@sat_config.source.name, @ssl)
  @sat_source=RHN::Satellite.new(@sat_config.source, @ssl, @log)
  @sat_source.connect(@sat_config.source)
end

#init_targetObject



328
329
330
331
332
# File 'lib/satops.rb', line 328

def init_target
  RHN::Session.running?(@sat_config.target.name, @ssl)
  @sat_target=RHN::Satellite.new(@sat_config.target, @ssl, @log)
  @sat_target.connect(@sat_config.target)
end

#operation_size?(param, size) ⇒ Boolean

Returns:

  • (Boolean)


316
317
318
319
320
# File 'lib/satops.rb', line 316

def operation_size?(param, size)
  if param.size != size
    Launcher.usage
  end
end