Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
xetorthio committed Jul 25, 2017
1 parent 8424479 commit 53e6078
Show file tree
Hide file tree
Showing 22 changed files with 307 additions and 144 deletions.
9 changes: 4 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"flag"
"log"
"os"
"regexp"
"time"
Expand All @@ -21,7 +20,7 @@ const (
var NameFilter = regexp.MustCompile(PWDHostPortGroupRegex)
var AliasFilter = regexp.MustCompile(AliasPortGroupRegex)

var SSLPortNumber, PortNumber, Key, Cert, SessionsFile, PWDContainerName, PWDCName, HashKey, SSHKeyPath string
var SSLPortNumber, PortNumber, Key, Cert, SessionsFile, PWDContainerName, L2ContainerName, L2Subdomain, PWDCName, HashKey, SSHKeyPath string
var MaxLoadAvg float64

func ParseFlags() {
Expand All @@ -31,13 +30,13 @@ func ParseFlags() {
flag.StringVar(&Cert, "cert", "./pwd/server.pem", "Give a SSL cert")
flag.StringVar(&SessionsFile, "save", "./pwd/sessions", "Tell where to store sessions file")
flag.StringVar(&PWDContainerName, "name", "pwd", "Container name used to run PWD (used to be able to connect it to the networks it creates)")
flag.StringVar(&PWDCName, "cname", "host1", "CNAME given to this host")
flag.StringVar(&L2ContainerName, "l2", "l2", "Container name used to run L2 Router")
flag.StringVar(&L2Subdomain, "l2-subdomain", "direct", "Subdomain to the L2 Router")
flag.StringVar(&PWDCName, "cname", "", "CNAME given to this host")
flag.StringVar(&HashKey, "hash_key", "salmonrosado", "Hash key to use for cookies")
flag.Float64Var(&MaxLoadAvg, "maxload", 100, "Maximum allowed load average before failing ping requests")
flag.StringVar(&SSHKeyPath, "ssh_key_path", "", "SSH Private Key to use")
flag.Parse()

log.Println("*****************************", SSHKeyPath)
}
func GetDindImageName() string {
dindImage := os.Getenv("DIND_IMAGE")
Expand Down
46 changes: 14 additions & 32 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
version: '3.2'
services:
haproxy:
container_name: proxy
container_name: haproxy
image: haproxy
ports:
- "80:8080"
- "443:8443"

volumes:
- ./haproxy:/usr/local/etc/haproxy
pwd1:

pwd:
# pwd daemon container always needs to be named this way
container_name: pwd1
container_name: pwd
# use the latest golang image
image: golang
# go to the right place and starts the app
command: /bin/sh -c 'ssh-keygen -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key >/dev/null; cd /go/src/github.com/play-with-docker/play-with-docker; go run api.go -save /pwd/sessions1 -name pwd1 -cname host1'
command: /bin/sh -c 'ssh-keygen -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key >/dev/null; cd /go/src/github.com/play-with-docker/play-with-docker; go run api.go -save /pwd/sessions -name l2'
volumes:
# since this app creates networks and launches containers, we need to talk to docker daemon
- /var/run/docker.sock:/var/run/docker.sock
Expand All @@ -24,37 +23,20 @@ services:
- sessions:/pwd
environment:
GOOGLE_RECAPTCHA_DISABLED: "true"
ports:
- "1022:1022"
pwd2:
# pwd daemon container always needs to be named this way
container_name: pwd2
l2:
container_name: l2
# use the latest golang image
image: golang
# go to the right place and starts the app
command: /bin/sh -c 'ssh-keygen -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key >/dev/null; cd /go/src/github.com/play-with-docker/play-with-docker; go run api.go -save /pwd/sessions2 -name pwd2 -cname host2'
command: /bin/sh -c 'ssh-keygen -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key >/dev/null; cd /go/src/github.com/play-with-docker/play-with-docker/router/l2; go run l2.go -ssh_key_path /etc/ssh/ssh_host_rsa_key -name l2 -save /pwd/networks'
volumes:
# since this app creates networks and launches containers, we need to talk to docker daemon
- /var/run/docker.sock:/var/run/docker.sock
# mount the box mounted shared folder to the container
- $GOPATH/src:/go/src
- sessions:/pwd
environment:
GOOGLE_RECAPTCHA_DISABLED: "true"
ports:
- "1023:1022"
prometheus:
container_name: prometheus
image: prom/prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
grafana:
container_name: grafana
image: grafana/grafana
- /var/run/docker.sock:/var/run/docker.sock
- networks:/pwd
ports:
- "3000:3000"
volumes:
- grafana:/var/lib/grafana
- "8022:22"
- "8053:53"
- "443:443"
volumes:
sessions:
grafana:
networks:
14 changes: 4 additions & 10 deletions handlers/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"log"
"os"

"github.com/docker/docker/client"
"github.com/googollee/go-socket.io"
"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/event"
"github.com/play-with-docker/play-with-docker/provider"
"github.com/play-with-docker/play-with-docker/pwd"
"github.com/play-with-docker/play-with-docker/storage"
)
Expand All @@ -18,23 +17,18 @@ var e event.EventApi
var ws *socketio.Server

func Bootstrap() {
c, err := client.NewEnvClient()
if err != nil {
log.Fatal(err)
}

d := docker.NewDocker(c)
sp := provider.NewLocalSessionProvider()

e = event.NewLocalBroker()

t := pwd.NewScheduler(e, d)
t := pwd.NewScheduler(e, sp)

s, err := storage.NewFileStorage(config.SessionsFile)

if err != nil && !os.IsNotExist(err) {
log.Fatal("Error initializing StorageAPI: ", err)
}
core = pwd.NewPWD(d, t, e, s)
core = pwd.NewPWD(sp, t, e, s)

}

Expand Down
5 changes: 4 additions & 1 deletion handlers/new_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
log.Println(err)
//TODO: Return some error code
} else {
hostname := fmt.Sprintf("%s.%s", config.PWDCName, req.Host)
hostname := req.Host
if config.PWDCName != "" {
hostname = fmt.Sprintf("%s.%s", config.PWDCName, req.Host)
}
// If request is not a form, return sessionId in the body
if req.Header.Get("X-Requested-With") == "XMLHttpRequest" {
resp := NewSessionResponse{SessionId: s.Id, Hostname: hostname}
Expand Down
26 changes: 7 additions & 19 deletions haproxy/haproxy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,13 @@ frontend http-in
bind *:8080

acl host_localhost hdr(host) localhost
acl host_pwd1 hdr_reg(host) -i ^.*\.?host1\.localhost?:?.*$
acl host_pwd2 hdr_reg(host) -i ^.*\.?host2\.localhost?:?.*$
acl host_direct_localhost hdr_reg(host) -i ^.*\.direct\.localhost?:?.*$

use_backend all if host_localhost
use_backend pwd1 if host_pwd1
use_backend pwd2 if host_pwd2
use_backend pwd if host_localhost
use_backend l2 if host_direct_localhost

backend all
balance roundrobin
backend pwd
server node1 pwd:3000

option httpchk GET /ping HTTP/1.0
http-check expect rstatus 200
default-server inter 3s fall 3 rise 2

server node1 pwd1:3000 check
server node2 pwd2:3000 check

backend pwd1
server node1 pwd1:3000

backend pwd2
server node2 pwd2:3000
backend l2
server node2 l2:443
36 changes: 36 additions & 0 deletions provider/local_session_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package provider

import (
"sync"

"github.com/docker/docker/client"
"github.com/play-with-docker/play-with-docker/docker"
)

type localSessionProvider struct {
rw sync.Mutex

docker docker.DockerApi
}

func (p *localSessionProvider) GetDocker(sessionId string) (docker.DockerApi, error) {
p.rw.Lock()
defer p.rw.Unlock()

if p.docker != nil {
return p.docker, nil
}

c, err := client.NewEnvClient()
if err != nil {
return nil, err
}
d := docker.NewDocker(c)

p.docker = d
return d, nil
}

func NewLocalSessionProvider() *localSessionProvider {
return &localSessionProvider{}
}
10 changes: 10 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package provider

import "github.com/play-with-docker/play-with-docker/docker"

type InstanceProvider interface {
}

type SessionProvider interface {
GetDocker(sessionId string) (docker.DockerApi, error)
}
15 changes: 9 additions & 6 deletions pwd/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
)

func TestClientNew(t *testing.T) {
docker := &mockDocker{}
d := &mockDocker{}
tasks := &mockTasks{}
e := event.NewLocalBroker()
storage := &mockStorage{}
sp := &mockSessionProvider{docker: d}

p := NewPWD(docker, tasks, e, storage)
p := NewPWD(sp, tasks, e, storage)

session, err := p.SessionNew(time.Hour, "", "", "")
assert.Nil(t, err)
Expand All @@ -27,12 +28,13 @@ func TestClientNew(t *testing.T) {
assert.Contains(t, session.Clients, client)
}
func TestClientCount(t *testing.T) {
docker := &mockDocker{}
d := &mockDocker{}
tasks := &mockTasks{}
e := event.NewLocalBroker()
storage := &mockStorage{}
sp := &mockSessionProvider{docker: d}

p := NewPWD(docker, tasks, e, storage)
p := NewPWD(sp, tasks, e, storage)

session, err := p.SessionNew(time.Hour, "", "", "")
assert.Nil(t, err)
Expand All @@ -45,9 +47,10 @@ func TestClientCount(t *testing.T) {
func TestClientResizeViewPort(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(1)
docker := &mockDocker{}
d := &mockDocker{}
tasks := &mockTasks{}
e := event.NewLocalBroker()
sp := &mockSessionProvider{docker: d}

broadcastedSessionId := ""
broadcastedArgs := []interface{}{}
Expand All @@ -60,7 +63,7 @@ func TestClientResizeViewPort(t *testing.T) {

storage := &mockStorage{}

p := NewPWD(docker, tasks, e, storage)
p := NewPWD(sp, tasks, e, storage)

session, err := p.SessionNew(time.Hour, "", "", "")
assert.Nil(t, err)
Expand Down
7 changes: 4 additions & 3 deletions pwd/collect_stats_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

dockerTypes "github.com/docker/docker/api/types"
units "github.com/docker/go-units"
"github.com/play-with-docker/play-with-docker/docker"
"github.com/play-with-docker/play-with-docker/provider"
"github.com/play-with-docker/play-with-docker/pwd/types"
)

Expand All @@ -20,11 +20,12 @@ type collectStatsTask struct {
previousCPU uint64
previousSystem uint64

docker docker.DockerApi
sessionProvider provider.SessionProvider
}

func (c collectStatsTask) Run(i *types.Instance) error {
reader, err := c.docker.GetContainerStats(i.Name)
docker, _ := c.sessionProvider.GetDocker(i.SessionId)
reader, err := docker.GetContainerStats(i.Name)
if err != nil {
log.Println("Error while trying to collect instance stats", err)
return err
Expand Down
Loading

0 comments on commit 53e6078

Please sign in to comment.