This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
ip_linux.go
62 lines (51 loc) · 1.59 KB
/
ip_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package driver
import (
"code.cloudfoundry.org/cfdev/config"
"encoding/json"
"fmt"
"io/ioutil"
"os/exec"
"path/filepath"
)
func IP(cfg config.Config) (string, error) {
var (
ipPath = filepath.Join(cfg.StateLinuxkit, "ip")
macAddrPath = filepath.Join(cfg.StateLinuxkit, "mac-addr")
vBridgeInfoPath = "/var/lib/libvirt/dnsmasq/virbr0.status"
)
// The logic below is a bit of a hack.
// Since the services will get started as root, the qemu files containing the ip address will be written as root.
// We don't want to escalate to root every time we need the ip throughout the lifecycle of the program, so we write
// the ip address as a normal file when we first get it. This logic is making an assumption that root privileges
// has been retrieved as part of a prior step and has not yet timed out.
data, err := ioutil.ReadFile(ipPath)
if err == nil {
return string(data), nil
}
macAddr, err := readAsSudo(macAddrPath)
if err != nil {
return "", err
}
vBridgeInfo, err := readAsSudo(vBridgeInfoPath)
if err != nil {
return "", err
}
var results []struct {
IPAddr string `json:"ip-address"`
MacAddr string `json:"mac-address"`
}
err = json.Unmarshal(vBridgeInfo, &results)
if err != nil {
return "", err
}
for _, result := range results {
if result.MacAddr == string(macAddr) {
ioutil.WriteFile(ipPath, []byte(result.IPAddr), 0600)
return result.IPAddr, nil
}
}
return "", fmt.Errorf("unable to find VM IP address from '%s'", vBridgeInfoPath)
}
func readAsSudo(path string) ([]byte, error) {
return exec.Command("sudo", "-S", "cat", path).Output()
}