luci-app-dockerman: cbi/images refactoring

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
This commit is contained in:
Florian Eckert 2020-07-23 13:42:32 +02:00
parent b8f63a5725
commit 0f170c1118

View file

@ -3,12 +3,11 @@ 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, images, res local containers, images, res
local m, s, o
res = dk.images:list() res = dk.images:list()
if res.code < 300 then if res.code < 300 then
@ -44,7 +43,7 @@ function get_images()
data[index]["_tags"] =(data[index]["_tags"] and ( data[index]["_tags"] .. "<br>" )or "") .. ((v1:match("<none>") or (#v.RepoTags == 1)) and v1 or ('<a href="javascript:un_tag(\''..v1..'\')" class="dockerman_link" title="'..translate("Remove tag")..'" >' .. v1 .. '</a>')) data[index]["_tags"] =(data[index]["_tags"] and ( data[index]["_tags"] .. "<br>" )or "") .. ((v1:match("<none>") or (#v.RepoTags == 1)) and v1 or ('<a href="javascript:un_tag(\''..v1..'\')" class="dockerman_link" title="'..translate("Remove tag")..'" >' .. v1 .. '</a>'))
if not data[index]["tag"] then if not data[index]["tag"] then
data[index]["tag"] = v1--:match("<none>") and nil or v1 data[index]["tag"] = v1
end end
end end
else else
@ -73,17 +72,18 @@ m = SimpleForm("docker", translate("Docker"))
m.submit=false m.submit=false
m.reset=false m.reset=false
local pull_value={_image_tag_name="", _registry="index.docker.io"} local pull_value={
local pull_section = m:section(SimpleSection, translate("Pull Image")) _image_tag_name="",
pull_section.template="cbi/nullsection" _registry="index.docker.io"
local tag_name = pull_section:option(Value, "_image_tag_name") }
tag_name.template = "dockerman/cbi/inlinevalue"
tag_name.placeholder="lisaac/luci:latest" s = m:section(SimpleSection, translate("Pull Image"))
local action_pull = pull_section:option(Button, "_pull") s.template="cbi/nullsection"
action_pull.inputtitle= translate("Pull")
action_pull.template = "dockerman/cbi/inlinebutton" o = s:option(Value, "_image_tag_name")
action_pull.inputstyle = "add" o.template = "dockerman/cbi/inlinevalue"
tag_name.write = function(self, section, value) o.placeholder="lisaac/luci:latest"
o.write = function(self, section, value)
local hastag = value:find(":") local hastag = value:find(":")
if not hastag then if not hastag then
@ -91,7 +91,12 @@ tag_name.write = function(self, section, value)
end end
pull_value["_image_tag_name"] = value pull_value["_image_tag_name"] = value
end end
action_pull.write = function(self, section)
o = s:option(Button, "_pull")
o.inputtitle= translate("Pull")
o.template = "dockerman/cbi/inlinebutton"
o.inputstyle = "add"
o.write = function(self, section)
local tag = pull_value["_image_tag_name"] local tag = pull_value["_image_tag_name"]
local json_stringify = luci.jsonc and luci.jsonc.stringify local json_stringify = luci.jsonc and luci.jsonc.stringify
@ -111,34 +116,40 @@ action_pull.write = function(self, section)
luci.http.redirect(luci.dispatcher.build_url("admin/docker/images")) luci.http.redirect(luci.dispatcher.build_url("admin/docker/images"))
end end
local import_section = m:section(SimpleSection, translate("Import Images")) s = m:section(SimpleSection, translate("Import Images"))
local im = import_section:option(DummyValue, "_image_import")
im.template = "dockerman/images_import"
local image_table = m:section(Table, image_list, translate("Images")) o = s:option(DummyValue, "_image_import")
image_table:option(DummyValue, "_tags", translate("RepoTags")).rawhtml = true o.template = "dockerman/images_import"
image_table:option(DummyValue, "_containers", translate("Containers")).rawhtml = true
image_table:option(DummyValue, "_size", translate("Size"))
image_table:option(DummyValue, "_created", translate("Created"))
local image_selecter = image_table:option(Flag, "_selected","") s = m:section(Table, image_list, translate("Images"))
image_selecter.disabled = 0
image_selecter.enabled = 1 o = s:option(Flag, "_selected","")
image_selecter.default = 0 o.disabled = 0
image_selecter.write = function(self, section, value) o.enabled = 1
o.default = 0
o.write = function(self, section, value)
image_list[section]._selected = value image_list[section]._selected = value
end end
local image_id = image_table:option(DummyValue, "_id", translate("ID")) o = s:option(DummyValue, "_tags", translate("RepoTags"))
image_id.rawhtml = true o.rawhtml = true
o = s:option(DummyValue, "_containers", translate("Containers"))
o.rawhtml = true
o = s:option(DummyValue, "_size", translate("Size"))
o = s:option(DummyValue, "_created", translate("Created"))
o = s:option(DummyValue, "_id", translate("ID"))
o.rawhtml = true
local remove_action = function(force) local remove_action = function(force)
local image_selected = {} local image_selected = {}
local image_table_sids = image_table:cfgsections() for k in pairs(image_list) do
for _, image_table_sid in ipairs(image_table_sids) do if image_list[k]._selected == 1 then
if image_list[image_table_sid]._selected == 1 then image_selected[#image_selected+1] = (image_list[k]["_tags"]:match("<br>") or image_list[k]["_tags"]:match("&lt;none&gt;")) and image_list[k].id or image_list[k].tag
image_selected[#image_selected+1] = (image_list[image_table_sid]["_tags"]:match("<br>") or image_list[image_table_sid]["_tags"]:match("&lt;none&gt;")) and image_list[image_table_sid].id or image_list[image_table_sid].tag
end end
end end
@ -154,7 +165,10 @@ local remove_action = function(force)
query = {force = true} query = {force = true}
end end
local msg = dk.images:remove({id = img, query = query}) local msg = dk.images:remove({
id = img,
query = query
})
if msg.code ~= 200 then if msg.code ~= 200 then
docker:append_status("code:" .. msg.code.." ".. (msg.body.message and msg.body.message or msg.message).. "\n") docker:append_status("code:" .. msg.code.." ".. (msg.body.message and msg.body.message or msg.message).. "\n")
success = false success = false
@ -171,49 +185,48 @@ local remove_action = function(force)
end end
end end
local docker_status = m:section(SimpleSection) s = m:section(SimpleSection)
docker_status.template = "dockerman/apply_widget" s.template = "dockerman/apply_widget"
docker_status.err = docker:read_status() s.err = docker:read_status()
docker_status.err = docker_status.err and docker_status.err:gsub("\n","<br>"):gsub(" ","&nbsp;") s.err = s.err and s.err:gsub("\n","<br>"):gsub(" ","&nbsp;")
if docker_status.err then if s.err then
docker:clear_status() docker:clear_status()
end end
local action = m:section(Table,{{}}) s = m:section(Table,{{}})
action.notitle=true s.notitle=true
action.rowcolors=false s.rowcolors=false
action.template="cbi/nullsection" s.template="cbi/nullsection"
local btnremove = action:option(Button, "remove") o = s:option(Button, "remove")
btnremove.inputtitle= translate("Remove") o.inputtitle= translate("Remove")
btnremove.template = "dockerman/cbi/inlinebutton" o.template = "dockerman/cbi/inlinebutton"
btnremove.inputstyle = "remove" o.inputstyle = "remove"
btnremove.forcewrite = true o.forcewrite = true
btnremove.write = function(self, section) o.write = function(self, section)
remove_action() remove_action()
end end
local btnforceremove = action:option(Button, "forceremove") o = s:option(Button, "forceremove")
btnforceremove.inputtitle= translate("Force Remove") o.inputtitle= translate("Force Remove")
btnforceremove.template = "dockerman/cbi/inlinebutton" o.template = "dockerman/cbi/inlinebutton"
btnforceremove.inputstyle = "remove" o.inputstyle = "remove"
btnforceremove.forcewrite = true o.forcewrite = true
btnforceremove.write = function(self, section) o.write = function(self, section)
remove_action(true) remove_action(true)
end end
local btnsave = action:option(Button, "save") o = s:option(Button, "save")
btnsave.inputtitle= translate("Save") o.inputtitle= translate("Save")
btnsave.template = "dockerman/cbi/inlinebutton" o.template = "dockerman/cbi/inlinebutton"
btnsave.inputstyle = "edit" o.inputstyle = "edit"
btnsave.forcewrite = true o.forcewrite = true
btnsave.write = function (self, section) o.write = function (self, section)
local image_selected = {} local image_selected = {}
local image_table_sids = image_table:cfgsections() for k in pairs(image_list) do
for _, image_table_sid in ipairs(image_table_sids) do if image_list[k]._selected == 1 then
if image_list[image_table_sid]._selected == 1 then image_selected[#image_selected + 1] = image_list[k].id
image_selected[#image_selected+1] = image_list[image_table_sid].id --image_id:cfgvalue(image_table_sid)
end end
end end
@ -253,9 +266,9 @@ btnsave.write = function (self, section)
end end
end end
local btnload = action:option(Button, "load") o = s:option(Button, "load")
btnload.inputtitle= translate("Load") o.inputtitle= translate("Load")
btnload.template = "dockerman/images_load" o.template = "dockerman/images_load"
btnload.inputstyle = "add" o.inputstyle = "add"
return m return m