-
Notifications
You must be signed in to change notification settings - Fork 10
/
container.go
95 lines (78 loc) · 2.02 KB
/
container.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
package occam
import (
"encoding/json"
"fmt"
"net/url"
"os"
"strings"
)
type Container struct {
ID string
Ports map[string]string
Env map[string]string
IPAddresses map[string]string
}
func NewContainerFromInspectOutput(output []byte) (Container, error) {
var inspect []struct {
ID string `json:"Id"`
Config struct {
Env []string `json:"Env"`
} `json:"Config"`
NetworkSettings struct {
Ports map[string][]struct {
HostPort string `json:"HostPort"`
} `json:"Ports"`
Networks map[string]struct {
IPAddress string `json:"IPAddress"`
} `json:"Networks"`
} `json:"NetworkSettings"`
}
err := json.Unmarshal(output, &inspect)
if err != nil {
return Container{}, err
}
container := Container{ID: inspect[0].ID}
if len(inspect[0].NetworkSettings.Ports) > 0 {
container.Ports = make(map[string]string)
for key, value := range inspect[0].NetworkSettings.Ports {
if len(value) > 0 {
container.Ports[strings.TrimSuffix(key, "/tcp")] = value[0].HostPort
}
}
}
if len(inspect[0].Config.Env) > 0 {
container.Env = make(map[string]string)
for _, e := range inspect[0].Config.Env {
parts := strings.SplitN(e, "=", 2)
container.Env[parts[0]] = parts[1]
}
}
if len(inspect[0].NetworkSettings.Networks) > 0 {
container.IPAddresses = make(map[string]string)
for networkName, network := range inspect[0].NetworkSettings.Networks {
container.IPAddresses[networkName] = network.IPAddress
}
}
return container, nil
}
func (c Container) HostPort(value string) string {
return c.Ports[value]
}
func (c Container) Host() string {
val, ok := os.LookupEnv("DOCKER_HOST")
if !ok || strings.HasPrefix(val, "unix://") {
return "localhost"
}
url, err := url.Parse(val)
if err != nil {
return "localhost"
}
return url.Hostname()
}
func (c Container) IPAddressForNetwork(networkName string) (string, error) {
ipAddress, ok := c.IPAddresses[networkName]
if !ok {
return "", fmt.Errorf("invalid network name: %s", networkName)
}
return ipAddress, nil
}