Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support etcd as data source. #292

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
4c5cc84
Create store.lua
EasonFeng5870 Sep 4, 2019
0336db0
init etcd tools
oracle219 Sep 4, 2019
3232011
support etcd and mysql as config data source.
EasonFeng5870 Sep 4, 2019
2650bcd
refactor compose_plugin_data method
EasonFeng5870 Sep 4, 2019
e709a4a
support etcd data source.
EasonFeng5870 Sep 4, 2019
292ed1a
refactor etcd
EasonFeng5870 Sep 4, 2019
363da8e
add etcd normalize
oracle219 Sep 4, 2019
03d3e8c
Merge remote-tracking branch 'origin/master'
oracle219 Sep 4, 2019
2288245
1. move mysql_db.lua, mysql_store.lua to mysql package.
Sep 5, 2019
8b5cc86
add etcd init_configurations
oracle219 Sep 5, 2019
90f8be1
Merge remote-tracking branch 'origin/master'
oracle219 Sep 5, 2019
2263cce
add etcd new
oracle219 Sep 5, 2019
dc0bad7
Revert "1. move mysql_db.lua, mysql_store.lua to mysql package."
redzippo1 Sep 5, 2019
0eb2498
new package
redzippo1 Sep 5, 2019
94e5768
add etcd content_type
oracle219 Sep 5, 2019
347e1ee
add etcd _request
oracle219 Sep 6, 2019
722b955
Merge remote-tracking branch 'origin/master'
oracle219 Sep 6, 2019
053b6a3
Add operation function for user.
Leonidasz Sep 6, 2019
60f188f
add etcd set
oracle219 Sep 6, 2019
5e46acc
Merge remote-tracking branch 'origin/master'
oracle219 Sep 6, 2019
e1a82fc
add etcd decode_dir_value
oracle219 Sep 6, 2019
9fcd449
add etcd get
oracle219 Sep 6, 2019
6ec2986
add etcd crud
oracle219 Sep 6, 2019
8338f7d
etcd.lua format
oracle219 Sep 6, 2019
ff63ee4
Update store.lua
Leonidasz Sep 9, 2019
5dcba8c
Merge branch 'master' of https://github.com/dangdangdotcom/orange
Leonidasz Sep 9, 2019
54b1c6d
change ngx.ERR to ERR
EasonFeng5870 Sep 11, 2019
0f8d6b2
package changed.
EasonFeng5870 Sep 11, 2019
fb363c4
methoed refactor.
EasonFeng5870 Sep 11, 2019
ef067f7
mysql method refactor.
EasonFeng5870 Sep 12, 2019
540abe6
dashboard support etcd.
EasonFeng5870 Sep 12, 2019
3395db1
dashboard support etcd
EasonFeng5870 Sep 12, 2019
fdf8d77
install script
EasonFeng5870 Sep 18, 2019
3c62213
etcd default data
EasonFeng5870 Sep 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ luac.out
# luarocks build files
*.src.rock
*.zip
*.tar.gz
#*.tar.gz

# Object files
*.o
Expand Down
60 changes: 26 additions & 34 deletions dashboard/model/node.lua
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
local DB = require("dashboard.model.db")
local json = require("orange.utils.json")
local store = context.store

return function(config)
local store_type = config.store

local node_model = {}
local mysql_config = config.store_mysql
local db = DB:new(mysql_config)

local table_name = 'cluster_node'
local stat_table_name = 'cluster_node_stat'

function node_model:new(name, ip, port, api_username, api_password)
return db:query("insert into " .. table_name .. "(name,ip,port,api_username,api_password) values(?,?,?,?,?)",
{ name, ip, port, api_username, api_password })
return store:node_new(name, ip, port, api_username, api_password)
end

function node_model:query_all()
local result, err = db:query("select * from " .. table_name .. " order by ip asc")

if not result or err or type(result) ~= "table" or #result < 1 then
return nil, err
else
return result, err
end
return store:node_query_all()
end

function node_model:get_stat(limit)

