diff options
Diffstat (limited to 'applications/luci-app-dockerman/luasrc')
-rw-r--r-- | applications/luci-app-dockerman/luasrc/model/cbi/dockerman/networks.lua | 153 |
1 files changed, 87 insertions, 66 deletions
diff --git a/applications/luci-app-dockerman/luasrc/model/cbi/dockerman/networks.lua b/applications/luci-app-dockerman/luasrc/model/cbi/dockerman/networks.lua index e8e392f6ab..51d0f89676 100644 --- a/applications/luci-app-dockerman/luasrc/model/cbi/dockerman/networks.lua +++ b/applications/luci-app-dockerman/luasrc/model/cbi/dockerman/networks.lua @@ -6,35 +6,49 @@ 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 dk = docker.new() -local networks -local res = dk.networks:list() -if res.code < 300 then networks = res.body else return end + +local m, s, o +local networks, dk, res + +dk = docker.new() +res = dk.networks:list() +if res.code < 300 then + networks = res.body +else + return +end local get_networks = function () - local data = {} - - if type(networks) ~= "table" then return nil end - for i, v in ipairs(networks) do - local index = v.Created .. v.Id - data[index]={} - data[index]["_selected"] = 0 - data[index]["_id"] = v.Id:sub(1,12) - data[index]["_name"] = v.Name - data[index]["_driver"] = v.Driver - if v.Driver == "bridge" then - data[index]["_interface"] = v.Options["com.docker.network.bridge.name"] - elseif v.Driver == "macvlan" then - data[index]["_interface"] = v.Options.parent - end - data[index]["_subnet"] = v.IPAM and v.IPAM.Config[1] and v.IPAM.Config[1].Subnet or nil - data[index]["_gateway"] = v.IPAM and v.IPAM.Config[1] and v.IPAM.Config[1].Gateway or nil - end - return data + local data = {} + + if type(networks) ~= "table" then + return nil + end + + for i, v in ipairs(networks) do + local index = v.Created .. v.Id + + data[index]={} + data[index]["_selected"] = 0 + data[index]["_id"] = v.Id:sub(1,12) + data[index]["_name"] = v.Name + data[index]["_driver"] = v.Driver + + if v.Driver == "bridge" then + data[index]["_interface"] = v.Options["com.docker.network.bridge.name"] + elseif v.Driver == "macvlan" then + data[index]["_interface"] = v.Options.parent + end + + data[index]["_subnet"] = v.IPAM and v.IPAM.Config[1] and v.IPAM.Config[1].Subnet or nil + data[index]["_gateway"] = v.IPAM and v.IPAM.Config[1] and v.IPAM.Config[1].Gateway or nil + end + + return data end local network_list = get_networks() --- m = Map("docker", translate("Docker")) + m = SimpleForm("docker", translate("Docker")) m.submit=false m.reset=false @@ -49,11 +63,11 @@ network_selecter.disabled = 0 network_selecter.enabled = 1 network_selecter.default = 0 network_selecter.render = function(self, section, scope) - self.disable = 0 - if network_list[section]["_name"] == "bridge" or network_list[section]["_name"] == "none" or network_list[section]["_name"] == "host" then - self.disable = 1 - end - Flag.render(self, section, scope) + self.disable = 0 + if network_list[section]["_name"] == "bridge" or network_list[section]["_name"] == "none" or network_list[section]["_name"] == "host" then + self.disable = 1 + end + Flag.render(self, section, scope) end network_name = network_table:option(DummyValue, "_name", translate("Network Name")) @@ -63,19 +77,22 @@ network_subnet = network_table:option(DummyValue, "_subnet", translate("Subnet") network_gateway = network_table:option(DummyValue, "_gateway", translate("Gateway")) network_selecter.write = function(self, section, value) - network_list[section]._selected = value + network_list[section]._selected = value end docker_status = m:section(SimpleSection) docker_status.template = "dockerman/apply_widget" docker_status.err=docker:read_status() 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 action = m:section(Table,{{}}) action.notitle=true action.rowcolors=false action.template="cbi/nullsection" + btnnew=action:option(Button, "_new") btnnew.inputtitle= translate("New") btnnew.template = "dockerman/cbi/inlinebutton" @@ -83,48 +100,52 @@ btnnew.notitle=true btnnew.inputstyle = "add" btnnew.forcewrite = true btnnew.write = function(self, section) - luci.http.redirect(luci.dispatcher.build_url("admin/docker/newnetwork")) + uci.http.redirect(luci.dispatcher.build_url("admin/docker/newnetwork")) end + btnremove = action:option(Button, "_remove") btnremove.inputtitle= translate("Remove") btnremove.template = "dockerman/cbi/inlinebutton" btnremove.inputstyle = "remove" btnremove.forcewrite = true btnremove.write = function(self, section) - local network_selected = {} - local network_name_selected = {} - local network_driver_selected = {} - -- 遍历table中sectionid - local network_table_sids = network_table:cfgsections() - for _, network_table_sid in ipairs(network_table_sids) do - -- 得到选中项的名字 - if network_list[network_table_sid]._selected == 1 then - network_selected[#network_selected+1] = network_list[network_table_sid]._id --network_name:cfgvalue(network_table_sid) - network_name_selected[#network_name_selected+1] = network_list[network_table_sid]._name - network_driver_selected[#network_driver_selected+1] = network_list[network_table_sid]._driver - end - end - if next(network_selected) ~= nil then - local success = true - docker:clear_status() - for ii, net in ipairs(network_selected) do - docker:append_status("Networks: " .. "remove" .. " " .. net .. "...") - local res = dk.networks["remove"](dk, {id = net}) - if res and res.code >= 300 then - docker:append_status("code:" .. res.code.." ".. (res.body.message and res.body.message or res.message).. "\n") - success = false - else - docker:append_status("done\n") - if network_driver_selected[ii] == "macvlan" then - docker.remove_macvlan_interface(network_name_selected[ii]) - end - end - end - if success then - docker:clear_status() - end - luci.http.redirect(luci.dispatcher.build_url("admin/docker/networks")) - end + local network_selected = {} + local network_name_selected = {} + local network_driver_selected = {} + + local network_table_sids = network_table:cfgsections() + for _, network_table_sid in ipairs(network_table_sids) do + if network_list[network_table_sid]._selected == 1 then + network_selected[#network_selected+1] = network_list[network_table_sid]._id + network_name_selected[#network_name_selected+1] = network_list[network_table_sid]._name + network_driver_selected[#network_driver_selected+1] = network_list[network_table_sid]._driver + end + end + + if next(network_selected) ~= nil then + local success = true + docker:clear_status() + + for ii, net in ipairs(network_selected) do + docker:append_status("Networks: " .. "remove" .. " " .. net .. "...") + local res = dk.networks["remove"](dk, {id = net}) + + if res and res.code >= 300 then + docker:append_status("code:" .. res.code.." ".. (res.body.message and res.body.message or res.message).. "\n") + success = false + else + docker:append_status("done\n") + if network_driver_selected[ii] == "macvlan" then + docker.remove_macvlan_interface(network_name_selected[ii]) + end + end + end + + if success then + docker:clear_status() + end + luci.http.redirect(luci.dispatcher.build_url("admin/docker/networks")) + end end return m |