diff --git a/lib/testcontainers.ex b/lib/testcontainers.ex index f6e724d..d3a0037 100644 --- a/lib/testcontainers.ex +++ b/lib/testcontainers.ex @@ -50,34 +50,20 @@ defmodule Testcontainers do defp setup(options) do {conn, docker_host_url, docker_host} = Connection.get_connection(options) - # Read properties first so we can configure Ryuk appropriately + # Read testcontainer properties {:ok, properties} = PropertiesParser.read_property_sources() session_id = :crypto.hash(:sha, "#{inspect(self())}#{DateTime.utc_now() |> DateTime.to_string()}") |> Base.encode16() - ryuk_privileged = Map.get(properties, "ryuk.container.privileged", "false") == "true" - - ryuk_config = - Container.new("testcontainers/ryuk:#{Constants.ryuk_version()}") - |> Container.with_exposed_port(8080) - |> then(&apply_docker_socket_volume_binding(&1, docker_host)) - |> Container.with_auto_remove(true) - |> Container.with_privileged(ryuk_privileged) - - with {:ok, _} <- Api.pull_image(ryuk_config.image, conn), - {:ok, docker_hostname} <- get_docker_hostname(docker_host_url, conn), - {:ok, ryuk_container_id} <- Api.create_container(ryuk_config, conn), - :ok <- Api.start_container(ryuk_container_id, conn), - {:ok, container} <- Api.get_container(ryuk_container_id, conn), - {:ok, socket} <- create_ryuk_socket(container, docker_hostname), - :ok <- register_ryuk_filter(session_id, socket) do + with {:ok, docker_hostname} <- get_docker_hostname(docker_host_url, conn), + {:ok} <- start_reaper(conn, session_id, properties, docker_host, docker_hostname), + {:ok, properties} <- PropertiesParser.read_property_file() do Logger.info("Testcontainers initialized") {:ok, %{ - socket: socket, conn: conn, docker_hostname: docker_hostname, session_id: session_id, @@ -258,6 +244,47 @@ defmodule Testcontainers do GenServer.call(name, call, @timeout) end + defp start_reaper(conn, session_id, properties, docker_host, docker_hostname) do + ryuk_disabled = Map.get(properties, "ryuk.disabled", "false") == "true" + + case ryuk_disabled do + true -> + ryukDisabledMessage = + """ + ******************************************************************************** + Ryuk has been disabled. This can cause unexpected behavior in your environment. + ******************************************************************************** + """ + + IO.puts(ryukDisabledMessage) + + {:ok, nil} + + _ -> + start_ryuk(conn, session_id, properties, docker_host, docker_hostname) + end + end + + defp start_ryuk(conn, session_id, properties, docker_host, docker_hostname) do + ryuk_privileged = Map.get(properties, "ryuk.container.privileged", "false") == "true" + + ryuk_config = + Container.new("testcontainers/ryuk:#{Constants.ryuk_version()}") + |> Container.with_exposed_port(8080) + |> then(&apply_docker_socket_volume_binding(&1, docker_host)) + |> Container.with_auto_remove(true) + |> Container.with_privileged(ryuk_privileged) + + with {:ok, _} <- Api.pull_image(ryuk_config.image, conn), + {:ok, ryuk_container_id} <- Api.create_container(ryuk_config, conn), + :ok <- Api.start_container(ryuk_container_id, conn), + {:ok, container} <- Api.get_container(ryuk_container_id, conn), + {:ok, socket} <- create_ryuk_socket(container, docker_hostname), + :ok <- register_ryuk_filter(session_id, socket) do + {:ok} + end + end + defp create_ryuk_socket(container, docker_hostname, reattempt_count \\ 0) defp create_ryuk_socket(%Container{} = container, docker_hostname, reattempt_count)