Class: Bolts::Docker

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

Instance Method Summary collapse

Methods inherited from Ssh

#build_ssh_host, #check_cluster_exists!, #check_service_exists!, #check_tasks_running!, #container_instance_or_task_arn?, #exit_on_specs, #find_by_container_instance, #find_by_task, #initialize, #instance_hostname, #instance_id?, #kernel_exec, #ssh_host, #task, #task_arns

Methods included from AwsServices

#ec2, #ecs

Methods included from Defaults

#default_cluster, #default_user, #settings

Constructor Details

This class inherits a constructor from Bolts::Ssh

Instance Method Details

#bash_scriptsObject



75
76
77
# File 'lib/bolts/docker.rb', line 75

def bash_scripts
  File.expand_path("../../bash_scripts", __FILE__)
end

#create_container_dataObject

Data that is needed in order to run a new docker container that mimics the docker container that is already running:

* task_arn
* env_vars
* image


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bolts/docker.rb', line 45

def create_container_data
  # For container env_vars and image info.
  task_definition_arn = task.task_definition_arn # task is a method in the superclass: Ssh
  response = ecs.describe_task_definition(task_definition: task_definition_arn)
  task_definition = response.to_h[:task_definition]
  container_definition = task_definition[:container_definitions].first # assumes care about the first container definition
  env_file_data = env_file_data(container_definition[:environment])

  bolts_folder = "/tmp/bolts"
  FileUtils.mkdir_p(bolts_folder) unless File.exist?(bolts_folder)
  IO.write("/tmp/bolts/task-arn.txt", task_arns.first)
  IO.write("/tmp/bolts/docker-image.txt", container_definition[:image])
  IO.write("/tmp/bolts/env-file.txt", env_file_data)
  FileUtils.cp_r(bash_scripts, "/tmp/bolts")
end

#data_pathObject



31
32
33
# File 'lib/bolts/docker.rb', line 31

def data_path
  "/tmp/bolts"
end

#env_file_data(environment) ⇒ Object

environment - [:value=>“xxx”, :value=>“1”]

Returns String with a simple form, the docker –env-file format

AUTH_TOKEN=xxx
RAILS_LOG_TO_STDOUT=1


67
68
69
70
71
72
73
# File 'lib/bolts/docker.rb', line 67

def env_file_data(environment)
  variables = []
  environment.each do |item|
    variables << "#{item[:name]}=#{item[:value]}"
  end
  variables.join("\n")
end

#execObject



16
17
18
19
20
# File 'lib/bolts/docker.rb', line 16

def exec
  setup
  docker_exec = "/tmp/bolts/bash_scripts/docker-exec.sh"
  kernel_exec("ssh", "-t", ssh_host, "bash #{docker_exec}")
end

#execute(command) ⇒ Object



35
36
37
38
# File 'lib/bolts/docker.rb', line 35

def execute(command)
  puts "Running: #{command}"
  system(command)
end

#runObject

I cannot name this run like ‘docker run’ because run is a keyword in Thor.



23
24
25
26
27
28
29
# File 'lib/bolts/docker.rb', line 23

def run
  setup
  docker_run = "/tmp/bolts/bash_scripts/docker-run.sh"
  # args = ["ssh", ssh_host, "bash #{docker_run} #{options[:docker_options]}"].compact
  args = ["ssh", "-t", ssh_host, "bash", docker_run, @options[:docker_command]].compact
  kernel_exec(*args)
end

#setupObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/bolts/docker.rb', line 5

def setup
  check_service_exists!
  check_tasks_running!
  create_container_data
  black_hole = " > /dev/null" unless @options[:verbose]
  black_hold = ''
  execute("scp -r #{data_path} #{ssh_host}:#{data_path} #{black_hole}")
  FileUtils.rm_rf(data_path) # clean up locally
  # the docker-exec.sh cleans up after itself and blows away /tmp/bolts
end