Skip to content

Commit

Permalink
dockerman: merge lisaac's commits (coolsnowwolf#3308)
Browse files Browse the repository at this point in the history
* luci-lib-docker: merge lisaac's commits:

* last commit:

df1588052311069b95e3e567621cc9d125bd1b54

* luci-app-dockerman: merge lisaac's commits:

* last commit:

86e354f18b0c95bc168d9266a7c1137ce4a988f0
  • Loading branch information
KFERMercer authored Feb 24, 2020
1 parent 30d8784 commit ef9d5f9
Show file tree
Hide file tree
Showing 28 changed files with 433 additions and 289 deletions.
26 changes: 19 additions & 7 deletions package/lean/luci-app-dockerman/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=luci-app-dockerman
PKG_VERSION:=v0.2.2
PKG_VERSION:=v0.3.0
PKG_RELEASE:=beta
PKG_MAINTAINER:=lisaac <https://github.com/lisaac/luci-app-dockerman>
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
PKG_LICENSE:=Apache-2.0
PKG_LICENSE:=AGPL-3.0

PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/lisaac/luci-app-dockerman.git
PKG_SOURCE_VERSION:=$(PKG_VERSION)

PKG_SOURCE_SUBDIR:=$(PKG_NAME)
PKG_SOURCE:=$(PKG_SOURCE_SUBDIR)-$(PKG_VERSION).tar.gz
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_SOURCE_SUBDIR)

include $(INCLUDE_DIR)/package.mk

Expand All @@ -23,6 +30,7 @@ define Package/$(PKG_NAME)/description
endef

define Build/Prepare
tar -xzvf $(DL_DIR)/$(PKG_SOURCE) -C $(BUILD_DIR)
endef

define Build/Compile
Expand All @@ -34,12 +42,16 @@ rm -fr /tmp/luci-indexcache /tmp/luci-modulecache
endef

define Package/$(PKG_NAME)/install
$(INSTALL_DIR) $(1)/usr/lib/lua/luci
cp -pR ./luasrc/* $(1)/usr/lib/lua/luci
$(INSTALL_DIR) $(1)/
cp -pR ./root/* $(1)/
cp -pR $(PKG_BUILD_DIR)/root/* $(1)/
# $(INSTALL_DIR) $(1)/www
# cp -pR $(PKG_BUILD_DIR)/htdoc/* $(1)/www
$(INSTALL_DIR) $(1)/usr/lib/lua/luci
cp -pR $(PKG_BUILD_DIR)/luasrc/* $(1)/usr/lib/lua/luci/
$(INSTALL_DIR) $(1)/usr/lib/lua/luci/i18n
po2lmo ./po/zh-cn/dockerman.po $(1)/usr/lib/lua/luci/i18n/dockerman.zh-cn.lmo
$(foreach po, $(shell find $(PKG_BUILD_DIR)/po/*/*.po),\
po2lmo $(po) $(1)/usr/lib/lua/luci/i18n/dockerman.$(shell echo $(po) | awk -F'/' '{print $$(NF-1)}').lmo;)
#po2lmo $(PKG_BUILD_DIR)/po/zh-cn/dockerman.po $(1)/usr/lib/lua/luci/i18n/dockerman.zh-cn.lmo
endef

$(eval $(call BuildPackage,$(PKG_NAME)))
76 changes: 62 additions & 14 deletions package/lean/luci-app-dockerman/luasrc/controller/dockerman.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ module("luci.controller.dockerman",package.seeall)
function index()

entry({"admin", "services","docker"}, firstchild(), "Docker", 40).dependent = false
entry({"admin","services","docker","overview"},cbi("docker/overview"),_("Overview"),0).leaf=true
entry({"admin","services","docker","overview"},cbi("dockerman/overview"),_("Overview"),0).leaf=true

