Method: Rake::Task.gl_run

Defined in:
lib/rake/config.rb

.gl_run(*args, &aBlock) ⇒ Object

Execute command (as is) on local_host using greenletters to enable expect like behaviour (useful for dealling with passwords).



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/rake/config.rb', line 654

def self.gl_run(*args, &aBlock)
  $TRACE = Rake.application.options.trace;

  options = Hash.new;
  if args.last.is_a?(Construct) then 
    raise ConfigurationError, "gl_run should not be passed a Construct";
  elsif args.last.is_a?(Hash)
    options = args.pop;
  end
  Conf.timeout = Hash.new() unless Conf.has_key?(:timeout);
  Conf.timeout.maxTimeout = 10 unless Conf.timeout.has_key?(:maxTimeout);
  if options.has_key?(:timeout) then
    options[:timeout] = Conf.timeout.maxTimeout if Conf.timeout.maxTimeout.to_i < options[:timeout].to_i ;
  else
    options[:timeout] = 10;
  end
  Conf.retries = 5 unless Conf.has_key?(:retries)
  options[:retries] = Conf.retries unless options.has_key?(:retries)

  Rake::Application.mesg args.join(' ') if $TRACE
  Rake::Application.mesg "Timeout: #{options[:timeout]}" if $TRACE
  Rake::Application.mesg "#{options}" if $TRACE && !options.empty?() && options.has_key?(:verbose);
  Rake::Application.mesg "Retries: #{options[:retries]}" if $TRACE

  asyncTriggersBlocks = Array.new;
  if options.has_key? :asyncTriggersBlocks then
    asyncTriggersBlocks = options[:asyncTriggersBlocks];
  end

  exitStatus = 0;
  if options.has_key? :exitStatus then
    exitStatus = options[:exitStatus];
  end

  options[:env] = ENV unless options.has_key?(:env);
  options[:transcript] = Rake::Application.logger unless options.has_key?(:transcript);

  attempt = 0
  tryAgain = true
  cmdProcess = nil
  while(tryAgain) do 
    attempt += 1
    tryAgain = false
    begin
      cmdProcess = Greenletters::Process.new(*args, options);
      cmdProcess.start!;

      asyncTriggersBlocks.each do | asyncTriggersBlock |
        if asyncTriggersBlock.kind_of? Proc then
          asyncTriggersBlock.call(cmdProcess);
        end
      end

      aBlock.call(cmdProcess) unless aBlock.nil?;

      cmdProcess.wait_for(:exit, exitStatus);
    rescue StandardError => se
      Rake::Application.mesg "Failed attempt #{attempt}" if $TRACE
      tryAgain = true
      raise se unless attempt < options[:retries]
    end
  end

  cmdProcess
end