Skip to content

Commit

Permalink
fix(xo-server): register mirror backup executor
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeauchamp committed May 29, 2023
1 parent 7a247fd commit 04f049c
Showing 1 changed file with 67 additions and 62 deletions.
129 changes: 67 additions & 62 deletions packages/xo-server/src/xo-mixins/backups-ng/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,74 +75,77 @@ export default class BackupNg {
const backupsConfig = app.config.get('backups')

let job = job_
let vmIds

if (job.type === 'backup') {
const vmsPattern = job.vms

// Make sure we are passing only the VM to run which can be
// different than the VMs in the job itself.
vmIds = data?.vms ?? extractIdsFromSimplePattern(vmsPattern)

await this._checkAuthorizations({ job, schedule, useSmartBackup: vmIds === undefined })
if (vmIds === undefined) {
const poolPattern = vmsPattern.$pool

// Log a failure task when a pool contained in the smart backup
// pattern doesn't exist
if (poolPattern !== undefined) {
const poolIds =
extractIdsFromSimplePattern({ id: poolPattern }) ??
poolPattern.__and?.flatMap?.(pattern => extractIdsFromSimplePattern({ id: pattern }) ?? []) ??
[]
poolIds.forEach(id => {
try {
app.getObject(id)
} catch (error) {
const taskId = logger.notice('missing pool', {
data: {
type: 'pool',
id,
},
event: 'task.start',
parentId: runJobId,
})
logger.error('missing pool', {
event: 'task.end',
result: serializeError(error),
status: 'failure',
taskId,
})
}
})
}

const vmsPattern = job.vms

// Make sure we are passing only the VM to run which can be
// different than the VMs in the job itself.
let vmIds = data?.vms ?? extractIdsFromSimplePattern(vmsPattern)

await this._checkAuthorizations({ job, schedule, useSmartBackup: vmIds === undefined })
if (vmIds === undefined) {
const poolPattern = vmsPattern.$pool

// Log a failure task when a pool contained in the smart backup
// pattern doesn't exist
if (poolPattern !== undefined) {
const poolIds =
extractIdsFromSimplePattern({ id: poolPattern }) ??
poolPattern.__and?.flatMap?.(pattern => extractIdsFromSimplePattern({ id: pattern }) ?? []) ??
[]
poolIds.forEach(id => {
try {
app.getObject(id)
} catch (error) {
const taskId = logger.notice('missing pool', {
data: {
type: 'pool',
id,
},
event: 'task.start',
parentId: runJobId,
})
logger.error('missing pool', {
event: 'task.end',
result: serializeError(error),
status: 'failure',
taskId,
})
}
})
vmIds = Object.keys(
app.getObjects({
filter: (() => {
const isMatchingVm = createPredicate({
type: 'VM',
...vmsPattern,
})

return obj =>
isMatchingVm(obj) &&
// don't match replicated VMs created by this very job otherwise
// they will be replicated again and again
!('start' in obj.blockedOperations && obj.other['xo:backup:job'] === job.id)
})(),
})
)
if (vmIds.length === 0) {
throw new Error('no VMs match this pattern')
}
}

vmIds = Object.keys(
app.getObjects({
filter: (() => {
const isMatchingVm = createPredicate({
type: 'VM',
...vmsPattern,
})
job = {
...job,

return obj =>
isMatchingVm(obj) &&
// don't match replicated VMs created by this very job otherwise
// they will be replicated again and again
!('start' in obj.blockedOperations && obj.other['xo:backup:job'] === job.id)
})(),
})
)
if (vmIds.length === 0) {
throw new Error('no VMs match this pattern')
vms: { id: { __or: vmIds } },
settings: merge(job.settings, data?.settings),
}
}

job = {
...job,

vms: { id: { __or: vmIds } },
settings: merge(job.settings, data?.settings),
}

const proxyId = job.proxy
const useXoProxy = proxyId !== undefined
const remoteIds = unboxIdsFromPattern(job.remotes)
Expand Down Expand Up @@ -191,7 +194,8 @@ export default class BackupNg {
log.warn(error)
}
}
vmIds.forEach(handleRecord)
// can be empty for mirror backup job
vmIds?.forEach(handleRecord)
unboxIdsFromPattern(job.srs).forEach(handleRecord)

// add xapi specific to the health check SR if needed
Expand Down Expand Up @@ -332,6 +336,7 @@ export default class BackupNg {
}
}
app.registerJobExecutor('backup', executor)
app.registerJobExecutor('mirrorBackup', executor)
})
}

Expand Down

0 comments on commit 04f049c

Please sign in to comment.