This repository has been archived by the owner on Dec 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
125 lines (104 loc) · 3.3 KB
/
utils.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package nethooks
import (
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/samalba/dockerclient"
)
type imageInfo struct {
portID int
protoName string
}
func getImageInfo(imageName string) ([]imageInfo, error) {
imageInfoList := []imageInfo{}
docker, err := dockerclient.NewDockerClient(os.Getenv("DOCKER_HOST"), nil)
if err != nil {
log.Errorf("Unable to connect to docker. Error %v", err)
return imageInfoList, err
}
info, err := docker.InspectImage(imageName)
log.Debugf("Got the following info for the image %#v", info)
if err != nil {
log.Errorf("Unable to inspect image '%s'. Error %v", imageName, err)
return imageInfoList, err
}
for exposedPort := range info.Config.ExposedPorts {
if strings.Contains(exposedPort, "/") {
imageInfo := imageInfo{}
values := strings.Split(exposedPort, "/")
imageInfo.portID, _ = strconv.Atoi(values[0])
imageInfo.protoName = values[1]
log.Infof("Extracted port info %v from image '%s'", imageInfo, imageName)
imageInfoList = append(imageInfoList, imageInfo)
}
}
return imageInfoList, nil
}
func getContainerIP(contName string) string {
ipAddress := ""
output, err := exec.Command("docker", "exec", contName, "/sbin/ip", "address", "show").CombinedOutput()
if err != nil {
log.Errorf("Unable to fetch container '%s' IP. Error %v", contName, err)
return ipAddress
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.Contains(line, "eth0") && strings.Contains(line, "inet ") {
words := strings.Split(line, " ")
for idx, word := range words {
if word == "inet" {
ipAddress = strings.Split(words[idx+1], "/")[0]
}
}
}
}
return ipAddress
}
func populateEtcHosts(contName, dnsSvcName, ipAddress string) error {
command := "echo " + ipAddress + " " + dnsSvcName + " >> /etc/hosts"
if _, err := exec.Command("docker", "exec", contName, "/bin/sh", "-c", command).CombinedOutput(); err != nil {
log.Errorf("Unable to populate etc hosts. Error %v", err)
return err
}
if output, err := exec.Command("docker", "exec", contName, "cat", "/etc/hosts").CombinedOutput(); err != nil {
log.Infof("VJ ===> output = %s ", output)
}
return nil
}
// getDnsInfo returns DNS information for a network
// - Query netmaster to get the DNS server address
func getDnsInfo(networkName, tenantName string) (string, error) {
var cfgList []map[string]*json.RawMessage
networkID := fmt.Sprintf("%s.%s", networkName, tenantName)
// Get network params from netmaster
netInfoUrl := "http://netmaster:9999/network/" + networkID
err := httpGet(netInfoUrl, &cfgList)
if err != nil {
log.Errorf("Error getting network info for %s. Err: %v", networkID, err)
return "", err
}
// Check if we havd DNS attribute
nwCfg := cfgList[0]
dnsAddr := ""
_, ok := nwCfg["dnsServer"]
if !ok {
log.Errorf("dns server not found for the network %s", networkID)
return "", errors.New("DNS Server not found")
}
// Get the DNS address
err = json.Unmarshal(*nwCfg["dnsServer"], &dnsAddr)
if err != nil {
log.Errorf("Error decoding json: %+v, Err: %v", nwCfg, err)
return "", err
}
return dnsAddr, nil
}
func containerExists(contName string) bool {
_, err := exec.Command("docker", "inspect", contName).CombinedOutput()
return err == nil
}