From f2726aa058b69d45e20e04409c45a22f48799032 Mon Sep 17 00:00:00 2001 From: CN_SZTL Date: Fri, 10 Apr 2020 20:24:53 +0800 Subject: [PATCH] luci-app-dockerman: bump to v0.5.10 --- package/ctcgfw/luci-app-dockerman/Makefile | 2 +- .../root/etc/init.d/dockerman | 1 + .../usr/share/dockerman/dockerd-config.lua | 2 +- package/ctcgfw/luci-lib-docker/Makefile | 8 +- .../ctcgfw/luci-lib-docker/luasrc/docker.lua | 116 ++++++++++-------- 5 files changed, 75 insertions(+), 54 deletions(-) diff --git a/package/ctcgfw/luci-app-dockerman/Makefile b/package/ctcgfw/luci-app-dockerman/Makefile index 786c66a026..50399fceaf 100644 --- a/package/ctcgfw/luci-app-dockerman/Makefile +++ b/package/ctcgfw/luci-app-dockerman/Makefile @@ -1,7 +1,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-dockerman -PKG_VERSION:=v0.5.9 +PKG_VERSION:=v0.5.10 PKG_RELEASE:=beta PKG_MAINTAINER:=lisaac PKG_LICENSE:=AGPL-3.0 diff --git a/package/ctcgfw/luci-app-dockerman/root/etc/init.d/dockerman b/package/ctcgfw/luci-app-dockerman/root/etc/init.d/dockerman index 285486d550..22629c1933 100755 --- a/package/ctcgfw/luci-app-dockerman/root/etc/init.d/dockerman +++ b/package/ctcgfw/luci-app-dockerman/root/etc/init.d/dockerman @@ -30,6 +30,7 @@ handle_allowed_interface(){ } start(){ + [ ! -x "/etc/init.d/dockerd" ] && return 0 init_dockerman_chain if [ -n "$daemon_ea" ]; then handle_allowed_interface diff --git a/package/ctcgfw/luci-app-dockerman/root/usr/share/dockerman/dockerd-config.lua b/package/ctcgfw/luci-app-dockerman/root/usr/share/dockerman/dockerd-config.lua index a51354c7a6..179868869b 100644 --- a/package/ctcgfw/luci-app-dockerman/root/usr/share/dockerman/dockerd-config.lua +++ b/package/ctcgfw/luci-app-dockerman/root/usr/share/dockerman/dockerd-config.lua @@ -4,7 +4,7 @@ uci = (require "luci.model.uci").cursor() raw_file_dir = arg[1] -raw_json_str = fs.readfile(raw_file_dir) +raw_json_str = fs.readfile(raw_file_dir) or "[]" raw_json = luci.jsonc.parse(raw_json_str) or {} new_json = {} diff --git a/package/ctcgfw/luci-lib-docker/Makefile b/package/ctcgfw/luci-lib-docker/Makefile index 25f183f81a..bd732676e2 100644 --- a/package/ctcgfw/luci-lib-docker/Makefile +++ b/package/ctcgfw/luci-lib-docker/Makefile @@ -1,8 +1,8 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-lib-docker -PKG_VERSION:=v0.3.0 -PKG_RELEASE:=beta +PKG_VERSION:=v0.3.3 +PKG_RELEASE:=1 PKG_MAINTAINER:=lisaac PKG_LICENSE:=AGPL-3.0 @@ -11,7 +11,7 @@ PKG_SOURCE_URL:=https://github.com/lisaac/luci-lib-docker.git PKG_SOURCE_VERSION:=$(PKG_VERSION) PKG_SOURCE_SUBDIR:=$(PKG_NAME) -PKG_SOURCE:=$(PKG_SOURCE_SUBDIR)-$(PKG_VERSION).tar.gz +PKG_SOURCE:=$(PKG_SOURCE_SUBDIR)-$(PKG_SOURCE_VERSION).tar.gz PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_SOURCE_SUBDIR) include $(INCLUDE_DIR)/package.mk @@ -46,4 +46,4 @@ define Package/$(PKG_NAME)/install cp -pR $(PKG_BUILD_DIR)/luasrc/* $(1)/usr/lib/lua/luci/ endef -$(eval $(call BuildPackage,$(PKG_NAME))) +$(eval $(call BuildPackage,$(PKG_NAME))) \ No newline at end of file diff --git a/package/ctcgfw/luci-lib-docker/luasrc/docker.lua b/package/ctcgfw/luci-lib-docker/luasrc/docker.lua index 8a5f056fc7..0361c05382 100644 --- a/package/ctcgfw/luci-lib-docker/luasrc/docker.lua +++ b/package/ctcgfw/luci-lib-docker/luasrc/docker.lua @@ -1,3 +1,7 @@ +--[[ +LuCI - Lua Configuration Interface +Copyright 2019 lisaac +]]-- require "nixio.util" require "luci.util" local jsonc = require "luci.jsonc" @@ -13,46 +17,42 @@ local chunksource = function(sock, buffer) buffer = buffer or "" return function() local output - local _, endp, count = buffer:find("^([0-9a-fA-F]+);?.-\r\n") - if not count then -- lua ^ only match start of stirng,not start of line - _, endp, count = buffer:find("\r\n([0-9a-fA-F]+);?.-\r\n") - end - while not count do + local _, endp, count = buffer:find("^([0-9a-fA-F]+)\r\n") + if not count then local newblock, code = sock:recv(1024) - if not newblock then - return nil, code - end + if not newblock then return nil, code end buffer = buffer .. newblock - _, endp, count = buffer:find("^([0-9a-fA-F]+);?.-\r\n") - if not count then - _, endp, count = buffer:find("\r\n([0-9a-fA-F]+);?.-\r\n") - end + _, endp, count = buffer:find("^([0-9a-fA-F]+)\r\n") end count = tonumber(count, 16) if not count then return nil, -1, "invalid encoding" elseif count == 0 then -- finial return nil - elseif count + 2 <= #buffer - endp then + elseif count <= #buffer - endp then + --data >= count output = buffer:sub(endp + 1, endp + count) - buffer = buffer:sub(endp + count + 3) -- don't forget handle buffer + if count == #buffer - endp then -- [data] + buffer = buffer:sub(endp + count + 1) + count, code = sock:recvall(2) --read \r\n + if not count then return nil, code end + elseif count + 1 == #buffer - endp then -- [data]\r + buffer = buffer:sub(endp + count + 2) + count, code = sock:recvall(1) --read \n + if not count then return nil, code end + else -- [data]\r\n[count]\r\n[data]... + buffer = buffer:sub(endp + count + 3) -- cut buffer + end return output else + -- data < count output = buffer:sub(endp + 1, endp + count) - buffer = "" - if count > #output then - local remain, code = sock:recvall(count - #output) --need read remaining - if not remain then - return nil, code - end - output = output .. remain - count, code = sock:recvall(2) --read \r\n - else - count, code = sock:recvall(count + 2 - #buffer + endp) - end - if not count then - return nil, code - end + buffer = buffer:sub(endp + count + 1) + local remain, code = sock:recvall(count - #output) --need read remaining + if not remain then return nil, code end + output = output .. remain + count, code = sock:recvall(2) --read \r\n + if not count then return nil, code end return output end end @@ -83,18 +83,27 @@ local docker_stream_filter = function(buffer) -- return string.sub(buffer, 9, valid_length + 8) end -local send_http_socket = function(socket_path, req_header, req_body, callback) - local docker_socket = nixio.socket("unix", "stream") - if docker_socket:connect(socket_path) ~= true then - return { - headers = {code=497, message="bad socket path", protocol="HTTP/1.1"}, - body = {message="can\'t connect to unix socket"} - } +local open_socket = function(req_options) + local socket + if type(req_options) ~= "table" then return socket end + if req_options.socket_path then + socket = nixio.socket("unix", "stream") + if socket:connect(req_options.socket_path) ~= true then return nil end + elseif req_options.host and req_options.port then + socket = nixio.connect(req_options.host, req_options.port) end + if socket then + return socket + else + return nil + end +end + +local send_http_socket = function(docker_socket, req_header, req_body, callback) if docker_socket:send(req_header) == 0 then return { - headers={code=498,message="bad socket path", protocol="HTTP/1.1"}, - body={message="can\'t send data to unix socket"} + headers={code=498,message="bad path", protocol="HTTP/1.1"}, + body={message="can\'t send data to socket"} } end @@ -107,8 +116,10 @@ local send_http_socket = function(socket_path, req_header, req_body, callback) elseif req_body and type(req_body) == "table" then -- json docker_socket:send(json_stringify(req_body)) + if options.debug then io.popen("echo '".. json_stringify(req_body) .. "' >> " .. options.debug_path) end elseif req_body then docker_socket:send(req_body) + if options.debug then io.popen("echo '".. req_body .. "' >> " .. options.debug_path) end end local linesrc = docker_socket:linesource() @@ -171,9 +182,7 @@ end local gen_header = function(options, http_method, api_group, api_action, name_or_id, request) local header, query, path - options = options or {} - options.protocol = options.protocol or "HTTP/1.1" - name_or_id = name_or_id ~= "" and name_or_id or nil + name_or_id = (name_or_id ~= "") and name_or_id or nil if request and type(request.query) == "table" then local k, v @@ -187,7 +196,6 @@ local gen_header = function(options, http_method, api_group, api_action, name_or end end end - path = (api_group and ("/" .. api_group) or "") .. (name_or_id and ("/" .. name_or_id) or "") .. (api_action and ("/" .. api_action) or "") .. (query or "") header = (http_method or "GET") .. " " .. path .. " " .. options.protocol .. "\r\n" header = header .. "Host: " .. options.host .. "\r\n" @@ -223,8 +231,16 @@ local call_docker = function(options, http_method, api_group, api_action, name_o local req_header = gen_header(req_options, http_method, api_group, api_action, name_or_id, request) local req_body = request and request.body or nil + local docker_socket = open_socket(req_options) - return send_http_socket(req_options.socket_path, req_header, req_body, callback) + if docker_socket then + return send_http_socket(docker_socket, req_header, req_body, callback) + else + return { + headers = {code=497, message="bad socket path or host", protocol="HTTP/1.1"}, + body = {message="can\'t connect to socket"} + } + end end local gen_api = function(_table, http_method, api_group, api_action) @@ -329,7 +345,8 @@ gen_api(_docker, "POST", "images", "tag") gen_api(_docker, "DELETE", "images", "remove") gen_api(_docker, "GET", "images", "search") gen_api(_docker, "POST", "images", "prune") --- TODO: build clear push commit export import +gen_api(_docker, "GET", "images", "get") +gen_api(_docker, "POST", "images", "load") gen_api(_docker, "GET", "networks", "list") gen_api(_docker, "GET", "networks", "inspect") @@ -354,15 +371,18 @@ function _docker.new(options) local docker = {} local _options = options or {} docker.options = { - socket_path = _options.socket_path or "/var/run/docker.sock", - host = _options.host or "localhost", + socket_path = _options.socket_path or nil, + host = _options.socket_path and "localhost" or _options.host, + port = not _options.socket_path and _options.port or nil, + tls = _options.tls or nil, + tls_cacert = _options.tls and _options.tls_cacert or nil, + tls_cert = _options.tls and _options.tls_cert or nil, + tls_key = _options.tls and _options.tls_key or nil, version = _options.version or "v1.40", user_agent = _options.user_agent or "LuCI", protocol = _options.protocol or "HTTP/1.1", - -- status_enabled = _options.status_enabled or true, - -- status_path = _options.status_path or "/tmp/.docker_action_status", debug = _options.debug or false, - debug_path = _options.debug_path or "/tmp/.docker_debug" + debug_path = _options.debug and _options.debug_path or nil } setmetatable( docker,