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:
@@ -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)
|
||||
}
|
||||
|
10
services/check_used_ports_task.go
Normal file
10
services/check_used_ports_task.go
Normal 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
|
||||
}
|
||||
}
|
@@ -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}
|
||||
|
@@ -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 {
|
||||
|
@@ -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{})
|
||||
}
|
||||
|
Reference in New Issue
Block a user