Giter Site home page Giter Site logo

Managed docker containers about ci HOT 3 OPEN

sasa1977 avatar sasa1977 commented on June 9, 2024
Managed docker containers

from ci.

Comments (3)

Hanspagh avatar Hanspagh commented on June 9, 2024

I think this sounds like a good start

Two question
Do we just parse
docker_arg_1 docker_arg_2 ... directly to docker run?

and when we call ManagedDocker.Container.run this would be run on the sidekick node though something like :rpc.call so the process id would exists on the sicknode or on the caller node. Or what does starter process refer to in this case?

Here is what I had from a while ago, I think it would be fine to continue based on this

defmodule Ci.Docker do
  use GenServer
  require Logger

  def start_link([]) do
    GenServer.start_link(__MODULE__, [], name: {:global, __MODULE__})
  end

  @impl true
  def init([]) do
    {:ok, %{}}
  end

  def run(name) do
    GenServer.call({:global, __MODULE__}, {:start, name})
  end

  def stop() do
    GenServer.call({:global, __MODULE__}, :stop)
  end

  @impl true
  def handle_call({:start, name}, {from, _ref}, state) do
    {id, _} = System.cmd("docker", ["run", "-dt", name])
    container_id = String.trim(id)
    monitor_ref = Process.monitor(from)
    state = Map.put(state, from, {container_id, monitor_ref})
    {:reply, :ok, state}
  end

  @impl true
  def handle_call(:stop, {pid, _ref}, state) do
    {{container_id, monitor_ref}, state} = Map.pop(state, pid)
    cleanup(container_id, monitor_ref)
    {:reply, :ok, state}
  end

  @impl true
  def handle_info({:down, pid}, state) do
    {{container_id, monitor_ref}, state} = Map.pop(state, pid)
    cleanup(container_id, monitor_ref)
    {:noreply, state}
  end

  @impl true
  def terminate(_reason, state) do
    Logger.debug("Termination #{__MODULE__}")

    Enum.each(state, fn {_pid, {container_id, monitor_ref}} ->
      cleanup(container_id, monitor_ref)
    end)

    state
  end

  defp cleanup(container_id, monitor_ref) do
    Process.demonitor(monitor_ref)
    System.cmd("docker", ["rm", "-f", container_id])
  end
end

from ci.

sasa1977 avatar sasa1977 commented on June 9, 2024

Do we just parse
docker_arg_1 docker_arg_2 ... directly to docker run?

If you mean "pass", then yeah, we'd just prepend "docker run" and invoke the command, e.g. using OsCmd.run from this library.

and when we call ManagedDocker.Container.run this would be run on the sidekick node though something like :rpc.call so the process id would exists on the sicknode or on the caller node.

Yes, we need to make a call on the sidekick node. I don't think you need :rpc. On the sidekick node we'd start a registered GenServer (ManagedDocker.Container registered under the same name). To make a call from the main node you simply need to do GenServer.call({ManagedDocker.Container, sidekick_node}, arg). So something like:

defmodule ManagedDocker.Container do
  use GenServer

  def start_link(arg), do: GenServer.start_link(__MODULE__, arg, name: __MODULE__)

  def run(sidekick_node, args), do: GenServer.call({__MODULE__, node}, {:run, args}, timeout)

  # ...
end

Or what does starter process refer to in this case?

The starter process in my original description refers to the process on the main node that invoked Container.run. The genserver on the sidekick node needs to keep the mapping of each such process to the list of container ids. It also needs to monitor the starter process. If the starter process stops, the containers it started should be terminated by the genserver on the sidekick node.

Does that make sense?

from ci.

Hanspagh avatar Hanspagh commented on June 9, 2024

Got started on this here
#11

from ci.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.