class MysqlInDocker::Commands

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/commands.rb, line 8
def self.exit_on_failure?
  true
end

Public Instance Methods

remove(id) click to toggle source
# File lib/commands.rb, line 38
def remove(id)
  stop id

  status = docker_command "volume rm #{get_container_name(id)}"
  if status[:stderr].include? "No such volume:"
    p "Volume not found."
  else
    p "Removed volume."
  end
end
start(id, version, port, mysql_root_password) click to toggle source
# File lib/commands.rb, line 13
def start(id, version, port, mysql_root_password)
  status = docker_command "start #{get_container_name(id)}"
  if status[:successed]
    p "#{get_container_name(id)} running on #{port}."
    return
  end

  command = up_command id, version, port, mysql_root_password
  status = docker_command(command)
  raise_if_status_failed status

  p "Created #{get_container_name(id)} on #{port}."
end
stop(id) click to toggle source
# File lib/commands.rb, line 28
def stop(id)
  status = docker_command "rm -f #{get_container_name(id)}"
  if status[:stderr].include? "No such container:"
    p "Container not found."
  else
    p "Removed container."
  end
end

Private Instance Methods

docker_command(command) click to toggle source
# File lib/commands.rb, line 55
def docker_command(command)
  o, e, s = Open3.capture3("docker #{command}")
  { stdout: o, stderr: e, status: s, successed: s.success? }
end
get_container_name(id) click to toggle source
# File lib/commands.rb, line 60
def get_container_name(id)
  "mysql_in_docker_#{id}"
end
raise_if_status_failed(status) click to toggle source
# File lib/commands.rb, line 51
def raise_if_status_failed(status)
  raise Error, status[:stderr] unless status[:successed]
end
up_command(id, version, port, mysql_root_password) click to toggle source
# File lib/commands.rb, line 64
def up_command(id, version, port, mysql_root_password) # rubocop:disable Metrics/MethodLength
  [
    "run",
    "--name", get_container_name(id),
    "--env",
    "MYSQL_ROOT_PASSWORD=#{mysql_root_password}",
    "--publish",
    "#{port}:3306",
    "--volume",
    "#{get_container_name(id)}:/var/lib/mysql",
    "-d",
    "mysql:#{version}"
  ].join(" ")
end