luci-app-dockerman: cbi/overview refactoring and update coding style

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
This commit is contained in:
Florian Eckert 2020-07-22 12:34:41 +02:00
parent 1b9cdfb7d9
commit f299b988d8

View file

@ -3,9 +3,9 @@ LuCI - Lua Configuration Interface
Copyright 2019 lisaac <https://github.com/lisaac/luci-app-dockerman> Copyright 2019 lisaac <https://github.com/lisaac/luci-app-dockerman>
]]-- ]]--
require "luci.util"
local docker = require "luci.model.docker" local docker = require "luci.model.docker"
local uci = require "luci.model.uci"
local m, s, o
function byte_format(byte) function byte_format(byte)
local suff = {"B", "KB", "MB", "GB", "TB"} local suff = {"B", "KB", "MB", "GB", "TB"}
@ -18,16 +18,14 @@ function byte_format(byte)
end end
end end
local map_dockerman = Map("dockerd", translate("Docker"), m = Map("dockerd", translate("Docker"),
translate("DockerMan is a Simple Docker manager client for LuCI, If you have any issue please visit:") .. translate("DockerMan is a Simple Docker manager client for LuCI, If you have any issue please visit:") ..
" " .. " " ..
[[<a href="https://github.com/lisaac/luci-app-dockerman" target="_blank">]] .. [[<a href="https://github.com/lisaac/luci-app-dockerman" target="_blank">]] ..
translate("Github") .. translate("Github") ..
[[</a>]]) [[</a>]])
local docker_info_table = {} local docker_info_table = {}
-- docker_info_table['0OperatingSystem'] = {_key=translate("Operating System"),_value='-'}
-- docker_info_table['1Architecture'] = {_key=translate("Architecture"),_value='-'}
-- docker_info_table['2KernelVersion'] = {_key=translate("Kernel Version"),_value='-'}
docker_info_table['3ServerVersion'] = {_key=translate("Docker Version"),_value='-'} docker_info_table['3ServerVersion'] = {_key=translate("Docker Version"),_value='-'}
docker_info_table['4ApiVersion'] = {_key=translate("Api Version"),_value='-'} docker_info_table['4ApiVersion'] = {_key=translate("Api Version"),_value='-'}
docker_info_table['5NCPU'] = {_key=translate("CPUs"),_value='-'} docker_info_table['5NCPU'] = {_key=translate("CPUs"),_value='-'}
@ -36,29 +34,29 @@ docker_info_table['7DockerRootDir'] = {_key=translate("Docker Root Dir"),_value=
docker_info_table['8IndexServerAddress'] = {_key=translate("Index Server Address"),_value='-'} docker_info_table['8IndexServerAddress'] = {_key=translate("Index Server Address"),_value='-'}
docker_info_table['9RegistryMirrors'] = {_key=translate("Registry Mirrors"),_value='-'} docker_info_table['9RegistryMirrors'] = {_key=translate("Registry Mirrors"),_value='-'}
local s = map_dockerman:section(Table, docker_info_table) s = m:section(Table, docker_info_table)
s:option(DummyValue, "_key", translate("Info")) s:option(DummyValue, "_key", translate("Info"))
s:option(DummyValue, "_value") s:option(DummyValue, "_value")
s = map_dockerman:section(SimpleSection)
s = m:section(SimpleSection)
s.template = "dockerman/overview"
s.containers_running = '-' s.containers_running = '-'
s.images_used = '-' s.images_used = '-'
s.containers_total = '-' s.containers_total = '-'
s.images_total = '-' s.images_total = '-'
s.networks_total = '-' s.networks_total = '-'
s.volumes_total = '-' s.volumes_total = '-'
local containers_list
-- local socket = luci.model.uci.cursor():get("dockerd", "globals", "socket_path") if docker.new():_ping().code == 200 then
if (require "luci.model.docker").new():_ping().code == 200 then
local dk = docker.new() local dk = docker.new()
containers_list = dk.containers:list({query = {all=true}}).body local containers_list = dk.containers:list({query = {all=true}}).body
local images_list = dk.images:list().body local images_list = dk.images:list().body
local vol = dk.volumes:list() local vol = dk.volumes:list()
local volumes_list = vol and vol.body and vol.body.Volumes or {} local volumes_list = vol and vol.body and vol.body.Volumes or {}
local networks_list = dk.networks:list().body or {} local networks_list = dk.networks:list().body or {}
local docker_info = dk:info() local docker_info = dk:info()
-- docker_info_table['0OperatingSystem']._value = docker_info.body.OperatingSystem
-- docker_info_table['1Architecture']._value = docker_info.body.Architecture
-- docker_info_table['2KernelVersion']._value = docker_info.body.KernelVersion
docker_info_table['3ServerVersion']._value = docker_info.body.ServerVersion docker_info_table['3ServerVersion']._value = docker_info.body.ServerVersion
docker_info_table['4ApiVersion']._value = docker_info.headers["Api-Version"] docker_info_table['4ApiVersion']._value = docker_info.headers["Api-Version"]
docker_info_table['5NCPU']._value = tostring(docker_info.body.NCPU) docker_info_table['5NCPU']._value = tostring(docker_info.body.NCPU)
@ -68,6 +66,7 @@ if (require "luci.model.docker").new():_ping().code == 200 then
local size = statvfs and (statvfs.bavail * statvfs.bsize) or 0 local size = statvfs and (statvfs.bavail * statvfs.bsize) or 0
docker_info_table['7DockerRootDir']._value = docker_info.body.DockerRootDir .. " (" .. tostring(byte_format(size)) .. " " .. translate("Available") .. ")" docker_info_table['7DockerRootDir']._value = docker_info.body.DockerRootDir .. " (" .. tostring(byte_format(size)) .. " " .. translate("Available") .. ")"
end end
docker_info_table['8IndexServerAddress']._value = docker_info.body.IndexServerAddress docker_info_table['8IndexServerAddress']._value = docker_info.body.IndexServerAddress
for i, v in ipairs(docker_info.body.RegistryConfig.Mirrors) do for i, v in ipairs(docker_info.body.RegistryConfig.Mirrors) do
docker_info_table['9RegistryMirrors']._value = docker_info_table['9RegistryMirrors']._value == "-" and v or (docker_info_table['9RegistryMirrors']._value .. ", " .. v) docker_info_table['9RegistryMirrors']._value = docker_info_table['9RegistryMirrors']._value == "-" and v or (docker_info_table['9RegistryMirrors']._value .. ", " .. v)
@ -82,6 +81,7 @@ if (require "luci.model.docker").new():_ping().code == 200 then
end end
end end
end end
s.containers_running = tostring(docker_info.body.ContainersRunning) s.containers_running = tostring(docker_info.body.ContainersRunning)
s.images_used = tostring(s.images_used) s.images_used = tostring(s.images_used)
s.containers_total = tostring(docker_info.body.Containers) s.containers_total = tostring(docker_info.body.Containers)
@ -89,49 +89,43 @@ if (require "luci.model.docker").new():_ping().code == 200 then
s.networks_total = tostring(#networks_list) s.networks_total = tostring(#networks_list)
s.volumes_total = tostring(#volumes_list) s.volumes_total = tostring(#volumes_list)
end end
s.template = "dockerman/overview"
local section_dockerman = map_dockerman:section(NamedSection, "globals", "section", translate("Setting")) s = m:section(NamedSection, "globals", "section", translate("Setting"))
section_dockerman:tab("daemon", translate("Docker Daemon")) s:tab("daemon", translate("Docker Daemon"))
section_dockerman:tab("dockerman", translate("DockerMan")) s:tab("dockerman", translate("DockerMan"))
local socket_path = section_dockerman:taboption("dockerman", Value, "socket_path", translate("Docker Socket Path")) o = s:taboption("dockerman", Value, "socket_path",
socket_path.default = "/var/run/docker.sock" translate("Docker Socket Path"))
socket_path.placeholder = "/var/run/docker.sock" o.default = "/var/run/docker.sock"
socket_path.rmempty = false o.placeholder = "/var/run/docker.sock"
o.rmempty = false
local remote_endpoint = section_dockerman:taboption("dockerman", Flag, "remote_endpoint", translate("Remote Endpoint"), translate("Dockerman connect to remote endpoint")) o = s:taboption("dockerman", Flag, "remote_endpoint",
remote_endpoint.rmempty = false translate("Remote Endpoint"),
remote_endpoint.enabled = "true" translate("Dockerman connect to remote endpoint"))
remote_endpoint.disabled = "false" o.rmempty = false
o.enabled = "true"
o.disabled = "false"
local remote_host = section_dockerman:taboption("dockerman", Value, "remote_host", translate("Remote Host")) o = s:taboption("dockerman", Value, "remote_host",
remote_host.placeholder = "10.1.1.2" translate("Remote Host"))
-- remote_host:depends("remote_endpoint", "true") o.placeholder = "10.1.1.2"
local remote_port = section_dockerman:taboption("dockerman", Value, "remote_port", translate("Remote Port")) o = s:taboption("dockerman", Value, "remote_port",
remote_port.placeholder = "2375" translate("Remote Port"))
remote_port.default = "2375" o.placeholder = "2375"
-- remote_port:depends("remote_endpoint", "true") o.default = "2375"
-- local status_path = section_dockerman:taboption("dockerman", Value, "status_path", translate("Action Status Tempfile Path"), translate("Where you want to save the docker status file"))
-- local debug = section_dockerman:taboption("dockerman", Flag, "debug", translate("Enable Debug"), translate("For debug, It shows all docker API actions of luci-app-dockerman in Debug Tempfile Path"))
-- debug.enabled="true"
-- debug.disabled="false"
-- local debug_path = section_dockerman:taboption("dockerman", Value, "debug_path", translate("Debug Tempfile Path"), translate("Where you want to save the debug tempfile"))
if nixio.fs.access("/usr/bin/dockerd") then if nixio.fs.access("/usr/bin/dockerd") then
local o o = s:taboption("daemon", Value, "data_root",
o = section_dockerman:taboption("daemon", Value, "data_root",
translate("Docker Root Dir")) translate("Docker Root Dir"))
o.placeholder = "/opt/docker/" o.placeholder = "/opt/docker/"
o = section_dockerman:taboption("daemon", DynamicList, "registry_mirrors", o = s:taboption("daemon", DynamicList, "registry_mirrors",
translate("Registry Mirrors")) translate("Registry Mirrors"))
o:value("https://hub-mirror.c.163.com", "https://hub-mirror.c.163.com") o:value("https://hub-mirror.c.163.com", "https://hub-mirror.c.163.com")
o = section_dockerman:taboption("daemon", ListValue, "log_level", o = s:taboption("daemon", ListValue, "log_level",
translate("Log Level"), translate("Log Level"),
translate('Set the logging level')) translate('Set the logging level'))
o:value("debug", "debug") o:value("debug", "debug")
@ -140,7 +134,7 @@ if nixio.fs.access("/usr/bin/dockerd") then
o:value("error", "error") o:value("error", "error")
o:value("fatal", "fatal") o:value("fatal", "fatal")
o = section_dockerman:taboption("daemon", DynamicList, "hosts", o = s:taboption("daemon", DynamicList, "hosts",
translate("Server Host"), translate("Server Host"),
translate('Daemon unix socket (unix:///var/run/docker.sock) or TCP Remote Hosts (tcp://0.0.0.0:2375), default: unix:///var/run/docker.sock')) translate('Daemon unix socket (unix:///var/run/docker.sock) or TCP Remote Hosts (tcp://0.0.0.0:2375), default: unix:///var/run/docker.sock'))
o:value("unix:///var/run/docker.sock", "unix:///var/run/docker.sock") o:value("unix:///var/run/docker.sock", "unix:///var/run/docker.sock")
@ -148,4 +142,4 @@ if nixio.fs.access("/usr/bin/dockerd") then
o.rmempty = true o.rmempty = true
end end
return map_dockerman return m