Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(xo-server/rest-api/dashboard): add vmsProtection information #7964

Merged
merged 6 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- [Netbox] Support version 4.1 [#7966](https://github.com/vatesfr/xen-orchestra/issues/7966) (PR [#8002](https://github.com/vatesfr/xen-orchestra/pull/8002))
- **XO 6**:
- [Dashboard] Display backup issues data (PR [#7974](https://github.com/vatesfr/xen-orchestra/pull/7974))
- [REST API] Add S3 backup repository information in the `/rest/v0/dashboard` endpoint (PR [#7978](https://github.com/vatesfr/xen-orchestra/pull/7978))
- [REST API] Add S3 backup repository and VMs protection information in the `/rest/v0/dashboard` endpoint (PRs [#7978](https://github.com/vatesfr/xen-orchestra/pull/7978), [#7964](https://github.com/vatesfr/xen-orchestra/pull/7964))
- [Backups] Display more informations in the _Notes_ column of the backup page (PR [#7977](https://github.com/vatesfr/xen-orchestra/pull/7977))

### Bug fixes
Expand Down Expand Up @@ -51,7 +51,7 @@
- @xen-orchestra/xapi patch
- xen-api patch
- xo-cli minor
- xo-server patch
- xo-server minor
- xo-server-netbox minor
- xo-web minor

Expand Down
7 changes: 7 additions & 0 deletions packages/xo-server/src/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,10 @@ export const unboxIdsFromPattern = pattern => {
// -------------------------------------------------------------------

export const isSrWritable = sr => sr !== undefined && sr.content_type !== 'iso' && sr.size > 0

// -------------------------------------------------------------------

export const isReplicaVm = vm => 'start' in vm.blockedOperations && vm.other['xo:backup:job'] !== undefined

// -------------------------------------------------------------------
export const vmContainsNoBakTag = vm => vm.tags.some(t => t.split('=', 1)[0] === 'xo:no-bak')
102 changes: 81 additions & 21 deletions packages/xo-server/src/xo-mixins/rest-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { asyncEach } from '@vates/async-each'
import { createGzip } from 'node:zlib'
import { defer } from 'golike-defer'
import { every } from '@vates/predicates'
import { extractIdsFromSimplePattern } from '@xen-orchestra/backups/extractIdsFromSimplePattern.mjs'
import { ifDef } from '@xen-orchestra/defined'
import { featureUnauthorized, invalidCredentials, noSuchObject } from 'xo-common/api-errors.js'
import { pipeline } from 'node:stream/promises'
Expand All @@ -19,8 +20,9 @@ import * as CM from 'complex-matcher'
import { VDI_FORMAT_RAW, VDI_FORMAT_VHD } from '@xen-orchestra/xapi'
import { parse } from 'xo-remote-parser'

import { getUserPublicProperties, isSrWritable } from '../utils.mjs'
import { getUserPublicProperties, isReplicaVm, isSrWritable, vmContainsNoBakTag } from '../utils.mjs'
import { compileXoJsonSchema } from './_xoJsonSchema.mjs'
import { createPredicate } from 'value-matcher'

// E.g: 'value: 0.6\nconfig:\n<variable>\n<name value="cpu_usage"/>\n<alarm_trigger_level value="0.4"/>\n<alarm_trigger_period value ="60"/>\n</variable>';
const ALARM_BODY_REGEX = /^value:\s*(\d+(?:\.\d+)?)\s*config:\s*<variable>\s*<name value="(.*?)"/
Expand Down Expand Up @@ -176,6 +178,9 @@ async function _getDashboardStats(app) {
const hosts = []
const writableSrs = []
const alarms = []
const nonReplicaVms = []
const vmIdsProtected = new Set()
const vmIdsUnprotected = new Set()

for (const obj of app.objects.values()) {
if (obj.type === 'host') {
Expand All @@ -195,6 +200,10 @@ async function _getDashboardStats(app) {
if (obj.type === 'message' && obj.name === 'ALARM') {
alarms.push(obj)
}

if (obj.type === 'VM' && !isReplicaVm(obj)) {
nonReplicaVms.push(obj)
}
}

dashboard.nPools = poolIds.size
Expand Down Expand Up @@ -300,6 +309,45 @@ async function _getDashboardStats(app) {
return false
}

function _extractVmIdsFromBackupJob(job) {
let vmIds
try {
vmIds = extractIdsFromSimplePattern(job.vms).filter(vmId => app.hasObject(vmId, 'VM'))
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
} catch (_) {
const predicate = createPredicate(job.vms)
vmIds = nonReplicaVms.filter(predicate).map(vm => vm.id)
}
return vmIds
}

function _updateVmProtection(vmId, isProtected) {
if (vmIdsProtected.has(vmId)) {
return
}

const vm = app.getObject(vmId, 'VM')
if (vmContainsNoBakTag(vm)) {
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
return
}

if (isProtected) {
vmIdsProtected.add(vmId)
vmIdsUnprotected.delete(vmId)
} else {
vmIdsUnprotected.add(vmId)
}
}

function _processVmsProtection(job, isProtected) {
if (job.type !== 'backup') {
return
}

_extractVmIdsFromBackupJob(job).forEach(vmId => {
_updateVmProtection(vmId, isProtected)
})
}

try {
const [logs, jobs] = await Promise.all([
app.getBackupNgLogsSorted({
Expand All @@ -319,42 +367,49 @@ async function _getDashboardStats(app) {

for (const job of jobs) {
if (!(await _jobHasAtLeastOneScheduleEnabled(job))) {
_processVmsProtection(job, false)
disabledJobs++
continue
}

const jobLogs = logsByJob[job.id]?.slice(-3)
// Get only the last 3 runs
const jobLogs = logsByJob[job.id]?.slice(-3).reverse()
fbeauchamp marked this conversation as resolved.
Show resolved Hide resolved
MathieuRA marked this conversation as resolved.
Show resolved Hide resolved
if (jobLogs === undefined || jobLogs.length === 0) {
_processVmsProtection(job, false)
continue
}

for (let i = 0; i < jobLogs.length; i++) {
const { status } = jobLogs[i]
const isLastElement = i === jobLogs.length - 1
if (job.type === 'backup') {
const lastJobLog = jobLogs[0]
const { tasks, status } = lastJobLog

if (status !== 'success') {
if (status === 'failure' || status === 'interrupted') {
failedJobs++
} else if (status === 'skipped') {
skippedJobs++
}

backupJobIssues.push({
logs: jobLogs.map(log => log.status),
name: job.name,
type: job.type,
uuid: job.id,
if (tasks === undefined) {
_processVmsProtection(job, status === 'success')
} else {
tasks.forEach(task => {
_updateVmProtection(task.data.id, task.status === 'success')
})

break
}
}

if (isLastElement) {
successfulJobs++
const failedLog = jobLogs.find(log => log.status !== 'success')
if (failedLog !== undefined) {
const { status } = failedLog
if (status === 'failure' || status === 'interrupted') {
failedJobs++
} else if (status === 'skipped') {
skippedJobs++
}
backupJobIssues.push({ logs: jobLogs.map(log => log.status), name: job.name, type: job.type, uuid: job.id })
} else {
successfulJobs++
}
}

const nVmsProtected = vmIdsProtected.size
const nVmsUnprotected = vmIdsUnprotected.size
const nVmsNotInJob = nonReplicaVms.length - (nVmsProtected + nVmsUnprotected)

dashboard.backups = {
jobs: {
disabled: disabledJobs,
Expand All @@ -364,6 +419,11 @@ async function _getDashboardStats(app) {
total: jobs.length,
},
issues: backupJobIssues,
vmsProtection: {
protected: nVmsProtected,
unprotected: nVmsUnprotected,
notInJob: nVmsNotInJob,
},
}
} catch (error) {
console.error(error)
Expand Down
8 changes: 8 additions & 0 deletions packages/xo-server/src/xo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ export default class Xo extends EventEmitter {
return obj
}

hasObject(key, type) {
try {
return this.getObject(key, type) !== undefined
} catch (_) {
return false
}
}

getObjects({ filter, limit } = {}) {
const { all } = this._objects

Expand Down
Loading