Add SessionProvisioner and InstanceProvisionerFactory

Remove AllowedImages and IsDockerHost as it is not really being used for
anything useful
This commit is contained in:
Jonathan Leibiusky @xetorthio
2017-08-24 15:36:59 -03:00
parent 11d9d59975
commit 438fe9f6e7
15 changed files with 187 additions and 131 deletions

View File

@@ -36,14 +36,6 @@ func checkHostnameExists(session *types.Session, hostname string) bool {
return exists
}
func (d *DinD) InstanceAllowedImages() []string {
return []string{
config.GetDindImageName(),
"franela/dind:overlay2-dev",
"franela/ucp:2.4.1",
}
}
func (d *DinD) InstanceNew(session *types.Session, conf types.InstanceConfig) (*types.Instance, error) {
if conf.ImageName == "" {
conf.ImageName = config.GetDindImageName()
@@ -70,15 +62,8 @@ func (d *DinD) InstanceNew(session *types.Session, conf types.InstanceConfig) (*
ServerCert: conf.ServerCert,
ServerKey: conf.ServerKey,
CACert: conf.CACert,
Privileged: false,
HostFQDN: conf.Host,
}
for _, imageName := range d.InstanceAllowedImages() {
if conf.ImageName == imageName {
opts.Privileged = true
break
}
Privileged: true,
}
dockerClient, err := d.factory.GetForSession(session.Id)
@@ -104,8 +89,6 @@ func (d *DinD) InstanceNew(session *types.Session, conf types.InstanceConfig) (*
instance.Session = session
instance.ProxyHost = router.EncodeHost(session.Id, ip, router.HostOpts{})
instance.SessionHost = session.Host
// For now this condition holds through. In the future we might need a more complex logic.
instance.IsDockerHost = opts.Privileged
return instance, nil
}

18
provisioner/factory.go Normal file
View File

@@ -0,0 +1,18 @@
package provisioner
type instanceProvisionerFactory struct {
windows InstanceProvisionerApi
dind InstanceProvisionerApi
}
func NewInstanceProvisionerFactory(w InstanceProvisionerApi, d InstanceProvisionerApi) InstanceProvisionerFactoryApi {
return &instanceProvisionerFactory{windows: w, dind: d}
}
func (p *instanceProvisionerFactory) GetProvisioner(instanceType string) (InstanceProvisionerApi, error) {
if instanceType == "windows" {
return p.windows, nil
} else {
return p.dind, nil
}
}

50
provisioner/overlay.go Normal file
View File

@@ -0,0 +1,50 @@
package provisioner
import (
"fmt"
"log"
"net/url"
"strings"
"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(s *types.Session) error {
dockerClient, err := p.dockerFactory.GetForSession(s.Id)
if err != nil {
// We assume we are out of capacity
return fmt.Errorf("Out of capacity")
}
u, _ := url.Parse(dockerClient.GetDaemonHost())
if u.Host == "" {
s.Host = "localhost"
} else {
chunks := strings.Split(u.Host, ":")
s.Host = chunks[0]
}
if err := dockerClient.CreateNetwork(s.Id); 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.ConnectNetwork(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
}

View File

@@ -7,7 +7,7 @@ import (
"github.com/play-with-docker/play-with-docker/pwd/types"
)
type ProvisionerApi interface {
type InstanceProvisionerApi interface {
InstanceNew(session *types.Session, conf types.InstanceConfig) (*types.Instance, error)
InstanceDelete(session *types.Session, instance *types.Instance) error
@@ -17,3 +17,11 @@ type ProvisionerApi interface {
InstanceUploadFromUrl(instance *types.Instance, fileName, dest, url string) error
InstanceUploadFromReader(instance *types.Instance, fileName, dest string, reader io.Reader) error
}
type SessionProvisionerApi interface {
SessionNew(session *types.Session) error
}
type InstanceProvisionerFactoryApi interface {
GetProvisioner(instanceType string) (InstanceProvisionerApi, error)
}

View File

@@ -110,8 +110,6 @@ func (d *windows) InstanceNew(session *types.Session, conf types.InstanceConfig)
instance.Session = session
instance.ProxyHost = router.EncodeHost(session.Id, instance.IP, router.HostOpts{})
instance.SessionHost = session.Host
// For now this condition holds through. In the future we might need a more complex logic.
instance.IsDockerHost = opts.Privileged
if cli, err := d.factory.GetForInstance(instance); err != nil {
if derr := d.InstanceDelete(session, instance); derr != nil {