Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 13, 2023
1 parent 1357899 commit 137bbea
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions lib/reaper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule TestcontainersElixir.Reaper do

alias DockerEngineAPI.Api
alias DockerEngineAPI.Model
alias TestcontainersElixir.Container

@ryuk_image "testcontainers/ryuk:0.5.1"
@ryuk_port 8080
Expand All @@ -18,11 +19,16 @@ defmodule TestcontainersElixir.Reaper do

@impl true
def init(connection) do
with {:ok, _} <- Api.Image.image_create(connection, fromImage: @ryuk_image),
{:ok, container} <- create_ryuk_container(connection),
container_id = container."Id",
{:ok, _} <- Api.Container.container_start(connection, container_id),
{:ok, socket} <- create_ryuk_socket(connection, container_id) do
with {:ok, _image_create_response} <-
Api.Image.image_create(connection, fromImage: @ryuk_image),
{:ok, %Model.ContainerCreateResponse{Id: container_id}} <-
create_ryuk_container(connection),
{:ok, _container_start_response} <-
Api.Container.container_start(connection, container_id),
{:ok, %Model.ContainerInspectResponse{} = container_info} <-
Api.Container.container_inspect(connection, container_id),
container = Container.of(container_info),
{:ok, socket} <- create_ryuk_socket(container) do
{:ok, socket}
end
end
Expand Down Expand Up @@ -56,19 +62,10 @@ defmodule TestcontainersElixir.Reaper do
})
end

defp create_ryuk_socket(
connection,
container_id
)
when is_binary(container_id) do
port_str = "#{@ryuk_port}/tcp"
defp create_ryuk_socket(%Container{} = container) do
host_port = Container.mapped_port(container, @ryuk_port)

{:ok,
%Model.ContainerInspectResponse{
NetworkSettings: %{Ports: %{^port_str => [%{"HostPort" => host_port} | _tail]}}
}} = connection |> Api.Container.container_inspect(container_id)

:gen_tcp.connect(~c"localhost", String.to_integer(host_port), [
:gen_tcp.connect(~c"localhost", host_port, [
:binary,
active: false,
packet: :line
Expand Down

0 comments on commit 137bbea

Please sign in to comment.