-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_runner.lua
116 lines (97 loc) · 2.65 KB
/
check_runner.lua
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
--[[
Copyright 2012 Rackspace
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
local async = require('async')
local Emitter = require('core').Emitter
local JSON = require('json')
local checks = require('/check')
local exports = {}
local CheckRunner = Emitter:extend()
function CheckRunner:initialize(checkType)
self._checkType = checkType
self._cr = nil
self._details = nil
end
function CheckRunner:getDetails(callback)
local ENV_PREFIX = 'RAX_'
local details = {}
self._id = process.env[ENV_PREFIX .. 'CHECK_ID']
self._period = process.env[ENV_PREFIX .. 'CHECK_PERIOD']
if self._period == nil then
self._period = 30
else
self._period = tonumber(self._period)
end
for k,v in pairs(process.env) do
local found, offset = k:find('^' .. ENV_PREFIX ..'DETAILS_')
if found then
local nk = k:sub(offset + 1)
details[nk:lower()] = v
end
end
self._details = details
callback(nil)
end
function CheckRunner:run(callback)
local checkParams = {
period = self._period,
id = self._id,
type = self._checkType,
details = self._details,
}
local check = checks.create(checkParams)
check:_runCheckInChild(function(cr)
self._cr = cr
callback(nil)
end)
end
function CheckRunner:reportError(err, callback)
local out = ''
if self._cr and self._cr:getState() ~= nil then
-- Hrm... we have an error, but check already failed, fall through with the 'upper' error?
out = self._cr:serializeAsPluginOutput()
else
out = out + 'state unavailable\n'
out = out + 'status err ' .. tostring(err) .. '\n'
end
process.stdout:write(out)
callback()
end
function CheckRunner:report(callback)
local out = self._cr:serializeAsPluginOutput()
process.stdout:write(out)
callback()
end
exports.run = function(argv)
local checkType = argv.x
local cr = CheckRunner:new(checkType)
async.series({
function(callback)
cr:getDetails(callback)
end,
function(callback)
cr:run(callback)
end,
},
function (err)
if err then
cr:reportError(err, function ()
process.exit(1)
end)
else
cr:report(function ()
process.exit(0)
end)
end
end)
end
return exports