Send email to one address

This commit is contained in:
wescoeur 2015-11-25 16:09:53 +01:00
parent 348c30b61e
commit 7e04f26f78
2 changed files with 89 additions and 65 deletions

View File

@ -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",

View File

@ -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))
}
}