local result, err = db:query("select op_time,sum(request_2xx) as request_2xx,sum(request_3xx) as request_3xx," ..
--[[ local result, err = db:query("select op_time,sum(request_2xx) as request_2xx,sum(request_3xx) as request_3xx," ..
" sum(request_4xx) as request_4xx," ..
" sum(request_5xx) as request_5xx," ..
" sum(total_request_count) as total_request_count," ..
Expand All @@ -43,84 +33,86 @@ return function(config)
return nil, err
else
return result, err
end
end]]
end

function node_model:get_stat_by_ip(ip, limit)

local result, err = db:query("select * from " .. stat_table_name .. " where ip = ? order by op_time asc limit ?", { ip, limit })
--[[ local result, err = db:query("select * from " .. stat_table_name .. " where ip = ? order by op_time asc limit ?", { ip, limit })

if not result or err or type(result) ~= "table" or #result < 1 then
return nil, err
else
return result, err
end
end]]
end


function node_model:query_by_id(id)
local result, err = db:query("select * from " .. table_name .. " where id=?", { tonumber(id) })
--[[ local result, err = db:query("select * from " .. table_name .. " where id=?", { tonumber(id) })
if not result or err or type(result) ~= "table" or #result ~= 1 then
return nil, err
else
return result[1], err
end
end]]
end

function node_model:query_by_ip(ip)
local result, err = db:query("select * from " .. table_name .. " where ip=?", { ip })
function node_model:query_by_ip(ip, port)
return store:node_query_by_ip(ip, port)
--[[ local result, err = db:query("select * from " .. table_name .. " where ip=?", { ip })
if not result or err or type(result) ~= "table" or #result ~= 1 then
return nil, err
else
return result[1], err
end
end]]
end

function node_model:update_node(id, name, ip, port, api_username, api_password)
local res, err = db:query("update " .. table_name .. " set name=?,ip=?,port=?,api_username=?,api_password=? where id=?", { name, ip, port, api_username, api_password, tonumber(id) })
return store:node_update_node(id, name, ip, port, api_username, api_password)
--[[ local res, err = db:query("update " .. table_name .. " set name=?,ip=?,port=?,api_username=?,api_password=? where id=?", { name, ip, port, api_username, api_password, tonumber(id) })
if not res or err then
return false
else
return true
end
end]]
end

function node_model:update_node_status(id, status)
local res, err = db:query("update " .. table_name .. " set sync_status=? where id=?", { status, tonumber(id) })
--[[ local res, err = db:query("update " .. table_name .. " set sync_status=? where id=?", { status, tonumber(id) })
if not res or err then
return false
else
return true
end
end]]
end

function node_model:delete(id)
local res, err = db:query("delete from " .. table_name .. " where id=?", { tonumber(id) })
return store:node_delete(id)
--[[ local res, err = db:query("delete from " .. table_name .. " where id=?", { tonumber(id) })
if not res or err then
return false
else
return true
end
end]]
end

function node_model:remove_error_nodes()
local res, err = db:query("delete from " .. table_name .. " where sync_status=? ", { json.encode({ ERROR = false }) })
--[[ local res, err = db:query("delete from " .. table_name .. " where sync_status=? ", { json.encode({ ERROR = false }) })
if not res or err then
return false
else
return true
end
end]]
end

function node_model:registry(ip, port, credentials)
local local_node = self:query_by_ip(ip)
--[[ local local_node = self:query_by_ip(ip)

if not local_node then
self:new(ip, ip, port, credentials.username, credentials.password)
local_node = self.query_by_ip(ip)
end

return local_node
return local_node]]
end

return node_model
Expand Down
56 changes: 13 additions & 43 deletions dashboard/model/user.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
local DB = require("dashboard.model.db")

local store = context.store

return function(config)
local user_model = {}
local mysql_config = config.store_mysql
local db = DB:new(mysql_config)

function user_model:new(username, password, enable)
return db:query("insert into dashboard_user(username, password, enable) values(?,?,?)",
{username, password, enable})
return store:user_new(username, password, enable)
end

function user_model:query(username, password)
local res, err = db:query("select * from dashboard_user where username=? and password=?", {username, password})
return res, err
return store:user_query(username, password)
end

function user_model:query_all()
local result, err = db:query("select id, username, is_admin, create_time, enable from dashboard_user order by id asc")
if not result or err or type(result) ~= "table" or #result < 1 then
return nil, err
else
return result, err
end
return store:user_query_all()
end

--@unused
function user_model:query_by_id(id)
local result, err = db:query("select * from dashboard_user where id=?", {tonumber(id)})
local result, err = self.db:query("select * from dashboard_user where id=?", {tonumber(id)})
if not result or err or type(result) ~= "table" or #result ~=1 then
return nil, err
else
Expand All @@ -36,39 +26,19 @@ return function(config)

-- return user, err
function user_model:query_by_username(username)
local res, err = db:query("select * from dashboard_user where username=? limit 1", {username})
if not res or err or type(res) ~= "table" or #res ~=1 then
return nil, err or "error"
end

return res[1], err
return store:user_query_by_username(username)
end

function user_model:update_enable(userid, enable)
local res, err = db:query("update dashboard_user set enable=? where id=?", {tonumber(enable), tonumber(userid)})
if not res or err then
return false
else
return true
end
function user_model:update_enable(username, enable)
return store:user_update_enable(username, enable)
end

function user_model:update_pwd_and_enable(userid, pwd, enable)
local res, err = db:query("update dashboard_user set password=?, enable=? where id=?", {pwd, tonumber(enable), tonumber(userid)})
if not res or err then
return false
else
return true
end
function user_model:update_pwd_and_enable(username, pwd, enable)
return store:user_update_pwd_and_enable(username, pwd, enable)
end

function user_model:delete(userid)
local res, err = db:query("delete from dashboard_user where id=?", {tonumber(userid)})
if not res or err then
return false
else
return true
end
function user_model:delete(username)
store:user_delete(username)
end

return user_model
Expand Down
23 changes: 13 additions & 10 deletions dashboard/routes/auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,41 @@ return function(config, store)
end

local isExist = false
local userid = 0
local userid = 1

password = encode(password .. "#" .. pwd_secret)
--ngx.say(username.. "#" ..password)
local result, err = user_model:query(username, password)
local result, err = user_model:query_by_username(username)

local user = {}
if result and not err then
if result and #result == 1 then
if result and type(result) == "table" then
isExist = true
user = result[1]
userid = user.id
user = result
--userid = user.id
end
else
isExist = false
end

if isExist == true then
local is_admin = false
if user.is_admin == 1 then
if user.is_admin == "1" then
ngx.log(ngx.INFO, "管理员[", user.username, "]登录")
is_admin = true
else
ngx.log(ngx.INFO, "普通用户[", user.username, "]登录")
end

req.session.set("user", {
local session = {
username = username,
is_admin = is_admin,
userid = userid,
create_time = user.create_time or ""
})
userid = userid
}

--ngx.log(ngx.ERR, session.create_time)

req.session.set("user", session)
return res:json({
success = true,
msg = "登录成功."
Expand Down
26 changes: 3 additions & 23 deletions dashboard/routes/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,17 @@ local string_format = string.format
local lor = require("lor.index")
local socket = require("socket")
local orange_db = require("orange.store.orange_db")

local ip_utils = require "lua_ip"

return function(config, store)

local node_router = lor:Router()
local node_model = require("dashboard.model.node")(config)

local function get_nodes()
local nodes = node_model:query_all()

if nodes then
for _, node in pairs(nodes) do
-- 格式化成 json 数据
node.sync_status = json.decode(node.sync_status)
end
end

return nodes
return node_model:query_all()
end

-- 获取 IP
local function get_ip_by_hostname(hostname)
local ip, resolved = socket.dns.toip(hostname)
local ListTab = {}
for k, v in ipairs(resolved.ip) do
table.insert(ListTab, v)
end
return unpack(ListTab)
end


-- 节点同步
local function sync_nodes(nodes, plugins)

Expand Down Expand Up @@ -85,7 +65,7 @@ return function(config, store)
end

function node_router:register()
local local_ip = get_ip_by_hostname(socket.dns.gethostname())
local local_ip = ip_utils.get_ipv4()
node_model:registry(local_ip, 7777, config.api.credentials[1])
end

Expand Down
Binary file added install/etcd_default_data/etcd_v2_backup.tar.gz
Binary file not shown.
27 changes: 27 additions & 0 deletions install/orange_install_quick_view.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
cd /opt/
wget https://openresty.org/download/openresty-1.15.8.2.tar.gz
tar zxvf openresty-1.15.8.2.tar.gz
cd openresty-1.15.8.2
./configure --prefix=/usr/local/openresty --with-luajit --with-pcre-jit --with-http_iconv_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-stream --with-stream_ssl_module
gmake && gmake install

wget https://luarocks.org/releases/luarocks-2.4.3.tar.gz
tar zxvf luarocks-2.4.3.tar.gz
./configure --prefix=/usr/local/openresty/luajit --with-lua=/usr/local/openresty/luajit/ --lua-suffix=jit --with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
make build && make install
/usr/local/openresty/luajit/bin/luarocks install luafilesystem
/usr/local/openresty/luajit/bin/luarocks install lrandom
/usr/local/openresty/luajit/bin/luarocks install https://luarocks.org/manifests/kong/lua_ip-0.1-2.rockspec
/usr/local/openresty/luajit/bin/luarocks install https://luarocks.org/manifests/membphis/lua-typeof-0.1-0.rockspec
/usr/local/openresty/luajit/bin/luarocks install penlight
/usr/local/openresty/luajit/bin/luarocks install https://luarocks.org/manifests/kong/lua-resty-dns-client-2.2.0-1.rockspec
/usr/local/openresty/luajit/bin/luarocks install luasocket

cd /opt
git clone https://github.com/dangdangdotcom/orange.git
cd orange
cd /usr/local/orange
/usr/local/openresty/bin/opm --install-dir=./ get zhangbao0325/orangelib
/usr/local/openresty/bin/opm install sumory/lor
make install
sh start.sh
Loading