Skip to content

Commit 71401dd

Browse files
committed
Add synchronous awesome spawn runner
1 parent 74216cf commit 71401dd

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

examples/awesome.asl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"Comment": "Directory Listing",
3+
"StartAt": "a",
4+
"States": {
5+
"a":{
6+
"Type": "Pass",
7+
"Next": "b"
8+
},
9+
"b": {
10+
"Type": "Wait",
11+
"Seconds": 1,
12+
"Next": "ls"
13+
},
14+
"ls": {
15+
"Type": "Task",
16+
"Resource": "awesome://ls -l Gemfile",
17+
"Comment": "awesome://ls -l $FILENAME",
18+
"Next": "c",
19+
"Parameters": {
20+
"FILENAME" : "Gemfile"
21+
}
22+
},
23+
"c": {
24+
"Type": "Succeed"
25+
}
26+
}
27+
}

exe/floe

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
66
require "optimist"
77
require "floe"
88
require "floe/container_runner"
9+
require "floe/awesome_runner"
910

1011
opts = Optimist.options do
1112
version("v#{Floe::VERSION}\n")

lib/floe/awesome_runner.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
module Floe
4+
class AwesomeRunner < Floe::Runner
5+
SCHEME = "awesome"
6+
SCHEME_PREFIX = "#{SCHEME}://"
7+
SCHEME_OFFSET = SCHEME.length + 3
8+
9+
def initialize(_options = {})
10+
require "awesome_spawn"
11+
12+
super
13+
end
14+
15+
# @return [Hash] runner_context
16+
def run_async!(resource, params = {}, _secrets = {}, _context = {})
17+
raise ArgumentError, "Invalid resource" unless resource&.start_with?(SCHEME_PREFIX)
18+
19+
args = resource[SCHEME_OFFSET..].split
20+
method = args.shift
21+
22+
runner_context = {}
23+
24+
# TODO: fix sanitization preventing params in args (e.g.: $PARAM1 => \$PARAM1)
25+
result = AwesomeSpawn.run(method, :env => params, :params => args)
26+
self.class.populate_results!(runner_context, :result => result)
27+
runner_context
28+
end
29+
30+
def status!(runner_context)
31+
end
32+
33+
def running?(runner_context)
34+
!runner_context["Output"]
35+
end
36+
37+
def success?(runner_context)
38+
!runner_context["Error"]
39+
end
40+
41+
def output(runner_context)
42+
runner_context["Output"]
43+
end
44+
45+
def cleanup(runner_context)
46+
end
47+
48+
# internal methods
49+
50+
def self.command_error_cause(command_result)
51+
command_result.error.nil? || command_result.error.empty? ? command_result.output.to_s : command_result.error.to_s
52+
end
53+
54+
def self.populate_results!(runner_context, result: nil, error: nil)
55+
error ||= command_error_cause(result) if result&.failure?
56+
57+
if error
58+
runner_context["Output"] = {"Error" => "States.TaskFailed", "Cause" => error}
59+
runner_context["Error"] = true
60+
else
61+
runner_context["Output"] = {"Result" => result.output.chomp.split("\n")}
62+
end
63+
64+
runner_context
65+
end
66+
end
67+
end
68+
69+
Floe::Runner.register_scheme(Floe::AwesomeRunner::SCHEME, Floe::AwesomeRunner.new)

spec/awesome_runner_spec.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
require_relative "../lib/floe/awesome_runner"
2+
3+
RSpec.describe Floe::AwesomeRunner, :uses_awesome_spawn => true do
4+
let(:subject) { described_class.new(runner_options) }
5+
let(:runner_options) { {} }
6+
let(:container_id) { SecureRandom.hex }
7+
8+
# let(:workflow) { make_workflow(ctx, {"State" => {"Type" => "Task", "Resource" => resource, "Parameters" => {"var1.$" => "$.foo.bar"}, "End" => true}}) }
9+
10+
describe "#run_async!" do
11+
it "raises an exception without a resource" do
12+
expect { subject.run_async!(nil) }.to raise_error(ArgumentError, "Invalid resource")
13+
end
14+
15+
it "raises an exception for an invalid resource uri" do
16+
expect { subject.run_async!("arn:abcd:efgh") }.to raise_error(ArgumentError, "Invalid resource")
17+
end
18+
19+
it "calls command run with the command name" do
20+
stub_good_run("ls", :params => [], :env => {}, :output => "file\nlisting\n")
21+
22+
subject.run_async!("awesome://ls")
23+
end
24+
25+
it "passes environment variables to command run" do
26+
stub_good_run("ls", :params => [], :env => {"FOO" => "BAR"}, :output => "file\nlisting\n")
27+
28+
subject.run_async!("awesome://ls", {"FOO" => "BAR"})
29+
end
30+
end
31+
32+
# describe "#status!" do
33+
# let(:runner_context) { {"container_ref" => container_id} }
34+
35+
# it "returns the updated container_state" do
36+
# stub_good_run!("ls", :params => ["inspect", container_id], :output => "[{\"State\": {\"Running\": true}}]")
37+
38+
# subject.status!(runner_context)
39+
40+
# expect(runner_context).to include("container_state" => {"Running" => true})
41+
# end
42+
# end
43+
44+
describe "#running?" do
45+
# it "retuns true when running" do
46+
# runner_context = {"container_ref" => container_id, "container_state" => {"Running" => true}}
47+
# expect(subject.running?(runner_context)).to be_truthy
48+
# end
49+
50+
# it "retuns false when not running" do
51+
# runner_context = {"container_ref" => container_id, "container_state" => {"Running" => false, "ExitCode" => 0}}
52+
# expect(subject.running?(runner_context)).to be_falsey
53+
# end
54+
end
55+
56+
describe "#success?" do
57+
# it "retuns true when successful" do
58+
# runner_context = {"container_ref" => container_id, "container_state" => {"Running" => false, "ExitCode" => 0}}
59+
# expect(subject.success?(runner_context)).to be_truthy
60+
# end
61+
62+
# it "retuns false when not successful" do
63+
# runner_context = {"container_ref" => container_id, "container_state" => {"Running" => false, "ExitCode" => 1}}
64+
# expect(subject.success?(runner_context)).to be_falsey
65+
# end
66+
end
67+
68+
describe "#output" do
69+
let(:runner_context) { {"Output" => ["output1", "output2"]} }
70+
71+
it "returns log output" do
72+
expect(subject.output(runner_context)).to eq(["output1", "output2"])
73+
end
74+
end
75+
76+
# describe "#cleanup" do
77+
# end
78+
end

0 commit comments

Comments
 (0)