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

Initial commit

This commit is contained in:
Jonathan Leibiusky (@xetorthio)
2016-10-08 03:12:48 +02:00
parent f2ae4344fd
commit dde49d8700
19 changed files with 5031 additions and 0 deletions

89
services/docker.go Normal file
View File

@@ -0,0 +1,89 @@
package services
import (
"log"
"strings"
ptypes "github.com/xetorthio/play-with-docker/types"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"golang.org/x/net/context"
)
var c *client.Client
func init() {
var err error
c, err = client.NewEnvClient()
if err != nil {
// this wont happen if daemon is offline, only for some critical errors
log.Fatal("Cannot initialize docker client")
}
}
func GetContainerInfo(id string) (types.ContainerJSON, error) {
return c.ContainerInspect(context.Background(), id)
}
func CreateNetwork(name string) error {
opts := types.NetworkCreate{Attachable: true}
_, err := c.NetworkCreate(context.Background(), name, opts)
if err != nil {
return err
}
return nil
}
func GetExecConnection(id string, ctx context.Context) (*types.HijackedResponse, error) {
conf := types.ExecConfig{Tty: true, AttachStdin: true, AttachStderr: true, AttachStdout: true, Cmd: []string{"sh"}}
resp, err := c.ContainerExecCreate(ctx, id, conf)
if err != nil {
return nil, err
}
//err = c.ContainerExecStart(context.Background(), resp.ID, types.ExecStartCheck{Tty: true})
//if err != nil {
//return nil, err
//}
conn, err := c.ContainerExecAttach(ctx, resp.ID, conf)
if err != nil {
return nil, err
}
return &conn, nil
}
func CreateInstance(net string) (*ptypes.Instance, error) {
h := &container.HostConfig{NetworkMode: container.NetworkMode(net), Privileged: true}
conf := &container.Config{Image: "docker:dind"}
container, err := c.ContainerCreate(context.Background(), conf, h, nil, "")
if err != nil {
return nil, err
}
err = c.ContainerStart(context.Background(), container.ID, types.ContainerStartOptions{})
if err != nil {
return nil, err
}
cinfo, err := GetContainerInfo(container.ID)
if err != nil {
return nil, err
}
return &ptypes.Instance{Name: strings.Replace(cinfo.Name, "/", "", 1), IP: cinfo.NetworkSettings.Networks[net].IPAddress}, nil
}
func DeleteContainer(id string) error {
return c.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{Force: true})
}

39
services/instance.go Normal file
View File

@@ -0,0 +1,39 @@
package services
import "github.com/xetorthio/play-with-docker/types"
var instances map[string]map[string]*types.Instance
func init() {
instances = make(map[string]map[string]*types.Instance)
}
func NewInstance(session *types.Session) (*types.Instance, error) {
//TODO: Validate that a session can only have 10 instances
//TODO: Create in redis
instance, err := CreateInstance(session.Id)
if err != nil {
return nil, err
}
if instances[session.Id] == nil {
instances[session.Id] = make(map[string]*types.Instance)
}
instances[session.Id][instance.Name] = instance
return instance, nil
}
func GetInstance(session *types.Session, instanceId string) *types.Instance {
//TODO: Use redis
i := instances[session.Id][instanceId]
return i
}
func DeleteInstance(session *types.Session, instance *types.Instance) error {
//TODO: Use redis
delete(instances[session.Id], instance.Name)
return DeleteContainer(instance.Name)
}

39
services/session.go Normal file
View File

@@ -0,0 +1,39 @@
package services
import (
"github.com/twinj/uuid"
"github.com/xetorthio/play-with-docker/types"
)
var sessions map[string]*types.Session
func init() {
sessions = make(map[string]*types.Session)
}
func NewSession() (*types.Session, error) {
s := &types.Session{}
s.Id = uuid.NewV4().String()
s.Instances = map[string]*types.Instance{}
//TODO: Store in something like redis
sessions[s.Id] = s
if err := CreateNetwork(s.Id); err != nil {
return nil, err
}
//TODO: Schedule deletion after an hour
return s, nil
}
func GetSession(sessionId string) *types.Session {
//TODO: Use redis
s := sessions[sessionId]
if instances[sessionId] != nil {
s.Instances = instances[sessionId]
}
return s
}