Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
add support for passing options to nomad
Browse files Browse the repository at this point in the history
  • Loading branch information
David Bresson committed Dec 9, 2020
1 parent cfd08f2 commit 06177fb
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Or install it yourself as:

Create one or more environment files and one or more ERB job templates, and then execute:

$ bedouin <environment file> <template1> <template2> ...
$ bedouin [nomad option ...] [--] <environment file> <template1> <template2> ...

Bedouin will evaluate each template with any attributes from the environment file available as instance variables. Bedouin will then run the results of each with "nomad run".

Expand Down
2 changes: 1 addition & 1 deletion bedouin.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "erubis", "~> 2.7"

spec.add_development_dependency "bundler", "~> 1.12"
spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
end
10 changes: 6 additions & 4 deletions lib/bedouin/cli.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
require 'bedouin/cli/params'
module Bedouin
class CLI
def execute(env_path, *template_paths)
e = Bedouin.environment_for(env_path)
def execute(*params)
p = Params.new(params)
e = Bedouin.environment_for(p.env_path)

template_paths.lazy.map do |path|
p.template_paths.lazy.map do |path|
t = Bedouin.template_for(path)
j = Bedouin::Job.new(e, t)
Bedouin::Runner.new.run(j)
Bedouin::Runner.new.run(job: j, opts: p.opts)
end.reduce(0) do |m,j|
puts j.to_s
j.status == 0 ? m : 1
Expand Down
21 changes: 21 additions & 0 deletions lib/bedouin/cli/params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Bedouin
class CLI
class Params
attr_reader :opts, :env_path, :template_paths
def initialize(params)
index_of_opt_terminator = params.find_index('--')
unchecked_params = params.slice!((index_of_opt_terminator)..-1) if index_of_opt_terminator

if args_count = params.reverse_each.find_index {|p| p.start_with? '-' }
args, @opts = params.pop(args_count), params
else
args, @opts = params, []
end

args.concat(unchecked_params.drop(1)) if unchecked_params

@env_path, *@template_paths = *args
end
end
end
end
4 changes: 2 additions & 2 deletions lib/bedouin/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def initialize(cfg = { nomad: "nomad" })
@cfg = cfg
end

def run(job)
Open3.popen3(@cfg[:nomad], "run", job.file_path) do |stdin, stdout, stderr, wait_thr|
def run(job:, opts: [])
Open3.popen3(@cfg[:nomad], "run", *opts, '--', job.file_path) do |stdin, stdout, stderr, wait_thr|
job.status = wait_thr.value
job.stdout = stdout.read
job.stderr = stderr.read
Expand Down
2 changes: 1 addition & 1 deletion lib/bedouin/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Bedouin
VERSION = "0.1.0"
VERSION = "0.2.0"
end
62 changes: 62 additions & 0 deletions spec/bedouin/cli/params_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'spec_helper'
require 'bedouin/cli/params'

describe Bedouin::CLI::Params do
[
{
params: %w(environment foo),
expect: {
opts: [],
env_path: 'environment',
template_paths: ['foo']
}
},
{
params: %w(-- environment foo),
expect: {
opts: [],
env_path: 'environment',
template_paths: ['foo']
}
},
{
params: %w(environment foo bar),
expect: {
opts: [],
env_path: 'environment',
template_paths: ['foo', 'bar']
}
},
{
params: %w(-a -b -c environment foo bar),
expect: {
opts: ['-a', '-b', '-c'],
env_path: 'environment',
template_paths: ['foo', 'bar']
}
},
{
params: %w(-a -b -c -- environment foo bar),
expect: {
opts: ['-a', '-b', '-c'],
env_path: 'environment',
template_paths: ['foo', 'bar']
}
},
{
params: %w(-a -b -c -- -environment foo bar),
expect: {
opts: ['-a', '-b', '-c'],
env_path: '-environment',
template_paths: ['foo', 'bar']
}
}
].each do |c|
context "for various parameter situations" do
it "extracts cli parameters into opts, env_path, and template_paths" do
p = Bedouin::CLI::Params.new(c[:params])
expect(p).to have_attributes(c[:expect])
end
end
end
end
17 changes: 13 additions & 4 deletions spec/bedouin/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@
it "runs nomad's 'run' command with the job's tempfile" do
job = instance_double("job", file_path: "/foo/bar")

expect(Open3).to receive(:popen3) { ["nomad", "run", "/foo/bar"] }
expect(Open3).to receive(:popen3) { ["nomad", "run", "-", "/foo/bar"] }

r = Bedouin::Runner.new
r.run(job)
r.run(job: job)
end

it "returns the job with status, stdout, and stderr set" do
job = instance_double("job", file_path: "/foo/bar")
expect(job).to receive(:status=).with(0)
expect(job).to receive(:stdout=).with("run /foo/bar\n")
expect(job).to receive(:stdout=).with("run -- /foo/bar\n")
expect(job).to receive(:stderr=).with("")

runner = Bedouin::Runner.new({ nomad: "echo" })
expect(runner.run(job)).to be job
expect(runner.run(job: job)).to be job
end

it "accepts options and passes them to nomad" do
job = instance_double("job", file_path: "/foo/bar")

expect(Open3).to receive(:popen3) { ["nomad", "run", "--foo", "-", "/foo/bar"] }

r = Bedouin::Runner.new
r.run(job: job, opts: ["-foo"])
end
end

0 comments on commit 06177fb

Please sign in to comment.