diff --git a/HTTP_API_README.md b/HTTP_API_README.md index 786d211..a498f66 100644 --- a/HTTP_API_README.md +++ b/HTTP_API_README.md @@ -348,6 +348,38 @@ json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) } ``` +### 6 删除容器 + +**URL**:`/container/remove` +**Method**:`POST` + +#### 请求参数 +| 参数名 | 类型 | 必填 | 说明 | +|--------|------|------|------| +| uuid | binary (string) | ✅ | 主机唯一标识符 | +| container_name | binary (string) | ✅ | 容器名称 | + +### 请求示例 +```json +{"uuid": "qbxmjyzrkpntfgswaevodhluicqzxplkm", "container_name": "my_nginx_new"} +``` + +#### 响应参数 +| 字段 | 类型 | 说明 | +|------|------|------| +| result | map | 停止结果 | + +#### 示例响应 +```json +{ + "result": { + "container_name": "sensor_service", + "status": "stopped" + } +} +``` + + #### 错误响应 ```json { diff --git a/apps/iot/src/http_handlers/container_handler.erl b/apps/iot/src/http_handlers/container_handler.erl index 208d882..65e2e87 100644 --- a/apps/iot/src/http_handlers/container_handler.erl +++ b/apps/iot/src/http_handlers/container_handler.erl @@ -116,6 +116,25 @@ handle_request("POST", "/container/stop", _, #{<<"uuid">> := UUID, <<"container_ end end; +%% 删除容器 +handle_request("POST", "/container/remove", _, #{<<"uuid">> := UUID, <<"container_name">> := ContainerName}) when is_binary(UUID), is_binary(ContainerName) -> + case iot_host:get_pid(UUID) of + undefined -> + {ok, 200, iot_util:json_error(404, <<"host not found">>)}; + Pid when is_pid(Pid) -> + case iot_host:remove_container(Pid, ContainerName) of + {ok, Ref} -> + case iot_host:await_reply(Ref, 5000) of + {ok, Result} -> + {ok, 200, iot_util:json_data(Result)}; + {error, Reason} -> + {ok, 200, iot_util:json_error(400, Reason)} + end; + {error, Reason} when is_binary(Reason) -> + {ok, 200, iot_util:json_error(400, Reason)} + end + end; + %handle_request("POST", "/container/task_log", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId}) when is_binary(UUID), is_integer(TaskId) -> % case iot_host:get_pid(UUID) of % undefined -> diff --git a/apps/iot/src/iot_host.erl b/apps/iot/src/iot_host.erl index 4aeb520..cc541f6 100644 --- a/apps/iot/src/iot_host.erl +++ b/apps/iot/src/iot_host.erl @@ -25,7 +25,7 @@ -export([get_metric/1, get_status/1]). %% 通讯相关 -export([pub/3, attach_channel/2, command/3]). --export([deploy_container/3, start_container/2, stop_container/2, config_container/3, get_containers/1, await_reply/2]). +-export([deploy_container/3, start_container/2, stop_container/2, remove_container/2, config_container/3, get_containers/1, await_reply/2]). %% 设备管理 -export([reload_device/2, delete_device/2, activate_device/3]). -export([heartbeat/1]). @@ -119,6 +119,12 @@ stop_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) -> EncCallBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request), gen_statem:call(Pid, {jsonrpc_call, self(), EncCallBin}). +-spec remove_container(Pid :: pid(), ContainerName :: binary()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. +remove_container(Pid, ContainerName) when is_pid(Pid), is_binary(ContainerName) -> + Request = #jsonrpc_request{method = <<"remove_container">>, params = #{<<"container_name">> => ContainerName}}, + EncCallBin = message_codec:encode(?MESSAGE_JSONRPC_REQUEST, Request), + gen_statem:call(Pid, {jsonrpc_call, self(), EncCallBin}). + %-spec task_log(Pid :: pid(), TaskId :: integer()) -> {ok, Ref :: reference()} | {error, Reason :: any()}. %task_log(Pid, TaskId) when is_pid(Pid), is_integer(TaskId) -> % TaskLogBin = message_pb:encode_msg(#fetch_task_log{task_id = TaskId}), diff --git a/rebar.config b/rebar.config index 7a87193..9acc2fa 100644 --- a/rebar.config +++ b/rebar.config @@ -9,7 +9,7 @@ {jiffy, ".*", {git, "https://github.com/davisp/jiffy.git", {tag, "1.1.1"}}}, {mysql, ".*", {git, "https://github.com/mysql-otp/mysql-otp", {tag, "1.8.0"}}}, {eredis, ".*", {git, "https://github.com/wooga/eredis.git", {tag, "v1.2.0"}}}, - %{emqtt, ".*", {git, "https://gitea.s5s8.com/anlicheng/emqtt.git", {branch, "main"}}}, + {emqtt, ".*", {git, "https://gitea.s5s8.com/anlicheng/emqtt.git", {branch, "main"}}}, {gproc, ".*", {git, "https://github.com/uwiger/gproc.git", {tag, "0.9.1"}}}, {parse_trans, ".*", {git, "https://github.com/uwiger/parse_trans", {tag, "3.0.0"}}}, {lager, ".*", {git,"https://github.com/erlang-lager/lager.git", {tag, "3.9.2"}}} diff --git a/rebar.lock b/rebar.lock index 51031e9..dcc80ad 100644 --- a/rebar.lock +++ b/rebar.lock @@ -13,6 +13,10 @@ {ref,"aca0ad953417b29bab2c41eeb4c37c98606c848b"}}, 1}, {<<"crc32cer">>,{pkg,<<"crc32cer">>,<<"1.0.3">>},2}, + {<<"emqtt">>, + {git,"https://gitea.s5s8.com/anlicheng/emqtt.git", + {ref,"5111914a9b1b92b0b497f825c77bdd365e3989b0"}}, + 0}, {<<"eredis">>, {git,"https://github.com/wooga/eredis.git", {ref,"9ad91f149310a7d002cb966f62b7e2c3330abb04"}},