Cosmos Nicolaou | 43b9535 | 2014-10-14 11:09:52 -0700 | [diff] [blame^] | 1 | // +build linux |
| 2 | |
| 3 | package internal |
| 4 | |
| 5 | import ( |
| 6 | "net" |
| 7 | |
| 8 | "veyron.io/veyron/veyron2/vlog" |
| 9 | |
| 10 | "veyron.io/veyron/veyron/profiles/internal/gce" |
| 11 | ) |
| 12 | |
| 13 | // GCEPublicAddress returns the public IP address of the GCE instance |
| 14 | // it is run from, or nil if run from anywhere else. The returned address |
| 15 | // is the public address of a 1:1 NAT tunnel to this host. |
| 16 | func GCEPublicAddress(log vlog.Logger) *net.IPAddr { |
| 17 | if !gce.RunningOnGCE() { |
| 18 | return nil |
| 19 | } |
| 20 | var pub *net.IPAddr |
| 21 | // Determine the IP address from GCE's metadata |
| 22 | if ip, err := gce.ExternalIPAddress(); err != nil { |
| 23 | log.Infof("failed to query GCE metadata: %s", err) |
| 24 | } else { |
| 25 | // 1:1 NAT case, our network config will not change. |
| 26 | pub = &net.IPAddr{IP: ip} |
| 27 | } |
| 28 | if pub == nil { |
| 29 | log.Infof("failed to determine public IP address to publish with") |
| 30 | } |
| 31 | return pub |
| 32 | } |