初始化环境命令make init
### init: Initialize the runtime environment
.PHONY: init
init: default
./bin/apisix init
./bin/apisix init_etcd
可以发现,初始化环境,实际上执行了两个lua命令init和init_etcd
bin目录下的apisix实际上就是一个lua脚本,脚本中定义了一些cmd命令。
#!/usr/bin/env lua
....
....
_M[cmd_action](arg[2])apisix init
init命令位于apisix脚本的init函数中:
大致要做的工作,加载yaml配置文件,解析配置、通过配置文件生成nginx配置文件,检查openresty版本。
local function init()
-- read_yaml_conf 读取yaml配置文件
local yaml_conf, err=read_yaml_conf()
if not yaml_conf then
error("failed to read local yaml config of apisix: " .. err)
end
-- print("etcd: ", yaml_conf.etcd)
-- 查看openresty版本
local or_ver=excute_cmd("openresty -V 2>&1")
local with_module_status=true
if or_ver and not or_ver:find("http_stub_status_module", 1, true) then
io.stderr:write("'http_stub_status_module' module is missing in ",
"your openresty, please check it out. Without this ",
"module, there will be fewer monitoring indicators.
")
with_module_status=false
end
-- Using templateder
local sys_conf={
lua_path=pkg_path_org,
lua_cpath=pkg_cpath_org,
os_name=exec("uname"),
apisix_lua_home=apisix_home,
with_module_status=with_module_status,
node_ssl_listen=9443, -- default value
error_log={level="warn"},
}
-- 校验配置
if not yaml_conf.apisix then
error("failed to read `apisix` field from yaml file")
end
-- 校验配置
if not yaml_conf.nginx_config then
error("failed to read `nginx_config` field from yaml file")
end
-- 解析配置
for k,v in pairs(yaml_conf.apisix) do
sys_conf[k]=v
end
for k,v in pairs(yaml_conf.nginx_config) do
sys_conf[k]=v
end
if(sys_conf["enable_dev_mode"]==true) then
sys_conf["worker_processes"]=1
else
sys_conf["worker_processes"]="auto"
end
--通过配置文件渲染生成nginx配置
local conf_render=templatepile(ngx_tpl)
local ngxconf=conf_render(sys_conf)
local ok, err=write_file(apisix_home .. "/conf/nginx.conf", ngxconf)
if not ok then
error("failed to update nginx.conf: " .. err)
end
-- 检查openresty版本
local op_ver=get_openresty_version()
if op_ver==nil then
io.stderr:write("can not find openresty
")
return
end
local need_ver="1.15.8"
if not check_or_version(op_ver, need_ver) then
io.stderr:write("openresty version must >=", need_ver, " current ", op_ver, "
")
return
end
endapisix init_etcd
大致工作如下,读取yaml配置,check数据源,生成etcd配置,执行etcd接口初始化apisix数据。
local function init_etcd(show_output)
-- read_yaml_conf 读取yaml配置文件
local yaml_conf, err=read_yaml_conf()
if not yaml_conf then
error("failed to read local yaml config of apisix: " .. err)
end
if not yaml_conf.apisix then
error("failed to read `apisix` field from yaml file when init etcd")
end
-- check 是否用的etcd数据源
if yaml_conf.apisix.config_center ~="etcd" then
return true
end
if not yaml_conf.etcd then
error("failed to read `etcd` field from yaml file when init etcd")
end
-- 生成etcd配置
local etcd_conf=yaml_conf.etcd
local uri=etcd_conf .. "/v2/keys" .. (etcd_conf.prefix or "")
local timeout=etcd_conf.timeout or 3
-- 执行相关命令,初始化数据
for _, dir_name in ipairs({"/routes", "/upstreams", "/services",
"/plugins", "/consumers", "/node_status",
"/ssl", "/global_rules", "/stream_routes",
"/proto"}) do
local cmd="curl " .. uri .. dir_name
.. "?prev_exist=false -X PUT -d dir=true "
.. "--connect-timeout " .. timeout
.. " --max-time " .. timeout * 2 .. " --retry 1 2>&1"
local res=exec(cmd)
if not res:find("index", 1, true)
and not res:find("createdIndex", 1, true) then
error(cmd .. "
" .. res)
end
if show_output then
print(cmd)
print(res)
end
end
end