luci-app-dockerman: cbi/newnetwork refactoring and update coding style

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
This commit is contained in:
Florian Eckert 2020-07-22 12:53:27 +02:00
parent f299b988d8
commit 4f04660860

View file

@ -3,219 +3,244 @@ LuCI - Lua Configuration Interface
Copyright 2019 lisaac <https://github.com/lisaac/luci-app-dockerman> Copyright 2019 lisaac <https://github.com/lisaac/luci-app-dockerman>
]]-- ]]--
require "luci.util"
local docker = require "luci.model.docker" local docker = require "luci.model.docker"
local m, s, o
local dk = docker.new() local dk = docker.new()
m = SimpleForm("docker", translate("Docker")) m = SimpleForm("docker", translate("Docker"))
m.redirect = luci.dispatcher.build_url("admin", "docker", "networks") m.redirect = luci.dispatcher.build_url("admin", "docker", "networks")
docker_status = m:section(SimpleSection) s = m:section(SimpleSection)
docker_status.template = "dockerman/apply_widget" s.template = "dockerman/apply_widget"
docker_status.err=docker:read_status() s.err=docker:read_status()
docker_status.err=docker_status.err and docker_status.err:gsub("\n","<br>"):gsub(" ","&nbsp;") s.err=s.err and s.err:gsub("\n","<br>"):gsub(" ","&nbsp;")
if docker_status.err then docker:clear_status() end if s.err then
docker:clear_status()
end
s = m:section(SimpleSection, translate("New Network")) s = m:section(SimpleSection, translate("New Network"))
s.addremove = true s.addremove = true
s.anonymous = true s.anonymous = true
d = s:option(Value, "name", translate("Network Name")) o = s:option(Value, "name", translate("Network Name"))
d.rmempty = true o.rmempty = true
d = s:option(ListValue, "dirver", translate("Driver")) o = s:option(ListValue, "dirver", translate("Driver"))
d.rmempty = true o.rmempty = true
d:value("bridge", "bridge") o:value("bridge", "bridge")
d:value("macvlan", "macvlan") o:value("macvlan", "macvlan")
d:value("ipvlan", "ipvlan") o:value("ipvlan", "ipvlan")
d:value("overlay", "overlay") o:value("overlay", "overlay")
d = s:option(Value, "parent", translate("Parent Interface")) o = s:option(Value, "parent", translate("Parent Interface"))
d.rmempty = true o.rmempty = true
d:depends("dirver", "macvlan") o:depends("dirver", "macvlan")
local interfaces = luci.sys and luci.sys.net and luci.sys.net.devices() or {} local interfaces = luci.sys and luci.sys.net and luci.sys.net.devices() or {}
for _, v in ipairs(interfaces) do for _, v in ipairs(interfaces) do
d:value(v, v) o:value(v, v)
end end
d.default="br-lan" o.default="br-lan"
d.placeholder="br-lan" o.placeholder="br-lan"
d = s:option(Value, "macvlan_mode", translate("Macvlan Mode")) o = s:option(Value, "macvlan_mode", translate("Macvlan Mode"))
d.rmempty = true o.rmempty = true
d:depends("dirver", "macvlan") o:depends("dirver", "macvlan")
d.default="bridge" o.default="bridge"
d:value("bridge", "bridge") o:value("bridge", "bridge")
d:value("private", "private") o:value("private", "private")
d:value("vepa", "vepa") o:value("vepa", "vepa")
d:value("passthru", "passthru") o:value("passthru", "passthru")
d = s:option(Value, "ipvlan_mode", translate("Ipvlan Mode")) o = s:option(Value, "ipvlan_mode", translate("Ipvlan Mode"))
d.rmempty = true o.rmempty = true
d:depends("dirver", "ipvlan") o:depends("dirver", "ipvlan")
d.default="l3" o.default="l3"
d:value("l2", "l2") o:value("l2", "l2")
d:value("l3", "l3") o:value("l3", "l3")
d = s:option(Flag, "ingress", translate("Ingress"), translate("Ingress network is the network which provides the routing-mesh in swarm mode")) o = s:option(Flag, "ingress",
d.rmempty = true translate("Ingress"),
d.disabled = 0 translate("Ingress network is the network which provides the routing-mesh in swarm mode"))
d.enabled = 1 o.rmempty = true
d.default = 0 o.disabled = 0
d:depends("dirver", "overlay") o.enabled = 1
o.default = 0
o:depends("dirver", "overlay")
d = s:option(DynamicList, "options", translate("Options")) o = s:option(DynamicList, "options", translate("Options"))
d.rmempty = true o.rmempty = true
d.placeholder="com.docker.network.driver.mtu=1500" o.placeholder="com.docker.network.driver.mtu=1500"
d = s:option(Flag, "internal", translate("Internal"), translate("Restrict external access to the network")) o = s:option(Flag, "internal", translate("Internal"), translate("Restrict external access to the network"))
d.rmempty = true o.rmempty = true
d:depends("dirver", "overlay") o:depends("dirver", "overlay")
d.disabled = 0 o.disabled = 0
d.enabled = 1 o.enabled = 1
d.default = 0 o.default = 0
if nixio.fs.access("/etc/config/network") and nixio.fs.access("/etc/config/firewall")then if nixio.fs.access("/etc/config/network") and nixio.fs.access("/etc/config/firewall")then
d = s:option(Flag, "op_macvlan", translate("Create macvlan interface"), translate("Auto create macvlan interface in Openwrt")) o = s:option(Flag, "op_macvlan", translate("Create macvlan interface"), translate("Auto create macvlan interface in Openwrt"))
d:depends("dirver", "macvlan") o:depends("dirver", "macvlan")
d.disabled = 0 o.disabled = 0
d.enabled = 1 o.enabled = 1
d.default = 1 o.default = 1
end end
d = s:option(Value, "subnet", translate("Subnet")) o = s:option(Value, "subnet", translate("Subnet"))
d.rmempty = true o.rmempty = true
d.placeholder="10.1.0.0/16" o.placeholder="10.1.0.0/16"
d.datatype="ip4addr" o.datatype="ip4addr"
d = s:option(Value, "gateway", translate("Gateway")) o = s:option(Value, "gateway", translate("Gateway"))
d.rmempty = true o.rmempty = true
d.placeholder="10.1.1.1" o.placeholder="10.1.1.1"
d.datatype="ip4addr" o.datatype="ip4addr"
d = s:option(Value, "ip_range", translate("IP range")) o = s:option(Value, "ip_range", translate("IP range"))
d.rmempty = true o.rmempty = true
d.placeholder="10.1.1.0/24" o.placeholder="10.1.1.0/24"
d.datatype="ip4addr" o.datatype="ip4addr"
d = s:option(DynamicList, "aux_address", translate("Exclude IPs")) o = s:option(DynamicList, "aux_address", translate("Exclude IPs"))
d.rmempty = true o.rmempty = true
d.placeholder="my-route=10.1.1.1" o.placeholder="my-route=10.1.1.1"
d = s:option(Flag, "ipv6", translate("Enable IPv6")) o = s:option(Flag, "ipv6", translate("Enable IPv6"))
d.rmempty = true o.rmempty = true
d.disabled = 0 o.disabled = 0
d.enabled = 1 o.enabled = 1
d.default = 0 o.default = 0
d = s:option(Value, "subnet6", translate("IPv6 Subnet")) o = s:option(Value, "subnet6", translate("IPv6 Subnet"))
d.rmempty = true o.rmempty = true
d.placeholder="fe80::/10" o.placeholder="fe80::/10"
d.datatype="ip6addr" o.datatype="ip6addr"
d:depends("ipv6", 1) o:depends("ipv6", 1)
d = s:option(Value, "gateway6", translate("IPv6 Gateway")) o = s:option(Value, "gateway6", translate("IPv6 Gateway"))
d.rmempty = true o.rmempty = true
d.placeholder="fe80::1" o.placeholder="fe80::1"
d.datatype="ip6addr" o.datatype="ip6addr"
d:depends("ipv6", 1) o:depends("ipv6", 1)
m.handle = function(self, state, data) m.handle = function(self, state, data)
if state == FORM_VALID then if state == FORM_VALID then
local name = data.name local name = data.name
local driver = data.dirver local driver = data.dirver
local internal = data.internal == 1 and true or false local internal = data.internal == 1 and true or false
local subnet = data.subnet local subnet = data.subnet
local gateway = data.gateway local gateway = data.gateway
local ip_range = data.ip_range local ip_range = data.ip_range
local aux_address = {} local aux_address = {}
local tmp = data.aux_address or {} local tmp = data.aux_address or {}
for i,v in ipairs(tmp) do for i,v in ipairs(tmp) do
_,_,k1,v1 = v:find("(.-)=(.+)") _,_,k1,v1 = v:find("(.-)=(.+)")
aux_address[k1] = v1 aux_address[k1] = v1
end end
local options = {} local options = {}
tmp = data.options or {} tmp = data.options or {}
for i,v in ipairs(tmp) do for i,v in ipairs(tmp) do
_,_,k1,v1 = v:find("(.-)=(.+)") _,_,k1,v1 = v:find("(.-)=(.+)")
options[k1] = v1 options[k1] = v1
end end
local ipv6 = data.ipv6 == 1 and true or false local ipv6 = data.ipv6 == 1 and true or false
local create_body={ local create_body = {
Name = name, Name = name,
Driver = driver, Driver = driver,
EnableIPv6 = ipv6, EnableIPv6 = ipv6,
IPAM = { IPAM = {
Driver= "default" Driver= "default"
}, },
Internal = internal Internal = internal
} }
if subnet or gateway or ip_range then if subnet or gateway or ip_range then
create_body["IPAM"]["Config"] = { create_body["IPAM"]["Config"] = {
{ {
Subnet = subnet, Subnet = subnet,
Gateway = gateway, Gateway = gateway,
IPRange = ip_range, IPRange = ip_range,
AuxAddress = aux_address, AuxAddress = aux_address,
AuxiliaryAddresses = aux_address AuxiliaryAddresses = aux_address
} }
} }
end end
if driver == "macvlan" then
create_body["Options"] = {
macvlan_mode = data.macvlan_mode,
parent = data.parent
}
elseif driver == "ipvlan" then
create_body["Options"] = {
ipvlan_mode = data.ipvlan_mode
}
elseif driver == "overlay" then
create_body["Ingress"] = data.ingerss == 1 and true or false
end
if ipv6 and data.subnet6 and data.subnet6 then if driver == "macvlan" then
if type(create_body["IPAM"]["Config"]) ~= "table" then create_body["Options"] = {
create_body["IPAM"]["Config"] = {} macvlan_mode = data.macvlan_mode,
end parent = data.parent
local index = #create_body["IPAM"]["Config"] }
create_body["IPAM"]["Config"][index+1] = { elseif driver == "ipvlan" then
Subnet = data.subnet6, create_body["Options"] = {
Gateway = data.gateway6 ipvlan_mode = data.ipvlan_mode
} }
end elseif driver == "overlay" then
create_body["Ingress"] = data.ingerss == 1 and true or false
end
if next(options) ~= nil then if ipv6 and data.subnet6 and data.subnet6 then
create_body["Options"] = create_body["Options"] or {} if type(create_body["IPAM"]["Config"]) ~= "table" then
for k, v in pairs(options) do create_body["IPAM"]["Config"] = {}
create_body["Options"][k] = v end
end local index = #create_body["IPAM"]["Config"]
end create_body["IPAM"]["Config"][index+1] = {
Subnet = data.subnet6,
Gateway = data.gateway6
}
end
create_body = docker.clear_empty_tables(create_body) if next(options) ~= nil then
docker:write_status("Network: " .. "create" .. " " .. create_body.Name .. "...") create_body["Options"] = create_body["Options"] or {}
local res = dk.networks:create({body = create_body}) for k, v in pairs(options) do
if res and res.code == 201 then create_body["Options"][k] = v
docker:write_status("Network: " .. "create macvlan interface...") end
res = dk.networks:inspect({ name = create_body.Name }) end
if driver == "macvlan" and data.op_macvlan ~= 0 and res.code == 200
and res.body and res.body.IPAM and res.body.IPAM.Config and res.body.IPAM.Config[1] create_body = docker.clear_empty_tables(create_body)
and res.body.IPAM.Config[1].Gateway and res.body.IPAM.Config[1].Subnet then docker:write_status("Network: " .. "create" .. " " .. create_body.Name .. "...")
docker.create_macvlan_interface(data.name, data.parent, res.body.IPAM.Config[1].Gateway, res.body.IPAM.Config[1].Subnet)
end local res = dk.networks:create({
docker:clear_status() body = create_body
luci.http.redirect(luci.dispatcher.build_url("admin/docker/networks")) })
else
docker:append_status("code:" .. res.code.." ".. (res.body.message and res.body.message or res.message).. "\n") if res and res.code == 201 then
luci.http.redirect(luci.dispatcher.build_url("admin/docker/newnetwork")) docker:write_status("Network: " .. "create macvlan interface...")
end res = dk.networks:inspect({
end name = create_body.Name
})
if driver == "macvlan" and
data.op_macvlan ~= 0 and
res.code == 200 and
res.body and
res.body.IPAM and
res.body.IPAM.Config and
res.body.IPAM.Config[1] and
res.body.IPAM.Config[1].Gateway and
res.body.IPAM.Config[1].Subnet then
docker.create_macvlan_interface(data.name,
data.parent,
res.body.IPAM.Config[1].Gateway,
res.body.IPAM.Config[1].Subnet)
end
docker:clear_status()
luci.http.redirect(luci.dispatcher.build_url("admin/docker/networks"))
else
docker:append_status("code:" .. res.code.." ".. (res.body.message and res.body.message or res.message).. "\n")
luci.http.redirect(luci.dispatcher.build_url("admin/docker/newnetwork"))
end
end
end end
return m return m