fix
This commit is contained in:
parent
cb4aa88dea
commit
a9a988bb99
@ -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) ->
|
||||
%% 工作逻辑,压缩文件需要解压到工作目录
|
||||
WorkDir = ServiceRootDir ++ "/work_dir/",
|
||||
ok = filelib:ensure_dir(WorkDir),
|
||||
{ok, WorkDir}.
|
||||
-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),
|
||||
|
||||
%% 清理目录下的文件
|
||||
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, [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user