Class: EcsSolo::Docker
- Inherits:
-
Object
- Object
- EcsSolo::Docker
- Defined in:
- lib/ecs_solo/docker.rb
Instance Method Summary collapse
- #container_definition ⇒ Object
- #execute ⇒ Object
- #image ⇒ Object
- #in_use? ⇒ Boolean
-
#initialize(options = {}) ⇒ Docker
constructor
A new instance of Docker.
-
#name ⇒ Object
Almost resembles the name ecs-agent generates.
- #pull ⇒ Object
- #rm ⇒ Object
- #run ⇒ Object
- #running? ⇒ Boolean
- #sh(command) ⇒ Object
- #stop ⇒ Object
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 = @task_definition = [:task_definition] # describe_task_definition resp.task_definition @command = [:command] # will be array end |
Instance Method Details
#container_definition ⇒ Object
74 75 76 |
# File 'lib/ecs_solo/docker.rb', line 74 def container_definition @task_definition.container_definitions.first end |
#execute ⇒ Object
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 |
#image ⇒ Object
70 71 72 |
# File 'lib/ecs_solo/docker.rb', line 70 def image container_definition.image end |
#in_use? ⇒ 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 |
#name ⇒ Object
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 |
#pull ⇒ Object
51 52 53 |
# File 'lib/ecs_solo/docker.rb', line 51 def pull sh "docker pull #{image}" end |
#rm ⇒ Object
47 48 49 |
# File 'lib/ecs_solo/docker.rb', line 47 def rm sh "docker rm #{name}" end |
#run ⇒ Object
34 35 36 37 |
# File 'lib/ecs_solo/docker.rb', line 34 def run = ENV['ECS_SOLO_DOCKER_RUN_OPTIONS'] sh "docker run --name #{name} -d #{} #{image} #{@command}" end |
#running? ⇒ 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 |
#stop ⇒ Object
43 44 45 |
# File 'lib/ecs_solo/docker.rb', line 43 def stop sh "docker stop #{name}" end |