networks.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 m, s, o
  7. local networks, dk, res
  8. dk = docker.new()
  9. res = dk.networks:list()
  10. if res.code < 300 then
  11. networks = res.body
  12. else
  13. return
  14. end
  15. local get_networks = function ()
  16. local data = {}
  17. if type(networks) ~= "table" then
  18. return nil
  19. end
  20. for i, v in ipairs(networks) do
  21. local index = v.Created .. v.Id
  22. data[index]={}
  23. data[index]["_selected"] = 0
  24. data[index]["_id"] = v.Id:sub(1,12)
  25. data[index]["_name"] = v.Name
  26. data[index]["_driver"] = v.Driver
  27. if v.Driver == "bridge" then
  28. data[index]["_interface"] = v.Options["com.docker.network.bridge.name"]
  29. elseif v.Driver == "macvlan" then
  30. data[index]["_interface"] = v.Options.parent
  31. end
  32. data[index]["_subnet"] = v.IPAM and v.IPAM.Config and v.IPAM.Config[1] and v.IPAM.Config[1].Subnet or nil
  33. data[index]["_gateway"] = v.IPAM and v.IPAM.Config and v.IPAM.Config[1] and v.IPAM.Config[1].Gateway or nil
  34. end
  35. return data
  36. end
  37. local network_list = get_networks()
  38. m = SimpleForm("docker",
  39. translate("Docker - Networks"),
  40. translate("This page displays all docker networks that have been created on the connected docker host."))
  41. m.submit=false
  42. m.reset=false
  43. s = m:section(Table, network_list, translate("Networks overview"))
  44. s.nodescr=true
  45. o = s:option(Flag, "_selected","")
  46. o.template = "dockerman/cbi/xfvalue"
  47. o.disabled = 0
  48. o.enabled = 1
  49. o.default = 0
  50. o.render = function(self, section, scope)
  51. self.disable = 0
  52. if network_list[section]["_name"] == "bridge" or network_list[section]["_name"] == "none" or network_list[section]["_name"] == "host" then
  53. self.disable = 1
  54. end
  55. Flag.render(self, section, scope)
  56. end
  57. o.write = function(self, section, value)
  58. network_list[section]._selected = value
  59. end
  60. o = s:option(DummyValue, "_id", translate("ID"))
  61. o = s:option(DummyValue, "_name", translate("Network Name"))
  62. o = s:option(DummyValue, "_driver", translate("Driver"))
  63. o = s:option(DummyValue, "_interface", translate("Parent Interface"))
  64. o = s:option(DummyValue, "_subnet", translate("Subnet"))
  65. o = s:option(DummyValue, "_gateway", translate("Gateway"))
  66. s = m:section(SimpleSection)
  67. s.template = "dockerman/apply_widget"
  68. s.err = docker:read_status()
  69. s.err = s.err and s.err:gsub("\n","<br />"):gsub(" ","&#160;")
  70. if s.err then
  71. docker:clear_status()
  72. end
  73. s = m:section(Table,{{}})
  74. s.notitle=true
  75. s.rowcolors=false
  76. s.template="cbi/nullsection"
  77. o = s:option(Button, "_new")
  78. o.inputtitle= translate("New")
  79. o.template = "dockerman/cbi/inlinebutton"
  80. o.notitle=true
  81. o.inputstyle = "add"
  82. o.forcewrite = true
  83. o.write = function(self, section)
  84. luci.http.redirect(luci.dispatcher.build_url("admin/docker/newnetwork"))
  85. end
  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 network_selected = {}
  93. local network_name_selected = {}
  94. local network_driver_selected = {}
  95. for k in pairs(network_list) do
  96. if network_list[k]._selected == 1 then
  97. network_selected[#network_selected + 1] = network_list[k]._id
  98. network_name_selected[#network_name_selected + 1] = network_list[k]._name
  99. network_driver_selected[#network_driver_selected + 1] = network_list[k]._driver
  100. end
  101. end
  102. if next(network_selected) ~= nil then
  103. local success = true
  104. docker:clear_status()
  105. for ii, net in ipairs(network_selected) do
  106. docker:append_status("Networks: " .. "remove" .. " " .. net .. "...")
  107. local res = dk.networks["remove"](dk, {id = net})
  108. if res and res.code >= 300 then
  109. docker:append_status("code:" .. res.code.." ".. (res.body.message and res.body.message or res.message).. "\n")
  110. success = false
  111. else
  112. docker:append_status("done\n")
  113. if network_driver_selected[ii] == "macvlan" then
  114. docker.remove_macvlan_interface(network_name_selected[ii])
  115. end
  116. end
  117. end
  118. if success then
  119. docker:clear_status()
  120. end
  121. luci.http.redirect(luci.dispatcher.build_url("admin/docker/networks"))
  122. end
  123. end
  124. return m