Skip to content

Commit

Permalink
feat(ops): use lua libs to backup config file insteadof shell command (
Browse files Browse the repository at this point in the history
…#7048)

Signed-off-by: kwanhur <[email protected]>
Co-authored-by: Alex Zhang <[email protected]>
  • Loading branch information
2 people authored and spacewander committed Jun 30, 2022
1 parent 6beeacc commit b953434
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 35 deletions.
47 changes: 26 additions & 21 deletions apisix/cli/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 signal = require("posix.signal")
local errno = require("posix.errno")

Expand All @@ -38,6 +39,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
Expand Down Expand Up @@ -469,9 +471,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, "doesn't exist\n")
end

yaml_conf.apisix.ssl.ssl_trusted_certificate = cert_path
Expand Down Expand Up @@ -763,16 +764,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)
Expand All @@ -787,15 +791,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
Expand All @@ -804,9 +808,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
Expand All @@ -820,7 +825,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
Expand Down
14 changes: 0 additions & 14 deletions apisix/cli/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b953434

Please sign in to comment.