summaryrefslogtreecommitdiffstats
path: root/aconf/model/service.lua
blob: 2d129fa9b07b878efb426fa04ce92e8e730f9564 (plain) (blame)
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-2015 Kaarle Ritvanen
See LICENSE file for license details
--]]

local fld = require('aconf.model.field')
local new = require('aconf.model.model').new
local super = require('aconf.object').super
local pth = require('aconf.path')
local store = require('aconf.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'), editable=false}

	  local function is_enabled() return store: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)
	     store:trigger('pre', context.addr, pre)
	     store:trigger('post', context.addr, post)
	     super(self, res):init(context)
	  end

	  return res
       end