0e7b8b11d0344fdac0cf81b29e00f81a6dabde29
[project/luci.git] / applications / luci-app-mwan3 / luasrc / model / cbi / mwan / advanced_hotplugscript.lua
1 -- ------ hotplug script configuration ------ --
2
3 fs = require "nixio.fs"
4 sys = require "luci.sys"
5 ut = require "luci.util"
6
7 script = "/etc/hotplug.d/iface/16-mwancustom"
8 scriptBackup = "/etc/hotplug.d/iface/16-mwancustombak"
9
10 if luci.http.formvalue("cbid.luci.1._restorebak") then -- restore button has been clicked
11         luci.http.redirect(luci.dispatcher.build_url("admin/network/mwan/advanced/hotplugscript") .. "?restore=yes")
12 elseif luci.http.formvalue("restore") == "yes" then -- restore script from backup
13         os.execute("cp -f " .. scriptBackup .. " " .. script)
14 end
15
16
17 m5 = SimpleForm("luci", nil)
18         m5:append(Template("mwan/advanced_hotplugscript")) -- highlight current tab
19
20 f = m5:section(SimpleSection, nil,
21         translate("This section allows you to modify the contents of /etc/hotplug.d/iface/16-mwancustom<br />" ..
22         "This is useful for running system commands and/or scripts based on interface ifup or ifdown hotplug events<br /><br />" ..
23         "Notes:<br />" ..
24         "The first line of the script must be &#34;#!/bin/sh&#34; without quotes<br />" ..
25         "Lines beginning with # are comments and are not executed<br /><br />" ..
26         "Available variables:<br />" ..
27         "$ACTION is the hotplug event (ifup, ifdown)<br />" ..
28         "$INTERFACE is the interface name (wan1, wan2, etc.)<br />" ..
29         "$DEVICE is the device name attached to the interface (eth0.1, eth1, etc.)"))
30
31
32 restore = f:option(Button, "_restorebak", translate("Restore default hotplug script"))
33         restore.inputtitle = translate("Restore...")
34         restore.inputstyle = "apply"
35
36 t = f:option(TextValue, "lines")
37         t.rmempty = true
38         t.rows = 20
39
40         function t.cfgvalue()
41                 local hps = fs.readfile(script)
42                 if not hps or hps == "" then -- if script does not exist or is blank restore from backup
43                         sys.call("cp -f " .. scriptBackup .. " " .. script)
44                         return fs.readfile(script)
45                 else
46                         return hps
47                 end
48         end
49
50         function t.write(self, section, data) -- format and write new data to script
51                 return fs.writefile(script, ut.trim(data:gsub("\r\n", "\n")) .. "\n")
52         end
53
54
55 return m5