summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-dockerman/luasrc/model/cbi/dockerman/newnetwork.lua
blob: b89686b2ab0d71542598d4ae9bb46b75504d567c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
--[[
LuCI - Lua Configuration Interface
Copyright 2019 lisaac <https://github.com/lisaac/luci-app-dockerman>
]]--

local docker = require "luci.model.docker"

local m, s, o

local dk = docker.new()

m = SimpleForm("docker", translate("Docker - Network"))
m.redirect = luci.dispatcher.build_url("admin", "docker", "networks")

s = m:section(SimpleSection)
s.template = "dockerman/apply_widget"
s.err=docker:read_status()
s.err=s.err and s.err:gsub("\n","<br>"):gsub(" ","&nbsp;")
if s.err then
	docker:clear_status()
end

s = m:section(SimpleSection, translate("Create new docker network"))
s.addremove = true
s.anonymous = true

o = s:option(Value, "name",
	translate("Network Name"),
	translate("Name of the network that can be selected during container creation"))
o.rmempty = true

o = s:option(ListValue, "driver", translate("Driver"))
o.rmempty = true
o:value("bridge", translate("Bridge device"))
o:value("macvlan", translate("MAC VLAN"))
o:value("ipvlan",  translate("IP VLAN"))
o:value("overlay", translate("Overlay network"))

o = s:option(Value, "parent", translate("Base device"))
o.rmempty = true
o:depends("driver", "macvlan")
local interfaces = luci.sys and luci.sys.net and luci.sys.net.devices() or {}
for _, v in ipairs(interfaces) do
	o:value(v, v)
end

o = s:option(ListValue, "macvlan_mode", translate("Mode"))
o.rmempty = true
o:depends("driver", "macvlan")
o.default="bridge"
o:value("bridge", translate("Bridge (Support direct communication between MAC VLANs)"))
o:value("private", translate("Private (Prevent communication between MAC VLANs)"))
o:value("vepa", translate("VEPA (Virtual Ethernet Port Aggregator)"))
o:value("passthru", translate("Pass-through (Mirror physical device to single MAC VLAN)"))

o = s:option(ListValue, "ipvlan_mode", translate("Ipvlan Mode"))
o.rmempty = true
o:depends("driver", "ipvlan")
o.default="l3"
o:value("l2", translate("L2 bridge"))
o:value("l3", translate("L3 bridge"))

o = s:option(Flag, "ingress",
	translate("Ingress"),
	translate("Ingress network is the network which provides the routing-mesh in swarm mode"))
o.rmempty = true
o.disabled = 0
o.enabled = 1
o.default = 0
o:depends("driver", "overlay")

o = s:option(DynamicList, "options", translate("Options"))
o.rmempty = true
o.placeholder="com.docker.network.driver.mtu=1500"

o = s:option(Flag, "internal", translate("Internal"), translate("Restrict external access to the network"))
o.rmempty = true
o:depends("driver", "overlay")
o.disabled = 0
o.enabled = 1
o.default = 0

if nixio.fs.access("/etc/config/network") and nixio.fs.access("/etc/config/firewall")then
	o = s:option(Flag, "op_macvlan", translate("Create macvlan interface"), translate("Auto create macvlan interface in Openwrt"))
	o:depends("driver", "macvlan")
	o.disabled = 0
	o.enabled = 1
	o.default = 1
end

o = s:option(Value, "subnet", translate("Subnet"))
o.rmempty = true
o.placeholder="10.1.0.0/16"
o.datatype="ip4addr"

o = s:option(Value, "gateway", translate("Gateway"))
o.rmempty = true
o.placeholder="10.1.1.1"
o.datatype="ip4addr"

o = s:option(Value, "ip_range", translate("IP range"))
o.rmempty = true
o.placeholder="10.1.1.0/24"
o.datatype="ip4addr"

o = s:option(DynamicList, "aux_address", translate("Exclude IPs"))
o.rmempty = true
o.placeholder="my-route=10.1.1.1"

o = s:option(Flag, "ipv6", translate("Enable IPv6"))
o.rmempty = true
o.disabled = 0
o.enabled = 1
o.default = 0

o = s:option(Value, "subnet6", translate("IPv6 Subnet"))
o.rmempty = true
o.placeholder="fe80::/10"
o.datatype="ip6addr"
o:depends("ipv6", 1)

o = s:option(Value, "gateway6", translate("IPv6 Gateway"))
o.rmempty = true
o.placeholder="fe80::1"
o.datatype="ip6addr"
o:depends("ipv6", 1)

m.handle = function(self, state, data)
	if state == FORM_VALID then
		local name = data.name
		local driver = data.driver

		local internal = data.internal == 1 and true or false

		local subnet = data.subnet
		local gateway = data.gateway
		local ip_range = data.ip_range

		local aux_address = {}
		local tmp = data.aux_address or {}
		for i,v in ipairs(tmp) do
			_,_,k1,v1 = v:find("(.-)=(.+)")
			aux_address[k1] = v1
		end

		local options = {}
		tmp = data.options or {}
		for i,v in ipairs(tmp) do
			_,_,k1,v1 = v:find("(.-)=(.+)")
			options[k1] = v1
		end

		local ipv6 = data.ipv6 == 1 and true or false

		local create_body = {
			Name = name,
			Driver = driver,
			EnableIPv6 = ipv6,
			IPAM = {
				Driver= "default"
			},
			Internal = internal
		}

		if subnet or gateway or ip_range then
			create_body["IPAM"]["Config"] = {
				{
					Subnet = subnet,
					Gateway = gateway,
					IPRange = ip_range,
					AuxAddress = aux_address,
					AuxiliaryAddresses = aux_address
				}
			}
		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 type(create_body["IPAM"]["Config"]) ~= "table" then
				create_body["IPAM"]["Config"] = {}
			end
			local index = #create_body["IPAM"]["Config"]
			create_body["IPAM"]["Config"][index+1] = {
				Subnet = data.subnet6,
				 Gateway = data.gateway6
			}
		end

		if next(options) ~= nil then
			create_body["Options"] = create_body["Options"] or {}
			for k, v in pairs(options) do
				create_body["Options"][k] = v
			end
		end

		create_body = docker.clear_empty_tables(create_body)
		docker:write_status("Network: " .. "create" .. " " .. create_body.Name .. "...")

		local res = dk.networks:create({
			body = create_body
		})

		if res and res.code == 201 then
			docker:write_status("Network: " .. "create macvlan interface...")
			res = dk.networks:inspect({
				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

return m