Class: EcsSolo::Docker

Inherits:
Object
  • Object
show all
Defined in:
lib/ecs_solo/docker.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Docker

Returns a new instance of Docker.



3
4
5
6
7
# 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

Instance Method Details

#container_definitionObject



74
75
76
# File 'lib/ecs_solo/docker.rb', line 74

def container_definition
  @task_definition.container_definitions.first
end

#executeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

#imageObject



70
71
72
# File 'lib/ecs_solo/docker.rb', line 70

def image
  container_definition.image
end

#in_use?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/ecs_solo/docker.rb', line 39

def in_use?
  sh "docker ps -a -f name=#{name} --format '{{.Names}}' | grep #{name}"
end

#nameObject

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


66
67
68
# File 'lib/ecs_solo/docker.rb', line 66

def name
  "ecs-#{@task_definition.family}-#{container_definition.name}"
end

#pullObject



51
52
53
# File 'lib/ecs_solo/docker.rb', line 51

def pull
  sh "docker pull #{image}"
end

#rmObject



47
48
49
# File 'lib/ecs_solo/docker.rb', line 47

def rm
  sh "docker rm #{name}"
end

#runObject



34
35
36
37
# 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?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/ecs_solo/docker.rb', line 29

def running?
  out = sh "docker inspect -f '{{.State.Running}}' #{name}"
  out.strip == "true"
end

#sh(command) ⇒ Object



78
79
80
81
82
83
# File 'lib/ecs_solo/docker.rb', line 78

def sh(command)
  puts "=> #{command}"
  out = `#{command}`
  puts out
  out
end

#stopObject



43
44
45
# File 'lib/ecs_solo/docker.rb', line 43

def stop
  sh "docker stop #{name}"
end