This is an automated email from the ASF dual-hosted git repository.
monkeydluffy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new fbe96b8ee refactor: access customized config directly (#9846)
fbe96b8ee is described below
commit fbe96b8eef7985b11683656122933640f2d1b2a5
Author: Fucheng Jiang <[email protected]>
AuthorDate: Wed Jul 19 10:15:40 2023 +0800
refactor: access customized config directly (#9846)
---
apisix/cli/file.lua | 5 ++-
apisix/cli/ops.lua | 63 +++++++++++++++++---------------
apisix/cli/util.lua | 6 +++
apisix/core/profile.lua | 16 ++++++++
t/cli/test_cmd.sh | 97 ++++++++++++++++++++++++++++++++++++++++---------
5 files changed, 139 insertions(+), 48 deletions(-)
diff --git a/apisix/cli/file.lua b/apisix/cli/file.lua
index 2fe9edd03..af071a840 100644
--- a/apisix/cli/file.lua
+++ b/apisix/cli/file.lua
@@ -223,7 +223,10 @@ function _M.read_yaml_conf(apisix_home)
return nil, "invalid config-default.yaml file"
end
- local_conf_path = profile:yaml_path("config")
+ local_conf_path = profile:customized_yaml_path()
+ if not local_conf_path then
+ local_conf_path = profile:yaml_path("config")
+ end
local user_conf_yaml, err = util.read_file(local_conf_path)
if not user_conf_yaml then
return nil, err
diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua
index 4d1eecc74..79b359f62 100644
--- a/apisix/cli/ops.lua
+++ b/apisix/cli/ops.lua
@@ -753,7 +753,22 @@ local function init_etcd(env, args)
end
+local function cleanup(env)
+ if env.apisix_home then
+ profile.apisix_home = env.apisix_home
+ end
+
+ os_remove(profile:customized_yaml_index())
+end
+
+
local function start(env, ...)
+ cleanup(env)
+
+ if env.apisix_home then
+ profile.apisix_home = env.apisix_home
+ end
+
-- Because the worker process started by apisix has "nobody" permission,
-- it cannot access the `/root` directory. Therefore, it is necessary to
-- prohibit APISIX from running in the /root directory.
@@ -817,21 +832,25 @@ local function start(env, ...)
local customized_yaml = args["config"]
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 customized_yaml_path
+ local idx = str_find(customized_yaml, "/")
+ if idx and idx == 1 then
+ customized_yaml_path = customized_yaml
+ else
+ local cur_dir, err = lfs.currentdir()
+ if err then
+ util.die("failed to get current directory")
+ end
+ customized_yaml_path = cur_dir .. "/" .. customized_yaml
+ end
- local ok, err = os_rename(local_conf_path, local_conf_path_bak)
- if not ok then
- util.die("failed to backup config, error: ", err)
+ if not util.file_exists(customized_yaml_path) then
+ util.die("customized config file not exists, path: " ..
customized_yaml_path)
end
- local ok, err1 = lfs.link(customized_yaml, local_conf_path)
+
+ local ok, err = util.write_file(profile:customized_yaml_index(),
customized_yaml_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)
+ util.die("write customized config index failed, err: " .. err)
end
print("Use customized yaml: ", customized_yaml)
@@ -847,22 +866,6 @@ local function start(env, ...)
end
-local function cleanup()
- local local_conf_path = profile:yaml_path("config")
- 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
- 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
-
-
local function test(env, backup_ngx_conf)
-- backup nginx.conf
local ngx_conf_path = env.apisix_home .. "/conf/nginx.conf"
@@ -902,7 +905,7 @@ end
local function quit(env)
- cleanup()
+ cleanup(env)
local cmd = env.openresty_args .. [[ -s quit]]
util.execute_cmd(cmd)
@@ -910,7 +913,7 @@ end
local function stop(env)
- cleanup()
+ cleanup(env)
local cmd = env.openresty_args .. [[ -s stop]]
util.execute_cmd(cmd)
diff --git a/apisix/cli/util.lua b/apisix/cli/util.lua
index cc6206a84..bcd56a241 100644
--- a/apisix/cli/util.lua
+++ b/apisix/cli/util.lua
@@ -19,6 +19,7 @@ local require = require
local pcall = pcall
local open = io.open
local popen = io.popen
+local close = io.close
local exit = os.exit
local stderr = io.stderr
local str_format = string.format
@@ -127,4 +128,9 @@ function _M.write_file(file_path, data)
end
+function _M.file_exists(file_path)
+ local f = open(file_path, "r")
+ return f ~= nil and close(f)
+end
+
return _M
diff --git a/apisix/core/profile.lua b/apisix/core/profile.lua
index 389a9d42c..a5dcdc81e 100644
--- a/apisix/core/profile.lua
+++ b/apisix/core/profile.lua
@@ -19,6 +19,8 @@
--
-- @module core.profile
+local util = require("apisix.cli.util")
+
local _M = {
version = 0.1,
profile = os.getenv("APISIX_PROFILE") or "",
@@ -48,4 +50,18 @@ function _M.yaml_path(self, file_name)
end
+function _M.customized_yaml_index(self)
+ return self.apisix_home .. "/conf/.customized_config_path"
+end
+
+
+function _M.customized_yaml_path(self)
+ local customized_config_index = self:customized_yaml_index()
+ if util.file_exists(customized_config_index) then
+ return util.read_file(customized_config_index)
+ end
+ return nil
+end
+
+
return _M
diff --git a/t/cli/test_cmd.sh b/t/cli/test_cmd.sh
index 449069577..dbefa88cf 100755
--- a/t/cli/test_cmd.sh
+++ b/t/cli/test_cmd.sh
@@ -86,10 +86,19 @@ fi
make stop
echo "pass: check APISIX running"
-# check customized config.yaml is copied and reverted.
+# check customized config
git checkout conf/config.yaml
+# start with not existed customized config
+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
+
+# start with customized config
echo "
deployment:
admin:
@@ -101,37 +110,91 @@ deployment:
admin_ssl_cert_key: '../t/certs/apisix_admin_ssl.key'
" > conf/customized_config.yaml
-cp conf/config.yaml conf/config_original.yaml
+./bin/apisix start -c conf/customized_config.yaml
-make init
+# check if .customized_config_path has been created
+if [ ! -e conf/.customized_config_path ]; then
+ rm conf/customized_config.yaml
+ echo ".config_path file should exits"
+ exit 1
+fi
-if ./bin/apisix start -c conf/not_existed_config.yaml; then
- echo "failed: apisix still start with invalid customized config.yaml"
+# check if the custom config is used
+code=$(curl -k -i -m 20 -o /dev/null -s -w %{http_code}
https://127.0.0.1:9180/apisix/admin/routes -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1')
+if [ ! $code -eq 200 ]; then
+ rm conf/customized_config.yaml
+ echo "failed: customized config.yaml not be used"
exit 1
fi
-./bin/apisix start -c conf/customized_config.yaml
+make stop
-if cmp -s "conf/config.yaml" "conf/config_original.yaml"; then
- rm conf/config_original.yaml
- echo "failed: customized config.yaml copied failed"
+# check if .customized_config_path has been removed
+if [ -e conf/.config_path ]; then
+ rm conf/customized_config_path.yaml
+ echo ".config_path file should be removed"
exit 1
fi
-code=$(curl -k -i -m 20 -o /dev/null -s -w %{http_code}
https://127.0.0.1:9180/apisix/admin/routes -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1')
+# start with invalied config
+echo "abc" > conf/customized_config.yaml
+
+if ./bin/apisix start -c conf/customized_config.yaml ; then
+ rm conf/customized_config.yaml
+ echo "start should be failed"
+ exit 1
+fi
+
+# check if apisix can be started use correctly default config.
(https://github.com/apache/apisix/issues/9700)
+./bin/apisix start
+
+code=$(curl -k -i -m 20 -o /dev/null -s -w %{http_code}
http://127.0.0.1:9180/apisix/admin/routes -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1')
if [ ! $code -eq 200 ]; then
- rm conf/config_original.yaml conf/customized_config.yaml
- echo "failed: customized config.yaml not be used"
+ rm conf/customized_config.yaml
+ echo "failed: should use default config"
exit 1
fi
make stop
-if ! cmp -s "conf/config.yaml" "conf/config_original.yaml"; then
- rm conf/config_original.yaml conf/customized_config.yaml
- echo "failed: customized config.yaml reverted failed"
+# check if apisix can be started after multiple start failures.
(https://github.com/apache/apisix/issues/9171)
+echo "
+deployment:
+ admin:
+ admin_listen:
+ port: 9180
+ https_admin: true
+ admin_api_mtls:
+ admin_ssl_cert: '../t/certs/apisix_admin_ssl.crt'
+ admin_ssl_cert_key: '../t/certs/apisix_admin_ssl.key'
+ etcd:
+ host:
+ - http://127.0.0.1:22379
+" > conf/customized_config.yaml
+
+./bin/apisix start -c conf/customized_config.yaml || true
+./bin/apisix start -c conf/customized_config.yaml || true
+./bin/apisix start -c conf/customized_config.yaml || true
+
+echo "
+deployment:
+ admin:
+ admin_listen:
+ port: 9180
+ https_admin: true
+ admin_api_mtls:
+ admin_ssl_cert: '../t/certs/apisix_admin_ssl.crt'
+ admin_ssl_cert_key: '../t/certs/apisix_admin_ssl.key'
+" > conf/customized_config.yaml
+
+./bin/apisix start -c conf/customized_config.yaml
+
+code=$(curl -k -i -m 20 -o /dev/null -s -w %{http_code}
https://127.0.0.1:9180/apisix/admin/routes -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1')
+if [ ! $code -eq 200 ]; then
+ rm conf/customized_config.yaml
+ echo "failed: should use default config"
exit 1
fi
-rm conf/config_original.yaml conf/customized_config.yaml
-echo "passed: customized config.yaml copied and reverted succeeded"
+rm conf/customized_config.yaml
+echo "passed: test customized config successful"