1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-10-04 17:33:21 +08:00

Query DinD instances for running containers and list the published ports

so the user can reverse proxy to the instance/port easily.
This commit is contained in:
Jonathan Leibiusky @xetorthio
2016-12-01 17:28:55 -03:00
parent afa47c0bfc
commit 77905f3fd8
7 changed files with 54 additions and 7 deletions

View File

@@ -4,5 +4,5 @@ type broadcastInfoTask struct {
}
func (c *broadcastInfoTask) Run(i *Instance) {
wsServer.BroadcastTo(i.session.Id, "instance stats", i.Name, i.Mem, i.Cpu, i.IsManager)
wsServer.BroadcastTo(i.session.Id, "instance stats", i.Name, i.Mem, i.Cpu, i.IsManager, i.Ports)
}

View File

@@ -0,0 +1,10 @@
package services
type checkUsedPortsTask struct {
}
func (c checkUsedPortsTask) Run(i *Instance) {
if ports, err := GetUsedPorts(i); err == nil {
i.Ports = ports
}
}

View File

@@ -46,6 +46,28 @@ func GetDaemonInfo(i *Instance) (types.Info, error) {
}
return i.dockerClient.Info(context.Background())
}
func GetUsedPorts(i *Instance) ([]uint16, error) {
if i.dockerClient == nil {
return nil, fmt.Errorf("Docker client for DinD (%s) is not ready", i.IP)
}
opts := types.ContainerListOptions{}
containers, err := i.dockerClient.ContainerList(context.Background(), opts)
if err != nil {
return nil, err
}
openPorts := []uint16{}
for _, c := range containers {
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
if p.PublicPort != 0 {
openPorts = append(openPorts, p.PublicPort)
}
}
}
return openPorts, nil
}
func CreateNetwork(name string) error {
opts := types.NetworkCreate{Driver: "overlay", Attachable: true}

View File

@@ -27,6 +27,7 @@ type Instance struct {
IsManager *bool `json:"is_manager"`
Mem string `json:"mem"`
Cpu string `json:"cpu"`
Ports []uint16 `json:"ports"`
}
func (i *Instance) IsConnected() bool {

View File

@@ -7,5 +7,5 @@ type periodicTask interface {
var periodicTasks []periodicTask
func init() {
periodicTasks = append(periodicTasks, &collectStatsTask{}, &checkSwarmStatusTask{}, &broadcastInfoTask{})
periodicTasks = append(periodicTasks, &collectStatsTask{}, &checkSwarmStatusTask{}, &checkUsedPortsTask{}, &broadcastInfoTask{})
}