Send email to one address
This commit is contained in:
parent
348c30b61e
commit
7e04f26f78
@ -23,7 +23,8 @@
|
|||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"babel-runtime": "^5.8.34",
|
"babel-runtime": "^5.8.34",
|
||||||
"lodash.foreach": "^3.0.3"
|
"lodash.foreach": "^3.0.3",
|
||||||
|
"moment": "^2.10.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"babel": "^5.8.34",
|
"babel": "^5.8.34",
|
||||||
|
@ -1,70 +1,10 @@
|
|||||||
import forEach from 'lodash.foreach'
|
import forEach from 'lodash.foreach'
|
||||||
|
import moment from 'moment'
|
||||||
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}
|
|
||||||
`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
class BackupReportsXoPlugin {
|
class BackupReportsXoPlugin {
|
||||||
constructor (xo) {
|
constructor (xo) {
|
||||||
this._xo = xo
|
this._xo = xo
|
||||||
|
this._report = ::this._wrapper
|
||||||
}
|
}
|
||||||
|
|
||||||
configure (conf) {
|
configure (conf) {
|
||||||
@ -72,11 +12,94 @@ class BackupReportsXoPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
load () {
|
load () {
|
||||||
this._xo.on('job:terminated', listener)
|
this._xo.on('job:terminated', this._report)
|
||||||
}
|
}
|
||||||
|
|
||||||
unload () {
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user