forked from play-with-docker/play-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay.go
76 lines (68 loc) · 2.04 KB
/
overlay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package provisioner
import (
"context"
"fmt"
"log"
"net/url"
"strings"
dtypes "github.com/docker/docker/api/types"
"github.com/play-with-docker/play-with-docker/config"
"github.com/play-with-docker/play-with-docker/docker"
"github.com/play-with-docker/play-with-docker/pwd/types"
)
type overlaySessionProvisioner struct {
dockerFactory docker.FactoryApi
}
func NewOverlaySessionProvisioner(df docker.FactoryApi) SessionProvisionerApi {
return &overlaySessionProvisioner{dockerFactory: df}
}
func (p *overlaySessionProvisioner) SessionNew(ctx context.Context, s *types.Session) error {
dockerClient, err := p.dockerFactory.GetForSession(s)
if err != nil {
// We assume we are out of capacity
return fmt.Errorf("Out of capacity")
}
u, _ := url.Parse(dockerClient.DaemonHost())
if u.Host == "" {
s.Host = "localhost"
} else {
chunks := strings.Split(u.Host, ":")
s.Host = chunks[0]
}
opts := dtypes.NetworkCreate{Driver: "overlay", Attachable: true}
if err := dockerClient.NetworkCreate(s.Id, opts); err != nil {
log.Println("ERROR NETWORKING", err)
return err
}
log.Printf("Network [%s] created for session [%s]\n", s.Id, s.Id)
ip, err := dockerClient.NetworkConnect(config.L2ContainerName, s.Id, s.PwdIpAddress)
if err != nil {
log.Println(err)
return err
}
s.PwdIpAddress = ip
log.Printf("Connected %s to network [%s]\n", config.PWDContainerName, s.Id)
return nil
}
func (p *overlaySessionProvisioner) SessionClose(s *types.Session) error {
// Disconnect L2 router from the network
dockerClient, err := p.dockerFactory.GetForSession(s)
if err != nil {
log.Println(err)
return err
}
if err := dockerClient.NetworkDisconnect(config.L2ContainerName, s.Id); err != nil {
if !strings.Contains(err.Error(), "is not connected to the network") {
log.Println("ERROR NETWORKING", err)
return err
}
}
log.Printf("Disconnected l2 from network [%s]\n", s.Id)
if err := dockerClient.NetworkDelete(s.Id); err != nil {
if !strings.Contains(err.Error(), "not found") {
log.Println(err)
return err
}
}
return nil
}