local socket = luci.model.uci.cursor():get("docker", "local", "socket_path")
local socket = luci.model.uci.cursor():get("dockerman", "local", "socket_path")
if not nixio.fs.access(socket) then return end
if (require "luci.model.docker").new():_ping().code ~= 200 then return end
entry({"admin","services","docker","containers"},form("docker/containers"),_("Containers"),1).leaf=true
entry({"admin","services","docker","images"},form("docker/images"),_("Images"),2).leaf=true
entry({"admin","services","docker","networks"},form("docker/networks"),_("Networks"),3).leaf=true
entry({"admin","services","docker","volumes"},form("docker/volumes"),_("Volumes"),4).leaf=true
entry({"admin","services","docker","containers"},form("dockerman/containers"),_("Containers"),1).leaf=true
entry({"admin","services","docker","images"},form("dockerman/images"),_("Images"),2).leaf=true
entry({"admin","services","docker","networks"},form("dockerman/networks"),_("Networks"),3).leaf=true
entry({"admin","services","docker","volumes"},form("dockerman/volumes"),_("Volumes"),4).leaf=true
entry({"admin","services","docker","events"},call("action_events"),_("Events"),5)
entry({"admin","services","docker","newcontainer"},form("docker/newcontainer")).leaf=true
entry({"admin","services","docker","newnetwork"},form("docker/newnetwork")).leaf=true
entry({"admin","services","docker","container"},form("docker/container")).leaf=true
entry({"admin","services","docker","newcontainer"},form("dockerman/newcontainer")).leaf=true
entry({"admin","services","docker","newnetwork"},form("dockerman/newnetwork")).leaf=true
entry({"admin","services","docker","container"},form("dockerman/container")).leaf=true
entry({"admin","services","docker","container_stats"},call("action_get_container_stats")).leaf=true
entry({"admin","services","docker","container_get_archive"},call("download_archive")).leaf=true
entry({"admin","services","docker","container_put_archive"},call("upload_archive")).leaf=true
entry({"admin","services","docker","confirm"},call("action_confirm")).leaf=true

end
Expand All @@ -35,7 +37,7 @@ function action_events()
local dk = docker.new()
local query ={}
query["until"] = os.time()
local events = dk:events(nil, query)
local events = dk:events({query = query})
for _, v in ipairs(events.body) do
if v.Type == "container" then
logs = (logs ~= "" and (logs .. "\n") or logs) .. "[" .. os.date("%Y-%m-%d %H:%M:%S", v.time) .."] "..v.Type.. " " .. (v.Action or "null") .. " Container ID:".. (v.Actor.ID or "null") .. " Container Name:" .. (v.Actor.Attributes.name or "null")
Expand All @@ -45,7 +47,7 @@ function action_events()
logs = (logs ~= "" and (logs .. "\n") or logs) .. "[" .. os.date("%Y-%m-%d %H:%M:%S", v.time) .."] "..v.Type.. " " .. v.Action .. " Image:".. (v.Actor.ID or "null").. " Image Name:" .. (v.Actor.Attributes.name or "null")
end
end
luci.template.render("docker/logs", {self={syslog = logs, title="Docker Events"}})
luci.template.render("dockerman/logs", {self={syslog = logs, title="Docker Events"}})
end

local calculate_cpu_percent = function(d)
Expand Down Expand Up @@ -94,9 +96,9 @@ end
function action_get_container_stats(container_id)
if container_id then
local dk = docker.new()
local response = dk.containers:inspect(container_id)
local response = dk.containers:inspect({id = container_id})
if response.code == 200 and response.body.State.Running then
response = dk.containers:stats(container_id, {stream=false})
response = dk.containers:stats({id = container_id, query = {stream = false}})
if response.code == 200 then
local container_stats = response.body
local cpu_percent = calculate_cpu_percent(container_stats)
Expand Down Expand Up @@ -136,7 +138,7 @@ function action_get_container_stats(container_id)
end

function action_confirm()
local status_path=luci.model.uci.cursor():get("docker", "local", "status_path")
local status_path=luci.model.uci.cursor():get("dockerman", "local", "status_path")
local data = nixio.fs.readfile(status_path)
if data then
code = 202
Expand All @@ -151,3 +153,49 @@ function action_confirm()
luci.http.prepare_content("application/json")
luci.http.write_json({info = data})
end

function download_archive()
local id = luci.http.formvalue("id")
local path = luci.http.formvalue("path")
local dk = docker.new()
local first

local cb = function(res, chunk)
if res.code == 200 then
if not first then
first = true
luci.http.header('Content-Disposition', 'inline; filename="archive.tar"')
luci.http.header('Content-Type', 'application\/x-tar')
end
luci.ltn12.pump.all(chunk, luci.http.write)
else
if not first then
first = true
luci.http.prepare_content("text/plain")
end
luci.ltn12.pump.all(chunk, luci.http.write)
end
end

local res = dk.containers:get_archive({id = id, query = {path = path}}, cb)
end

function upload_archive(container_id)
local path = luci.http.formvalue("upload-path")
local dk = docker.new()
local ltn12 = require "luci.ltn12"

rec_send = function(sinkout)
luci.http.setfilehandler(function (meta, chunk, eof)
if chunk then
ltn12.pump.step(ltn12.source.string(chunk), sinkout)
end
end)
end

