Skip to content

Commit

Permalink
fix: add error handling when use customized config (#4342)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiyiyimu authored Jun 1, 2021
1 parent 0d7fa65 commit 98d4447
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
24 changes: 20 additions & 4 deletions apisix/cli/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,18 @@ local function start(env, ...)
if customized_yaml then
profile.apisix_home = env.apisix_home .. "/"
local local_conf_path = profile:yaml_path("config")
util.execute_cmd("mv " .. local_conf_path .. " " .. local_conf_path .. ".bak")
util.execute_cmd("ln " .. customized_yaml .. " " .. local_conf_path)

local err = util.execute_cmd_with_error("mv " .. local_conf_path .. " "
.. local_conf_path .. ".bak")
if #err > 0 then
util.die("failed to mv config to backup, error: ", err)
end
err = util.execute_cmd_with_error("ln " .. customized_yaml .. " " .. local_conf_path)
if #err > 0 then
util.execute_cmd("mv " .. local_conf_path .. ".bak " .. local_conf_path)
util.die("failed to link customized config, error: ", err)
end

print("Use customized yaml: ", customized_yaml)
end

Expand All @@ -673,8 +683,14 @@ local function stop(env)
local local_conf_path = profile:yaml_path("config")
local bak_exist = io_open(local_conf_path .. ".bak")
if bak_exist then
util.execute_cmd("rm " .. local_conf_path)
util.execute_cmd("mv " .. local_conf_path .. ".bak " .. local_conf_path)
local err = util.execute_cmd_with_error("rm " .. local_conf_path)
if #err > 0 then
print("failed to remove customized config, error: ", err)
end
err = util.execute_cmd_with_error("mv " .. local_conf_path .. ".bak " .. local_conf_path)
if #err > 0 then
util.die("failed to mv original config file, error: ", err)
end
end
local cmd = env.openresty_args .. [[ -s stop]]
util.execute_cmd(cmd)
Expand Down
10 changes: 9 additions & 1 deletion apisix/cli/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ local _M = {}

-- Note: The `execute_cmd` return value will have a line break at the end,
-- it is recommended to use the `trim` function to handle the return value.
function _M.execute_cmd(cmd)
local function execute_cmd(cmd)
local t, err = popen(cmd)
if not t then
return nil, "failed to execute command: "
Expand All @@ -46,6 +46,14 @@ function _M.execute_cmd(cmd)

return data
end
_M.execute_cmd = execute_cmd


-- For commands which stdout would be always be empty,
-- forward stderr to stdout to get the error msg
function _M.execute_cmd_with_error(cmd)
return execute_cmd(cmd .. " 2>&1")
end


function _M.trim(s)
Expand Down
5 changes: 5 additions & 0 deletions t/cli/test_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ cp conf/config.yaml conf/config_original.yaml

make init

if ./bin/apisix start -c conf/not_existed_config.yaml; then
echo "failed: apisix still start with invalid customized config.yaml"
exit 1
fi

./bin/apisix start -c conf/customized_config.yaml

if cmp -s "conf/config.yaml" "conf/config_original.yaml"; then
Expand Down

0 comments on commit 98d4447

Please sign in to comment.