summaryrefslogtreecommitdiffstats
path: root/openntpd-controller.lua
blob: b7d8c8e899b982262f92d8497b548ec0328c20e5 (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
-- Controller module
module (..., package.seeall)

-- Cause an http redirect to our "read" action
-- We use the self.conf table because it already has prefix,controller,etc
-- The redir code is defined in the application error handler (acf-controller)
require("posix")
local list_redir = function (self)
	self.conf.action = "read"
	self.conf.type = "redir"
	error (self.conf)
end

mvc={}
mvc.on_load = function(self, parent)
	if (self.worker[self.conf.action] == nil ) or ( self.conf.action == "init" ) then
		self.worker[self.conf.action] = list_redir(self)
	end
end


config = function (self)
	local cmd = self.clientdata.cmd
	local errors = {}
	local modify_opts = nil
	local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller
	local hosts_cmd = tostring(self.clientdata.hosts_cmd)
	local hosts_cmd = string.lower(hosts_cmd)
	local listen_cmd = tostring(self.clientdata.listen_cmd)
	local listen_cmd = string.lower(listen_cmd)
	local settings_cmd = tostring(self.clientdata.settings_cmd)
	local settings_cmd = string.lower(settings_cmd)

	-- SECTION WHERE YOU SAVE NEW SETTINGS
	if ( hosts_cmd == "delete") then
		if not (self.clientdata.hosts_list) then
			errors["hosts_list"]="You need to choose something in the list to delete"
		end
		if (self.clientdata.hosts_list) then
			modify_opts = self.model:modify_config(hosts_cmd, nil, self.clientdata.hosts_list)
		end
	elseif (hosts_cmd == "add") then
		if (self.clientdata.hosts_add == "") then
			errors["hosts_add"]="You need to enter a server/IP"
		end
		if (self.clientdata.hosts_type == nil) then
			errors["hosts_type"]="You need to choose type of server"
		end
		if (self.clientdata.hosts_add ~= "") and (self.clientdata.hosts_type ~= nil) then
			modify_opts = self.model:modify_config(hosts_cmd, nil, self.clientdata.hosts_type .. " " .. self.clientdata.hosts_add)
		end
	elseif (listen_cmd == "add") then
		if (self.clientdata.listen_add == "") then
			errors["listen_add"]="You need to enter what you want to listen at"
		end
		if (self.clientdata.listen_add ~= "") then
			modify_opts = self.model:modify_config(listen_cmd, nil, "listen on " .. self.clientdata.listen_add)
		end
	elseif (listen_cmd == "delete") then
		if not (self.clientdata.listen_list) then
			errors["listen_list"]="You need to choose something in the list to delete"
		end
		if (self.clientdata.listen_list) then
			modify_opts = self.model:modify_config(listen_cmd, nil, self.clientdata.listen_list)
		end
	elseif (settings_cmd == "save") then
		if (self.clientdata.settings_startup) then
			modify_opts = self.model:modify_opts("add", "/etc/conf.d/ntpd", "NTPD_OPTS", "-s")
		else
			modify_opts = self.model:modify_opts("remove", "/etc/conf.d/ntpd", "NTPD_OPTS", "-s")
		end
	end

	DEBUGMODEL = modify_opts		-- <<< DEBUG INFO >>>
	DEBUGCLIENTDATA = self.clientdata	-- <<< DEBUG INFO >>>
	if ( cmd ~= nil ) then
		local startstop = self.model:startstop_service( cmd )
		posix.sleep(1)	-- Wait for the process to start|stop
		return ( {status = self.model:get(), 
			startstop = "", 
			modify_opts=modify_opts, 
			url = url } )
	else
		return ( {status = self.model:get(), startstop = "", 
			errors = errors, 
			modify_opts=modify_opts, 
			url = url, 
			DEBUGMODEL=DEBUGMODEL,
			DEBUGCLIENTDATA=DEBUGCLIENTDATA } )
	end
end

logfile = function (self)
	return ( {logfile = self.model:get_logfile(), url = url } )
end

status = function (self)
	local cmd = self.clientdata.cmd
	local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller
	return ( {status = self.model:get(), url = url } )
end

expert = function (self)
	local file = self.clientdata.modifications or ""
	local cmd = self.clientdata.cmd
	local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller

	if ( file ~= "") then
		local me = ( {file = self.model:update_filecontent(file), url = url } )
		return ( {startstop = "", 
			status = self.model:get(), 
			file = self.model:get_filecontent(), 
			url = url } )
	else
		if ( cmd ~= nil ) then
			local startstop = self.model:startstop_service( cmd )
			posix.sleep(1)	-- Wait for the process to start|stop
			return ( {startstop = startstop, 
				status = self.model:get(), 
				filec = self.model:get_filecontent(), 
				url = url } )
		else
			return ( {startstop = "", 
				status = self.model:get(), 
				file = self.model:get_filecontent(), 
				url = url } )
		end
	end
end