local res = dk.containers:put_archive({id = container_id, query = {path = path}, body = rec_send})
local msg = res and res.body and res.body.message or nil
luci.http.status(res.code, msg)
luci.http.prepare_content("application/json")
luci.http.write_json({message = msg})
end
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local action = arg[2] or "info"

local images, networks, containers_info
if not container_id then return end
local res = dk.containers:inspect(container_id)
local res = dk.containers:inspect({id = container_id})
if res.code < 300 then container_info = res.body else return end
res = dk.networks:list()
if res.code < 300 then networks = res.body else return end
Expand Down Expand Up @@ -110,9 +110,9 @@ local start_stop_remove = function(m, cmd)
docker:append_status("Containers: " .. cmd .. " " .. container_id .. "...")
local res
if cmd ~= "upgrade" then
res = dk.containers[cmd](dk, container_id)
res = dk.containers[cmd](dk, {id = container_id})
else
res = dk.containers_upgrade(dk, container_id)
res = dk.containers_upgrade(dk, {id = container_id})
end
if res and res.code >= 300 then
docker:append_status("fail code:" .. res.code.." ".. (res.body.message and res.body.message or res.message))
Expand All @@ -128,11 +128,11 @@ local start_stop_remove = function(m, cmd)
end

m=SimpleForm("docker", container_info.Name:sub(2), translate("Docker Container") )
m.template = "docker/cbi/xsimpleform"
m.template = "dockerman/cbi/xsimpleform"
m.redirect = luci.dispatcher.build_url("admin/services/docker/containers")
-- m:append(Template("docker/container"))
-- m:append(Template("dockerman/container"))
docker_status = m:section(SimpleSection)
docker_status.template="docker/apply_widget"
docker_status.template = "dockerman/apply_widget"
docker_status.err=nixio.fs.readfile(dk.options.status_path)
-- luci.util.perror(docker_status.err)
if docker_status.err then docker:clear_status() end
Expand All @@ -141,35 +141,35 @@ if docker_status.err then docker:clear_status() end
action_section = m:section(Table,{{}})
action_section.notitle=true
action_section.rowcolors=false
action_section.template="cbi/nullsection"
action_section.template = "cbi/nullsection"

btnstart=action_section:option(Button, "_start")
btnstart.template="docker/cbi/inlinebutton"
btnstart.template = "dockerman/cbi/inlinebutton"
btnstart.inputtitle=translate("Start")
btnstart.inputstyle = "apply"
btnstart.forcewrite = true
btnrestart=action_section:option(Button, "_restart")
btnrestart.template="docker/cbi/inlinebutton"
btnrestart.template = "dockerman/cbi/inlinebutton"
btnrestart.inputtitle=translate("Restart")
btnrestart.inputstyle = "reload"
btnrestart.forcewrite = true
btnstop=action_section:option(Button, "_stop")
btnstop.template="docker/cbi/inlinebutton"
btnstop.template = "dockerman/cbi/inlinebutton"
btnstop.inputtitle=translate("Stop")
btnstop.inputstyle = "reset"
btnstop.forcewrite = true
btnupgrade=action_section:option(Button, "_upgrade")
btnupgrade.template="docker/cbi/inlinebutton"
btnupgrade.template = "dockerman/cbi/inlinebutton"
btnupgrade.inputtitle=translate("Upgrade")
btnupgrade.inputstyle = "reload"
btnstop.forcewrite = true
btnduplicate=action_section:option(Button, "_duplicate")
btnduplicate.template="docker/cbi/inlinebutton"
btnduplicate.template = "dockerman/cbi/inlinebutton"
btnduplicate.inputtitle=translate("Duplicate")
btnduplicate.inputstyle = "add"
btnstop.forcewrite = true
btnremove=action_section:option(Button, "_remove")
btnremove.template="docker/cbi/inlinebutton"
btnremove.template = "dockerman/cbi/inlinebutton"
btnremove.inputtitle=translate("Remove")
btnremove.inputstyle = "remove"
btnremove.forcewrite = true
Expand All @@ -194,7 +194,7 @@ btnduplicate.write = function(self, section)
end

tab_section = m:section(SimpleSection)
tab_section.template="docker/container"
tab_section.template = "dockerman/container"

