This commit is contained in:
anlicheng 2025-09-17 17:11:52 +08:00
parent 274a54c889
commit 08aaf01016
6 changed files with 52 additions and 2649 deletions

View File

@ -82,10 +82,10 @@ deploy(Pid) when is_pid(Pid) ->
gen_server:cast(Pid, deploy). gen_server:cast(Pid, deploy).
%% @doc Spawns the server and registers the local name (unique) %% @doc Spawns the server and registers the local name (unique)
-spec(start_link(TaskId :: integer(), RootDir :: string(), Config :: map()) -> -spec(start_link(TaskId :: integer(), ContainerDir :: string(), Config :: map()) ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}). {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link(TaskId, RootDir, Config) when is_integer(TaskId), is_list(RootDir), is_map(Config) -> start_link(TaskId, ContainerDir, Config) when is_integer(TaskId), is_list(ContainerDir), is_map(Config) ->
gen_server:start_link(?MODULE, [TaskId, RootDir, Config], []). gen_server:start_link(?MODULE, [TaskId, ContainerDir, Config], []).
%%%=================================================================== %%%===================================================================
%%% gen_server callbacks %%% gen_server callbacks
@ -96,8 +96,8 @@ start_link(TaskId, RootDir, Config) when is_integer(TaskId), is_list(RootDir), i
-spec(init(Args :: term()) -> -spec(init(Args :: term()) ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} | {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore). {stop, Reason :: term()} | ignore).
init([TaskId, RootDir, Config]) -> init([TaskId, ContainerDir, Config]) ->
{ok, #state{task_id = TaskId, root_dir = RootDir, config = Config}}. {ok, #state{task_id = TaskId, root_dir = ContainerDir, config = Config}}.
%% @private %% @private
%% @doc Handling call messages %% @doc Handling call messages
@ -165,14 +165,13 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
% "command": ["nginx", "-g", "daemon off;"], % "command": ["nginx", "-g", "daemon off;"],
% "restart": "always" % "restart": "always"
%} %}
-spec do_deploy(TaskId :: integer(), RootDir :: string(), Config :: map()) -> ok | {error, Reason :: any()}. -spec do_deploy(TaskId :: integer(), ContainerDir :: string(), Config :: map()) -> ok | {error, Reason :: any()}.
do_deploy(TaskId, RootDir, Config) when is_integer(TaskId), is_list(RootDir), is_map(Config) -> do_deploy(TaskId, ContainerDir, Config) when is_integer(TaskId), is_list(ContainerDir), is_map(Config) ->
%% %%
ContainerName = maps:get(<<"container_name">>, Config), ContainerName = maps:get(<<"container_name">>, Config),
Image0 = maps:get(<<"image">>, Config), Image0 = maps:get(<<"image">>, Config),
Image = normalize_image(Image0), Image = normalize_image(Image0),
{ok, ContainerDir} = ensure_dirs(RootDir, ContainerName),
case try_pull_image(Image) of case try_pull_image(Image) of
ok -> ok ->
%% container %% container
@ -212,13 +211,6 @@ maybe_create_env_file(ContainerDir, Envs) when is_list(Envs)->
ok = file:close(IoDevice), ok = file:close(IoDevice),
{ok, TargetFile}. {ok, TargetFile}.
-spec ensure_dirs(RootDir :: string(), ContainerName :: binary()) -> {ok, ServerRootDir :: string()}.
ensure_dirs(RootDir, ContainerName) when is_list(RootDir), is_binary(ContainerName) ->
%%
ServiceRootDir = RootDir ++ "/" ++ binary_to_list(ContainerName) ++ "/",
ok = filelib:ensure_dir(ServiceRootDir),
{ok, ServiceRootDir}.
-spec normalize_image(binary()) -> binary(). -spec normalize_image(binary()) -> binary().
normalize_image(Image) when is_binary(Image) -> normalize_image(Image) when is_binary(Image) ->
Parts = binary:split(Image, <<"/">>, [global]), Parts = binary:split(Image, <<"/">>, [global]),

View File

