fix docker
This commit is contained in:
parent
929a736b17
commit
1411d093da
@ -59,8 +59,8 @@ create_container(ContainerName, ContainerDir, Config) when is_binary(ContainerNa
|
||||
Cmd = maps:get(<<"command">>, Config, []),
|
||||
|
||||
%% 挂载预留的目录,用来作为配置文件的存放
|
||||
BinContainerDir = list_to_binary(ContainerDir),
|
||||
BaseOptions = [<<"-v">>, <<BinContainerDir/binary, ":/etc/">>],
|
||||
BinContainerDir = list_to_binary(docker_container_helper:make_etc_dir_name(ContainerDir)),
|
||||
BaseOptions = [<<"-v">>, <<BinContainerDir/binary, ":/usr/local/etc/">>],
|
||||
|
||||
Options = build_options(Config),
|
||||
Args = lists:flatten([Image | BaseOptions ++ Options ++ Cmd]),
|
||||
@ -122,14 +122,14 @@ check_container_exist(ContainerName) when is_binary(ContainerName) ->
|
||||
-spec start_container(ContainerName :: binary()) -> ok | {error, Reason :: binary()}.
|
||||
start_container(ContainerName) when is_binary(ContainerName) ->
|
||||
PortSettings = [stream, exit_status, use_stdio, binary],
|
||||
ExecCmd = "docker start " ++ binary_to_list(ContainerName) ++ " >/dev/null 2>&1",
|
||||
ExecCmd = "docker start " ++ binary_to_list(ContainerName) ++ " 2>&1",
|
||||
case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of
|
||||
Port when is_port(Port) ->
|
||||
case gather_output(Port) of
|
||||
{0, _} ->
|
||||
ok;
|
||||
{_ExitCode, _Error} ->
|
||||
{error, <<"启动失败"/utf8>>}
|
||||
{_ExitCode, Error} ->
|
||||
{error, Error}
|
||||
end;
|
||||
_Error ->
|
||||
{error, <<"启动失败"/utf8>>}
|
||||
@ -138,17 +138,18 @@ start_container(ContainerName) when is_binary(ContainerName) ->
|
||||
-spec stop_container(ContainerName :: binary()) -> ok | {error, Reason :: binary()}.
|
||||
stop_container(ContainerName) when is_binary(ContainerName) ->
|
||||
PortSettings = [stream, exit_status, use_stdio, binary],
|
||||
ExecCmd = "docker stop " ++ binary_to_list(ContainerName),
|
||||
ExecCmd = "docker stop " ++ binary_to_list(ContainerName) ++ " 2>&1",
|
||||
lager:debug("[docker_commands] cmd : ~p", [ExecCmd]),
|
||||
case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of
|
||||
Port when is_port(Port) ->
|
||||
case gather_output(Port) of
|
||||
{0, _} ->
|
||||
ok;
|
||||
{_ExitCode, _Error} ->
|
||||
{error, <<>>}
|
||||
{_ExitCode, Error} ->
|
||||
{error, Error}
|
||||
end;
|
||||
_Error ->
|
||||
{error, <<>>}
|
||||
false
|
||||
end.
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
@ -165,24 +166,6 @@ gather_output(Port, Acc) ->
|
||||
{Status, iolist_to_binary(Acc)}
|
||||
end.
|
||||
|
||||
extract_sha256(Output) when is_binary(Output) ->
|
||||
Parts = binary:split(Output, <<$\n>>, [global]),
|
||||
lager:debug("parts: ~p", [Parts]),
|
||||
case lists:search(fun(Line) -> starts_with(Line, <<"Digest:">>) end, Parts) of
|
||||
{value, Digest} ->
|
||||
Sha256 = lists:last(binary:split(Digest, <<":">>, [global])),
|
||||
{ok, Sha256};
|
||||
false ->
|
||||
error
|
||||
end.
|
||||
|
||||
starts_with(Binary, Prefix) when is_binary(Binary), is_binary(Prefix) ->
|
||||
PrefixSize = byte_size(Prefix),
|
||||
case Binary of
|
||||
<<Prefix:PrefixSize/binary, _Rest/binary>> -> true;
|
||||
_ -> false
|
||||
end.
|
||||
|
||||
%% 构建所有参数
|
||||
build_options(Config) ->
|
||||
lists:flatten([
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
-author("anlicheng").
|
||||
|
||||
%% API
|
||||
-export([ensure_dir/2, get_dir/2, ensure_etc_dir/1]).
|
||||
-export([ensure_dir/2, get_dir/2, ensure_etc_dir/1, make_etc_dir_name/1]).
|
||||
|
||||
-spec ensure_dir(RootDir :: string(), ContainerName :: binary()) -> {ok, ServerRootDir :: string()}.
|
||||
ensure_dir(RootDir, ContainerName) when is_list(RootDir), is_binary(ContainerName) ->
|
||||
@ -22,7 +22,7 @@ ensure_dir(RootDir, ContainerName) when is_list(RootDir), is_binary(ContainerNam
|
||||
-spec ensure_etc_dir(ContainerDir :: string()) -> {ok, EtcDir :: string()}.
|
||||
ensure_etc_dir(ContainerDir) when is_list(ContainerDir) ->
|
||||
%% 根目录
|
||||
EtcDir = ContainerDir ++ "etc/",
|
||||
EtcDir = make_etc_dir_name(ContainerDir),
|
||||
ok = filelib:ensure_dir(EtcDir),
|
||||
{ok, EtcDir}.
|
||||
|
||||
@ -36,3 +36,8 @@ get_dir(RootDir, ContainerName) when is_list(RootDir), is_binary(ContainerName)
|
||||
false ->
|
||||
error
|
||||
end.
|
||||
|
||||
-spec make_etc_dir_name(ContainerDir :: string()) -> string().
|
||||
make_etc_dir_name(ContainerDir) when is_list(ContainerDir) ->
|
||||
%% 根目录
|
||||
ContainerDir ++ "etc/".
|
||||
@ -114,6 +114,7 @@ handle_call({start_container, ContainerId}, _From, State) ->
|
||||
{reply, {error, Reason}, State}
|
||||
end;
|
||||
|
||||
|
||||
%% 停止服务, 主动停止的时候会改变服务配置的status字段
|
||||
handle_call({stop_container, ContainerId}, _From, State = #state{}) ->
|
||||
case docker_commands:stop_container(ContainerId) of
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user