luci-app-dockerman: cbi/images update coding style
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
This commit is contained in:
parent
18ae86e458
commit
b8f63a5725
1 changed files with 170 additions and 132 deletions
|
@ -8,48 +8,67 @@ 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
|
local containers, images, res
|
||||||
local res = dk.images:list()
|
|
||||||
if res.code <300 then images = res.body else return end
|
res = dk.images:list()
|
||||||
res = dk.containers:list({query = {all=true}})
|
if res.code < 300 then
|
||||||
if res.code <300 then containers = res.body else return end
|
images = res.body
|
||||||
|
else
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
res = dk.containers:list({
|
||||||
|
query = {
|
||||||
|
all=true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if res.code < 300 then
|
||||||
|
containers = res.body
|
||||||
|
else
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
function get_images()
|
function get_images()
|
||||||
local data = {}
|
local data = {}
|
||||||
for i, v in ipairs(images) do
|
|
||||||
local index = v.Created .. v.Id
|
for i, v in ipairs(images) do
|
||||||
data[index]={}
|
local index = v.Created .. v.Id
|
||||||
data[index]["_selected"] = 0
|
|
||||||
data[index]["id"] = v.Id:sub(8)
|
data[index]={}
|
||||||
data[index]["_id"] = '<a href="javascript:new_tag(\''..v.Id:sub(8,20)..'\')" class="dockerman-link" title="'..translate("New tag")..'">' .. v.Id:sub(8,20) .. '</a>'
|
data[index]["_selected"] = 0
|
||||||
if v.RepoTags and next(v.RepoTags)~=nil then
|
data[index]["id"] = v.Id:sub(8)
|
||||||
for i, v1 in ipairs(v.RepoTags) do
|
data[index]["_id"] = '<a href="javascript:new_tag(\''..v.Id:sub(8,20)..'\')" class="dockerman-link" title="'..translate("New tag")..'">' .. v.Id:sub(8,20) .. '</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 v.RepoTags and next(v.RepoTags)~=nil then
|
||||||
data[index]["tag"] = v1--:match("<none>") and nil or v1
|
for i, v1 in ipairs(v.RepoTags) do
|
||||||
end
|
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>'))
|
||||||
end
|
|
||||||
else
|
if not data[index]["tag"] then
|
||||||
data[index]["_tags"] = v.RepoDigests[1] and v.RepoDigests[1]:match("^(.-)@.+")
|
data[index]["tag"] = v1--:match("<none>") and nil or v1
|
||||||
data[index]["_tags"] = (data[index]["_tags"] and data[index]["_tags"] or "<none>" ).. ":<none>"
|
end
|
||||||
end
|
end
|
||||||
data[index]["_tags"] = data[index]["_tags"]:gsub("<none>","<none>")
|
else
|
||||||
-- data[index]["_tags"] = '<a href="javascript:handle_tag(\''..data[index]["_id"]..'\')">' .. data[index]["_tags"] .. '</a>'
|
data[index]["_tags"] = v.RepoDigests[1] and v.RepoDigests[1]:match("^(.-)@.+")
|
||||||
for ci,cv in ipairs(containers) do
|
data[index]["_tags"] = (data[index]["_tags"] and data[index]["_tags"] or "<none>" ).. ":<none>"
|
||||||
if v.Id == cv.ImageID then
|
end
|
||||||
data[index]["_containers"] = (data[index]["_containers"] and (data[index]["_containers"] .. " | ") or "")..
|
|
||||||
'<a href='..luci.dispatcher.build_url("admin/docker/container/"..cv.Id)..' class="dockerman_link" title="'..translate("Container detail")..'">'.. cv.Names[1]:sub(2).."</a>"
|
data[index]["_tags"] = data[index]["_tags"]:gsub("<none>","<none>")
|
||||||
end
|
for ci,cv in ipairs(containers) do
|
||||||
end
|
if v.Id == cv.ImageID then
|
||||||
data[index]["_size"] = string.format("%.2f", tostring(v.Size/1024/1024)).."MB"
|
data[index]["_containers"] = (data[index]["_containers"] and (data[index]["_containers"] .. " | ") or "")..
|
||||||
data[index]["_created"] = os.date("%Y/%m/%d %H:%M:%S",v.Created)
|
'<a href='..luci.dispatcher.build_url("admin/docker/container/"..cv.Id)..' class="dockerman_link" title="'..translate("Container detail")..'">'.. cv.Names[1]:sub(2).."</a>"
|
||||||
end
|
end
|
||||||
return data
|
end
|
||||||
|
|
||||||
|
data[index]["_size"] = string.format("%.2f", tostring(v.Size/1024/1024)).."MB"
|
||||||
|
data[index]["_created"] = os.date("%Y/%m/%d %H:%M:%S",v.Created)
|
||||||
|
end
|
||||||
|
|
||||||
|
return data
|
||||||
end
|
end
|
||||||
|
|
||||||
local image_list = get_images()
|
local image_list = get_images()
|
||||||
|
|
||||||
-- 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
|
||||||
|
@ -65,29 +84,31 @@ action_pull.inputtitle= translate("Pull")
|
||||||
action_pull.template = "dockerman/cbi/inlinebutton"
|
action_pull.template = "dockerman/cbi/inlinebutton"
|
||||||
action_pull.inputstyle = "add"
|
action_pull.inputstyle = "add"
|
||||||
tag_name.write = function(self, section, value)
|
tag_name.write = function(self, section, value)
|
||||||
local hastag = value:find(":")
|
local hastag = value:find(":")
|
||||||
if not hastag then
|
|
||||||
value = value .. ":latest"
|
if not hastag then
|
||||||
end
|
value = value .. ":latest"
|
||||||
pull_value["_image_tag_name"] = value
|
end
|
||||||
|
pull_value["_image_tag_name"] = value
|
||||||
end
|
end
|
||||||
action_pull.write = function(self, section)
|
action_pull.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
|
||||||
if tag and tag ~= "" then
|
|
||||||
docker:write_status("Images: " .. "pulling" .. " " .. tag .. "...\n")
|
if tag and tag ~= "" then
|
||||||
-- local x_auth = nixio.bin.b64encode(json_stringify({serveraddress= server})) , header={["X-Registry-Auth"] = x_auth}
|
docker:write_status("Images: " .. "pulling" .. " " .. tag .. "...\n")
|
||||||
local res = dk.images:create({query = {fromImage=tag}}, docker.pull_image_show_status_cb)
|
local res = dk.images:create({query = {fromImage=tag}}, docker.pull_image_show_status_cb)
|
||||||
-- {"errorDetail": {"message": "failed to register layer: ApplyLayer exit status 1 stdout: stderr: write \/docker: no space left on device" }, "error": "failed to register layer: ApplyLayer exit status 1 stdout: stderr: write \/docker: no space left on device" }
|
|
||||||
if res and res.code == 200 and (res.body[#res.body] and not res.body[#res.body].error and res.body[#res.body].status and (res.body[#res.body].status == "Status: Downloaded newer image for ".. tag)) then
|
if res and res.code == 200 and (res.body[#res.body] and not res.body[#res.body].error and res.body[#res.body].status and (res.body[#res.body].status == "Status: Downloaded newer image for ".. tag)) then
|
||||||
docker:clear_status()
|
docker:clear_status()
|
||||||
else
|
else
|
||||||
docker:append_status("code:" .. res.code.." ".. (res.body[#res.body] and res.body[#res.body].error or (res.body.message or res.message)).. "\n")
|
docker:append_status("code:" .. res.code.." ".. (res.body[#res.body] and res.body[#res.body].error or (res.body.message or res.message)).. "\n")
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
docker:append_status("code: 400 please input the name of image name!")
|
docker:append_status("code: 400 please input the name of image name!")
|
||||||
end
|
end
|
||||||
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"))
|
local import_section = m:section(SimpleSection, translate("Import Images"))
|
||||||
|
@ -95,57 +116,68 @@ local im = import_section:option(DummyValue, "_image_import")
|
||||||
im.template = "dockerman/images_import"
|
im.template = "dockerman/images_import"
|
||||||
|
|
||||||
local image_table = m:section(Table, image_list, translate("Images"))
|
local image_table = m:section(Table, image_list, translate("Images"))
|
||||||
|
image_table:option(DummyValue, "_tags", translate("RepoTags")).rawhtml = true
|
||||||
|
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","")
|
local image_selecter = image_table:option(Flag, "_selected","")
|
||||||
image_selecter.disabled = 0
|
image_selecter.disabled = 0
|
||||||
image_selecter.enabled = 1
|
image_selecter.enabled = 1
|
||||||
image_selecter.default = 0
|
image_selecter.default = 0
|
||||||
|
image_selecter.write = function(self, section, value)
|
||||||
|
image_list[section]._selected = value
|
||||||
|
end
|
||||||
|
|
||||||
local image_id = image_table:option(DummyValue, "_id", translate("ID"))
|
local image_id = image_table:option(DummyValue, "_id", translate("ID"))
|
||||||
image_id.rawhtml = true
|
image_id.rawhtml = true
|
||||||
image_table:option(DummyValue, "_tags", translate("RepoTags")).rawhtml = true
|
|
||||||
image_table:option(DummyValue, "_containers", translate("Containers")).rawhtml = true
|
|
||||||
image_table:option(DummyValue, "_size", translate("Size"))
|
|
||||||
image_table:option(DummyValue, "_created", translate("Created"))
|
|
||||||
image_selecter.write = function(self, section, value)
|
|
||||||
image_list[section]._selected = value
|
|
||||||
end
|
|
||||||
|
|
||||||
local remove_action = function(force)
|
local remove_action = function(force)
|
||||||
local image_selected = {}
|
local image_selected = {}
|
||||||
-- 遍历table中sectionid
|
|
||||||
local image_table_sids = image_table:cfgsections()
|
local image_table_sids = image_table:cfgsections()
|
||||||
for _, image_table_sid in ipairs(image_table_sids) do
|
for _, image_table_sid in ipairs(image_table_sids) do
|
||||||
-- 得到选中项的名字
|
if image_list[image_table_sid]._selected == 1 then
|
||||||
if image_list[image_table_sid]._selected == 1 then
|
image_selected[#image_selected+1] = (image_list[image_table_sid]["_tags"]:match("<br>") or image_list[image_table_sid]["_tags"]:match("<none>")) and image_list[image_table_sid].id or image_list[image_table_sid].tag
|
||||||
image_selected[#image_selected+1] = (image_list[image_table_sid]["_tags"]:match("<br>") or image_list[image_table_sid]["_tags"]:match("<none>")) and image_list[image_table_sid].id or image_list[image_table_sid].tag
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
if next(image_selected) ~= nil then
|
if next(image_selected) ~= nil then
|
||||||
local success = true
|
local success = true
|
||||||
docker:clear_status()
|
|
||||||
for _,img in ipairs(image_selected) do
|
docker:clear_status()
|
||||||
docker:append_status("Images: " .. "remove" .. " " .. img .. "...")
|
for _,img in ipairs(image_selected) do
|
||||||
local query
|
local query
|
||||||
if force then query = {force = true} end
|
docker:append_status("Images: " .. "remove" .. " " .. img .. "...")
|
||||||
local msg = dk.images:remove({id = img, query = query})
|
|
||||||
if msg.code ~= 200 then
|
if force then
|
||||||
docker:append_status("code:" .. msg.code.." ".. (msg.body.message and msg.body.message or msg.message).. "\n")
|
query = {force = true}
|
||||||
success = false
|
end
|
||||||
else
|
|
||||||
docker:append_status("done\n")
|
local msg = dk.images:remove({id = img, query = query})
|
||||||
end
|
if msg.code ~= 200 then
|
||||||
end
|
docker:append_status("code:" .. msg.code.." ".. (msg.body.message and msg.body.message or msg.message).. "\n")
|
||||||
if success then docker:clear_status() end
|
success = false
|
||||||
luci.http.redirect(luci.dispatcher.build_url("admin/docker/images"))
|
else
|
||||||
end
|
docker:append_status("done\n")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if success then
|
||||||
|
docker:clear_status()
|
||||||
|
end
|
||||||
|
|
||||||
|
luci.http.redirect(luci.dispatcher.build_url("admin/docker/images"))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local docker_status = m:section(SimpleSection)
|
local docker_status = m:section(SimpleSection)
|
||||||
docker_status.template = "dockerman/apply_widget"
|
docker_status.template = "dockerman/apply_widget"
|
||||||
docker_status.err = docker:read_status()
|
docker_status.err = docker:read_status()
|
||||||
docker_status.err = docker_status.err and docker_status.err:gsub("\n","<br>"):gsub(" "," ")
|
docker_status.err = docker_status.err and docker_status.err:gsub("\n","<br>"):gsub(" "," ")
|
||||||
if docker_status.err then docker:clear_status() end
|
if docker_status.err then
|
||||||
|
docker:clear_status()
|
||||||
|
end
|
||||||
|
|
||||||
local action = m:section(Table,{{}})
|
local action = m:section(Table,{{}})
|
||||||
action.notitle=true
|
action.notitle=true
|
||||||
|
@ -158,7 +190,7 @@ btnremove.template = "dockerman/cbi/inlinebutton"
|
||||||
btnremove.inputstyle = "remove"
|
btnremove.inputstyle = "remove"
|
||||||
btnremove.forcewrite = true
|
btnremove.forcewrite = true
|
||||||
btnremove.write = function(self, section)
|
btnremove.write = function(self, section)
|
||||||
remove_action()
|
remove_action()
|
||||||
end
|
end
|
||||||
|
|
||||||
local btnforceremove = action:option(Button, "forceremove")
|
local btnforceremove = action:option(Button, "forceremove")
|
||||||
|
@ -167,7 +199,7 @@ btnforceremove.template = "dockerman/cbi/inlinebutton"
|
||||||
btnforceremove.inputstyle = "remove"
|
btnforceremove.inputstyle = "remove"
|
||||||
btnforceremove.forcewrite = true
|
btnforceremove.forcewrite = true
|
||||||
btnforceremove.write = function(self, section)
|
btnforceremove.write = function(self, section)
|
||||||
remove_action(true)
|
remove_action(true)
|
||||||
end
|
end
|
||||||
|
|
||||||
local btnsave = action:option(Button, "save")
|
local btnsave = action:option(Button, "save")
|
||||||
|
@ -176,48 +208,54 @@ btnsave.template = "dockerman/cbi/inlinebutton"
|
||||||
btnsave.inputstyle = "edit"
|
btnsave.inputstyle = "edit"
|
||||||
btnsave.forcewrite = true
|
btnsave.forcewrite = true
|
||||||
btnsave.write = function (self, section)
|
btnsave.write = function (self, section)
|
||||||
local image_selected = {}
|
local image_selected = {}
|
||||||
local image_table_sids = image_table:cfgsections()
|
|
||||||
for _, image_table_sid in ipairs(image_table_sids) do
|
local image_table_sids = image_table:cfgsections()
|
||||||
if image_list[image_table_sid]._selected == 1 then
|
for _, image_table_sid in ipairs(image_table_sids) do
|
||||||
image_selected[#image_selected+1] = image_list[image_table_sid].id --image_id:cfgvalue(image_table_sid)
|
if image_list[image_table_sid]._selected == 1 then
|
||||||
end
|
image_selected[#image_selected+1] = image_list[image_table_sid].id --image_id:cfgvalue(image_table_sid)
|
||||||
end
|
end
|
||||||
if next(image_selected) ~= nil then
|
end
|
||||||
local names
|
|
||||||
for _,img in ipairs(image_selected) do
|
if next(image_selected) ~= nil then
|
||||||
names = names and (names .. "&names=".. img) or img
|
local names, first
|
||||||
end
|
|
||||||
local first
|
for _,img in ipairs(image_selected) do
|
||||||
local cb = function(res, chunk)
|
names = names and (names .. "&names=".. img) or img
|
||||||
if res.code == 200 then
|
end
|
||||||
if not first then
|
|
||||||
first = true
|
local cb = function(res, chunk)
|
||||||
luci.http.header('Content-Disposition', 'inline; filename="images.tar"')
|
if res.code == 200 then
|
||||||
luci.http.header('Content-Type', 'application\/x-tar')
|
if not first then
|
||||||
end
|
first = true
|
||||||
luci.ltn12.pump.all(chunk, luci.http.write)
|
luci.http.header('Content-Disposition', 'inline; filename="images.tar"')
|
||||||
else
|
luci.http.header('Content-Type', 'application\/x-tar')
|
||||||
if not first then
|
end
|
||||||
first = true
|
luci.ltn12.pump.all(chunk, luci.http.write)
|
||||||
luci.http.prepare_content("text/plain")
|
else
|
||||||
end
|
if not first then
|
||||||
luci.ltn12.pump.all(chunk, luci.http.write)
|
first = true
|
||||||
end
|
luci.http.prepare_content("text/plain")
|
||||||
end
|
end
|
||||||
docker:write_status("Images: " .. "save" .. " " .. table.concat(image_selected, "\n") .. "...")
|
luci.ltn12.pump.all(chunk, luci.http.write)
|
||||||
local msg = dk.images:get({query = {names = names}}, cb)
|
end
|
||||||
if msg.code ~= 200 then
|
end
|
||||||
docker:append_status("code:" .. msg.code.." ".. (msg.body.message and msg.body.message or msg.message).. "\n")
|
|
||||||
success = false
|
docker:write_status("Images: " .. "save" .. " " .. table.concat(image_selected, "\n") .. "...")
|
||||||
else
|
local msg = dk.images:get({query = {names = names}}, cb)
|
||||||
docker:clear_status()
|
|
||||||
end
|
if msg.code ~= 200 then
|
||||||
end
|
docker:append_status("code:" .. msg.code.." ".. (msg.body.message and msg.body.message or msg.message).. "\n")
|
||||||
|
success = false
|
||||||
|
else
|
||||||
|
docker:clear_status()
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local btnload = action:option(Button, "load")
|
local btnload = action:option(Button, "load")
|
||||||
btnload.inputtitle= translate("Load")
|
btnload.inputtitle= translate("Load")
|
||||||
btnload.template = "dockerman/images_load"
|
btnload.template = "dockerman/images_load"
|
||||||
btnload.inputstyle = "add"
|
btnload.inputstyle = "add"
|
||||||
|
|
||||||
return m
|
return m
|
||||||
|
|
Loading…
Reference in a new issue