summaryrefslogtreecommitdiffstats
path: root/cron-model.lua
blob: ad4ba1b31417d46fec1c4edefcc476db6f12f9e5 (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
module (..., package.seeall)

require("modelfunctions")
require("format")
require("fs")
require("validator")

local configfile = "/etc/crontabs/root"
local processname = "cron"
local packagename = "busybox"
local baseurl = "/etc/periodic/"

local periods
-- ################################################################################
-- LOCAL FUNCTIONS

local function list_periods()
	if not periods then
		periods = {}
		local file = fs.read_file(configfile) or ""
		for dir in string.gmatch(file, "/etc/periodic/(%S+)") do
			periods[#periods+1] = dir
		end
--[[	local reverseperiods = {}
	for i,per in ipairs(periods) do reverseperiods[per] = i end
	if fs.is_dir(baseurl) then
		for dir in posix.files(baseurl) do
			if fs.is_dir(baseurl .. dir) and (dir ~= ".") and (dir ~= "..") and not reverseperiods[dir] then
				periods[#periods+1] = dir
			end
		end
	end--]]
	end
	return periods
end

local function validate_filename(name)
	local success = false
	for i,per in ipairs(list_periods()) do
		if validator.is_valid_filename(name, baseurl..per) then
			success = true
			break
		end
	end
	return success
end

-- ################################################################################
-- PUBLIC FUNCTIONS

function startstop_service(action)
	return modelfunctions.startstop_service(processname, action)
end

function getstatus()
	return modelfunctions.getstatus(processname, packagename, "Cron Status")
end

function listjobs()
	local jobs = {}
	for i,period in ipairs(list_periods()) do
		local temp = {period=period, jobs={}}
		for file in fs.find("[^.]+", baseurl..period) do
			table.insert(temp.jobs, file)
		end
		jobs[#jobs+1] = temp
	end
	return cfe({ type="structure", value=jobs, label="Cron Jobs" })
end

function read_job(filename)
	return modelfunctions.getfiledetails(filename, validate_filename)
end

function update_job(filedetails)
	return modelfunctions.setfiledetails(filedetails, validate_filename)
end

function delete_job(filename)
	local retval = cfe({ label="Delete Job Result", errtxt="Invalid file" })
	if validate_filename(filename) then
		os.remove(filename)
		retval.value = "Job File Deleted"
		retval.errtxt = nil
	end
	return retval
end

function create_new_job()
	local newjob = {}
	newjob.name = cfe({ label="Name" })
	newjob.period = cfe({ type="select", label="Period", option=list_periods() })
	return cfe({ type="group", value=newjob, label="Create New Job" })
end

function create_job(newjob)
	local success = modelfunctions.validateselect(newjob.value.period)

	if newjob.value.name.value == "" then
		newjob.value.name.errtxt = "Missing File Name"
		success = false
	elseif string.find(newjob.value.name.value, "[^%w_-]") then
		newjob.value.name.errtxt = "Invalid File Name"
		success = false
	elseif posix.stat(baseurl..newjob.value.period.value.."/"..newjob.value.name.value) then
		newjob.value.name.errtxt = "File already exists"
		success = false
	end

	if success then
		fs.create_file(baseurl..newjob.value.period.value.."/"..newjob.value.name.value)
	else
		newjob.errtxt = "Failed to create new job"
	end

	return newjob
end

function read_configfile()
	-- FIXME validate
 	return modelfunctions.getfiledetails(configfile)
end

function update_configfile(filedetails)
	-- FIXME validate
	return modelfunctions.setfiledetails(filedetails, {configfile})
end