-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpinky.lua
336 lines (301 loc) · 7.71 KB
/
pinky.lua
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
local json = require 'cjson'
local lfs = require 'lfs'
local socket = require 'socket'
local debug;
local chomp;
local dispatch;
local do_error;
local exec_command;
local file_exists;
local find_first_file;
local find_file_in_array;
local get_home;
local get_localname;
local get_ip;
local get_os;
local get_username;
local init;
local lsdir ;
local read_file;
local return_fields;
local split;
local treewalker;
local trim;
local print_table;
local xml_find_tags;
function file_exists(filename)
if filename then
local fd = io.open(filename,"r")
if fd then
io.close(fd)
return true
else
return false
end
else
return false
end
end
function exec_command(command,fields,key_field,sep,tokenize)
local out_table = {}
local out = ""
if command and not file_exists(split(command, " ")[1]) then
return { data = {}, status = { value = "FAIL", error = split(command," ")[1] .. " could not be found" } }
end
local cmd_out, cmd_err = io.popen(command)
if cmd_out then
for line in cmd_out:lines() do
if tokenize then
line_array = split(line, sep)
out_table[line_array[key_field]] = line_array
else
out = out .. line
end
end
if tokenize then
if fields then
return return_fields(out_table, fields)
else
return out_table
end
else
return out
end
else
return cmd_err
end
end
function return_fields(in_table,fields)
local out_table = {}
for k,v in pairs(in_table) do
for I = 1, #fields do
if v[fields[I]] then
if out_table[k] then
table.insert(out_table[k],v[fields[I]])
else
out_table[k] = { v[fields[I]] }
end
end
end
end
return out_table
end
function dispatch(uri)
local uri = split(uri,"/")
local ps = init()
local PINKY_HOME = "/data/pinky-server/vendor/projects/pinky/"
local short_uri = ""
if #uri < 1 then
ps = do_error("Unable to find functions in uri", ps)
-- return json.encode({ data = {}, status = { value = "FAIL", error =
end
local custom_lib = "pinky_" .. uri[2]
for I=3,#uri do
short_uri = short_uri .. "/" .. uri[I]
end
if not uri[2] then
ps = do_error("No controller passed.",ps)
-- return json.encode({ data = {}, status = { value = "FAIL", error = "uri[2] is nil!" }})
end
if file_exists(PINKY_HOME .. "/" .. custom_lib .. ".lua") then
local custom_lib = require(custom_lib)
-- make sure main exists first, then error.
if type(custom_lib) ~= "table" then
ps = do_error("Error: type is " .. type(custom_lib) .. " value:" .. tostring(custom_lib),ps)
-- return json.encode({ data = {}, status = { value = "FAIL", error =
end
if custom_lib.pinky_main then
ps = custom_lib.pinky_main(short_uri,ps)
else
ps = do_error("Could not locate " .. uri[2] .. ".pinky_main", ps)
-- return json.encode({ data = {}, status = { value = "FAIL", error = }})
end
else
ps = do_error("Error: could not locate " .. custom_lib, ps)
-- return json.encode({ data = {}, status = { value = "FAIL", error = }})
end
return json.encode(ps)
end
function lsdir (path)
local out_table = {}
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
table.insert(out_table, file)
end
end
return out_table
end
function read_file(file)
local guts = ""
local fd = io.open(file, "rb")
if fd then
guts = fd:read("*all")
fd:close()
else
local err = "Could not find " .. file
end
return guts,err
end
function get_home()
local OS = get_os()
local USER = get_username()
if OS == "Darwin" then
return "/Users/" .. USER .. "/"
elseif OS == "Linux" then
return "/home/" .. USER .. "/"
else
return "/home/" .. USER .. "/"
end
end
function get_username()
return exec_command("/usr/bin/whoami",nil,nil," +",false)
end
function get_os()
return exec_command("/usr/bin/env uname -s",nil,nil," +",false)
end
function find_first_file(files)
-- We take a table of files, and return the first one that exists
for file = 1, #files do
if file_exists(files[file]) then
return files[file]
end
end
end
function find_file_in_array(array,find_file)
for file = 1, #array do
if array[file] == find_file then
return array[file]
end
end
return nil
end
function treewalker(path)
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
local f = path..'/'..file
print ("\t "..f)
local attr = lfs.attributes (f)
assert (type(attr) == "table")
if attr.mode == "directory" then
treewalker (f)
else
for name, value in pairs(attr) do
print (name, value)
end
end
end
end
end
function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
function get_ip(hostname)
local socket = require 'socket'
host = socket.dns.toip(hostname)
if host then
return tostring(host)
else
return host
end
end
function xml_find_tags(xmldata,tags)
local out = {}
require 'LuaXml'
local x = xml.eval(xmldata)
if x then
for i,t in ipairs(tags) do
out[t] = xml.find(x,t)[1]
end
else
local err = "No xml found"
end
return out,err
end
function print_table(in_table)
local out = ""
for k,v in pairs(in_table) do
if type(v) == "table" then
v = print_table(v)
end
if type(k) == "table" then
k = print_table(k)
end
if type(v) == "function" then
v = tostring(v)
end
if type(k) == "function" then
k = tostring(k)
end
out = out .. " " .. k .. " " .. v
end
return out
end
function do_error(error_msg,ps)
ps.status.value,ps.status.error = "FAIL", error_msg
return ps
end
function do_usage(usage_msg,ps)
end
function get_localname()
local pconfig = "/data/pinky-server/pinky.yml"
local name = ""
if file_exists(pconfig) then
local yaml = require "lyaml"
local config = yaml.load(p.read_file(pconfig))
return config.hostname
else
return socket.dns.gethostname()
end
end
function init()
return { system = { time = os.time(), name = get_localname() }, data = {}, status = { value = "OK", error = ""}}
end
-- cribbed from http://stackoverflow.com/questions/1426954/split-string-in-luac
function split(pString, pPattern)
if not pString or not pPattern then
return "pString or pPattern were nil"
end
local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table,cap)
end
last_end = e+1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end
-- Provide chomp
function chomp(s)
return s:gsub("\n$", "")
end
return {
dispatch = dispatch;
chomp = chomp;
do_error = do_error;
exec_command = exec_command;
file_exists = file_exists;
find_file_in_array = find_file_in_array;
find_first_file = find_first_file;
get_home = get_home;
get_localname = get_localname;
get_ip = get_ip;
get_os = get_os;
get_username = get_username;
init = init;
lsdir = lsdir ;
print_table = print_table;
read_file = read_file;
return_fields = return_fields;
split = split;
treewalker = treewalker;
trim = trim;
xml_find_tags = xml_find_tags;
}