fix(xo-server,xo-web): add mainIpAddress to VM objects (#4943)

Fixes #4927
This commit is contained in:
badrAZ 2020-04-24 10:16:13 +02:00 committed by GitHub
parent bf4d4a4742
commit 53f9b5d131
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 30 deletions

View File

@ -0,0 +1,23 @@
// See: https://github.com/xapi-project/xen-api/blob/324bc6ee6664dd915c0bbe57185f1d6243d9ed7e/ocaml/xapi/xapi_guest_agent.ml#L59-L81
//
// Returns <min(n)>/ipv4/<min(m)> || <min(n)>/ipv6/<min(m)> || undefined
// where n corresponds to the network interface and m to its IP
const IPV4_KEY_RE = /^\d+\/ipv4\/\d+$/
const IPV6_KEY_RE = /^\d+\/ipv6\/\d+$/
export const extractIpFromVmNetworks = networks => {
if (networks === undefined) {
return
}
let ipv6
for (const key of Object.keys(networks).sort()) {
if (IPV4_KEY_RE.test(key)) {
return networks[key]
}
if (ipv6 === undefined && IPV6_KEY_RE.test(key)) {
ipv6 = networks[key]
}
}
return ipv6
}

View File

@ -1,5 +1,6 @@
import * as sensitiveValues from './sensitive-values'
import ensureArray from './_ensureArray'
import { extractIpFromVmNetworks } from './_extractIpFromVmNetworks'
import {
extractProperty,
forEach,
@ -324,6 +325,7 @@ const TRANSFORMS = {
}
})(),
expNestedHvm: obj.platform['exp-nested-hvm'] === 'true',
mainIpAddress: extractIpFromVmNetworks(guestMetrics?.networks),
high_availability: obj.ha_restart_priority,
memory: (function() {

View File

@ -16,32 +16,9 @@ import Collection from '../collection/redis'
import parseDuration from '../_parseDuration'
import patch from '../patch'
import readChunk from '../_readStreamChunk'
import { extractIpFromVmNetworks } from '../_extractIpFromVmNetworks'
import { generateToken } from '../utils'
// See: https://github.com/xapi-project/xen-api/blob/324bc6ee6664dd915c0bbe57185f1d6243d9ed7e/ocaml/xapi/xapi_guest_agent.ml#L59-L81
//
// Returns <min(n)>/ipv4/<min(m)> || <min(n)>/ipv6/<min(m)> || undefined
// where n corresponds to the network interface and m to its IP
const IPV4_KEY_RE = /^\d+\/ipv4\/\d+$/
const IPV6_KEY_RE = /^\d+\/ipv6\/\d+$/
const extractIp = networks => {
if (networks === undefined) {
return
}
let ipv6
for (const key of Object.keys(networks).sort()) {
if (IPV4_KEY_RE.test(key)) {
return networks[key]
}
if (ipv6 === undefined && IPV6_KEY_RE.test(key)) {
ipv6 = networks[key]
}
}
return ipv6
}
const extractProperties = _ => _.properties
const omitToken = proxy => omit(proxy, 'authenticationToken')
const synchronizedWrite = synchronized()
@ -361,7 +338,9 @@ export default class Proxy {
const vm = this._app.getXapi(proxy.vmUuid).getObjectByUuid(proxy.vmUuid)
if (
(proxy.address = extractIp(vm.$guest_metrics?.networks)) === undefined
(proxy.address = extractIpFromVmNetworks(
vm.$guest_metrics?.networks
)) === undefined
) {
throw new Error(`cannot get the proxy VM IP (${proxy.vmUuid})`)
}

View File

@ -100,7 +100,7 @@ export default class TabConsole extends Component {
_openSsh = (username = 'root') => {
window.location = `ssh://${encodeURIComponent(username)}@${
this.props.vm.addresses['0/ip']
this.props.vm.mainIpAddress
}`
}
@ -131,7 +131,7 @@ export default class TabConsole extends Component {
render() {
const { statsOverview, vm } = this.props
const { minimalLayout, scale } = this.state
const canSsh = vm.addresses && vm.addresses['0/ip']
const canSsh = vm.mainIpAddress !== undefined
if (!isVmRunning(vm)) {
return (

View File

@ -69,10 +69,10 @@ export default connectStore(() => {
vmTotalDiskSpace,
}) => {
const {
addresses,
CPUs: cpus,
id,
installTime,
mainIpAddress,
memory,
os_version: osVersion,
power_state: powerState,
@ -180,8 +180,8 @@ export default connectStore(() => {
</Col>
<Col mediumSize={3}>
<BlockLink to={`/vms/${id}/network`}>
{addresses && addresses['0/ip'] ? (
<Copiable tagName='p'>{addresses['0/ip']}</Copiable>
{mainIpAddress !== undefined ? (
<Copiable tagName='p'>{mainIpAddress}</Copiable>
) : (
<p>{_('noIpv4Record')}</p>
)}