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/xostor): bind a XOSTOR with a license at the XOSTOR creation #7015

Merged
merged 5 commits into from
Oct 24, 2023
Merged
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
60 changes: 55 additions & 5 deletions packages/xo-server/src/api/xostor.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { asyncEach } from '@vates/async-each'
import { createLogger } from '@xen-orchestra/log'
import { defer } from 'golike-defer'
import { operationFailed } from 'xo-common/api-errors.js'

const ENUM_PROVISIONING = {
Expand Down Expand Up @@ -105,10 +106,46 @@ formatDisks.resolve = {
host: ['host', 'host', 'administrate'],
}

export async function create({ description, disksByHost, force, ignoreFileSystems, name, provisioning, replication }) {
export const create = defer(async function (
$defer,
{ description, disksByHost, force, ignoreFileSystems, name, provisioning, replication }
) {
const hostIds = Object.keys(disksByHost)
const hosts = hostIds.map(hostId => this.getObject(hostId, 'host'))

const tmpBoundObjectId = `tmp_${hostIds.join(',')}_${Math.random().toString(32).slice(2)}`

const xostorLicenses = await this.getLicenses({ productType: 'xostor' })

const now = Date.now()
const availableLicenses = xostorLicenses.filter(
({ boundObjectId, expires }) => boundObjectId === undefined && (expires === undefined || expires > now)
)

let license = availableLicenses.find(license => license.productId === 'xostor')
julien-f marked this conversation as resolved.
Show resolved Hide resolved

if (license === undefined) {
license = availableLicenses.find(license => license.productId === 'xostor.trial')
julien-f marked this conversation as resolved.
Show resolved Hide resolved
}

if (license === undefined) {
license = await this.createBoundXostorTrialLicense({
boundObjectId: tmpBoundObjectId,
})
} else {
await this.bindLicense({
licenseId: license.id,
boundObjectId: tmpBoundObjectId,
})
}
$defer.onFailure(() =>
this.unbindLicense({
licenseId: license.id,
productId: license.productId,
boundObjectId: tmpBoundObjectId,
})
)

const hosts = hostIds.map(hostId => this.getObject(hostId, 'host'))
if (!hosts.every(host => host.$pool === hosts[0].$pool)) {
// we need to do this test to ensure it won't create a partial LV group with only the host of the pool of the first master
throw new Error('All hosts must be in the same pool')
Expand Down Expand Up @@ -140,7 +177,7 @@ export async function create({ description, disksByHost, force, ignoreFileSystem
const xapi = this.getXapi(host)

log.info(`Create XOSTOR (${name}) with provisioning: ${provisioning}`)
const srRef = await this.getXapi(host).SR_create({
const srRef = await xapi.SR_create({
device_config: {
'group-name': 'linstor_group/' + LV_NAME,
redundancy: String(replication),
Expand All @@ -152,9 +189,17 @@ export async function create({ description, disksByHost, force, ignoreFileSystem
shared: true,
type: 'linstor',
})
const srUuid = await xapi.getField('SR', srRef, 'uuid')

await this.rebindLicense({
licenseId: license.id,
oldBoundObjectId: tmpBoundObjectId,
newBoundObjectId: srUuid,
})

return srUuid
})

return xapi.getObject(srRef).uuid
}
create.description = 'Create a XOSTOR storage'
create.permission = 'admin'
create.params = {
Expand All @@ -176,6 +221,11 @@ export async function destroy({ sr }) {
const hosts = Object.values(xapi.objects.indexes.type.host).map(host => this.getObject(host.uuid, 'host'))

await xapi.destroySr(sr._xapiId)
const license = (await this.getLicenses({ productType: 'xostor' })).find(license => license.boundObjectId === sr.uuid)
await this.unbindLicense({
boundObjectId: license.boundObjectId,
productId: license.productId,
})
return asyncEach(hosts, host => destroyVolumeGroup(xapi, host, true), { stopOnError: false })
}
destroy.description = 'Destroy a XOSTOR storage'
Expand Down