Advertisement
Guest User

rc.lua

a guest
Mar 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. local rc = require("rc")
  2. local fs = require("filesystem")
  3. local shell = require("shell")
  4.  
  5. local function loadConfig()
  6.   local env = {}
  7.   local result, reason = loadfile('/etc/rc.cfg', 't', env)
  8.   if result then
  9.     result, reason = xpcall(result, debug.traceback)
  10.     if result then
  11.       return env
  12.     end
  13.   end
  14.   return nil, reason
  15. end
  16.  
  17. local function saveConfig(conf)
  18.   local file, reason = io.open('/etc/rc.cfg', 'w')
  19.   if not file then
  20.     return nil, reason
  21.   end
  22.   for key, value in pairs(conf) do
  23.     file:write(tostring(key) .. " = " .. require("serialization").serialize(value) .. "\n")
  24.   end
  25.  
  26.   file:close()
  27.   return true
  28. end
  29.  
  30. local function load(name, args)
  31.   if rc.loaded[name] then
  32.     return rc.loaded[name]
  33.   end
  34.   local fileName = fs.concat('/etc/rc.d/', name .. '.lua')
  35.   local env = setmetatable({args = args}, {__index = _G})
  36.   local result, reason = loadfile(fileName, 't', env)
  37.   if result then
  38.     result, reason = xpcall(result, debug.traceback)
  39.     if result then
  40.       rc.loaded[name] = env
  41.       return env
  42.     end
  43.   end
  44.   return nil, reason
  45. end
  46.  
  47. function rc.unload(name)
  48.   rc.loaded[name] = nil
  49. end
  50.  
  51. local function rawRunCommand(conf, name, cmd, args, ...)
  52.   local result, what = load(name, args)
  53.   if result then
  54.     if not cmd then
  55.       io.output():write("Commands for service " .. name .. "\n")
  56.       for command, val in pairs(result) do
  57.         if type(val) == "function" then
  58.           io.output():write(tostring(command) .. " ")
  59.         end
  60.       end
  61.       return true
  62.     elseif type(result[cmd]) == "function" then
  63.       result, what = xpcall(result[cmd], debug.traceback, ...)
  64.       if result then
  65.         return true
  66.       end
  67.     elseif cmd == "restart" and type(result["stop"]) == "function" and type(result["start"]) == "function" then
  68.       local daemon = result
  69.       result, what = xpcall(daemon["stop"], debug.traceback, ...)
  70.       if result then
  71.         result, what = xpcall(daemon["start"], debug.traceback, ...)
  72.         if result then
  73.           return true
  74.         end
  75.       end
  76.     elseif cmd == "enable" then
  77.       conf.enabled = conf.enabled or {}
  78.       for _, _name in ipairs(conf.enabled) do
  79.         if name == _name then
  80.           return nil, "Service already enabled"
  81.         end
  82.       end
  83.       conf.enabled[#conf.enabled + 1] = name
  84.       return saveConfig(conf)
  85.     elseif cmd == "disable" then
  86.       conf.enabled = conf.enabled or {}
  87.       for n, _name in ipairs(conf.enabled) do
  88.         if name == _name then
  89.           table.remove(conf.enabled, n)
  90.         end
  91.       end
  92.       return saveConfig(conf)
  93.     else
  94.       what = "Command '" .. cmd .. "' not found in daemon '" .. name .. "'"
  95.     end
  96.   end
  97.   return nil, what
  98. end
  99.  
  100. local function runCommand(name, cmd, ...)
  101.   local conf, reason = loadConfig()
  102.   if not conf then
  103.     return nil, reason
  104.   end
  105.   return rawRunCommand(conf, name, cmd, conf[name], ...)
  106. end
  107.  
  108. local function allRunCommand(cmd, ...)
  109.   local conf, reason = loadConfig()
  110.   if not conf then
  111.     return nil, reason
  112.   end
  113.   local results = {}
  114.   for _, name in ipairs(conf.enabled or {}) do
  115.     results[name] = table.pack(rawRunCommand(conf, name, cmd, conf[name], ...))
  116.   end
  117.   return results
  118. end
  119.  
  120. local args = table.pack(...)
  121.  
  122. if #args == 0 then  
  123.   local results,reason = allRunCommand("start")
  124.   if not results then
  125.     local msg = "rc failed to start:"..tostring(reason)
  126.     io.stderr:write(msg)
  127.     require("event").onError(msg)
  128.     return
  129.   end
  130.   for name, result in pairs(results) do
  131.     local ok, reason = table.unpack(result)
  132.     if not ok then
  133.       io.stderr:write(reason .. "\n")
  134.     end
  135.   end
  136. else
  137.   local result, reason = runCommand(table.unpack(args))
  138.   if not result then
  139.     io.stderr:write(reason .. "\n")
  140.     return 1
  141.   end
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement