volumes.lua 3.4 KB

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