class Dmatrix::Executor

Constants

Result

Attributes

combination[R]
executor[R]
log_dir[R]
run_command[R]

Public Class Methods

new(combination:, run_command:, log_dir:, executor: Kernel.method(:system)) click to toggle source
# File lib/dmatrix/executor.rb, line 9
def initialize(combination:, run_command:, log_dir:, executor: Kernel.method(:system))
  @combination = combination
  @run_command = run_command
  @log_dir = log_dir
  @executor = executor
end

Public Instance Methods

build_run() click to toggle source
# File lib/dmatrix/executor.rb, line 16
def build_run
  run_success = false
  build_success = build

  if build_success
    run_success = run
  end

  Result.new(build_success, run_success, tag)
end

Private Instance Methods

build() click to toggle source
# File lib/dmatrix/executor.rb, line 33
def build
  executor.call("docker build %{build_args} --tag %{tag} . > #{log_path(type: "build")} 2>&1" % build_params)
end
build_args() click to toggle source
# File lib/dmatrix/executor.rb, line 57
def build_args
  args = combination.aspects.select { |a| a[:type] == "build_arg" }

  if args.empty?
    return ""
  end

  args.map do |build_arg|
    "--build-arg #{build_arg.name}=#{build_arg.value}"
  end.join(" ")
end
build_params() click to toggle source
# File lib/dmatrix/executor.rb, line 41
def build_params
  { tag: tag, build_args: build_args }
end
env_args() click to toggle source
# File lib/dmatrix/executor.rb, line 69
def env_args
  args = combination.aspects.select { |a| a[:type] == "env" }

  if args.empty?
    return ""
  end

  args.map do |env_arg|
    "--env #{env_arg.name}=#{env_arg.value}"
  end.join(" ")
end
formatted_run_command() click to toggle source
# File lib/dmatrix/executor.rb, line 81
def formatted_run_command
  if run_command.nil? || run_command.empty?
    return ""
  end

  Shellwords.join(run_command)
end
image_tag() click to toggle source
# File lib/dmatrix/executor.rb, line 53
def image_tag
  @image_tag ||= ImageTag.new(values: combination.aspects.map(&:value))
end
log_path(type:) click to toggle source
# File lib/dmatrix/executor.rb, line 89
def log_path(type:)
  File.join(log_dir, "#{type}-#{tag.gsub(":", "-")}.log")
end
run() click to toggle source
# File lib/dmatrix/executor.rb, line 37
def run
  executor.call("docker run %{env_args} %{tag} %{run_command} > #{log_path(type: "run")} 2>&1" % run_params)
end
run_params() click to toggle source
# File lib/dmatrix/executor.rb, line 45
def run_params
  {
    tag: tag,
    env_args: env_args,
    run_command: formatted_run_command,
  }
end