WIP
This commit is contained in:
38
handlers/bootstrap.go
Normal file
38
handlers/bootstrap.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/play-with-docker/play-with-docker/docker"
|
||||
"github.com/play-with-docker/play-with-docker/pwd"
|
||||
)
|
||||
|
||||
var core pwd.PWDApi
|
||||
var Broadcast pwd.BroadcastApi
|
||||
|
||||
func Bootstrap() {
|
||||
c, err := client.NewEnvClient()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
d := docker.NewDocker(c)
|
||||
|
||||
Broadcast, err = pwd.NewBroadcast(WS, WSError)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
t := pwd.NewScheduler(Broadcast, d)
|
||||
|
||||
s := pwd.NewStorage()
|
||||
|
||||
core = pwd.NewPWD(d, t, Broadcast, s)
|
||||
|
||||
err = core.SessionLoadAndPrepare()
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
log.Fatal("Error decoding sessions from disk ", err)
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/play-with-docker/play-with-docker/services"
|
||||
)
|
||||
|
||||
func DeleteInstance(rw http.ResponseWriter, req *http.Request) {
|
||||
@@ -12,11 +11,9 @@ func DeleteInstance(rw http.ResponseWriter, req *http.Request) {
|
||||
sessionId := vars["sessionId"]
|
||||
instanceName := vars["instanceName"]
|
||||
|
||||
s := services.GetSession(sessionId)
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
i := services.GetInstance(s, instanceName)
|
||||
err := services.DeleteInstance(s, i)
|
||||
s := core.SessionGet(sessionId)
|
||||
i := core.InstanceGet(s, instanceName)
|
||||
err := core.InstanceDelete(s, i)
|
||||
if err != nil {
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/play-with-docker/play-with-docker/config"
|
||||
"github.com/play-with-docker/play-with-docker/services"
|
||||
)
|
||||
|
||||
func DnsRequest(w dns.ResponseWriter, r *dns.Msg) {
|
||||
@@ -37,7 +36,7 @@ func DnsRequest(w dns.ResponseWriter, r *dns.Msg) {
|
||||
|
||||
match := config.AliasFilter.FindStringSubmatch(question)
|
||||
|
||||
i := services.FindInstanceByAlias(match[2], match[1])
|
||||
i := core.InstanceFindByAlias(match[2], match[1])
|
||||
|
||||
m := new(dns.Msg)
|
||||
m.SetReply(r)
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/play-with-docker/play-with-docker/services"
|
||||
)
|
||||
|
||||
type execRequest struct {
|
||||
@@ -19,6 +18,7 @@ type execResponse struct {
|
||||
|
||||
func Exec(rw http.ResponseWriter, req *http.Request) {
|
||||
vars := mux.Vars(req)
|
||||
sessionId := vars["sessionId"]
|
||||
instanceName := vars["instanceName"]
|
||||
|
||||
var er execRequest
|
||||
@@ -28,7 +28,18 @@ func Exec(rw http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
code, err := services.Exec(instanceName, er.Cmd)
|
||||
s := core.SessionGet(sessionId)
|
||||
if s == nil {
|
||||
rw.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
i := core.InstanceGet(s, instanceName)
|
||||
if i == nil {
|
||||
rw.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
code, err := core.InstanceExec(i, er.Cmd)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/play-with-docker/play-with-docker/services"
|
||||
)
|
||||
|
||||
func FileUpload(rw http.ResponseWriter, req *http.Request) {
|
||||
@@ -13,14 +12,14 @@ func FileUpload(rw http.ResponseWriter, req *http.Request) {
|
||||
sessionId := vars["sessionId"]
|
||||
instanceName := vars["instanceName"]
|
||||
|
||||
s := services.GetSession(sessionId)
|
||||
i := services.GetInstance(s, instanceName)
|
||||
s := core.SessionGet(sessionId)
|
||||
i := core.InstanceGet(s, instanceName)
|
||||
|
||||
// allow up to 32 MB which is the default
|
||||
|
||||
// has a url query parameter, ignore body
|
||||
if url := req.URL.Query().Get("url"); url != "" {
|
||||
err := i.UploadFromURL(req.URL.Query().Get("url"))
|
||||
err := core.InstanceUploadFromUrl(i, req.URL.Query().Get("url"))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
@@ -3,11 +3,9 @@ package handlers
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/play-with-docker/play-with-docker/services"
|
||||
)
|
||||
|
||||
func GetInstanceImages(rw http.ResponseWriter, req *http.Request) {
|
||||
instanceImages := services.InstanceImages()
|
||||
instanceImages := core.InstanceAllowedImages()
|
||||
json.NewEncoder(rw).Encode(instanceImages)
|
||||
}
|
||||
|
||||
@@ -5,14 +5,13 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/play-with-docker/play-with-docker/services"
|
||||
)
|
||||
|
||||
func GetSession(rw http.ResponseWriter, req *http.Request) {
|
||||
vars := mux.Vars(req)
|
||||
sessionId := vars["sessionId"]
|
||||
|
||||
session := services.GetSession(sessionId)
|
||||
session := core.SessionGet(sessionId)
|
||||
|
||||
if session == nil {
|
||||
rw.WriteHeader(http.StatusNotFound)
|
||||
|
||||
@@ -4,16 +4,15 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/play-with-docker/play-with-docker/services"
|
||||
)
|
||||
|
||||
func Home(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sessionId := vars["sessionId"]
|
||||
|
||||
s := services.GetSession(sessionId)
|
||||
s := core.SessionGet(sessionId)
|
||||
if s.Stack != "" {
|
||||
go s.DeployStack()
|
||||
go core.SessionDeployStack(s)
|
||||
}
|
||||
http.ServeFile(w, r, "./www/index.html")
|
||||
}
|
||||
|
||||
@@ -6,27 +6,25 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/play-with-docker/play-with-docker/services"
|
||||
"github.com/play-with-docker/play-with-docker/pwd"
|
||||
)
|
||||
|
||||
func NewInstance(rw http.ResponseWriter, req *http.Request) {
|
||||
vars := mux.Vars(req)
|
||||
sessionId := vars["sessionId"]
|
||||
|
||||
body := services.InstanceConfig{}
|
||||
body := pwd.InstanceConfig{}
|
||||
|
||||
json.NewDecoder(req.Body).Decode(&body)
|
||||
|
||||
s := services.GetSession(sessionId)
|
||||
s := core.SessionGet(sessionId)
|
||||
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
if len(s.Instances) >= 5 {
|
||||
rw.WriteHeader(http.StatusConflict)
|
||||
return
|
||||
}
|
||||
|
||||
i, err := services.NewInstance(s, body)
|
||||
i, err := core.InstanceNew(s, body)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/play-with-docker/play-with-docker/config"
|
||||
"github.com/play-with-docker/play-with-docker/services"
|
||||
"github.com/play-with-docker/play-with-docker/recaptcha"
|
||||
)
|
||||
|
||||
type NewSessionResponse struct {
|
||||
@@ -17,7 +17,7 @@ type NewSessionResponse struct {
|
||||
|
||||
func NewSession(rw http.ResponseWriter, req *http.Request) {
|
||||
req.ParseForm()
|
||||
if !services.IsHuman(req, rw) {
|
||||
if !recaptcha.IsHuman(req, rw) {
|
||||
// User it not a human
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
@@ -38,8 +38,8 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
}
|
||||
duration := services.GetDuration(reqDur)
|
||||
s, err := services.NewSession(duration, stack)
|
||||
duration := config.GetDuration(reqDur)
|
||||
s, err := core.SessionNew(duration, stack, "")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
//TODO: Return some error code
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/play-with-docker/play-with-docker/config"
|
||||
"github.com/play-with-docker/play-with-docker/services"
|
||||
)
|
||||
|
||||
func getTargetInfo(vars map[string]string, req *http.Request) (string, string) {
|
||||
@@ -29,7 +28,7 @@ func getTargetInfo(vars map[string]string, req *http.Request) (string, string) {
|
||||
}
|
||||
|
||||
if alias != "" {
|
||||
instance := services.FindInstanceByAlias(sessionPrefix, alias)
|
||||
instance := core.InstanceFindByAlias(sessionPrefix, alias)
|
||||
if instance != nil {
|
||||
node = instance.IP
|
||||
return node, port
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
|
||||
vhost "github.com/inconshreveable/go-vhost"
|
||||
"github.com/play-with-docker/play-with-docker/config"
|
||||
"github.com/play-with-docker/play-with-docker/services"
|
||||
)
|
||||
|
||||
func StartTLSProxy(port string) {
|
||||
@@ -51,7 +50,7 @@ func StartTLSProxy(port string) {
|
||||
} else {
|
||||
alias := match[1]
|
||||
sessionPrefix := match[2]
|
||||
instance := services.FindInstanceByAlias(sessionPrefix, alias)
|
||||
instance := core.InstanceFindByAlias(sessionPrefix, alias)
|
||||
if instance != nil {
|
||||
targetIP = instance.IP
|
||||
} else {
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/googollee/go-socket.io"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/play-with-docker/play-with-docker/services"
|
||||
)
|
||||
|
||||
func WS(so socketio.Socket) {
|
||||
@@ -19,74 +18,36 @@ func WS(so socketio.Socket) {
|
||||
|
||||
sessionId := vars["sessionId"]
|
||||
|
||||
session := services.GetSession(sessionId)
|
||||
session := core.SessionGet(sessionId)
|
||||
if session == nil {
|
||||
log.Printf("Session with id [%s] does not exist!\n", sessionId)
|
||||
return
|
||||
}
|
||||
|
||||
session.AddNewClient(services.NewClient(so, session))
|
||||
}
|
||||
func WSError(so socketio.Socket) {
|
||||
log.Println("error ws")
|
||||
}
|
||||
so.Join(session.Id)
|
||||
|
||||
/*
|
||||
so.Join(sessionId)
|
||||
client := core.ClientNew(so.Id(), session)
|
||||
|
||||
// TODO: Reset terminal geometry
|
||||
so.On("session close", func() {
|
||||
core.SessionClose(session)
|
||||
})
|
||||
|
||||
so.On("resize", func(cols, rows int) {
|
||||
// TODO: Reset terminal geometry
|
||||
so.On("terminal in", func(name, data string) {
|
||||
// User wrote something on the terminal. Need to write it to the instance terminal
|
||||
instance := core.InstanceGet(session, name)
|
||||
core.InstanceWriteToTerminal(instance, data)
|
||||
})
|
||||
|
||||
so.On("viewport resize", func(cols, rows uint) {
|
||||
// User resized his viewport
|
||||
core.ClientResizeViewPort(client, cols, rows)
|
||||
})
|
||||
|
||||
so.On("disconnection", func() {
|
||||
//TODO: reset the best terminal geometry
|
||||
core.ClientClose(client)
|
||||
})
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
session := services.GetSession(sessionId)
|
||||
instance := services.GetInstance(session, instanceName)
|
||||
|
||||
if instance.Stdout == nil {
|
||||
id, err := services.CreateExecConnection(instance.Name, ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
conn, err := services.AttachExecConnection(id, ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
encoder := encoding.Replacement.NewEncoder()
|
||||
instance.Conn = conn
|
||||
instance.Stdout = &cookoo.MultiWriter{}
|
||||
instance.Stdout.Init()
|
||||
u1 := uuid.NewV4()
|
||||
instance.Stdout.AddWriter(u1.String(), ws)
|
||||
go func() {
|
||||
io.Copy(encoder.Writer(instance.Stdout), instance.Conn.Reader)
|
||||
instance.Stdout.RemoveWriter(u1.String())
|
||||
}()
|
||||
go func() {
|
||||
io.Copy(instance.Conn.Conn, ws)
|
||||
instance.Stdout.RemoveWriter(u1.String())
|
||||
}()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
}
|
||||
} else {
|
||||
u1 := uuid.NewV4()
|
||||
instance.Stdout.AddWriter(u1.String(), ws)
|
||||
|
||||
go func() {
|
||||
io.Copy(instance.Conn.Conn, ws)
|
||||
instance.Stdout.RemoveWriter(u1.String())
|
||||
}()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func WSError(so socketio.Socket) {
|
||||
log.Println("error ws")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user