summaryrefslogtreecommitdiffstats
path: root/acf2/model/service.lua
blob: 8d7d6f84d66f2558b7574d336cdeac86db0c8ba8 (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-2014 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 store = 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'), 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