volumes.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. --[[
  2. LuCI - Lua Configuration Interface
  3. Copyright 2019 lisaac <https://github.com/lisaac/luci-app-dockerman>
  4. ]]--
  5. require "luci.util"
  6. local uci = luci.model.uci.cursor()
  7. local docker = require "luci.model.docker"
  8. local dk = docker.new()
  9. local containers, volumes
  10. local res = dk.volumes:list()
  11. if res.code <300 then volumes = res.body.Volumes else return end
  12. res = dk.containers:list({query = {all=true}})
  13. if res.code <300 then containers = res.body else return end
  14. function get_volumes()
  15. local data = {}
  16. for i, v in ipairs(volumes) do
  17. -- local index = v.CreatedAt .. v.Name
  18. local index = v.Name
  19. data[index]={}
  20. data[index]["_selected"] = 0
  21. data[index]["_nameraw"] = v.Name
  22. data[index]["_name"] = v.Name:sub(1,12)
  23. for ci,cv in ipairs(containers) do
  24. if cv.Mounts and type(cv.Mounts) ~= "table" then break end
  25. for vi, vv in ipairs(cv.Mounts) do
  26. if v.Name == vv.Name then
  27. data[index]["_containers"] = (data[index]["_containers"] and (data[index]["_containers"] .. " | ") or "")..
  28. '<a href='..luci.dispatcher.build_url("admin/docker/container/"..cv.Id)..' class="dockerman_link" title="'..translate("Container detail")..'">'.. cv.Names[1]:sub(2)..'</a>'
  29. end
  30. end
  31. end
  32. data[index]["_driver"] = v.Driver
  33. data[index]["_mountpoint"] = nil
  34. for v1 in v.Mountpoint:gmatch('[^/]+') do
  35. if v1 == index then
  36. data[index]["_mountpoint"] = data[index]["_mountpoint"] .."/" .. v1:sub(1,12) .. "..."
  37. else
  38. data[index]["_mountpoint"] = (data[index]["_mountpoint"] and data[index]["_mountpoint"] or "").."/".. v1
  39. end
  40. end
  41. data[index]["_created"] = v.CreatedAt
  42. end
  43. return data
  44. end
  45. local volume_list = get_volumes()
  46. -- m = Map("docker", translate("Docker"))
  47. m = SimpleForm("docker", translate("Docker"))
  48. m.submit=false
  49. m.reset=false
  50. volume_table = m:section(Table, volume_list, translate("Volumes"))
  51. volume_selecter = volume_table:option(Flag, "_selected","")
  52. volume_selecter.disabled = 0
  53. volume_selecter.enabled = 1
  54. volume_selecter.default = 0
  55. volume_id = volume_table:option(DummyValue, "_name", translate("Name"))
  56. volume_table:option(DummyValue, "_driver", translate("Driver"))
  57. volume_table:option(DummyValue, "_containers", translate("Containers")).rawhtml = true
  58. volume_table:option(DummyValue, "_mountpoint", translate("Mount Point"))
  59. volume_table:option(DummyValue, "_created", translate("Created"))
  60. volume_selecter.write = function(self, section, value)
  61. volume_list[section]._selected = value
  62. end
  63. docker_status = m:section(SimpleSection)
  64. docker_status.template = "dockerman/apply_widget"
  65. docker_status.err=docker:read_status()
  66. docker_status.err=docker_status.err and docker_status.err:gsub("\n","<br>"):gsub(" ","&nbsp;")
  67. if docker_status.err then docker:clear_status() end
  68. action = m:section(Table,{{}})
  69. action.notitle=true
  70. action.rowcolors=false
  71. action.template="cbi/nullsection"
  72. btnremove = action:option(Button, "remove")
  73. btnremove.inputtitle= translate("Remove")
  74. btnremove.template = "dockerman/cbi/inlinebutton"
  75. btnremove.inputstyle = "remove"
  76. btnremove.forcewrite = true
  77. btnremove.write = function(self, section)
  78. local volume_selected = {}
  79. -- 遍历table中sectionid
  80. local volume_table_sids = volume_table:cfgsections()
  81. for _, volume_table_sid in ipairs(volume_table_sids) do
  82. -- 得到选中项的名字
  83. if volume_list[volume_table_sid]._selected == 1 then
  84. -- volume_selected[#volume_selected+1] = volume_id:cfgvalue(volume_table_sid)
  85. volume_selected[#volume_selected+1] = volume_table_sid
  86. end
  87. end
  88. if next(volume_selected) ~= nil then
  89. local success = true
  90. docker:clear_status()
  91. for _,vol in ipairs(volume_selected) do
  92. docker:append_status("Volumes: " .. "remove" .. " " .. vol .. "...")
  93. local msg = dk.volumes["remove"](dk, {id = vol})
  94. if msg.code ~= 204 then
  95. docker:append_status("code:" .. msg.code.." ".. (msg.body.message and msg.body.message or msg.message).. "\n")
  96. success = false
  97. else
  98. docker:append_status("done\n")
  99. end
  100. end
  101. if success then docker:clear_status() end
  102. luci.http.redirect(luci.dispatcher.build_url("admin/docker/volumes"))
  103. end
  104. end
  105. return m