@ -81,8 +81,8 @@ init([]) ->
{stop, Reason :: term(), NewState :: #state{}}). {stop, Reason :: term(), NewState :: #state{}}).
handle_call({deploy, TaskId, Config = #{<<"container_name">> := ContainerName}}, _From, State = #state{root_dir = RootDir, task_map = TaskMap}) -> handle_call({deploy, TaskId, Config = #{<<"container_name">> := ContainerName}}, _From, State = #state{root_dir = RootDir, task_map = TaskMap}) ->
%% %%
{ok, ContainerDir} = docker_container_helper:ensure_dir(ContainerDir, ContainerName), {ok, ContainerDir} = docker_container_helper:ensure_dir(RootDir, ContainerName),
{ok, TaskPid} = docker_deployer:start_link(TaskId, RootDir, Config), {ok, TaskPid} = docker_deployer:start_link(TaskId, ContainerDir, Config),
docker_deployer:deploy(TaskPid), docker_deployer:deploy(TaskPid),
lager:debug("[efka_inetd] start deploy task_id: ~p, config: ~p", [TaskId, Config]), lager:debug("[efka_inetd] start deploy task_id: ~p, config: ~p", [TaskId, Config]),
{reply, ok, State#state{task_map = maps:put(TaskPid, TaskId, TaskMap)}}; {reply, ok, State#state{task_map = maps:put(TaskPid, TaskId, TaskMap)}};

View File

@ -14,10 +14,14 @@
-define(Bytes, 2). -define(Bytes, 2).
%% API %% API
-export([encode/1, decode/1]). -export([encode/2, decode/1]).
-spec encode(Message :: any()) -> binary(). -spec encode0(Message :: any()) -> binary().
encode(#auth_request{uuid = UUID, username = Username, salt = Salt, token = Token, timestamp = Timestamp}) -> encode(PacketType, Packet) when is_integer(PacketType) ->
Bin = encode0(Packet),
<<PacketType, Bin/binary>>.
encode0(#auth_request{uuid = UUID, username = Username, salt = Salt, token = Token, timestamp = Timestamp}) ->
iolist_to_binary([ iolist_to_binary([
marshal(?Bytes, UUID), marshal(?Bytes, UUID),
marshal(?Bytes, Username), marshal(?Bytes, Username),
@ -25,40 +29,40 @@ encode(#auth_request{uuid = UUID, username = Username, salt = Salt, token = Toke
marshal(?Bytes, Token), marshal(?Bytes, Token),
marshal(?I32, Timestamp) marshal(?I32, Timestamp)
]); ]);
encode(#auth_reply{code = Code, message = Message}) -> encode0(#auth_reply{code = Code, message = Message}) ->
iolist_to_binary([ iolist_to_binary([
marshal(?I32, Code), marshal(?I32, Code),
marshal(?Bytes, Message) marshal(?Bytes, Message)
]); ]);
encode(#pub{topic = Topic, content = Content}) -> encode0(#pub{topic = Topic, content = Content}) ->
iolist_to_binary([ iolist_to_binary([
marshal(?Bytes, Topic), marshal(?Bytes, Topic),
marshal(?Bytes, Content) marshal(?Bytes, Content)
]); ]);
encode(#command{command_type = CommandType, command = Command}) -> encode0(#command{command_type = CommandType, command = Command}) ->
iolist_to_binary([ iolist_to_binary([
marshal(?I32, CommandType), marshal(?I32, CommandType),
marshal(?Bytes, Command) marshal(?Bytes, Command)
]); ]);
encode(#rpc_deploy{task_id = TaskId, config = Config}) -> encode0(#rpc_deploy{task_id = TaskId, config = Config}) ->
iolist_to_binary([ iolist_to_binary([
marshal(?I32, TaskId), marshal(?I32, TaskId),
marshal(?Bytes, Config) marshal(?Bytes, Config)
]); ]);
encode(#rpc_container{method = Method, container_name = ContainerName, params = Params}) -> encode0(#rpc_container{method = Method, container_name = ContainerName, params = Params}) ->
iolist_to_binary([ iolist_to_binary([
marshal(?Bytes, Method), marshal(?Bytes, Method),
marshal(?Bytes, ContainerName), marshal(?Bytes, ContainerName),
marshal(?Bytes, Params) marshal(?Bytes, Params)
]); ]);
encode(#data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey, metric = Metric}) -> encode0(#data{service_id = ServiceId, device_uuid = DeviceUUID, route_key = RouteKey, metric = Metric}) ->
iolist_to_binary([ iolist_to_binary([
marshal(?Bytes, ServiceId), marshal(?Bytes, ServiceId),
marshal(?Bytes, DeviceUUID), marshal(?Bytes, DeviceUUID),
marshal(?Bytes, RouteKey), marshal(?Bytes, RouteKey),
marshal(?Bytes, Metric) marshal(?Bytes, Metric)
]); ]);
encode(#event{service_id = ServiceId, event_type = EventType, params = Params}) -> encode0(#event{service_id = ServiceId, event_type = EventType, params = Params}) ->
iolist_to_binary([ iolist_to_binary([
marshal(?Bytes, ServiceId), marshal(?Bytes, ServiceId),
marshal(?I32, EventType), marshal(?I32, EventType),

View File

@ -8,7 +8,6 @@
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(efka_remote_agent). -module(efka_remote_agent).
-author("anlicheng"). -author("anlicheng").
-include("message_pb.hrl").
-include("efka.hrl"). -include("efka.hrl").
-include("efka_tables.hrl"). -include("efka_tables.hrl").
@ -84,35 +83,35 @@ callback_mode() ->
%% , mnesia %% , mnesia
handle_event(cast, {metric_data, ServiceId, DeviceUUID, RouteKey, Metric}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid}) -> handle_event(cast, {metric_data, ServiceId, DeviceUUID, RouteKey, Metric}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid}) ->
Packet = message_pb:encode_msg(#data{ Packet = efka_codec:encode(?METHOD_DATA, #data{
service_id = ServiceId, service_id = ServiceId,
device_uuid = DeviceUUID, device_uuid = DeviceUUID,
route_key = RouteKey, route_key = RouteKey,
metric = Metric metric = Metric
}), }),
efka_transport:send(TransportPid, ?METHOD_DATA, Packet), efka_transport:send(TransportPid, Packet),
{keep_state, State}; {keep_state, State};
handle_event(cast, {metric_data, ServiceId, DeviceUUID, LineProtocolData}, _, State) -> handle_event(cast, {metric_data, ServiceId, DeviceUUID, LineProtocolData}, _, State) ->
Packet = message_pb:encode_msg(#data{ Packet = efka_codec:encode(?METHOD_DATA, #data{
service_id = ServiceId, service_id = ServiceId,
device_uuid = DeviceUUID, device_uuid = DeviceUUID,
metric = LineProtocolData metric = LineProtocolData
}), }),
ok = cache_model:insert(?METHOD_DATA, Packet), ok = cache_model:insert(Packet),
{keep_state, State}; {keep_state, State};
%% %%
handle_event(cast, {event, ServiceId, EventType, Params}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid}) -> handle_event(cast, {event, ServiceId, EventType, Params}, ?STATE_ACTIVATED, State = #state{transport_pid = TransportPid}) ->
EventPacket = message_pb:encode_msg(#event{ EventPacket = efka_codec:encode(?METHOD_EVENT, #event{
service_id = ServiceId, service_id = ServiceId,
event_type = EventType, event_type = EventType,
params = Params params = Params
}), }),
efka_transport:send(TransportPid, ?METHOD_EVENT, EventPacket), efka_transport:send(TransportPid, EventPacket),
{keep_state, State}; {keep_state, State};
handle_event(cast, {event, ServiceId, EventType, Params}, ?STATE_ACTIVATED, State) -> handle_event(cast, {event, ServiceId, EventType, Params}, ?STATE_ACTIVATED, State) ->
EventPacket = message_pb:encode_msg(#event{ EventPacket = efka_codec:encode(?METHOD_EVENT, #event{
service_id = ServiceId, service_id = ServiceId,
event_type = EventType, event_type = EventType,
params = Params params = Params
@ -120,26 +119,26 @@ handle_event(cast, {event, ServiceId, EventType, Params}, ?STATE_ACTIVATED, Stat
ok = cache_model:insert(?METHOD_EVENT, EventPacket), ok = cache_model:insert(?METHOD_EVENT, EventPacket),
{keep_state, State}; {keep_state, State};
handle_event(cast, {ping, AdCode, BootTime, Province, City, EfkaVersion, KernelArch, Ips, CpuCore, CpuLoad, CpuTemperature, Disk, Memory, Interfaces}, ?STATE_ACTIVATED, %handle_event(cast, {ping, AdCode, BootTime, Province, City, EfkaVersion, KernelArch, Ips, CpuCore, CpuLoad, CpuTemperature, Disk, Memory, Interfaces}, ?STATE_ACTIVATED,
State = #state{transport_pid = TransportPid}) -> % State = #state{transport_pid = TransportPid}) ->
%
Ping = message_pb:encode_msg(#ping{ % Ping = message_pb:encode_msg(#ping{
adcode = AdCode, % adcode = AdCode,
boot_time = BootTime, % boot_time = BootTime,
province = Province, % province = Province,
city = City, % city = City,
efka_version = EfkaVersion, % efka_version = EfkaVersion,
kernel_arch = KernelArch, % kernel_arch = KernelArch,
ips = Ips, % ips = Ips,
cpu_core = CpuCore, % cpu_core = CpuCore,
cpu_load = CpuLoad, % cpu_load = CpuLoad,
cpu_temperature = CpuTemperature, % cpu_temperature = CpuTemperature,
disk = Disk, % disk = Disk,
memory = Memory, % memory = Memory,
interfaces = Interfaces % interfaces = Interfaces
}), % }),
efka_transport:send(TransportPid, ?METHOD_PING, Ping), % efka_transport:send(TransportPid, ?METHOD_PING, Ping),
{keep_state, State}; % {keep_state, State};
%% %%
handle_event(info, {timeout, _, create_transport}, ?STATE_DENIED, State) -> handle_event(info, {timeout, _, create_transport}, ?STATE_DENIED, State) ->
@ -336,7 +335,7 @@ auth_request() ->
Salt = proplists:get_value(salt, AuthInfo), Salt = proplists:get_value(salt, AuthInfo),
Token = proplists:get_value(token, AuthInfo), Token = proplists:get_value(token, AuthInfo),
message_pb:encode_msg(#auth_request{ efka_codec:encode(?METHOD_AUTH, #auth_request{
uuid = unicode:characters_to_binary(UUID), uuid = unicode:characters_to_binary(UUID),
username = unicode:characters_to_binary(Username), username = unicode:characters_to_binary(Username),
salt = unicode:characters_to_binary(Salt), salt = unicode:characters_to_binary(Salt),

View File

@ -8,7 +8,6 @@
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(efka_transport). -module(efka_transport).
-author("anlicheng"). -author("anlicheng").
-include("message_pb.hrl").
-include("efka.hrl"). -include("efka.hrl").
-behaviour(gen_server). -behaviour(gen_server).
@ -115,7 +114,7 @@ handle_cast(connect, State = #state{host = Host, port = Port, parent_pid = Paren
%% auth校验 %% auth校验
handle_cast({auth_request, AuthRequestBin}, State = #state{parent_pid = ParentPid, socket = Socket}) -> handle_cast({auth_request, AuthRequestBin}, State = #state{parent_pid = ParentPid, socket = Socket}) ->
PacketId = 1, PacketId = 1,
ok = ssl:send(Socket, <<?PACKET_REQUEST, PacketId:32, ?METHOD_AUTH, AuthRequestBin/binary>>), ok = ssl:send(Socket, <<?PACKET_REQUEST, PacketId:32, AuthRequestBin/binary>>),
%% auth返回的结果 %% auth返回的结果
receive receive
{ssl, Socket, <<?PACKET_RESPONSE, PacketId:32, ReplyBin/binary>>} -> {ssl, Socket, <<?PACKET_RESPONSE, PacketId:32, ReplyBin/binary>>} ->

File diff suppressed because it is too large Load Diff