func NewNetworkLinkFromString(s string) (*NetworkLink, error) { value := strings.Split(s, ":") if len(value) < 3 { return nil, errors.New(fmt.Sprintf("The network link '%s' must be of the form <from_host>:<from_port>:<to_host>:<to_port> where <from_host> is optional", s)) } // Handle the case where from_host isn't specified if len(value) == 3 { value = append([]string{"127.0.0.1"}, value...) } link := NetworkLink{} link.FromHost = value[0] from_port, err := strconv.Atoi(value[1]) if err != nil { return nil, err } link.FromPort = port.Port(from_port) if err := link.FromPort.Check(); err != nil { return nil, errors.New("From port value must be between 0 and 65535") } link.ToHost = value[2] if value[3] != "" { to, err := strconv.Atoi(value[3]) if err != nil { return nil, err } link.ToPort = port.Port(to) if err := link.ToPort.Check(); err != nil { return nil, errors.New("To port value must be between 0 and 65535") } } return &link, nil }
func NewHostLocator(value string) (*HostLocator, error) { if strings.Contains(value, "/") { return nil, errors.New("Host identifiers may not have a slash") } if value == "" || value == LocalHostName { return &HostLocator{}, nil } id := &HostLocator{} if strings.Contains(value, ":") { host, portString, err := net.SplitHostPort(value) if err != nil { return nil, err } if portString != "" { p, err := strconv.Atoi(portString) if err != nil { return nil, err } id.Port = port.Port(p) if err := id.Port.Check(); err != nil { return nil, err } } id.Host = host } else { id.Host = value } return id, nil }
// Return an object representing an IP host func NewHostLocator(value string) (HostLocator, error) { if strings.Contains(value, "/") { return "", errors.New("Host identifiers may not have a slash") } if value == "" || value == localTransport { return Local, nil } if strings.Contains(value, ":") { _, portString, err := net.SplitHostPort(value) if err != nil { return "", err } if portString != "" { p, err := strconv.Atoi(portString) if err != nil { return "", err } port := port.Port(p) if err := port.Check(); err != nil { return "", err } } } return HostLocator(value), nil }
func assignPorts(dep *Deployment) { p := 10000 for i := range dep.Instances { instance := &dep.Instances[i] for j := range instance.Ports { mapping := &instance.Ports[j] if mapping.External.Default() { mapping.External = port.Port(p) p++ } } } }
func TestPrepareDeploymentError(t *testing.T) { dep := createDeployment(`{ "containers":[ { "name":"web", "count":2, "image":"pmorie/sti-html-app", "publicports":[ {"internal":8080,"external":0} ], "links":[ {"to":"web"} ] }, { "name":"db", "count":3, "image":"pmorie/sti-db-app" } ] }`) if _, _, err := dep.Describe(oneHost, loopbackTransport); err != nil { t.Fatal("Should not have received an error", err.Error()) } dep.Containers[0].Links[0].Ports = []port.Port{port.Port(8081)} if _, _, err := dep.Describe(oneHost, loopbackTransport); err == nil { t.Fatal("Should have received an error") } else { if !regexp.MustCompile("target port 8081 on web is not found").MatchString(err.Error()) { t.Fatal("Unexpected error message", err.Error()) } } link := &dep.Containers[0].Links[0] link.Ports = []port.Port{} link.To = "db" if _, _, err := dep.Describe(oneHost, loopbackTransport); err == nil { t.Fatal("Should have received an error") } else { if !regexp.MustCompile("target db has no public ports to link to from web").MatchString(err.Error()) { t.Fatal("Unexpected error message", err.Error()) } } dep.Containers[1].PublicPorts = port.PortPairs{port.PortPair{port.Port(27017), 0}} next, removed, err := dep.Describe(oneHost, loopbackTransport) if err != nil { t.Fatal("Should not have received an error", err.Error()) } if len(next.Instances) != 5 { t.Fatalf("Expected %d instances, got %d", 5, len(next.Instances)) } if len(next.Instances[0].links) != 3 { t.Fatalf("Should have exactly 1 link %+v", next.Instances[0].links) } if len(removed) != 0 { t.Fatal("Should not have removed instances", removed) } dep.RandomizeIds = true dep.Containers[1].PublicPorts = port.PortPairs{port.PortPair{port.Port(27017), 0}} dep.Containers[0].Links = append(dep.Containers[0].Links, Link{ To: "web", }) next, removed, err = dep.Describe(oneHost, loopbackTransport) if err != nil { t.Fatal("Should not have received an error", err.Error()) } if len(next.Instances) != 5 { t.Fatalf("Expected %d instances, got %d", 5, len(next.Instances)) } if len(next.Instances[0].links) != 5 { t.Fatalf("Should have exactly 5 links (2 web links, 3 mongo links) %+v", next.Instances[0].links) } if len(removed) != 0 { t.Fatal("Should not have removed instances", removed) } if next.Instances[0].Id == "web-1" { t.Fatal("Should randomize ids", next.Instances[0]) } // b, _ := json.MarshalIndent(next, "", " ") // t.Log(string(b)) }
func TcpPort(p int) Port { return Port{port.Port(p)} }