class EcsOneshot::CLI

Public Instance Methods

run(args = ARGV) click to toggle source
# File lib/ecs_oneshot/cli.rb, line 9
def run(args = ARGV)
  options = parse_options(args)

  run_task(options)
rescue Error => e
  warn e.message
  exit 1
end

Private Instance Methods

load_config(options) click to toggle source
# File lib/ecs_oneshot/cli.rb, line 20
def load_config(options)
  path = options[:config]
  env = options[:environment]
  config = File.exist?(path) ? Config.load(path, env) : Config.new

  other = Config.safe_build(options)
  config.merge(other)
end
parse_options(args) click to toggle source
# File lib/ecs_oneshot/cli.rb, line 43
def parse_options(args) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  opts = OptionParser.new
  opts.banner = "Usage: ecs_oneshot [options] -- <commmand>"
  opts.version = VERSION

  opts.on("-c", "--config FILE", "Specify configuration file. (default: .ecs_oneshot.yml)")
  opts.on("-e", "--environment ENVIRONMENT", "Specify environment. (default: production)")
  opts.on("--cluster CLUSTER")
  opts.on("--service SERVICE")
  opts.on("--task-definition TASK_DEFINITION")
  opts.on("--container CONTAINER")

  {}.tap do |h|
    h[:command] = opts.parse(args, into: h)
    h[:config] ||= ".ecs_oneshot.yml"
    h[:environment] ||= "production"
    h[:task_definition] = h.delete(:"task-definition")
  end
end
run_task(options) click to toggle source
# File lib/ecs_oneshot/cli.rb, line 29
def run_task(options)
  config = load_config(options)
  raise Error, "<command> is required." if config.command.empty?

  t = Task.new(config)
  t.run
  puts "Task started. Watch this task's details in the Amazon ECS console: #{t.console_url}\n\n"
  puts "=== Wait for Task Starting..."
  t.wait_running
  puts "=== Following Logs..."
  t.each_log { |log| puts(log) }
  puts "\n=== Task Stopped."
end