@@ -17,10 +17,25 @@ defmodule MixDocker do
1717 end
1818
1919 def build ( args ) do
20+ { opts , args } = extract_opts ( args )
21+ build ( args , opts )
22+ end
23+ def build ( args , opts ) do
24+ { ip , identity_file } = ssh_identity_opts ( opts )
25+ opts =
26+ if ip != nil do
27+ Plug.Adapters.Cowboy . http MixDocker.IdentityPlug , [ identity_file: identity_file ] , ip: ip
28+ host = Keyword . fetch! ( opts , :host )
29+ Keyword . update ( opts , :build_arg , "host=" <> host , & ( & 1 <> ",host=" <> host ) )
30+ else
31+ opts
32+ end
33+
2034 with_dockerfile @ dockerfile_build , fn ->
21- docker :build , @ dockerfile_build , image ( :build ) , args
35+ docker :build , @ dockerfile_build , image ( :build ) , ( if ip != nil , do: args ++ [ "--build-arg" , Keyword . fetch! ( opts , :build_arg ) ] , else: args )
2236 end
2337
38+ if ip != nil , do: Plug.Adapters.Cowboy . shutdown MixDocker.Plug.HTTP
2439 Mix . shell . info "Docker image #{ image ( :build ) } has been successfully created"
2540 end
2641
@@ -59,7 +74,7 @@ defmodule MixDocker do
5974 def shipit ( args ) do
6075 { opts , args } = extract_opts ( args )
6176
62- build ( args )
77+ build ( args , opts )
6378 release ( args )
6479 publish ( args , opts )
6580 end
@@ -112,9 +127,20 @@ defmodule MixDocker do
112127 # Simple recursive extraction instead of OptionParser to keep other (docker) flags intact
113128 defp extract_opts ( args ) , do: extract_opts ( [ ] , args , [ ] )
114129 defp extract_opts ( head , [ "--tag" , tag | tail ] , opts ) , do: extract_opts ( head , tail , Keyword . put ( opts , :tag , tag ) )
130+ defp extract_opts ( head , [ "--build-arg" , build_arg | tail ] , opts ) , do: extract_opts ( head , tail , Keyword . put ( opts , :build_arg , build_arg ) )
131+ defp extract_opts ( head , [ "--host" , host | tail ] , opts ) , do: extract_opts ( head , tail , Keyword . put ( opts , :host , host ) )
132+ defp extract_opts ( head , [ "--identity" , identity_file | tail ] , opts ) , do: extract_opts ( head , tail , Keyword . put ( opts , :identity_file , identity_file ) )
115133 defp extract_opts ( head , [ ] , opts ) , do: { opts , head }
116134 defp extract_opts ( head , [ h | tail ] , opts ) , do: extract_opts ( head ++ [ h ] , tail , opts )
117135
136+ defp ssh_identity_opts ( opts ) do
137+ with { :ok , host } <- Keyword . fetch ( opts , :host ) ,
138+ { :ok , ip } <- host |> String . to_char_list |> :inet_parse . address do
139+ { ip , Keyword . get ( opts , :identity_file , "~/.ssh/id_rsa" ) }
140+ else
141+ _ -> { nil , nil }
142+ end
143+ end
118144
119145 defp docker ( :cp , cid , source , dest ) do
120146 system! "docker" , [ "cp" , "#{ cid } :#{ source } " , dest ]
0 commit comments