summaryrefslogtreecommitdiffstats
path: root/lib/service_controller.lua
blob: 09595a4bc7bc53badb4ec09b4e28226ddb7a6159 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
local function build_status(model, name)
    return {
	type = "form",
	method = "post",
	action = cf.uri .. "/chrun",
	value = {
	    {
		type = "formtext",
		name = "status",
		label = "Status: <B>" .. name .. "</B>",
		value = model.status(name)
	    },
	    {
		type = "hidden",
		name = "service",
		value = name
	    },
	    {
		type = "submit",
		name = "cmd",
		label = "Running controls",
		value = { "Start", "Stop", "Restart" }
	    },
	}
    }
end

local function build_note(model, name)
    return {
	type = "form",
	method = "post",
	action = cf.uri .. "/note_set",
	value = {
	    {
		type = "textarea",
		name = "note",
		label = "Notes",
		cols = 80,
		rows = 8,
		value = model.get_note(name)
	    },
	    {
		type = "submit",
		label = "Command",
		value = "Save"
	    }
	}
    }
end

local function mkerr(str)
    return {
	{
	    type = "formtext",
	    class = "error",
	    value = str
	}
    }
end

local function mkresult(str)
    return {
	{
	    type = "formtext",
	    value = str
	}
    }
end

local function chrun(model, name, ucmd)
    local cmd = string.lower(ucmd)
    --TODO chect that name is in get_service_names()
    if cmd ~= "start" and cmd ~= "stop" and cmd ~= "restart" then
	return mkerr("unknown command")
    end
    return mkresult(model.initd(name, cmd))
end

local function build_log_file(model, name)
    return {
	type = "group",
	label = name,
	text = "FILE " .. model.get_log_names()[name].path,
	value = {
	    {
		type = "log",
		lines = model.get_log_producer(name)
	    }
	}
    }
end

local function build_edit_file(model, name)
    return {
	type = "form",
	method = "post",
	action = cf.uri .. "/cfg_set",
	value = {
	    {
		type = "textarea",
		name = "cfg",
		label = name,
		cols = 80,
		rows = 12,
		value = model.get_cfg(name)
	    },
	    {
		type = "hidden",
		name = "name",
		value = name
	    },
	    {
		type = "submit",
		label = "Command",
		value = "Save"
	    }
	}
    }

end

local function build_cfg(model)
    local ret = {}
    for name, whatever in pairs(model.get_cfg_names()) do
	table.insert(ret, build_edit_file(model, name))
    end
    return ret
end

local function set_cfg(model)
    for name, whatever in pairs(model.get_cfg_names()) do
	if name == FORM.name then
	    model.set_cfg(name, FORM.cfg)
	    return
	end
    end 
    return mkerr("unknown config")
end

local function build_log(model)
    local ret = {}
    for name, whatever in pairs(model.get_log_names()) do
	table.insert(ret, build_log_file(model, name))
    end
    return ret 
end

local function build_service(model)
    local ret = {}
    for name, whatever in pairs(model.get_service_names()) do
	table.insert(ret, build_status(model, name))
    end
    table.insert(ret, build_note(model))
    return ret
end

function create_service_controller()

    local me = {}

    me.service = build_service
    me.default = me.service
    me.cfg = build_cfg
    me.log = build_log

    function me.chrun(model)
	return chrun(model, FORM.service, FORM.cmd)
    end

    function me.note_set(model)
	local ret = model.set_note(FORM.note)
	if ret then
	    return ret
	end
	return me.service(model)
    end


    function me.cfg_set(model)
	set_cfg(model)
	return me.cfg(model)
    end

    return me

end

-- /* vim: set filetype=lua shiftwidth=4: */