-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.lua
410 lines (372 loc) · 12.4 KB
/
setup.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
--[[
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 native = require('uv_native')
local os = require('os')
local Object = require('core').Object
local fmt = require('string').format
local fs = require('fs')
local timer = require('timer')
local JSON = require('json')
local table = require('table')
local async = require('async')
local ask = require('/util/prompt').ask
local errors = require('errors')
local constants = require('/util/constants')
local sigarCtx = require('sigar').ctx
local maas = require('rackspace-monitoring')
local Setup = Object:extend()
function Setup:initialize(argv, configFile, agent)
self._configFile = configFile
self._agent = agent
self._receivedPromotion = false
self._username = argv.U
self._apikey = argv.K
self._agent:on('promote', function()
self._receivedPromotion = true
end)
self._addresses = {}
-- build a "set" (table keyed by address) of local IP addresses
local netifs = sigarCtx:netifs()
for i=1,#netifs do
local info = netifs[i]:info()
if info['address'] then
self._addresses[info['address']] = true
end
if info['addres6'] then
self._addresses[info['address6']] = true
end
end
end
function Setup:saveTest(callback)
fs.open(self._configFile, 'a', "0644", function(err)
if err then
process.stdout:write(fmt('Error: cannot open config file: %s\n', self._configFile))
callback(err)
return
end
callback()
end)
end
function Setup:save(token, hostname, callback)
process.stdout:write(fmt('Writing token to %s: ', self._configFile))
local data = fmt('monitoring_token %s\n', token)
data = data .. fmt('monitoring_id %s\n', hostname)
--[[
1. We are using an environment variable because we thought adding special hidden
command line arguments logic may make it too complex.
2. this feature will be ran by such a small set of people who are very
technically minded anyways so setting an environment variable is OK.
]]--
fs.writeFile(self._configFile, data, function(err)
if err then
process.stdout:write('failed writing config filen\n')
callback(err)
return
end
process.stdout:write('done\n')
callback()
end)
end
function Setup:_out(msg)
process.stdout:write(msg .. '\n')
end
function Setup:_getOsStartString()
return 'service rackspace-monitoring-agent start'
end
function Setup:_isLocalEntity(entity)
if entity.label == os.hostname() then
return true
end
if entity.ip_addresses then
for k, address in pairs(entity.ip_addresses) do
-- TODO: we should really translate all v6 addresses to a standard form
-- for this comparison, currently there is a good chance of us missing a
-- v6 match
if self._addresses[address] then
return true
end
end
end
return false
end
function Setup:_buildLocalEntity(agentId)
local addresses = {}
local netifs = sigarCtx:netifs()
for i=1,#netifs do
local info = netifs[i]:info()
if info.type ~= 'Local Loopback' then
if info['address'] then
addresses[info['name'] .. '_v4'] = info['address']
end
if info['addres6'] then
addresses[info['name'] .. '_v6'] = info['address6']
end
end
end
return {
label = agentId,
agent_id = agentId,
ip_addresses = addresses
}
end
function Setup:run(callback)
local username, token, hostname
local agentToken, client
hostname = os.hostname()
self:_out('')
self:_out('Setup Settings:')
self:_out(fmt(' Agent ID: %s', hostname))
self:_out(fmt(' Config File: %s', self._configFile))
self:_out(fmt(' State Directory: %s', self._agent._stateDirectory))
self:_out('')
function createToken(callback)
client.agent_tokens.create({ ['label'] = hostname }, function(err, token)
if err then
callback(err)
return
end
self._agent:setConfig({ ['monitoring_token'] = token })
self:save(token, hostname, callback)
end)
end
async.waterfall({
function(callback)
self:saveTest(callback)
end,
function(callback)
if (self._username == nil and self._apikey ~= nil)
or (self._username ~= nil and self._apikey == nil) then
callback(errors.UserResponseError:new('Username and password/apikey must be provided together.'))
else
callback()
end
end,
function(callback)
if self._username == nil then
ask('Username:', callback)
else
callback(nil, self._username)
end
end,
function(_username, callback)
if self._apikey == nil then
ask('API Key or Password:', function(err, _token)
username = _username
token = _token
callback(err, username, token)
end)
else
callback(nil, self._username, self._apikey)
end
end,
-- fetch all tokens
function(username, token, callback)
client = maas.Client:new(username, token)
client.agent_tokens.get(callback)
end,
-- is there a token for the host
function(tokens, callback)
for i, v in ipairs(tokens.values) do
if v.label and v.label == hostname then
agentToken = v.token
break
end
end
callback(nil, agentToken, tokens)
end,
function(agentToken, tokens, callback)
-- save the token if we found it
if agentToken then
self:_out('')
self:_out(fmt('Found existing Agent Token for %s', hostname))
self:_out('')
self._agent:setConfig({ ['monitoring_token'] = agentToken })
self:save(agentToken, hostname, callback)
-- display a list of tokens
elseif self._username and self._apikey then
createToken(callback)
elseif #tokens.values > 0 then
self:_out('')
self:_out('The Monitoring Agent uses an authentication token to communicate with the Cloud Monitoring Service.')
self:_out('')
self:_out('Please select from an existing token, or create a new token:')
for i, v in ipairs(tokens.values) do
if v.label then
self:_out(fmt(' %i. %s - %s', i, v.label, v.id))
else
self:_out(fmt(' %i. %s', i, v.id))
end
end
self:_out(fmt(' %i. Create New Token', #tokens.values + 1))
self:_out('')
ask('Select Option:', function(err, index)
if err then
callback(err)
return
end
self:_out('')
local validatedIndex = tonumber(index) -- validate response
if validatedIndex >= 1 and validatedIndex <= #tokens.values then
self._agent:setConfig({ ['monitoring_token'] = tokens.values[validatedIndex].id })
self:save(tokens.values[validatedIndex].id, hostname, callback)
elseif validatedIndex == (#tokens.values + 1) then
createToken(callback)
else
callback(errors.UserResponseError:new('User input is not valid. Expected integer.'))
end
end)
-- create a token and save it
else
createToken(callback)
end
end,
-- test connectivity
function(callback)
self:_out('')
self:_out('Testing Agent connectivity to Cloud Monitoring Service...')
self:_out('')
async.series({
function(callback)
self._agent:loadEndpoints(callback)
end,
function(callback)
self._agent:_preConfig(callback)
end,
function(callback)
self._agent:connect()
callback()
end,
function(callback)
function timeout()
callback(errors.AuthTimeoutError:new('Authentication timed out.'))
end
local authTimer = timer.setTimeout(constants.SETUP_AUTH_TIMEOUT, timeout)
function testAuth()
timer.clearTimer(authTimer)
if self._receivedPromotion then
self:_out('')
self:_out('Agent successfuly connected!')
callback()
else
authTimer = timer.setTimeout(constants.SETUP_AUTH_TIMEOUT, timeout)
timer.setTimeout(constants.SETUP_AUTH_CHECK_INTERVAL, testAuth)
end
end
timer.setTimeout(constants.SETUP_AUTH_CHECK_INTERVAL, testAuth)
end
}, callback)
end,
-- Bind to an entity
function(connections, callback)
self:_out('')
self:_out('In order to execute checks, the agent must be associated with a Cloud Monitoring Entity.')
self:_out('')
async.waterfall({
function(callback)
client.entities.list(callback)
end,
function(entities, callback)
local addresses = {}
local localEntities = {}
function displayEntities()
for i, entity in ipairs(localEntities) do
if entity.label then
self:_out(fmt(' %i. %s - %s', i, entity.label, entity.id))
else
self:_out(fmt(' %i. %s', i, entity.id))
end
if entity.ip_addresses then
for k, address in pairs(entity.ip_addresses) do
self:_out(fmt(' %s: %s', k, address))
end
end
end
end
for i, entity in ipairs(entities.values) do
if (entity.agent_id == hostname) then
self:_out(fmt('Agent already associated Entity with id=%s and label=%s', entity.id, entity.label))
callback(nil)
return
end
if self:_isLocalEntity(entity) then
table.insert(localEntities, entity)
end
end
function entitySelection()
self:_out('Please select the Entity that corresponds to this server:')
displayEntities()
self:_out(fmt(' %i. Create an new Entity for this server (not supported by Rackspace Cloud Control Panel)', #localEntities + 1))
self:_out(fmt(' %i. Do not associate with an Entity', #localEntities + 2))
self:_out('')
ask('Select Option (e.g., 1, 2):', function(err, index)
if err then
callback(err)
return
end
local validatedIndex = tonumber(index)
if validatedIndex == #localEntities + 1 then
client.entities.create(self:_buildLocalEntity(hostname), function(err, entity)
if err then
callback(err)
return
end
self:_out('')
self:_out(fmt('New Entity Created: %s', entity))
callback(nil, entity)
end)
elseif validatedIndex == #localEntities + 2 then
callback()
elseif validatedIndex >= 1 and validatedIndex <= #localEntities then
client.entities.update(localEntities[validatedIndex].id, { agent_id = hostname }, callback)
else
self:_out('')
self:_out('Invalid selection')
entitySelection()
end
end)
end
entitySelection()
end
}, callback)
end
}, function(err)
if err then
local msg = nil
if type(err) == 'string' then
msg = err
elseif err.message then
msg = err.message
elseif err.data then
pcall(function() msg = JSON.parse(err.data).message end)
end
if not msg then
msg = JSON.stringify(err)
end
process.stdout:write(fmt('Error: %s\n', msg))
else
-- TODO: detect Platform, iniit.d system, etc
self:_out('')
self:_out('Your Agent configuration is now complete.')
self:_out('')
self:_out('To start the Agent on your server, now run:')
self:_out('')
self:_out(fmt(' %s', self:_getOsStartString()))
self:_out('')
self:_out('')
end
process.exit(0)
end)
end
local exports = {}
exports.Setup = Setup
return exports