summaryrefslogtreecommitdiffstats
path: root/aconf/model/service.lua
blob: a7c02b0f3c357d74e896b03db8eb3c05db95997e (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
37
38
--[[
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 address = require('aconf.path.address')
local store = require('aconf.persistence')

return function(name)
	  local res = new()

	  local addr = address.join('/service', name)
	  local eaddr = address.join(addr, 'enabled')
	  res.enabled = fld.Boolean{addr=eaddr, required=true}
	  res.status = fld.String{
	     addr=address.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