luci-app-dockerman: cbi/newnetwork refactoring and update coding style
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
This commit is contained in:
parent
f299b988d8
commit
4f04660860
1 changed files with 197 additions and 172 deletions
|
@ -3,121 +3,127 @@ 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(" "," ")
|
s.err=s.err and s.err:gsub("\n","<br>"):gsub(" "," ")
|
||||||
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
|
||||||
|
@ -146,7 +152,7 @@ m.handle = function(self, state, data)
|
||||||
|
|
||||||
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,
|
||||||
|
@ -167,6 +173,7 @@ m.handle = function(self, state, data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
if driver == "macvlan" then
|
if driver == "macvlan" then
|
||||||
create_body["Options"] = {
|
create_body["Options"] = {
|
||||||
macvlan_mode = data.macvlan_mode,
|
macvlan_mode = data.macvlan_mode,
|
||||||
|
@ -200,15 +207,33 @@ m.handle = function(self, state, data)
|
||||||
|
|
||||||
create_body = docker.clear_empty_tables(create_body)
|
create_body = docker.clear_empty_tables(create_body)
|
||||||
docker:write_status("Network: " .. "create" .. " " .. create_body.Name .. "...")
|
docker:write_status("Network: " .. "create" .. " " .. create_body.Name .. "...")
|
||||||
local res = dk.networks:create({body = create_body})
|
|
||||||
|
local res = dk.networks:create({
|
||||||
|
body = create_body
|
||||||
|
})
|
||||||
|
|
||||||
if res and res.code == 201 then
|
if res and res.code == 201 then
|
||||||
docker:write_status("Network: " .. "create macvlan interface...")
|
docker:write_status("Network: " .. "create macvlan interface...")
|
||||||
res = dk.networks:inspect({ name = create_body.Name })
|
res = dk.networks:inspect({
|
||||||
if driver == "macvlan" and data.op_macvlan ~= 0 and res.code == 200
|
name = create_body.Name
|
||||||
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)
|
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
|
end
|
||||||
|
|
||||||
docker:clear_status()
|
docker:clear_status()
|
||||||
luci.http.redirect(luci.dispatcher.build_url("admin/docker/networks"))
|
luci.http.redirect(luci.dispatcher.build_url("admin/docker/networks"))
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in a new issue