summaryrefslogtreecommitdiffstats
path: root/acf2
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-10-15 21:44:52 +0300
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-10-15 21:45:43 +0300
commit7021795e1f73b7e66ffefe7956cf59290c6cdf05 (patch)
tree0cf7fa2e5dd8278758952c2faf5422887834239a /acf2
parent36c41b8bf5fef1ecd77c6bad1f882eccff959db4 (diff)
downloadaconf-7021795e1f73b7e66ffefe7956cf59290c6cdf05.tar.bz2
aconf-7021795e1f73b7e66ffefe7956cf59290c6cdf05.tar.xz
enabled & status fields for service models
new PM back-end for OpenRC reload configuration only when service running
Diffstat (limited to 'acf2')
-rw-r--r--acf2/model/service.lua28
-rw-r--r--acf2/persistence/backends/service.lua32
-rw-r--r--acf2/persistence/init.lua18
3 files changed, 61 insertions, 17 deletions
diff --git a/acf2/model/service.lua b/acf2/model/service.lua
index 2ed84e8..4db634e 100644
--- a/acf2/model/service.lua
+++ b/acf2/model/service.lua
@@ -3,26 +3,34 @@ Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]
-local model = require('acf2.model.model')
+local fld = require('acf2.model.field')
+local new = require('acf2.model.model').new
local super = require('acf2.object').super
+local pth = require('acf2.path')
local DataStore = require('acf2.persistence')
return function(name)
- local res = model.new()
+ local res = new()
- local function afunc(action)
- return function() os.execute('rc-service '..name..' '..action) end
+ local addr = pth.join('/service', name)
+ local eaddr = pth.join(addr, 'enabled')
+ res.enabled = fld.Boolean{addr=eaddr, required=true}
+ res.status = fld.String{addr=pth.join(addr, 'status')}
+
+ local function is_enabled() return DataStore:get(eaddr) end
+ local enabled
+ local function pre() enabled = is_enabled() end
+ local function post()
+ if enabled and is_enabled() then
+ os.execute('rc-service '..name..' reload')
+ end
end
- local reload = afunc('reload')
function res:init(context)
- DataStore:trigger(context.addr, reload)
+ DataStore:trigger('pre', context.addr, pre)
+ DataStore:trigger('post', context.addr, post)
super(self, res):init(context)
end
- for _, action in ipairs{'start', 'stop', 'restart'} do
- res[action] = model.Action{func=afunc(action)}
- end
-
return res
end
diff --git a/acf2/persistence/backends/service.lua b/acf2/persistence/backends/service.lua
new file mode 100644
index 0000000..2163184
--- /dev/null
+++ b/acf2/persistence/backends/service.lua
@@ -0,0 +1,32 @@
+--[[
+Copyright (c) 2012-2013 Kaarle Ritvanen
+See LICENSE file for license details
+--]]
+
+local rc = require('rc')
+local stringy = require('stringy')
+
+
+local backend = require('acf2.object').class()
+
+function backend:get(path, top)
+ if #path == 1 then return {'enabled', 'status'} end
+ assert(#path == 2)
+ local status = rc.service_status(path[1])
+ if path[2] == 'status' then return status end
+ if path[2] == 'enabled' then return stringy.startswith(status, 'start') end
+end
+
+function backend:set(mods)
+ for _, mod in ipairs(mods) do
+ local path, value = unpack(mod)
+ assert(#path == 2 and path[2] == 'enabled')
+
+ local name = path[1]
+ if value then rc.service_add(name)
+ else rc.service_delete(name) end
+ os.execute('rc-service '..name..' '..(value and 'start' or 'stop'))
+ end
+end
+
+return backend
diff --git a/acf2/persistence/init.lua b/acf2/persistence/init.lua
index 1cdea75..74ca447 100644
--- a/acf2/persistence/init.lua
+++ b/acf2/persistence/init.lua
@@ -27,11 +27,11 @@ function DataStore:init()
function(m) return m() end,
loadmods('persistence/backends')
)
- self.triggers = {}
+ self.triggers = {pre={}, post={}}
end
-function DataStore:trigger(path, func)
- local funcs = setdefault(self.triggers, path, {})
+function DataStore:trigger(phase, path, func)
+ local funcs = setdefault(self.triggers[phase], path, {})
if not contains(funcs, func) then table.insert(funcs, func) end
end
@@ -98,11 +98,15 @@ function DataStore:_set_multiple(mods)
table.insert(setdefault(bms, backend, {}), {comps, value})
end
- for backend, bm in pairs(bms) do backend:set(bm) end
-
- for path, _ in pairs(trigger) do
- for _, func in ipairs(self.triggers[path] or {}) do func() end
+ local function exec_triggers(phase)
+ for path, _ in pairs(trigger) do
+ for _, func in ipairs(self.triggers[phase][path] or {}) do func() end
+ end
end
+
+ exec_triggers('pre')
+ for backend, bm in pairs(bms) do backend:set(bm) end
+ exec_triggers('post')
end