From a7697c2d4bf90a62209673437d6d742dae55f422 Mon Sep 17 00:00:00 2001 From: Yuansheng Wang Date: Tue, 4 Jun 2019 08:11:39 +0800 Subject: [PATCH] CLI: Initialized etcd and `nginx.conf` at `apisix` server startup. --- cli/apisix.lua | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/cli/apisix.lua b/cli/apisix.lua index 2eb095a4..0a7519ca 100755 --- a/cli/apisix.lua +++ b/cli/apisix.lua @@ -182,10 +182,11 @@ local _M = {version = 0.1} function _M.help() print([[ -Usage: apisix +Usage: apisix [action] help: show this message, then exit -init: initialize all configurations +init: initialize the local nginx.conf +init_etcd: initialize the data of etcd start: start the apisix server stop: stop the apisix server reload: reload the apisix server @@ -223,8 +224,37 @@ local function init() end _M.init = init -function _M.start() - init() +local function init_etcd(show_output) + -- read_yaml_conf + local yaml_conf, err = read_yaml_conf() + if not yaml_conf then + error("failed to read local yaml config of apisix: " .. err) + end + + local etcd_conf = yaml_conf.etcd + local uri = etcd_conf.host .. etcd_conf.prefix + + for _, dir_name in ipairs({"/routes", "/upstreams", "/services"}) do + local cmd = "curl " .. uri .. dir_name + .. "?prev_exist=false -X PUT -d dir=true 2>&1" + local res = exec(cmd) + if not res:find([[index]], 1, true) + and not res:find([[createdIndex]], 1, true) then + error(cmd .. "\n" .. res) + end + + if show_output then + print(cmd) + print(res) + end + end +end +_M.init_etcd = init_etcd + +function _M.start(...) + init(...) + init_etcd(...) + local home_path = apisix_home() if not home_path then error("failed to find home path of apisix") @@ -263,4 +293,4 @@ if not _M[cmd_action] then return end -_M[cmd_action]() +_M[cmd_action](arg[2])