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): implement Api for esxi migration UX #6662

Merged
merged 2 commits into from Feb 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 62 additions & 21 deletions @xen-orchestra/vmware-explorer/esxi.mjs
Expand Up @@ -124,29 +124,41 @@ export default class Esxi extends EventEmitter {
objectSet: [objectSpec],
}

result = await this.#exec('RetrievePropertiesEx', {
_this: propertyCollector,
specSet: [propertyFilterSpec],
options: { attributes: { type: 'RetrieveOptions' } },
})

let token
const objects = {}
const returnObj = Array.isArray(result.returnval.objects) ? result.returnval.objects : [result.returnval.objects]
do {
if (token !== undefined) {
result = await this.#exec('ContinueRetrievePropertiesEx', {
_this: propertyCollector,
token,
})
} else {
result = await this.#exec('RetrievePropertiesEx', {
_this: propertyCollector,
specSet: [propertyFilterSpec],
options: { attributes: { type: 'RetrieveOptions' } },
})
}

returnObj.forEach(({ obj, propSet }) => {
objects[obj.$value] = {}
propSet = Array.isArray(propSet) ? propSet : [propSet]
propSet.forEach(({ name, val }) => {
// don't care about the type for now
delete val.attributes
// a scalar value : simplify it
if (val.$value) {
objects[obj.$value][name] = val.$value
} else {
objects[obj.$value][name] = val
}
const returnObj = Array.isArray(result.returnval.objects) ? result.returnval.objects : [result.returnval.objects]
returnObj.forEach(({ obj, propSet }) => {
objects[obj.$value] = {}
propSet = Array.isArray(propSet) ? propSet : [propSet]
propSet.forEach(({ name, val }) => {
// don't care about the type for now
delete val.attributes
// a scalar value : simplify it
if (val.$value) {
objects[obj.$value][name] = val.$value
} else {
objects[obj.$value][name] = val
}
})
})
})

token = result.returnval.token
} while (token)

return objects
}

Expand Down Expand Up @@ -176,6 +188,35 @@ export default class Esxi extends EventEmitter {
}
}

async getAllVmMetadata() {
const datas = await this.search('VirtualMachine', ['config', 'storage', 'runtime'])

return Object.keys(datas).map(id => {
const { config, storage, runtime } = datas[id]
const perDatastoreUsage = Array.isArray(storage.perDatastoreUsage)
? storage.perDatastoreUsage
: [storage.perDatastoreUsage]
return {
id,
nameLabel: config.name,
memory: +config.hardware.memoryMB * 1024 * 1024,
nCpus: +config.hardware.numCPU,
guestToolsInstalled: false,
firmware: config.firmware === 'efi' ? 'uefi' : config.firmware, // bios or uefi
powerState: runtime.powerState,
storage: perDatastoreUsage.reduce(
(prev, curr) => {
return {
used: prev.used + +curr.committed,
free: prev.free + +curr.uncommitted,
}
},
{ used: 0, free: 0 }
),
}
})
}

async getTransferableVmMetadata(vmId) {
const search = await this.search('VirtualMachine', ['name', 'config', 'storage', 'runtime', 'snapshot'])
if (search[vmId] === undefined) {
Expand Down Expand Up @@ -241,7 +282,7 @@ export default class Esxi extends EventEmitter {
return {
name_label: config.name,
memory: +config.hardware.memoryMB * 1024 * 1024,
numCpu: +config.hardware.numCPU,
nCpus: +config.hardware.numCPU,
guestToolsInstalled: false,
firmware: config.firmware === 'efi' ? 'uefi' : config.firmware, // bios or uefi
powerState: runtime.powerState,
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.unreleased.md
Expand Up @@ -18,6 +18,7 @@

- [xo-cli] Fix `write EPIPE` error when used with piped output is closed (e.g. like `| head`) [#6680](https://github.com/vatesfr/xen-orchestra/issues/6680)
- [VM] Show distro icon for openSUSE [Forum#6965](https://xcp-ng.org/forum/topic/6965)
- [ESXI import] Handle listing more than 100 VMs

### Packages to release

Expand All @@ -35,6 +36,7 @@

<!--packages-start-->

- @xen-orchestra/vmware-explorer minor
- @xen-orchestra/backups minor
- xo-cli minor
- xo-server-auth-oidc minor
Expand Down
12 changes: 12 additions & 0 deletions packages/xo-server/src/api/esxi.mjs
@@ -0,0 +1,12 @@
export function listVms({ host, password, sslVerify = true, user }) {
return this.connectToEsxiAndList({ host, user, password, sslVerify })
}
julien-f marked this conversation as resolved.
Show resolved Hide resolved

listVms.params = {
host: { type: 'string' },
user: { type: 'string' },
password: { type: 'string' },
sslVerify: { type: 'boolean', optional: true },
}

listVms.permission = 'admin'
11 changes: 8 additions & 3 deletions packages/xo-server/src/xo-mixins/migrate-vm.mjs
Expand Up @@ -161,6 +161,11 @@ export default class MigrateVm {
})
}

async connectToEsxiAndList({ host, user, password, sslVerify }) {
const esxi = await this.#connectToEsxi(host, user, password, sslVerify)
return esxi.getAllVmMetadata()
}

@decorateWith(deferrable)
async migrationfromEsxi(
$defer,
Expand All @@ -173,7 +178,7 @@ export default class MigrateVm {
return esxi.getTransferableVmMetadata(vmId)
})

const { disks, firmware, memory, name_label, networks, numCpu, powerState, snapshots } = esxiVmMetadata
const { disks, firmware, memory, name_label, networks, nCpus, powerState, snapshots } = esxiVmMetadata
const isRunning = powerState !== 'poweredOff'

const chainsByNodes = await new Task({ name: `build disks and snapshots chains for ${vmId}` }).run(async () => {
Expand All @@ -194,8 +199,8 @@ export default class MigrateVm {
memory_static_min: memory,
name_description: 'from esxi',
name_label,
VCPUs_at_startup: numCpu,
VCPUs_max: numCpu,
VCPUs_at_startup: nCpus,
VCPUs_max: nCpus,
})
)
await Promise.all([
Expand Down