if action == "info" then
m.submit = false
Expand Down Expand Up @@ -302,7 +302,7 @@ if action == "info" then
end
dv_opts.render = function(self, section, scope)
if table_info[section]._key==translate("Connect Network") then
self.template="cbi/value"
self.template = "cbi/value"
self.keylist = {}
self.vallist = {}
self.placeholder = "10.1.1.254"
Expand All @@ -324,7 +324,7 @@ if action == "info" then
self.template = "cbi/button"
Button.render(self, section, scope)
else
self.template = "docker/cbi/dummyvalue"
self.template = "dockerman/cbi/dummyvalue"
self.default=""
DummyValue.render(self, section, scope)
end
Expand All @@ -336,16 +336,16 @@ if action == "info" then
if section == "01name" then
docker:append_status("Containers: rename " .. container_id .. "...")
local new_name = table_info[section]._value
res = dk.containers:rename(container_id,{name=new_name})
res = dk.containers:rename({id = container_id, query = {name=new_name}})
elseif section == "08restart" then
docker:append_status("Containers: update " .. container_id .. "...")
local new_restart = table_info[section]._value
res = dk.containers:update(container_id, nil, {RestartPolicy = {Name = new_restart}})
res = dk.containers:update({id = container_id, body = {RestartPolicy = {Name = new_restart}}})
elseif table_info[section]._key == translate("Network") then
local _,_,leave_network = table_info[section]._value:find("(.-) | .+")
leave_network = leave_network or table_info[section]._value
docker:append_status("Network: disconnect " .. leave_network .. container_id .. "...")
res = dk.networks:disconnect(leave_network, nil, {Container = container_id})
res = dk.networks:disconnect({name = leave_network, body = {Container = container_id}})
elseif section == "15connect" then
local connect_network = table_info[section]._value
local network_opiton
Expand All @@ -358,7 +358,7 @@ if action == "info" then
} or nil
end
docker:append_status("Network: connect " .. connect_network .. container_id .. "...")
res = dk.networks:connect(connect_network, nil, {Container = container_id, EndpointConfig= network_opiton})
res = dk.networks:connect({name = connect_network, body = {Container = container_id, EndpointConfig= network_opiton}})
end
if res and res.code > 300 then
docker:append_status("fail code:" .. res.code.." ".. (res.body.message and res.body.message or res.message))
Expand All @@ -370,7 +370,7 @@ if action == "info" then

-- info end
elseif action == "edit" then
editsection= m:section(SimpleSection)
local editsection= m:section(SimpleSection)
d = editsection:option( Value, "cpus", translate("CPUs"), translate("Number of CPUs. Number is a fractional number. 0.000 means no limit."))
d.placeholder = "1.5"
d.rmempty = true
Expand Down Expand Up @@ -420,7 +420,7 @@ elseif action == "edit" then
}
docker:clear_status()
docker:append_status("Containers: update " .. container_id .. "...")
local res = dk.containers:update(container_id, nil, request_body)
local res = dk.containers:update({id = container_id, body = request_body})
if res and res.code >= 300 then
docker:append_status("fail code:" .. res.code.." ".. (res.body.message and res.body.message or res.message))
else
Expand All @@ -429,31 +429,37 @@ elseif action == "edit" then
luci.http.redirect(luci.dispatcher.build_url("admin/services/docker/container/"..container_id.."/edit"))
end
end
elseif action == "file" then
local filesection= m:section(SimpleSection)
m.submit = false
m.reset = false
filesection.template = "dockerman/container_file"
filesection.container = container_id
elseif action == "logs" then
logsection= m:section(SimpleSection)
local logsection= m:section(SimpleSection)
local logs = ""
local query ={
stdout = 1,
stderr = 1,
tail = 1000
}
local logs = dk.containers:logs(container_id, query)
local logs = dk.containers:logs({id = container_id, query = query})
if logs.code == 200 then
logsection.syslog=logs.body
else
logsection.syslog="Get Logs ERROR\n"..logs.code..": "..logs.body
end
logsection.title=translate("Container Logs")
logsection.template="docker/logs"
logsection.template = "dockerman/logs"
m.submit = false
m.reset = false
m.reset = false
elseif action == "stats" then
local response = dk.containers:top(container_id, {ps_args="-aux"})
local response = dk.containers:top({id = container_id, query = {ps_args="-aux"}})
local container_top
if response.code == 200 then
container_top=response.body
else
response = dk.containers:top(container_id)
response = dk.containers:top({id = container_id})
if response.code == 200 then
container_top=response.body
end
Expand All @@ -463,7 +469,7 @@ elseif action == "stats" then
container_top=response.body
stat_section = m:section(SimpleSection)
stat_section.container_id = container_id
stat_section.template="docker/stats"
stat_section.template = "dockerman/stats"
table_stats = {cpu={key=translate("CPU Useage"),value='-'},memory={key=translate("Memory Useage"),value='-'}}
stat_section = m:section(Table, table_stats, translate("Stats"))
stat_section:option(DummyValue, "key", translate("Stats")).width="33%"
Expand Down
Loading

0 comments on commit ef9d5f9

Please sign in to comment.