1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 01:57:32 +08:00

Fix port ordering

This commit is contained in:
Marcos Lilljedahl 2017-04-16 20:22:37 -03:00
parent d6c359645c
commit 6acb6f47c5
5 changed files with 22 additions and 13 deletions

View File

@ -2,10 +2,11 @@ FROM docker:17.04.0-ce-dind
RUN apk add --no-cache git tmux py2-pip apache2-utils vim build-base gettext-dev curl bash-completion bash util-linux jq RUN apk add --no-cache git tmux py2-pip apache2-utils vim build-base gettext-dev curl bash-completion bash util-linux jq
ENV COMPOSE_VERSION=1.11.1 ENV COMPOSE_VERSION=1.12.0
ENV MACHINE_VERSION=v0.10.0
# Install Compose and Machine # Install Compose and Machine
RUN pip install docker-compose==${COMPOSE_VERSION} RUN pip install docker-compose==${COMPOSE_VERSION}
RUN curl -L https://github.com/docker/machine/releases/download/v0.9.0-rc1/docker-machine-Linux-x86_64 \ RUN curl -L https://github.com/docker/machine/releases/download/${MACHINE_VERSION}/docker-machine-Linux-x86_64 \
-o /usr/bin/docker-machine && chmod +x /usr/bin/docker-machine -o /usr/bin/docker-machine && chmod +x /usr/bin/docker-machine
# Compile and install httping # Compile and install httping

View File

@ -5,7 +5,6 @@ import (
"io" "io"
"log" "log"
"os" "os"
"sort"
"strconv" "strconv"
"strings" "strings"
@ -101,7 +100,7 @@ func SetInstanceSwarmPorts(i *Instance) error {
return nil return nil
} }
func GetUsedPorts(i *Instance) ([]int, error) { func GetUsedPorts(i *Instance) ([]uint16, error) {
if i.dockerClient == nil { if i.dockerClient == nil {
return nil, fmt.Errorf("Docker client for DinD (%s) is not ready", i.IP) return nil, fmt.Errorf("Docker client for DinD (%s) is not ready", i.IP)
} }
@ -111,16 +110,15 @@ func GetUsedPorts(i *Instance) ([]int, error) {
return nil, err return nil, err
} }
openPorts := sort.IntSlice{} openPorts := []uint16{}
for _, c := range containers { for _, c := range containers {
for _, p := range c.Ports { for _, p := range c.Ports {
// When port is not published on the host docker return public port as 0, so we need to avoid it // When port is not published on the host docker return public port as 0, so we need to avoid it
if p.PublicPort != 0 { if p.PublicPort != 0 {
openPorts = append(openPorts, int(p.PublicPort)) openPorts = append(openPorts, p.PublicPort)
} }
} }
} }
sort.Sort(openPorts)
return openPorts, nil return openPorts, nil
} }

View File

@ -17,6 +17,12 @@ import (
var rw sync.Mutex var rw sync.Mutex
type UInt16Slice []uint16
func (p UInt16Slice) Len() int { return len(p) }
func (p UInt16Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p UInt16Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type Instance struct { type Instance struct {
session *Session `json:"-"` session *Session `json:"-"`
Name string `json:"name"` Name string `json:"name"`
@ -28,11 +34,11 @@ type Instance struct {
IsManager *bool `json:"is_manager"` IsManager *bool `json:"is_manager"`
Mem string `json:"mem"` Mem string `json:"mem"`
Cpu string `json:"cpu"` Cpu string `json:"cpu"`
Ports []uint16 `json:"ports"` Ports UInt16Slice
tempPorts []uint16 `json:"-"` tempPorts []uint16 `json:"-"`
ServerCert []byte `json:"server_cert"` ServerCert []byte `json:"server_cert"`
ServerKey []byte `json:"server_key"` ServerKey []byte `json:"server_key"`
cert *tls.Certificate `json:"-"` cert *tls.Certificate `json:"-"`
} }
func (i *Instance) setUsedPort(port uint16) { func (i *Instance) setUsedPort(port uint16) {

View File

@ -5,6 +5,7 @@ func InstanceImages() []string {
return []string{ return []string{
dindImage, dindImage,
"franela/dind:overlay2-dev", "franela/dind:overlay2-dev",
"franela/dind:overlay2-ucp",
} }
} }

View File

@ -8,6 +8,7 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"sort"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -131,8 +132,10 @@ func (s *Session) SchedulePeriodicTasks() {
wg.Wait() wg.Wait()
// broadcast all information // broadcast all information
for _, ins := range s.Instances { for _, ins := range s.Instances {
ins.Ports = ins.tempPorts ins.Ports = UInt16Slice(ins.tempPorts)
sort.Sort(ins.Ports)
ins.tempPorts = []uint16{} ins.tempPorts = []uint16{}
wsServer.BroadcastTo(ins.session.Id, "instance stats", ins.Name, ins.Mem, ins.Cpu, ins.IsManager, ins.Ports) wsServer.BroadcastTo(ins.session.Id, "instance stats", ins.Name, ins.Mem, ins.Cpu, ins.IsManager, ins.Ports)
} }
} }