class Sonic::Docker

Public Instance Methods

bash_scripts() click to toggle source
# File lib/sonic/docker.rb, line 149
def bash_scripts
  File.expand_path("../../bash_scripts", __FILE__)
end
black_hole() click to toggle source
# File lib/sonic/docker.rb, line 153
def black_hole
  @options[:verbose] ? [] : %w[> /dev/null 2>&1]
end
build_host() click to toggle source
# File lib/sonic/docker.rb, line 50
def build_host
  host = @bastion ? bastion_host : ssh_host
  host = "#{@user}@#{host}" unless host.include?('@')
  host
end
call(script) click to toggle source
# File lib/sonic/docker.rb, line 41
def call(script)
  setup

  ssh = build_ssh_command
  args = ssh + ["bash", script]

  kernel_exec(*args)
end
confirm_ssh_access() click to toggle source
# File lib/sonic/docker.rb, line 56
def confirm_ssh_access
  host = build_host
  puts "Checking access to instance #{detector.instance_id}"

  ssh = ["ssh", ssh_options, "-At", host, "uptime", "2>&1"]
  command = ssh.join(' ')
  puts "=> #{command}".color(:green)
  output = `#{command}`
  if output.include?("Permission denied")
    puts output
    UI.error("Access to the instance denied. Maybe check your ssh keys.")
    exit 1
  elsif output.include?(" up ")
    puts "Access OK!"
  else
    puts output
    UI.error("There was an error trying to access the instnace.")
    exit 1
  end
end
copy_over_container_data() click to toggle source
# File lib/sonic/docker.rb, line 77
def copy_over_container_data
  create_container_data

  host = build_host

  # LEVEL 1
  # Always clean up remote /tmp/sonic in case of previous interrupted run.
  # Dont use build_ssh_command because we always want to build only the first level server
  ssh = ["ssh", ssh_options, "-At", host]
  clean = ssh + %w[rm -rf /tmp/sonic] + black_hole
  execute(clean.join(' '))

  # Copy over the data files
  dest = "#{host}:#{tmp_sonic_path}"
  scp = ["scp", ssh_options, "-r", tmp_sonic_path, dest] + black_hole
  execute(scp.join(' ')) # need to use String form for black_hole redirection to work

  # LEVEL 2
  # Need to scp the files over another hop if bastion is involved
  if @bastion
    # Always clean up remote /tmp/sonic in case of previous interrupted run.
    ssh = build_ssh_command
    clean = ssh + %w[rm -rf /tmp/sonic] + black_hole
    execute(clean.join(' '))

    # Dont use build_ssh_command because we want to scp from the first level to the second level server
    ssh = ["ssh", ssh_options, "-At", bastion_host]
    dest = "#{ssh_host}:#{tmp_sonic_path}"
    scp = ["scp"] + ssh_options + ["-r", tmp_sonic_path, dest] + black_hole
    command = ssh + scp
    execute(command.join(' '))
  end
  # Clean up locally now that everything has been successfully copied over remotely.
  FileUtils.rm_rf(tmp_sonic_path)
  # The bash_scripts cleans up after themselves on the servers by and blows away /tmp/sonic.
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/sonic/docker.rb, line 119
def create_container_data
  # For container env_vars and image info.
  task_definition_arn = first_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])

  sonic_folder = "/tmp/sonic"
  FileUtils.mkdir_p(sonic_folder) unless File.exist?(sonic_folder)
  IO.write("/tmp/sonic/task-arn.txt", task_arns.first)
  IO.write("/tmp/sonic/docker-image.txt", container_definition[:image])
  IO.write("/tmp/sonic/env-file.txt", env_file_data)
  FileUtils.cp_r(bash_scripts, "/tmp/sonic")
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/sonic/docker.rb, line 141
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/sonic/docker.rb, line 7
def exec
  call("/tmp/sonic/bash_scripts/docker-exec.sh")
end
execute(*command) click to toggle source

command is an Array

# File lib/sonic/docker.rb, line 32
def execute(*command)
  UI.say "=> #{command.join(' ')}".color(:green)
  success = system(*command)
  unless success
    UI.error(command.join(' '))
    exit 1
  end
end
run() click to toggle source

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

# File lib/sonic/docker.rb, line 12
def run
  call("/tmp/sonic/bash_scripts/docker-run.sh")
end
setup() click to toggle source
# File lib/sonic/docker.rb, line 20
def setup
  validate!
  confirm_ssh_access
  copy_over_container_data
end
tmp_sonic_path() click to toggle source
# File lib/sonic/docker.rb, line 16
def tmp_sonic_path
  "/tmp/sonic"
end
validate!() click to toggle source
# File lib/sonic/docker.rb, line 26
def validate!
  check_service_exists!
  check_tasks_running!
end