class EcsSolo::Docker

Public Class Methods

new(options={}) click to toggle source
# File lib/ecs_solo/docker.rb, line 3
def initialize(options={})
  @options = options
  @task_definition = options[:task_definition] # describe_task_definition resp.task_definition
  @command = options[:command] # will be array
end

Public Instance Methods

container_definition() click to toggle source
# File lib/ecs_solo/docker.rb, line 74
def container_definition
  @task_definition.container_definitions.first
end
execute() click to toggle source
# File lib/ecs_solo/docker.rb, line 9
def execute
  unless in_use?
    run
    return
  end

  if @options[:force_new]
    puts "INFO: Forcing new container"
    stop
    rm
    run
  elsif !running?
    rm
    run
  else
    puts "WARN: container name is already in use".color(:yellow)
    puts "If you want to force a new container, use the --force-new option."
  end
end
image() click to toggle source
# File lib/ecs_solo/docker.rb, line 70
def image
  container_definition.image
end
in_use?() click to toggle source
# File lib/ecs_solo/docker.rb, line 39
def in_use?
  sh "docker ps -a -f name=#{name} --format '{{.Names}}' | grep #{name}"
end
name() click to toggle source

Almost resembles the name ecs-agent generates. Unsure how about the random looking id at the end though. Example:

ecs-demo-web-217-web-86a0bcbac2b7d1b92d00

So not including that.

Also not including revision to make this script simpler. So final result:

ecs-demo-web-web
# File lib/ecs_solo/docker.rb, line 66
def name
  "ecs-#{@task_definition.family}-#{container_definition.name}"
end
pull() click to toggle source
# File lib/ecs_solo/docker.rb, line 51
def pull
  sh "docker pull #{image}"
end
rm() click to toggle source
# File lib/ecs_solo/docker.rb, line 47
def rm
  sh "docker rm #{name}"
end
run() click to toggle source
# File lib/ecs_solo/docker.rb, line 34
def run
  docker_options = ENV['ECS_SOLO_DOCKER_RUN_OPTIONS']
  sh "docker run --name #{name} -d #{docker_options} #{image} #{@command}"
end
running?() click to toggle source
# File lib/ecs_solo/docker.rb, line 29
def running?
  out = sh "docker inspect -f '{{.State.Running}}' #{name}"
  out.strip == "true"
end
sh(command) click to toggle source
# File lib/ecs_solo/docker.rb, line 78
def sh(command)
  puts "=> #{command}"
  out = `#{command}`
  puts out
  out
end
stop() click to toggle source
# File lib/ecs_solo/docker.rb, line 43
def stop
  sh "docker stop #{name}"
end