root/luci/trunk/applications/luci-olsr/luasrc/model/cbi/olsr/olsrdplugins.lua @ 4341

Revision 4341, 6.5 KB (checked in by jow, 4 years ago)

applications/luci-olsr: add support for watchdog plugin

  • Property svn:keywords set to Id
Line 
1--[[
2LuCI - Lua Configuration Interface
3
4Copyright 2008 Steven Barth <steven@midlink.org>
5Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
6
7Licensed under the Apache License, Version 2.0 (the "License");
8you may not use this file except in compliance with the License.
9You may obtain a copy of the License at
10
11    http://www.apache.org/licenses/LICENSE-2.0
12
13$Id$
14]]--
15
16require("luci.fs")
17require("luci.ip")
18
19
20if arg[1] then
21    mp = Map("olsrd", translate("olsrd_plugins", "OLSR - Plugins"))
22
23    p = mp:section(TypedSection, "LoadPlugin")
24    p:depends("library", arg[1])
25    p.anonymous = true
26
27    ign = p:option(Flag, "ignore", "Enable")
28    ign.enabled  = "0"
29    ign.disabled = "1"
30    ign.rmempty  = false
31    function ign.cfgvalue(self, section)
32        return Flag.cfgvalue(self, section) or "0"
33    end
34
35    lib = p:option(DummyValue, "library", translate("library"))
36    lib.default = arg[1]
37
38    local function Range(x,y)
39        local t = {}
40        for i = x, y do t[#t+1] = i end
41        return t
42    end
43
44    local function Cidr2IpMask(val)
45        if val then
46            for i = 1, #val do
47                local cidr = luci.ip.IPv4(val[i]) or luci.ip.IPv6(val[i])
48                if cidr then
49                    val[i] = cidr:network():string() .. " " .. cidr:mask():string()
50                end
51            end
52            return val
53        end
54    end
55
56    local function IpMask2Cidr(val)
57        if val then
58            for i = 1, #val do
59                local ip, mask = val[i]:gmatch("([^%s+])%s+([^%s+])")()
60                local cidr
61                if ip and mask and ip:match(":") then
62                    cidr = luci.ip.IPv6(ip, mask)
63                elseif ip and mask then
64                    cidr = luci.ip.IPv4(ip, mask)
65                end
66
67                if cidr then
68                    val[i] = cidr:string()
69                end
70            end
71            return val
72        end
73    end
74
75
76    local knownPlParams = {
77        ["olsrd_bmf.so.1.5.3"] = {
78            { Value,        "BmfInterface",         "bmf0" },
79            { Value,        "BmfInterfaceIp",       "10.10.10.234/24" },
80            { Flag,         "DoLocalBroadcast",     "no" },
81            { Flag,         "CapturePacketsOnOlsrInterfaces", "yes" },
82            { ListValue,    "BmfMechanism",         { "UnicastPromiscuous", "Broadcast" } },
83            { Value,        "BroadcastRetransmitCount", "2" },
84            { Value,        "FanOutLimit",          "4" },
85            { DynamicList,  "NonOlsrIf",            "eth1" }
86        },
87
88        ["olsrd_dyn_gw.so.0.4"] = {
89            { Value,        "Interval",             "40" },
90            { DynamicList,  "Ping",                 "141.1.1.1" },
91            { DynamicList,  "HNA",                  "192.168.80.0/24", IpMask2Cidr, Cidr2IpMask }
92        },
93
94        ["olsrd_httpinfo.so.0.1"] = {
95            { Value,        "port",                 "80" },
96            { DynamicList,  "Host",                 "163.24.87.3" },
97            { DynamicList,  "Net",                  "0.0.0.0/0", IpMask2Cidr, Cidr2IpMask }
98        },
99
100        ["olsrd_nameservice.so.0.3"] = {
101            { DynamicList,  "name",                 "my-name.mesh" },
102            { DynamicList,  "hosts",                "1.2.3.4 name-for-other-interface.mesh" },
103            { Value,        "suffix",               ".olsr" },
104            { Value,        "hosts_file",           "/path/to/hosts_file" },
105            { Value,        "add_hosts",            "/path/to/file" },
106            { Value,        "dns_server",           "141.1.1.1" },
107            { Value,        "resolv_file",          "/path/to/resolv.conf" },
108            { Value,        "interval",             "120" },
109            { Value,        "timeout",              "240" },
110            { Value,        "lat",                  "12.123" },
111            { Value,        "lon",                  "12.123" },
112            { Value,        "latlon_file",          "/var/run/latlon.js" },
113            { Value,        "latlon_infile",        "/var/run/gps.txt" },
114            { Value,        "sighup_pid_file",      "/var/run/dnsmasq.pid" },
115            { Value,        "name_change_script",   "/usr/local/bin/announce_new_hosts.sh" },
116            { Value,        "services_change_script",   "/usr/local/bin/announce_new_services.sh" }
117        },
118
119        ["olsrd_quagga.so.0.2.2"] = {
120            { StaticList,   "redistribute",         {
121                "system", "kernel", "connect", "static", "rip", "ripng", "ospf",
122                "ospf6", "isis", "bgp", "hsls"
123            } },
124            { ListValue,    "ExportRoutes",         { "only", "both" } },
125            { Flag,         "LocalPref",            "true" },
126            { Value,        "Distance",             Range(0,255) }
127        },
128
129        ["olsrd_secure.so.0.5"] = {
130            { Value,        "Keyfile",              "/etc/private-olsr.key" }
131        },
132
133        ["olsrd_txtinfo.so.0.1"] = {
134            { Value,        "accept",               "10.247.200.4" }
135        },
136
137        ["olsrd_watchdog.so.0.1"] = {
138            { Value,        "file",                 "/var/run/olsrd.watchdog" },
139            { Value,        "interval",             "30" }
140        },
141
142        ["olsrd_arprefresh.so.0.1"]     = {},
143        ["olsrd_dot_draw.so.0.3"]       = {},
144        ["olsrd_dyn_gw_plain.so.0.4"]   = {},
145        ["olsrd_pgraph.so.1.1"]         = {},
146        ["olsrd_tas.so.0.1"]            = {}
147    }
148
149
150    -- build plugin options with dependencies
151    if knownPlParams[arg[1]] then
152        for _, option in ipairs(knownPlParams[arg[1]]) do
153            local otype, name, default, uci2cbi, cbi2uci = unpack(option)
154            local values
155
156            if type(default) == "table" then
157                values  = default
158                default = default[1]
159            end
160
161            if otype == Flag then
162                local bool = p:option( Flag, name )
163                if default == "yes" or default == "no" then
164                    bool.enabled  = "yes"
165                    bool.disabled = "no"
166                elseif default == "on" or default == "off" then
167                    bool.enabled  = "on"
168                    bool.disabled = "off"
169                elseif default == "1" or default == "0" then
170                    bool.enabled  = "1"
171                    bool.disabled = "0"
172                else
173                    bool.enabled  = "true"
174                    bool.disabled = "false"
175                end
176                bool.optional = true
177                bool.default = default
178                bool:depends({ library = plugin })
179            else
180                local field = p:option( otype, name )
181                if values then
182                    for _, value in ipairs(values) do
183                        field:value( value )
184                    end
185                end
186                if type(uci2cbi) == "function" then
187                    function field.cfgvalue(self, section)
188                        return uci2cbi(otype.cfgvalue(self, section))
189                    end
190                end
191                if type(cbi2uci) == "function" then
192                    function field.formvalue(self, section)
193                        return cbi2uci(otype.formvalue(self, section))
194                    end
195                end
196                field.optional = true
197                field.default = default
198                --field:depends({ library = arg[1] })
199            end
200        end
201    end
202
203    return mp
204
205else
206
207    mpi = Map("olsrd", "OLSR - Plugins")
208
209    local plugins = {}
210    mpi.uci:foreach("olsrd", "LoadPlugin",
211        function(section)
212            if section.library and not plugins[section.library] then
213                plugins[section.library] = true
214            end
215        end
216    )
217
218    -- create a loadplugin section for each found plugin
219    for k, v in pairs(luci.fs.dir("/usr/lib")) do
220        if v:sub(1, 6) == "olsrd_" then
221            if not plugins[v] then
222                mpi.uci:section(
223                    "olsrd", "LoadPlugin", nil,
224                    { library = v, ignore = 1 }
225                )
226            end
227        end
228    end
229
230    t = mpi:section( TypedSection, "LoadPlugin", "Plugins" )
231    t.anonymous = true
232    t.template  = "cbi/tblsection"
233    t.override_scheme = true
234    function t.extedit(self, section)
235        local lib = self.map:get(section, "library") or ""
236        return luci.dispatcher.build_url("admin", "services", "olsrd", "plugins") .. "/" .. lib
237    end
238
239    ign = t:option( Flag, "ignore", "Enabled" )
240    ign.enabled  = "0"
241    ign.disabled = "1"
242    ign.rmempty  = false
243    function ign.cfgvalue(self, section)
244        return Flag.cfgvalue(self, section) or "0"
245    end
246
247    t:option( DummyValue, "library", "Library" )
248
249    return mpi
250end
Note: See TracBrowser for help on using the browser.