github.com/vmware/govmomi@v0.43.0/govc/host/esxcli/guest_info.go (about) 1 /* 2 Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package esxcli 18 19 import ( 20 "context" 21 "strings" 22 23 "github.com/vmware/govmomi/object" 24 "github.com/vmware/govmomi/property" 25 "github.com/vmware/govmomi/vim25" 26 "github.com/vmware/govmomi/vim25/mo" 27 "github.com/vmware/govmomi/vim25/types" 28 ) 29 30 type hostInfo struct { 31 *Executor 32 wids map[string]string 33 } 34 35 type GuestInfo struct { 36 c *vim25.Client 37 hosts map[string]*hostInfo 38 } 39 40 func NewGuestInfo(c *vim25.Client) *GuestInfo { 41 return &GuestInfo{ 42 c: c, 43 hosts: make(map[string]*hostInfo), 44 } 45 } 46 47 func (g *GuestInfo) hostInfo(ref *types.ManagedObjectReference) (*hostInfo, error) { 48 // cache exectuor and uuid -> worldid map 49 if h, ok := g.hosts[ref.Value]; ok { 50 return h, nil 51 } 52 53 host := object.NewHostSystem(g.c, *ref) 54 55 e, err := NewExecutor(g.c, host) 56 if err != nil { 57 return nil, err 58 } 59 60 res, err := e.Run([]string{"vm", "process", "list"}) 61 if err != nil { 62 return nil, err 63 } 64 65 ids := make(map[string]string, len(res.Values)) 66 67 for _, process := range res.Values { 68 // Normalize uuid, esxcli and mo.VirtualMachine have different formats 69 uuid := strings.Replace(process["UUID"][0], " ", "", -1) 70 uuid = strings.Replace(uuid, "-", "", -1) 71 72 ids[uuid] = process["WorldID"][0] 73 } 74 75 h := &hostInfo{e, ids} 76 g.hosts[ref.Value] = h 77 78 return h, nil 79 } 80 81 // IpAddress attempts to find the guest IP address using esxcli. 82 // ESX hosts must be configured with the /Net/GuestIPHack enabled. 83 // For example: 84 // $ govc host.esxcli -- system settings advanced set -o /Net/GuestIPHack -i 1 85 func (g *GuestInfo) IpAddress(vm *object.VirtualMachine) (string, error) { 86 ctx := context.TODO() 87 const any = "0.0.0.0" 88 var mvm mo.VirtualMachine 89 90 pc := property.DefaultCollector(g.c) 91 err := pc.RetrieveOne(ctx, vm.Reference(), []string{"runtime.host", "config.uuid"}, &mvm) 92 if err != nil { 93 return "", err 94 } 95 96 h, err := g.hostInfo(mvm.Runtime.Host) 97 if err != nil { 98 return "", err 99 } 100 101 // Normalize uuid, esxcli and mo.VirtualMachine have different formats 102 uuid := strings.Replace(mvm.Config.Uuid, "-", "", -1) 103 104 if wid, ok := h.wids[uuid]; ok { 105 res, err := h.Run([]string{"network", "vm", "port", "list", "--world-id", wid}) 106 if err != nil { 107 return "", err 108 } 109 110 for _, val := range res.Values { 111 if ip, ok := val["IPAddress"]; ok { 112 if ip[0] != any { 113 return ip[0], nil 114 } 115 } 116 } 117 } 118 119 return any, nil 120 }