From 7e04f26f7807b1dda4db5b378c5deaa360b1fdc8 Mon Sep 17 00:00:00 2001 From: wescoeur Date: Wed, 25 Nov 2015 16:09:53 +0100 Subject: [PATCH] Send email to one address --- .../xo-server-backup-reports/package.json | 3 +- .../xo-server-backup-reports/src/index.js | 151 ++++++++++-------- 2 files changed, 89 insertions(+), 65 deletions(-) diff --git a/packages/xo-server-backup-reports/package.json b/packages/xo-server-backup-reports/package.json index 9d6056e14..f4a864395 100644 --- a/packages/xo-server-backup-reports/package.json +++ b/packages/xo-server-backup-reports/package.json @@ -23,7 +23,8 @@ ], "dependencies": { "babel-runtime": "^5.8.34", - "lodash.foreach": "^3.0.3" + "lodash.foreach": "^3.0.3", + "moment": "^2.10.6" }, "devDependencies": { "babel": "^5.8.34", diff --git a/packages/xo-server-backup-reports/src/index.js b/packages/xo-server-backup-reports/src/index.js index dda17a7dc..d2b62308c 100644 --- a/packages/xo-server-backup-reports/src/index.js +++ b/packages/xo-server-backup-reports/src/index.js @@ -1,70 +1,10 @@ import forEach from 'lodash.foreach' - -function listener (status) { - let nSuccess = 0 - let nCalls = 0 - - forEach(status.calls, call => { - // Ignore call if it's not a Backup or a Snapshot. - if (call.method !== 'vm.rollingBackup' && call.method !== 'vm.rollingSnapshot') { - return - } - - let vmStatus - - if (call.error) { - vmStatus = 'Fail' - } else { - nSuccess++ - vmStatus = 'Success' - } - - nCalls++ - - let vmName - - try { - const vm = this._xo.getObject(call.params.id) - vmName = vm.name_label - } catch (e) { - vmName = 'NoSuchObject, no vm name found' - } - - console.log( - `VM Name: ${vmName} - UUID: ${call.params.id} - Status: ${vmStatus} - Start time: ${call.start} - End time: ${call.end} - Duration: ${call.end - call.start} - ` - ) - }) - - // No backup calls. - if (nCalls === 0) { - return - } - - const globalStatus = nSuccess === nCalls - ? 'Success' - : 'Fail' - - // Global status. - console.log( - `Global status: ${globalStatus} - Start time: ${status.start} - End time: ${status.end} - Duration: ${status.end - status.start} - Successful backed up VM number: ${nSuccess} - Failed backed up VM: ${nCalls - nSuccess} - ` - ) -} +import moment from 'moment' class BackupReportsXoPlugin { constructor (xo) { this._xo = xo + this._report = ::this._wrapper } configure (conf) { @@ -72,11 +12,94 @@ class BackupReportsXoPlugin { } load () { - this._xo.on('job:terminated', listener) + this._xo.on('job:terminated', this._report) } unload () { - this._xo.removeListener('job:terminated', listener) + this._xo.removeListener('job:terminated', this._report) + } + + async _wrapper (status) { + try { + await this._listener(status) + } catch (e) { + console.error('backup report error: ' + e) + } + } + + async _listener (status) { + let nSuccess = 0 + let nCalls = 0 + + const text = [] + + forEach(status.calls, call => { + // Ignore call if it's not a Backup or a Snapshot. + if (call.method !== 'vm.rollingBackup' && call.method !== 'vm.rollingSnapshot') { + return + } + + let vmStatus + + if (call.error) { + vmStatus = 'Fail' + } else { + nSuccess++ + vmStatus = 'Success' + } + + nCalls++ + + let vmName + + try { + const vm = this._xo.getObject(call.params.id) + vmName = vm.name_label + } catch (e) { + vmName = 'Vm name not found' + } + + const start = moment(call.start) + const end = moment(call.end) + const duration = moment.duration(end - start).humanize() + + text.push([`### VM : ${vmName}`, + `UUID: ${call.params.id}`, + `Status: ${vmStatus}`, + `Start time: ${start.toString()}`, + `End time: ${end.toString()}`, + `Duration: ${duration}` + ].join('\n - ')) + }) + + // No backup calls. + if (nCalls === 0) { + return + } + + const globalStatus = nSuccess === nCalls + ? 'Success' + : 'Fail' + + const start = moment(status.start) + const end = moment(status.end) + const duration = moment.duration(end - start).humanize() + + // Global status. + text.unshift([`## Global status: ${globalStatus}`, + `Start time: ${start.toString()}`, + `End time: ${end.toString()}`, + `Duration: ${duration}`, + `Successful backed up VM number: ${nSuccess}`, + `Failed backed up VM: ${nCalls - nSuccess}` + ].join('\n - ')) + + // TODO : Handle errors when `sendEmail` isn't present. (Plugin dependencies) + this._xo.sendEmail({ + to: 'ronan.abhamon@gmail.com', // FIXME + subject: 'Backup Reports (XenOrchestra)', + markdown: text.join('\n') + }).catch(e => console.error('Unable to send email: ', e)) } }