class Bolts::Docker

Public Instance Methods

bash_scripts() click to toggle source
# File lib/bolts/docker.rb, line 75
def bash_scripts
  File.expand_path("../../bash_scripts", __FILE__)
end
create_container_data() click to toggle source

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
# 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_path() click to toggle source
# File lib/bolts/docker.rb, line 31
def data_path
  "/tmp/bolts"
end
env_file_data(environment) click to toggle source

environment - [{:name=>“AUTH_TOKEN”, :value=>“xxx”}, {:name=>“RAILS_LOG_TO_STDOUT”, :value=>“1”}]

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

AUTH_TOKEN=xxx
RAILS_LOG_TO_STDOUT=1
# 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
exec() click to toggle source
# 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) click to toggle source
# File lib/bolts/docker.rb, line 35
def execute(command)
  puts "Running: #{command}"
  system(command)
end
run() click to toggle source

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

# 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
setup() click to toggle source
# 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