1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-13 17:42:53 +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{})
}

View File

@ -141,10 +141,11 @@
$scope.connected = true;
});
socket.on('instance stats', function(name, mem, cpu, isManager) {
socket.on('instance stats', function(name, mem, cpu, isManager, ports) {
$scope.idx[name].mem = mem;
$scope.idx[name].cpu = cpu;
$scope.idx[name].isManager = isManager;
$scope.idx[name].ports = ports;
$scope.$apply();
});
@ -167,6 +168,12 @@
});
}
$scope.getProxyUrl = function(instance, port) {
var url = window.location.protocol + '//ip' + instance.ip.replace(/\./g, '_') + '-' + port + '.' + window.location.host;
return url;
}
$scope.showInstance = function(instance) {
$scope.selectedInstance = instance;
if (!instance.creatingTerminal) {

View File

@ -64,10 +64,17 @@
<md-card-title>
<md-card-title-text>
<span class="md-headline">{{instance.name}}</span>
<md-input-container class="md-icon-float md-block">
<label>IP</label>
<input ng-model="instance.ip" type="text" readonly="readonly">
</md-input-container>
<div layout-gt-sm="row">
<md-input-container class="md-icon-float md-block">
<label>IP</label>
<input ng-model="instance.ip" type="text" readonly="readonly">
</md-input-container>
<md-chips ng-model="instance.ports" name="port" readonly="true" md-removable="false">
<md-chip-template>
<strong><a href="{{getProxyUrl(instance, $chip)}}" title="{{getProxyUrl(instance, $chip)}}" target="_blank">{{$chip}}</a></strong>
</md-chip-template>
</md-chips>
</div>
<div layout-gt-sm="row">
<md-input-container class="md-block" flex-gt-sm>
<label>Memory</label>