2014-02-15 02:18:16 +00:00
|
|
|
package links
|
2013-10-05 02:25:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
"github.com/docker/docker/nat"
|
2013-10-05 02:25:15 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Link struct {
|
|
|
|
ParentIP string
|
|
|
|
ChildIP string
|
|
|
|
Name string
|
|
|
|
ChildEnvironment []string
|
2014-02-12 00:48:44 +00:00
|
|
|
Ports []nat.Port
|
2013-10-05 02:25:15 +00:00
|
|
|
IsEnabled bool
|
2014-01-30 20:43:49 +00:00
|
|
|
eng *engine.Engine
|
2013-10-05 02:25:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 02:07:33 +00:00
|
|
|
func NewLink(parentIP, childIP, name string, env []string, exposedPorts map[nat.Port]struct{}, eng *engine.Engine) (*Link, error) {
|
|
|
|
|
|
|
|
var (
|
|
|
|
i int
|
|
|
|
ports = make([]nat.Port, len(exposedPorts))
|
|
|
|
)
|
2013-10-05 02:25:15 +00:00
|
|
|
|
2014-02-15 02:07:33 +00:00
|
|
|
for p := range exposedPorts {
|
2013-10-05 02:25:15 +00:00
|
|
|
ports[i] = p
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
l := &Link{
|
|
|
|
Name: name,
|
2014-02-15 02:07:33 +00:00
|
|
|
ChildIP: childIP,
|
|
|
|
ParentIP: parentIP,
|
|
|
|
ChildEnvironment: env,
|
2013-10-05 02:25:15 +00:00
|
|
|
Ports: ports,
|
2014-01-30 20:43:49 +00:00
|
|
|
eng: eng,
|
2013-10-05 02:25:15 +00:00
|
|
|
}
|
|
|
|
return l, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Link) Alias() string {
|
|
|
|
_, alias := path.Split(l.Name)
|
|
|
|
return alias
|
|
|
|
}
|
|
|
|
|
2014-09-17 01:08:30 +00:00
|
|
|
func nextContiguous(ports []nat.Port, value int, index int) int {
|
|
|
|
if index+1 == len(ports) {
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
for i := index + 1; i < len(ports); i++ {
|
|
|
|
if ports[i].Int() > value+1 {
|
|
|
|
return i - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
value++
|
|
|
|
}
|
|
|
|
return len(ports) - 1
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:25:15 +00:00
|
|
|
func (l *Link) ToEnv() []string {
|
|
|
|
env := []string{}
|
2014-05-26 07:40:24 +00:00
|
|
|
alias := strings.Replace(strings.ToUpper(l.Alias()), "-", "_", -1)
|
2013-10-05 02:25:15 +00:00
|
|
|
|
|
|
|
if p := l.getDefaultPort(); p != nil {
|
2013-10-28 23:58:59 +00:00
|
|
|
env = append(env, fmt.Sprintf("%s_PORT=%s://%s:%s", alias, p.Proto(), l.ChildIP, p.Port()))
|
2013-10-05 02:25:15 +00:00
|
|
|
}
|
|
|
|
|
2014-09-17 01:08:30 +00:00
|
|
|
//sort the ports so that we can bulk the continuous ports together
|
|
|
|
nat.Sort(l.Ports, func(ip, jp nat.Port) bool {
|
|
|
|
// If the two ports have the same number, tcp takes priority
|
|
|
|
// Sort in desc order
|
|
|
|
return ip.Int() < jp.Int() || (ip.Int() == jp.Int() && strings.ToLower(ip.Proto()) == "tcp")
|
|
|
|
})
|
|
|
|
|
|
|
|
for i := 0; i < len(l.Ports); {
|
|
|
|
p := l.Ports[i]
|
|
|
|
j := nextContiguous(l.Ports, p.Int(), i)
|
|
|
|
if j > i+1 {
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_START=%s://%s:%s", alias, p.Port(), strings.ToUpper(p.Proto()), p.Proto(), l.ChildIP, p.Port()))
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_ADDR=%s", alias, p.Port(), strings.ToUpper(p.Proto()), l.ChildIP))
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_PROTO=%s", alias, p.Port(), strings.ToUpper(p.Proto()), p.Proto()))
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_PORT_START=%s", alias, p.Port(), strings.ToUpper(p.Proto()), p.Port()))
|
|
|
|
|
|
|
|
q := l.Ports[j]
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_END=%s://%s:%s", alias, p.Port(), strings.ToUpper(q.Proto()), q.Proto(), l.ChildIP, q.Port()))
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_PORT_END=%s", alias, p.Port(), strings.ToUpper(q.Proto()), q.Port()))
|
|
|
|
|
|
|
|
i = j + 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:25:15 +00:00
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s=%s://%s:%s", alias, p.Port(), strings.ToUpper(p.Proto()), p.Proto(), l.ChildIP, p.Port()))
|
2013-10-29 06:02:28 +00:00
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_ADDR=%s", alias, p.Port(), strings.ToUpper(p.Proto()), l.ChildIP))
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_PORT=%s", alias, p.Port(), strings.ToUpper(p.Proto()), p.Port()))
|
|
|
|
env = append(env, fmt.Sprintf("%s_PORT_%s_%s_PROTO=%s", alias, p.Port(), strings.ToUpper(p.Proto()), p.Proto()))
|
2014-09-17 01:08:30 +00:00
|
|
|
i++
|
2013-10-05 02:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the linked container's name into the environment
|
|
|
|
env = append(env, fmt.Sprintf("%s_NAME=%s", alias, l.Name))
|
|
|
|
|
|
|
|
if l.ChildEnvironment != nil {
|
|
|
|
for _, v := range l.ChildEnvironment {
|
|
|
|
parts := strings.Split(v, "=")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
2014-02-23 03:01:45 +00:00
|
|
|
// Ignore a few variables that are added during docker build (and not really relevant to linked containers)
|
2013-10-05 02:25:15 +00:00
|
|
|
if parts[0] == "HOME" || parts[0] == "PATH" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
env = append(env, fmt.Sprintf("%s_ENV_%s=%s", alias, parts[0], parts[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default port rules
|
2014-02-12 00:48:44 +00:00
|
|
|
func (l *Link) getDefaultPort() *nat.Port {
|
|
|
|
var p nat.Port
|
2013-10-05 02:25:15 +00:00
|
|
|
i := len(l.Ports)
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
return nil
|
|
|
|
} else if i > 1 {
|
2014-02-12 00:48:44 +00:00
|
|
|
nat.Sort(l.Ports, func(ip, jp nat.Port) bool {
|
2013-10-05 02:25:15 +00:00
|
|
|
// If the two ports have the same number, tcp takes priority
|
|
|
|
// Sort in desc order
|
|
|
|
return ip.Int() < jp.Int() || (ip.Int() == jp.Int() && strings.ToLower(ip.Proto()) == "tcp")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
p = l.Ports[0]
|
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Link) Enable() error {
|
2014-06-27 07:29:55 +00:00
|
|
|
// -A == iptables append flag
|
|
|
|
if err := l.toggle("-A", false); err != nil {
|
2013-10-05 02:25:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
l.IsEnabled = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Link) Disable() {
|
|
|
|
// We do not care about errors here because the link may not
|
|
|
|
// exist in iptables
|
2014-06-27 07:29:55 +00:00
|
|
|
// -D == iptables delete flag
|
2013-10-05 02:25:15 +00:00
|
|
|
l.toggle("-D", true)
|
|
|
|
|
|
|
|
l.IsEnabled = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Link) toggle(action string, ignoreErrors bool) error {
|
2014-01-30 20:43:49 +00:00
|
|
|
job := l.eng.Job("link", action)
|
|
|
|
|
|
|
|
job.Setenv("ParentIP", l.ParentIP)
|
|
|
|
job.Setenv("ChildIP", l.ChildIP)
|
|
|
|
job.SetenvBool("IgnoreErrors", ignoreErrors)
|
|
|
|
|
|
|
|
out := make([]string, len(l.Ports))
|
|
|
|
for i, p := range l.Ports {
|
2014-09-17 01:08:30 +00:00
|
|
|
out[i] = string(p)
|
2014-01-30 20:43:49 +00:00
|
|
|
}
|
|
|
|
job.SetenvList("Ports", out)
|
|
|
|
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
// TODO: get ouput from job
|
|
|
|
return err
|
2013-10-05 02:25:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|