class Launcher

Public Class Methods

new(params) click to toggle source
# 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
usage() click to toggle source
# File lib/satops.rb, line 311
def self.usage
  puts syntax
  exit
end

Public Instance Methods

init_source() click to toggle source
# 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_target() click to toggle source
# 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) click to toggle source
# File lib/satops.rb, line 316
def operation_size?(param, size)
  if param.size != size
    Launcher.usage
  end
end
run() { || ... } click to toggle source
# File lib/satops.rb, line 463
def run
  puts "###\nExcuting #@file_to_run\n###"
  yield
end