From 65fefa383031c55ed21ed8ca9671eb34e3894a7b Mon Sep 17 00:00:00 2001 From: kwanhur Date: Sat, 14 May 2022 12:13:15 +0800 Subject: [PATCH 1/4] feat(ops): use lua libs to backup config file insteadof shell command * backup and recover files with os.rename insteadof mv * remove files with os.remove insteadof rm * link file with lfs.link insteadof ln * exist file with penlight.exist insteadof io.open Signed-off-by: kwanhur --- apisix/cli/ops.lua | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index ae38a9dd5f7e..38974fb60ab8 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -25,6 +25,7 @@ local profile = require("apisix.core.profile") local template = require("resty.template") local argparse = require("argparse") local pl_path = require("pl.path") +local lfs = require("lfs") local stderr = io.stderr local ipairs = ipairs @@ -36,6 +37,7 @@ local tonumber = tonumber local io_open = io.open local execute = os.execute local os_rename = os.rename +local os_remove = os.remove local table_insert = table.insert local getenv = os.getenv local max = math.max @@ -756,16 +758,19 @@ local function start(env, ...) if customized_yaml then profile.apisix_home = env.apisix_home .. "/" local local_conf_path = profile:yaml_path("config") + local local_conf_path_bak = local_conf_path .. ".bak" - 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) + local ok, err = os_rename(local_conf_path, local_conf_path_bak) + if not ok then + util.die("failed to backup config, 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) + local ok, err1 = lfs.link(customized_yaml, local_conf_path) + if not ok then + ok, err = os_rename(local_conf_path_bak, local_conf_path) + if not ok then + util.die("failed to recover original config file, error: ", err) + end + util.die("failed to link customized config, error: ", err1) end print("Use customized yaml: ", customized_yaml) @@ -780,15 +785,15 @@ end local function cleanup() local local_conf_path = profile:yaml_path("config") - local bak_exist = io_open(local_conf_path .. ".bak") - if bak_exist then - local err = util.execute_cmd_with_error("rm " .. local_conf_path) - if #err > 0 then + local local_conf_path_bak = local_conf_path .. ".bak" + if pl_path.exists(local_conf_path_bak) then + local ok, err = os_remove(local_conf_path) + if not ok 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) + ok, err = os_rename(local_conf_path_bak, local_conf_path) + if not ok then + util.die("failed to recover original config file, error: ", err) end end end @@ -797,9 +802,10 @@ end local function test(env, backup_ngx_conf) -- backup nginx.conf local ngx_conf_path = env.apisix_home .. "/conf/nginx.conf" - local ngx_conf_exist = util.is_file_exist(ngx_conf_path) + local ngx_conf_path_bak = ngx_conf_path .. ".bak" + local ngx_conf_exist = pl_path.exists(ngx_conf_path) if ngx_conf_exist then - local ok, err = os_rename(ngx_conf_path, ngx_conf_path .. ".bak") + local ok, err = os_rename(ngx_conf_path, ngx_conf_path_bak) if not ok then util.die("failed to backup nginx.conf, error: ", err) end @@ -813,7 +819,7 @@ local function test(env, backup_ngx_conf) -- restore nginx.conf if ngx_conf_exist then - local ok, err = os_rename(ngx_conf_path .. ".bak", ngx_conf_path) + local ok, err = os_rename(ngx_conf_path_bak, ngx_conf_path) if not ok then util.die("failed to restore original nginx.conf, error: ", err) end From 592a75693130e0114ec2394d03e325866bb67a70 Mon Sep 17 00:00:00 2001 From: kwanhur Date: Sun, 15 May 2022 22:36:44 +0800 Subject: [PATCH 2/4] feat(ops): check certificate path with penlight.exist Signed-off-by: kwanhur --- apisix/cli/ops.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index 38974fb60ab8..39bc70d68f82 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -469,9 +469,8 @@ Please modify "admin_key" in conf/config.yaml . -- Therefore we need to check the absolute version instead cert_path = pl_path.abspath(cert_path) - local ok, err = util.is_file_exist(cert_path) - if not ok then - util.die(err, "\n") + if not pl_path.exists(cert_path) then + util.die("certificate path", cert_path, "isn't exist\n") end yaml_conf.apisix.ssl.ssl_trusted_certificate = cert_path From b2a8cb5aac90bec631d8966ad4e91ada422a31e6 Mon Sep 17 00:00:00 2001 From: kwanhur Date: Sun, 15 May 2022 22:38:58 +0800 Subject: [PATCH 3/4] feat(util): remove unused api is_file_exist Signed-off-by: kwanhur --- apisix/cli/util.lua | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/apisix/cli/util.lua b/apisix/cli/util.lua index bab857d9d113..daadee51e558 100644 --- a/apisix/cli/util.lua +++ b/apisix/cli/util.lua @@ -121,18 +121,4 @@ function _M.write_file(file_path, data) end -function _M.is_file_exist(file_path) - local file, err = open(file_path) - if not file then - return false, "failed to open file: " - .. file_path - .. ", error info: " - .. err - end - - file:close() - return true -end - - return _M From a9dcb73f6cb559f9eb067d01ff7abcdcb2449054 Mon Sep 17 00:00:00 2001 From: kwanhur Date: Wed, 18 May 2022 11:30:05 +0800 Subject: [PATCH 4/4] fix: semantic word Co-authored-by: Alex Zhang --- apisix/cli/ops.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index 39bc70d68f82..db1e9b1fa326 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -470,7 +470,7 @@ Please modify "admin_key" in conf/config.yaml . cert_path = pl_path.abspath(cert_path) if not pl_path.exists(cert_path) then - util.die("certificate path", cert_path, "isn't exist\n") + util.die("certificate path", cert_path, "doesn't exist\n") end yaml_conf.apisix.ssl.ssl_trusted_certificate = cert_path