fix(xen-api/{get,put}Resource): use provided address when possible

Fixes #5896

Introduced by ea10df8a92

Don't use the address provided by XAPI when connecting to the pool master and without a default migration network as it will unnecessarily break NATted hosts.
This commit is contained in:
Julien Fontanet 2021-09-14 13:51:01 +02:00
parent a9de4ceb30
commit a84fac1b6a
2 changed files with 29 additions and 10 deletions

View File

@ -14,6 +14,7 @@
- [SSH keys] Allow SSH key to be broken anywhere to avoid breaking page formatting (Thanks @tstivers1990!) [#5891](https://github.com/vatesfr/xen-orchestra/issues/5891) (PR [#5892](https://github.com/vatesfr/xen-orchestra/pull/5892))
- [VM/Advanced] Fix conversion from UEFI to BIOS boot firmware (PR [#5895](https://github.com/vatesfr/xen-orchestra/pull/5895))
- [VM/network] Support newline-delimited IP addresses reported by some guest tools
- Fix VM/host stats, VM creation with Cloud-init, and VM backups, with NATted hosts [#5896](https://github.com/vatesfr/xen-orchestra/issues/5896)
### Packages to release
@ -32,5 +33,7 @@
>
> In case of conflict, the highest (lowest in previous list) `$version` wins.
- xen-api patch
- @xen-orchestra/proxy patch
- xo-server patch
- xo-web patch

View File

@ -361,9 +361,9 @@ export class Xapi extends EventEmitter {
let url = new URL('http://localhost')
url.protocol = this._url.protocol
url.hostname = await this._getHostAddress(host ?? (await this.getRecord('host', this._pool.master)))
url.pathname = pathname
url.search = new URLSearchParams(query)
await this._setHostAddressInUrl(url, host)
const response = await pRetry(
async () =>
@ -436,9 +436,9 @@ export class Xapi extends EventEmitter {
const url = new URL('http://localhost')
url.protocol = this._url.protocol
url.hostname = await this._getHostAddress(host ?? (await this.getRecord('host', this._pool.master)))
url.pathname = pathname
url.search = new URLSearchParams(query)
await this._setHostAddressInUrl(url, host)
const doRequest = httpRequest.put.bind(undefined, $cancelToken, {
body,
@ -803,14 +803,29 @@ export class Xapi extends EventEmitter {
}
}
async _getHostAddress({ address, PIFs, $pool }) {
const poolMigrationNetwork = $pool.other_config['xo:migrationNetwork']
async _setHostAddressInUrl(url, host) {
const pool = this._pool
const poolMigrationNetwork = pool.other_config['xo:migrationNetwork']
if (host === undefined) {
if (poolMigrationNetwork === undefined) {
const xapiUrl = this._url
url.hostname = xapiUrl.hostname
url.port = xapiUrl.port
return
}
host = await this.getRecord('host', pool.master)
}
let { address } = host
if (poolMigrationNetwork !== undefined) {
const _PIFs = new Set(PIFs)
const hostPifs = new Set(host.PIFs)
try {
const migrationNetworkPifRef = (await this.getRecordByUuid('network', poolMigrationNetwork)).PIFs.find(pifRef =>
_PIFs.has(pifRef)
)
const networkRef = await this.call('host.get_by_uuid', poolMigrationNetwork)
const networkPifs = await this.getField('network', networkRef, 'PIFs')
const migrationNetworkPifRef = networkPifs.find(hostPifs.has, hostPifs)
address = await this.getField('PIF', migrationNetworkPifRef, 'IP')
} catch (error) {
console.warn('unable to get the host address linked to the pool migration network', poolMigrationNetwork, error)
@ -824,7 +839,8 @@ export class Xapi extends EventEmitter {
console.warn('reversing host address', address, error)
}
}
return address
url.hostname = address
}
_setUrl(url) {
@ -891,7 +907,7 @@ export class Xapi extends EventEmitter {
// TODO: look for hostname in all addresses of this host (including all its PIFs)
const host = (await this.getAllRecords('host')).find(host => host.address === url.hostname)
if (host !== undefined) {
url.hostname = await this._getHostAddress(host)
await this._setHostAddressInUrl(url, host)
}
} catch (error) {
console.warn('_replaceHostAddressInUrl', url, error)