This commit is contained in:
anlicheng 2025-05-09 18:34:33 +08:00
parent cb4aa88dea
commit a9a988bb99

View File

@ -74,39 +74,9 @@ handle_call(_Request, _From, State = #state{}) ->
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_cast(deploy, State = #state{task_id = TaskId, service_root_dir = ServiceRootDir, service_id = ServiceId, tar_url = TarUrl}) ->
case download(binary_to_list(TarUrl), ServiceRootDir) of
{ok, TarFile, CostTs} ->
Log = io_lib:format("download: ~p completed, cost time: ~p(ms)", [binary_to_list(TarUrl), CostTs]),
efka_inetd_task_log:stash(TaskId, list_to_binary(Log)),
{ok, WorkDir} = make_work_dir(ServiceRootDir),
%%
Result = delete_directory(WorkDir),
lager:debug("delete_directory result is: ~p", [Result]),
case tar_extract(TarFile, WorkDir) of
ok ->
%% lock文件
touch_lock(ServiceRootDir, TarUrl),
%%
ok = service_model:insert(#service{
service_id = ServiceId,
tar_url = TarUrl,
%%
root_dir = ServiceRootDir,
params = <<"">>,
metrics = <<"">>,
%% 0: , 1:
status = 0
}),
efka_inetd_task_log:stash(TaskId, <<"deploy success">>);
{error, Reason} ->
TarLog = io_lib:format("tar decompression: ~p, error: ~p", [filename:basename(TarFile), Reason]),
efka_inetd_task_log:stash(TaskId, list_to_binary(TarLog))
end;
{error, Reason} ->
DownloadLog = io_lib:format("download: ~p, error: ~p", [binary_to_list(TarUrl), Reason]),
efka_inetd_task_log:stash(TaskId, list_to_binary(DownloadLog))
end,
do_deploy(TaskId, ServiceRootDir, ServiceId, TarUrl),
{stop, normal, State};
handle_cast(_Request, State) ->
{stop, normal, State}.
%% @private
@ -140,16 +110,47 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
%%% Internal functions
%%%===================================================================
%%
-spec make_work_dir(ServiceRootDir :: string()) -> {ok, WorkDir :: string()}.
make_work_dir(ServiceRootDir) when is_list(ServiceRootDir) ->
%%
-spec do_deploy(TaskId :: integer(), ServiceRootDir :: string(), ServiceId :: binary(), TarUrl :: binary()) -> no_return().
do_deploy(TaskId, ServiceRootDir, ServiceId, TarUrl) when is_integer(TaskId), is_list(ServiceRootDir), is_binary(ServiceId), is_binary(TarUrl) ->
case download(binary_to_list(TarUrl), ServiceRootDir) of
{ok, TarFile, CostTs} ->
Log = io_lib:format("download: ~p completed, cost time: ~p(ms)", [binary_to_list(TarUrl), CostTs]),
efka_inetd_task_log:stash(TaskId, list_to_binary(Log)),
%%
WorkDir = ServiceRootDir ++ "/work_dir/",
ok = filelib:ensure_dir(WorkDir),
{ok, WorkDir}.
%%
Result = delete_directory(WorkDir),
lager:debug("delete_directory result is: ~p", [Result]),
case tar_extract(TarFile, WorkDir) of
ok ->
%% lock文件
touch_lock(ServiceRootDir, TarUrl),
%%
ok = service_model:insert(#service{
service_id = ServiceId,
tar_url = TarUrl,
%%
root_dir = ServiceRootDir,
params = <<"">>,
metrics = <<"">>,
%% 0: , 1:
status = 0
}),
efka_inetd_task_log:stash(TaskId, <<"deploy success">>);
{error, Reason} ->
TarLog = io_lib:format("tar decompression: ~p, error: ~p", [filename:basename(TarFile), Reason]),
efka_inetd_task_log:stash(TaskId, list_to_binary(TarLog))
end;
{error, Reason} ->
DownloadLog = io_lib:format("download: ~p, error: ~p", [binary_to_list(TarUrl), Reason]),
efka_inetd_task_log:stash(TaskId, list_to_binary(DownloadLog))
end.
%%
-spec delete_directory(Dir :: string()) -> ok | {error, Reason :: any()}.
-spec delete_directory(string()) -> ok | {error, term()}.
delete_directory(Dir) when is_list(Dir) ->
%
case file:list_dir(Dir) of
@ -171,7 +172,7 @@ delete_directory(Dir) when is_list(Dir) ->
{error, Reason}
end.
-spec touch_lock(DirName :: string(), TarUrl :: binary()) -> boolean().
-spec touch_lock(string(), binary()) -> boolean().
touch_lock(DirName, TarUrl) when is_list(DirName), is_binary(TarUrl) ->
FileName = DirName ++ ".efka.lock",
filelib:is_file(FileName) andalso file:delete(FileName),
@ -183,7 +184,7 @@ touch_lock(DirName, TarUrl) when is_list(DirName), is_binary(TarUrl) ->
end.
%%
-spec tar_extract(TarFile :: string(), TargetDir :: string()) -> ok | {error, Reason :: term()}.
-spec tar_extract(string(), string()) -> ok | {error, term()}.
tar_extract(TarFile, TargetDir) when is_list(TarFile), is_list(TargetDir) ->
%%
Ext = filename:extension(TarFile),
@ -195,7 +196,8 @@ tar_extract(TarFile, TargetDir) when is_list(TarFile), is_list(TargetDir) ->
end.
%%
-spec download(Url :: string(), TargetDir :: string()) -> {ok, TarFile :: string(), CostTs :: integer()} | {error, Reason :: term()}.
-spec download(Url :: string(), TargetDir :: string()) ->
{ok, TarFile :: string(), CostTs :: integer()} | {error, Reason :: term()}.
download(Url, TargetDir) when is_list(Url), is_list(TargetDir) ->
SslOpts = [
{ssl, [