luci-app-dockerman: cbi/volumes refactoring and update coding style
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
This commit is contained in:
parent
4f04660860
commit
ef3cfa0539
1 changed files with 119 additions and 92 deletions
|
@ -3,28 +3,27 @@ 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 uci = luci.model.uci.cursor()
|
|
||||||
local docker = require "luci.model.docker"
|
local docker = require "luci.model.docker"
|
||||||
local dk = docker.new()
|
local dk = docker.new()
|
||||||
|
|
||||||
local containers, volumes
|
local m, s, o
|
||||||
local res = dk.volumes:list()
|
|
||||||
if res.code <300 then volumes = res.body.Volumes else return end
|
local res, containers, volumes
|
||||||
res = dk.containers:list({query = {all=true}})
|
|
||||||
if res.code <300 then containers = res.body else return end
|
|
||||||
|
|
||||||
function get_volumes()
|
function get_volumes()
|
||||||
local data = {}
|
local data = {}
|
||||||
for i, v in ipairs(volumes) do
|
for i, v in ipairs(volumes) do
|
||||||
-- local index = v.CreatedAt .. v.Name
|
|
||||||
local index = v.Name
|
local index = v.Name
|
||||||
data[index]={}
|
data[index]={}
|
||||||
data[index]["_selected"] = 0
|
data[index]["_selected"] = 0
|
||||||
data[index]["_nameraw"] = v.Name
|
data[index]["_nameraw"] = v.Name
|
||||||
data[index]["_name"] = v.Name:sub(1,12)
|
data[index]["_name"] = v.Name:sub(1,12)
|
||||||
|
|
||||||
for ci,cv in ipairs(containers) do
|
for ci,cv in ipairs(containers) do
|
||||||
if cv.Mounts and type(cv.Mounts) ~= "table" then break end
|
if cv.Mounts and type(cv.Mounts) ~= "table" then
|
||||||
|
break
|
||||||
|
end
|
||||||
for vi, vv in ipairs(cv.Mounts) do
|
for vi, vv in ipairs(cv.Mounts) do
|
||||||
if v.Name == vv.Name then
|
if v.Name == vv.Name then
|
||||||
data[index]["_containers"] = (data[index]["_containers"] and (data[index]["_containers"] .. " | ") or "")..
|
data[index]["_containers"] = (data[index]["_containers"] and (data[index]["_containers"] .. " | ") or "")..
|
||||||
|
@ -34,6 +33,7 @@ function get_volumes()
|
||||||
end
|
end
|
||||||
data[index]["_driver"] = v.Driver
|
data[index]["_driver"] = v.Driver
|
||||||
data[index]["_mountpoint"] = nil
|
data[index]["_mountpoint"] = nil
|
||||||
|
|
||||||
for v1 in v.Mountpoint:gmatch('[^/]+') do
|
for v1 in v.Mountpoint:gmatch('[^/]+') do
|
||||||
if v1 == index then
|
if v1 == index then
|
||||||
data[index]["_mountpoint"] = data[index]["_mountpoint"] .."/" .. v1:sub(1,12) .. "..."
|
data[index]["_mountpoint"] = data[index]["_mountpoint"] .."/" .. v1:sub(1,12) .. "..."
|
||||||
|
@ -43,59 +43,82 @@ function get_volumes()
|
||||||
end
|
end
|
||||||
data[index]["_created"] = v.CreatedAt
|
data[index]["_created"] = v.CreatedAt
|
||||||
end
|
end
|
||||||
|
|
||||||
return data
|
return data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
res = dk.volumes:list()
|
||||||
|
if res.code <300 then
|
||||||
|
volumes = res.body.Volumes
|
||||||
|
else
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
res = dk.containers:list({
|
||||||
|
query = {
|
||||||
|
all=true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if res.code <300 then
|
||||||
|
containers = res.body
|
||||||
|
else
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local volume_list = get_volumes()
|
local volume_list = get_volumes()
|
||||||
|
|
||||||
-- m = Map("docker", translate("Docker"))
|
|
||||||
m = SimpleForm("docker", translate("Docker"))
|
m = SimpleForm("docker", translate("Docker"))
|
||||||
m.submit=false
|
m.submit=false
|
||||||
m.reset=false
|
m.reset=false
|
||||||
|
|
||||||
|
s = m:section(Table, volume_list, translate("Volumes"))
|
||||||
|
|
||||||
volume_table = m:section(Table, volume_list, translate("Volumes"))
|
o = s:option(Flag, "_selected","")
|
||||||
|
o.disabled = 0
|
||||||
volume_selecter = volume_table:option(Flag, "_selected","")
|
o.enabled = 1
|
||||||
volume_selecter.disabled = 0
|
o.default = 0
|
||||||
volume_selecter.enabled = 1
|
o.write = function(self, section, value)
|
||||||
volume_selecter.default = 0
|
|
||||||
|
|
||||||
volume_id = volume_table:option(DummyValue, "_name", translate("Name"))
|
|
||||||
volume_table:option(DummyValue, "_driver", translate("Driver"))
|
|
||||||
volume_table:option(DummyValue, "_containers", translate("Containers")).rawhtml = true
|
|
||||||
volume_table:option(DummyValue, "_mountpoint", translate("Mount Point"))
|
|
||||||
volume_table:option(DummyValue, "_created", translate("Created"))
|
|
||||||
volume_selecter.write = function(self, section, value)
|
|
||||||
volume_list[section]._selected = value
|
volume_list[section]._selected = value
|
||||||
end
|
end
|
||||||
|
|
||||||
docker_status = m:section(SimpleSection)
|
o = s:option(DummyValue, "_name", translate("Name"))
|
||||||
docker_status.template = "dockerman/apply_widget"
|
|
||||||
docker_status.err=docker:read_status()
|
|
||||||
docker_status.err=docker_status.err and docker_status.err:gsub("\n","<br>"):gsub(" "," ")
|
|
||||||
if docker_status.err then docker:clear_status() end
|
|
||||||
|
|
||||||
action = m:section(Table,{{}})
|
o = s:option(DummyValue, "_driver", translate("Driver"))
|
||||||
action.notitle=true
|
|
||||||
action.rowcolors=false
|
o = s:option(DummyValue, "_containers", translate("Containers"))
|
||||||
action.template="cbi/nullsection"
|
o.rawhtml = true
|
||||||
btnremove = action:option(Button, "remove")
|
|
||||||
btnremove.inputtitle= translate("Remove")
|
o = s:option(DummyValue, "_mountpoint", translate("Mount Point"))
|
||||||
btnremove.template = "dockerman/cbi/inlinebutton"
|
|
||||||
btnremove.inputstyle = "remove"
|
o = s:option(DummyValue, "_created", translate("Created"))
|
||||||
btnremove.forcewrite = true
|
|
||||||
btnremove.write = function(self, section)
|
s = m:section(SimpleSection)
|
||||||
|
s.template = "dockerman/apply_widget"
|
||||||
|
s.err=docker:read_status()
|
||||||
|
s.err=s.err and s.err:gsub("\n","<br>"):gsub(" "," ")
|
||||||
|
if s.err then
|
||||||
|
docker:clear_status()
|
||||||
|
end
|
||||||
|
|
||||||
|
s = m:section(Table,{{}})
|
||||||
|
s.notitle=true
|
||||||
|
s.rowcolors=false
|
||||||
|
s.template="cbi/nullsection"
|
||||||
|
|
||||||
|
o = s:option(Button, "remove")
|
||||||
|
o.inputtitle= translate("Remove")
|
||||||
|
o.template = "dockerman/cbi/inlinebutton"
|
||||||
|
o.inputstyle = "remove"
|
||||||
|
o.forcewrite = true
|
||||||
|
o.write = function(self, section)
|
||||||
local volume_selected = {}
|
local volume_selected = {}
|
||||||
-- 遍历table中sectionid
|
|
||||||
local volume_table_sids = volume_table:cfgsections()
|
for _, volume_table_sid in ipairs(volume_list) do
|
||||||
for _, volume_table_sid in ipairs(volume_table_sids) do
|
|
||||||
-- 得到选中项的名字
|
|
||||||
if volume_list[volume_table_sid]._selected == 1 then
|
if volume_list[volume_table_sid]._selected == 1 then
|
||||||
-- volume_selected[#volume_selected+1] = volume_id:cfgvalue(volume_table_sid)
|
|
||||||
volume_selected[#volume_selected+1] = volume_table_sid
|
volume_selected[#volume_selected+1] = volume_table_sid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if next(volume_selected) ~= nil then
|
if next(volume_selected) ~= nil then
|
||||||
local success = true
|
local success = true
|
||||||
docker:clear_status()
|
docker:clear_status()
|
||||||
|
@ -109,8 +132,12 @@ btnremove.write = function(self, section)
|
||||||
docker:append_status("done\n")
|
docker:append_status("done\n")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if success then docker:clear_status() end
|
|
||||||
|
if success then
|
||||||
|
docker:clear_status()
|
||||||
|
end
|
||||||
luci.http.redirect(luci.dispatcher.build_url("admin/docker/volumes"))
|
luci.http.redirect(luci.dispatcher.build_url("admin/docker/volumes"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return m
|
return m
|
||||||
|
|
Loading…
Reference in a new issue