Add SDWAN build and test script
[icn.git] / sdwan / build / commands.lua
1 -- Licensed to the public under the GNU General Public License v2.
2
3 module("luci.controller.commands", package.seeall)
4
5 sys = require "luci.sys"
6 ut = require "luci.util"
7 io = require "io"
8
9 ip = "ip -4 "
10
11 function index()
12     entry({"admin", "config", "command"},
13         call("execute")).dependent = false
14 end
15
16 function trim(s)
17     return s:match("^%s*(.-)%s*$")
18 end
19
20 function split_and_trim(str, sep)
21     local array = {}
22     local reg = string.format("([^%s]+)", sep)
23     for item in string.gmatch(str, reg) do
24         item_trimed = trim(item)
25         if string.len(item_trimed) > 0 then
26             table.insert(array, item_trimed)
27         end
28     end
29     return array
30 end
31
32 function execute()
33     local commands = luci.http.formvalue("command")
34     io.stderr:write("Execute command: %s\n" % commands)
35
36     local command_array = split_and_trim(commands, ";")
37     for index, command in ipairs(command_array) do
38         sys.exec(command)
39     end
40
41     luci.http.prepare_content("application/json")
42     luci.http.write_json("{'status':'ok'}")
43 end