Skip to content

Instantly share code, notes, and snippets.

@binarytemple
Last active February 23, 2016 17:34
Show Gist options
  • Save binarytemple/195153fc2dc4645a770c to your computer and use it in GitHub Desktop.
Save binarytemple/195153fc2dc4645a770c to your computer and use it in GitHub Desktop.
[elixir] extracting env information from docker-machine via command execution/parsing
```
defmodule DockerMachine do
def get_env() do
case System.cmd("docker-machine", ["env","local"]) do
{output,0} -> {
:success, output |>
String.split("\n") |>
Enum.filter(fn x -> x =~ "export DOCKER" end) |>
Enum.map( fn l ->
String.replace(l, ~r/export (.*)/,"\\1") |>
String.replace(~r/\"/,"") |>
String.split("=") |>
List.to_tuple end)|>
Enum.map(
fn
{"DOCKER_TLS_VERIFY", docker_tls_verify} -> {:docker_tls_verify, docker_tls_verify}
{"DOCKER_HOST", docker_host} -> {:docker_host, docker_host}
{"DOCKER_CERT_PATH", docker_cert_path} -> {:docker_cert_path, docker_cert_path}
{"DOCKER_MACHINE_NAME",docker_machine_name} -> {:docker_machine_name,docker_machine_name}
other -> {:error, other}
end
)
}
{output,number} -> {:error,output,number}
other -> {:error,other}
end
end
end
```
@binarytemple
Copy link
Author

iex(11)> {:success, keylist} = Docker.DockerMachine.get_env
{:success,
 [docker_tls_verify: "1", docker_host: "tcp://192.168.103.138:2376",
  docker_cert_path: "/Users/bryanhunt/.docker/machine/machines/local",
  docker_machine_name: "local"]}

iex(13)> Map.new(keylist)
%{docker_cert_path: "/Users/binarytemple/.docker/machine/machines/local",
  docker_host: "tcp://192.168.103.138:2376", docker_machine_name: "local",
  docker_tls_verify: "1"}

@binarytemple
Copy link
Author

Hoping to use it in this way ( config/dev.exs )

use Mix.Config
{:success, keylist} = DockerMachine.get_env
m = Map.new(keylist)

config :docker,
        base_url: m[:docker_host],
        ssl_options: [
          {:ca_file,   m[:docker_cert_path] <> "/docker.crt"},
          {:cert_file, m[:docker_cert_path] <> "/docker.crt"},
          {:key_file,  m[:docker_cert_path] <> "/ca.crt"},
        ],  
        machine_name: m[:docker_machine_name]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment