summaryrefslogtreecommitdiffstats
path: root/acf2/model/service.lua
blob: 4db634e1893a6e2fa63265a8bbb29f94a9e23866 (plain)
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
--[[
Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]

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 = new()

	  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

	  function res:init(context)
	     DataStore:trigger('pre', context.addr, pre)
	     DataStore:trigger('post', context.addr, post)
	     super(self, res):init(context)
	  end

	  return res
       end