- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6
[WIP] [POC] Awesome runner #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            kbrock
  wants to merge
  3
  commits into
  ManageIQ:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
kbrock:awesome_runner
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "Comment": "Directory Listing", | ||
| "StartAt": "a", | ||
| "States": { | ||
| "a":{ | ||
| "Type": "Pass", | ||
| "Next": "b" | ||
| }, | ||
| "b": { | ||
| "Type": "Wait", | ||
| "Seconds": 1, | ||
| "Next": "ls" | ||
| }, | ||
| "ls": { | ||
| "Type": "Task", | ||
| "Resource": "awesome://ls -l Gemfile", | ||
| "Comment": "awesome://ls -l $FILENAME", | ||
| "Next": "c", | ||
| "Parameters": { | ||
| "FILENAME" : "Gemfile" | ||
| } | ||
| }, | ||
| "c": { | ||
| "Type": "Succeed" | ||
| } | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| # frozen_string_literal: true | ||
|  | ||
| require "concurrent/array" | ||
|  | ||
| module Floe | ||
| class AwesomeProcess < Thread | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would have preferred not using a thread per process and not using an  | ||
| attr_reader :result | ||
| attr_accessor :error | ||
|  | ||
| def initialize(queue, context, *args) | ||
| self.report_on_exception = true | ||
| @processed = false | ||
| @context = context | ||
|  | ||
| # Don't like changing the value of context here, | ||
| # but want to make sure thread is set before the `queue.push` | ||
| # `queue.pop` will look potentially at status, which is through thread | ||
| context["thread"] = self | ||
|  | ||
| super do | ||
| @result = AwesomeSpawn.run(*args) | ||
|  | ||
| # this is changing the value of the context | ||
| # in the non-main thread | ||
| # Potential race condition here | ||
| Floe::AwesomeRunner.populate_results!(@context, :result => @result) | ||
|  | ||
| # trigger an event | ||
| queue.push(["delete", context]) | ||
| rescue => err | ||
| # Shouldn't ever get in here | ||
| @error = err | ||
|  | ||
| Floe::AwesomeRunner.populate_results!(@context, :error => err) | ||
|  | ||
| # trigger an event | ||
| queue.push(["delete", context]) | ||
| end | ||
| end | ||
| end | ||
|  | ||
| class AwesomeRunner < Floe::Runner | ||
| SCHEME = "awesome" | ||
| SCHEME_PREFIX = "#{SCHEME}://" | ||
| SCHEME_OFFSET = SCHEME.length + 3 | ||
|  | ||
| # only exposed for tests | ||
| # use wait instead | ||
| attr_reader :queue | ||
|  | ||
| def initialize(_options = {}) | ||
| require "awesome_spawn" | ||
|  | ||
| # events triggered | ||
| @queue = Queue.new | ||
|  | ||
| super | ||
| end | ||
|  | ||
| # @return [Hash] runner_context | ||
| def run_async!(resource, params = {}, _secrets = {}, _context = {}) | ||
| raise ArgumentError, "Invalid resource" unless resource&.start_with?(SCHEME_PREFIX) | ||
|  | ||
| args = resource[SCHEME_OFFSET..].split | ||
| method = args.shift | ||
|  | ||
| runner_context = {} | ||
|  | ||
| # NOTE: this adds itself to the runner_context | ||
| AwesomeProcess.new(@queue, runner_context, method, :env => params, :params => args) | ||
|  | ||
| runner_context | ||
| end | ||
|  | ||
| def status!(runner_context) | ||
| # check if it has no output (i.e.: we think it is running) but it is not running | ||
| if !runner_context.key?("Output") && !runner_context["thread"]&.alive? | ||
| runner_context["Output"] = {"Error" => "Lambda.Unknown", "Cause" => "no output and no thread"} | ||
| runner_context["Error"] = true | ||
| end | ||
| end | ||
|  | ||
| def running?(runner_context) | ||
| !runner_context["Output"] | ||
| end | ||
|  | ||
| def success?(runner_context) | ||
| !runner_context["Error"] | ||
| end | ||
|  | ||
| def output(runner_context) | ||
| runner_context["Output"] | ||
| end | ||
|  | ||
| def cleanup(runner_context) | ||
| runner_context["thread"] = nil | ||
| end | ||
|  | ||
| def wait(timeout: nil, _events: %i[create update delete]) | ||
| # TODO: implement whole interface | ||
| raise "wait needs a block and doesn't support timeout" unless timeout.nil? && block_given? | ||
|  | ||
| loop do | ||
| event_context = @queue.pop | ||
| yield event_context if block_given? | ||
| end | ||
| end | ||
|  | ||
| # internal methods | ||
|  | ||
| def self.command_error_cause(command_result) | ||
| command_result.error.nil? || command_result.error.empty? ? command_result.output.to_s : command_result.error.to_s | ||
| end | ||
|  | ||
| def self.populate_results!(runner_context, result: nil, error: nil) | ||
| error ||= command_error_cause(result) if result&.failure? | ||
|  | ||
| if error | ||
| runner_context["Output"] = {"Error" => "States.TaskFailed", "Cause" => error} | ||
| runner_context["Error"] = true | ||
| else | ||
| runner_context["Output"] = {"Result" => result.output.chomp.split("\n")} | ||
| end | ||
|  | ||
| runner_context | ||
| end | ||
| end | ||
| end | ||
|  | ||
| Floe::Runner.register_scheme(Floe::AwesomeRunner::SCHEME, Floe::AwesomeRunner.new) | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| require "floe" | ||
| require "floe/container_runner" | ||
| require "floe/awesome_runner" | ||
|  | ||
| module Floe | ||
| class CLI | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| require_relative "../lib/floe/awesome_runner" | ||
|  | ||
| RSpec.describe Floe::AwesomeRunner, :uses_awesome_spawn => true do | ||
| let(:subject) { described_class.new(runner_options) } | ||
| let(:runner_options) { {} } | ||
| let(:container_id) { SecureRandom.hex } | ||
|  | ||
| # let(:workflow) { make_workflow(ctx, {"State" => {"Type" => "Task", "Resource" => resource, "Parameters" => {"var1.$" => "$.foo.bar"}, "End" => true}}) } | ||
|  | ||
| describe "#run_async!" do | ||
| it "raises an exception without a resource" do | ||
| expect { subject.run_async!(nil) }.to raise_error(ArgumentError, "Invalid resource") | ||
| end | ||
|  | ||
| it "raises an exception for an invalid resource uri" do | ||
| expect { subject.run_async!("arn:abcd:efgh") }.to raise_error(ArgumentError, "Invalid resource") | ||
| end | ||
|  | ||
| it "calls command run with the command name" do | ||
| stub_good_run("ls", :params => [], :env => {}, :output => "file\nlisting\n") | ||
|  | ||
| subject.run_async!("awesome://ls") | ||
| subject.queue.pop | ||
| end | ||
|  | ||
| it "passes environment variables to command run" do | ||
| stub_good_run("ls", :params => [], :env => {"FOO" => "BAR"}, :output => "file\nlisting\n") | ||
|  | ||
| subject.run_async!("awesome://ls", {"FOO" => "BAR"}) | ||
| subject.queue.pop | ||
| end | ||
| end | ||
|  | ||
| # describe "#status!" do | ||
| # let(:runner_context) { {"container_ref" => container_id} } | ||
|  | ||
| # it "returns the updated container_state" do | ||
| # stub_good_run!("ls", :params => ["inspect", container_id], :output => "[{\"State\": {\"Running\": true}}]") | ||
|  | ||
| # subject.status!(runner_context) | ||
|  | ||
| # expect(runner_context).to include("container_state" => {"Running" => true}) | ||
| # end | ||
| # end | ||
|  | ||
| describe "#running?" do | ||
| # it "retuns true when running" do | ||
| # runner_context = {"container_ref" => container_id, "container_state" => {"Running" => true}} | ||
| # expect(subject.running?(runner_context)).to be_truthy | ||
| # end | ||
|  | ||
| # it "retuns false when not running" do | ||
| # runner_context = {"container_ref" => container_id, "container_state" => {"Running" => false, "ExitCode" => 0}} | ||
| # expect(subject.running?(runner_context)).to be_falsey | ||
| # end | ||
| end | ||
|  | ||
| describe "#success?" do | ||
| # it "retuns true when successful" do | ||
| # runner_context = {"container_ref" => container_id, "container_state" => {"Running" => false, "ExitCode" => 0}} | ||
| # expect(subject.success?(runner_context)).to be_truthy | ||
| # end | ||
|  | ||
| # it "retuns false when not successful" do | ||
| # runner_context = {"container_ref" => container_id, "container_state" => {"Running" => false, "ExitCode" => 1}} | ||
| # expect(subject.success?(runner_context)).to be_falsey | ||
| # end | ||
| end | ||
|  | ||
| describe "#output" do | ||
| let(:runner_context) { {"Output" => ["output1", "output2"]} } | ||
|  | ||
| it "returns log output" do | ||
| expect(subject.output(runner_context)).to eq(["output1", "output2"]) | ||
| end | ||
| end | ||
|  | ||
| # describe "#cleanup" do | ||
| # end | ||
| end | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking we should adhere to the AwesomeSpawn style of params handling, so something roughtly like:
Though, I'm not sure how we would handle duplicate parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aah. I think
$FILENAMEdid not work and that is why I put that comment in there.Couldn't get shell env var expansion working. (There was a legit use case in there)