summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-dockerman/luasrc/model/cbi/dockerman/newnetwork.lua
blob: bdadbaf88175adbad8b507fae5b0f120a5c13ac1 (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
--[[
LuCI - Lua Configuration Interface
Copyright 2019 lisaac <https://github.com/lisaac/luci-app-dockerman>
]]--

require "luci.util"
local docker = require "luci.model.docker"
local dk = docker.new()

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

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(" ","&nbsp;")
if docker_status.err then docker:clear_status() end

s = m:section(SimpleSection, translate("New Network"))
s.addremove = true
s.anonymous = true

d = s:option(Value, "name", translate("Network Name"))
d.rmempty = true

d = s:option(ListValue, "dirver", translate("Driver"))
d.rmempty = true
d:value("bridge", "bridge")
d:value("macvlan", "macvlan")
d:value("ipvlan", "ipvlan")
d:value("overlay", "overlay")

d = s:option(Value, "parent", translate("Parent Interface"))
d.rmempty = true
d:depends("dirver", "macvlan")
local interfaces = luci.sys and luci.sys.net and luci.sys.net.devices() or {}
for _, v in ipairs(interfaces) do
  d:value(v, v)
end
d.default="br-lan"
d.placeholder="br-lan"

d = s:option(Value, "macvlan_mode", translate("Macvlan Mode"))
d.rmempty = true
d:depends("dirver", "macvlan")
d.default="bridge"
d:value("bridge", "bridge")
d:value("private", "private")
d:value("vepa", "vepa")
d:value("passthru", "passthru")

d = s:option(Value, "ipvlan_mode", translate("Ipvlan Mode"))
d.rmempty = true
d:depends("dirver", "ipvlan")
d.default="l3"
d:value("l2", "l2")
d:value("l3", "l3")

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

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

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

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"))
  d:depends("dirver", "macvlan")
  d.disabled = 0
  d.enabled = 1
  d.default = 1
end

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

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

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

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

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

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

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

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

    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