Move stack creation when starting session
- If stack is not found return 400 status code
This commit is contained in:
6
api.go
6
api.go
@@ -73,11 +73,7 @@ func main() {
|
|||||||
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}", handlers.DeleteInstance).Methods("DELETE")
|
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}", handlers.DeleteInstance).Methods("DELETE")
|
||||||
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/exec", handlers.Exec).Methods("POST")
|
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/exec", handlers.Exec).Methods("POST")
|
||||||
|
|
||||||
h := func(w http.ResponseWriter, r *http.Request) {
|
r.HandleFunc("/p/{sessionId}", handlers.Home).Methods("GET")
|
||||||
http.ServeFile(w, r, "./www/index.html")
|
|
||||||
}
|
|
||||||
|
|
||||||
r.HandleFunc("/p/{sessionId}", h).Methods("GET")
|
|
||||||
r.PathPrefix("/assets").Handler(http.FileServer(http.Dir("./www")))
|
r.PathPrefix("/assets").Handler(http.FileServer(http.Dir("./www")))
|
||||||
r.HandleFunc("/robots.txt", func(rw http.ResponseWriter, r *http.Request) {
|
r.HandleFunc("/robots.txt", func(rw http.ResponseWriter, r *http.Request) {
|
||||||
http.ServeFile(rw, r, "www/robots.txt")
|
http.ServeFile(rw, r, "www/robots.txt")
|
||||||
|
|||||||
41
handlers/home.go
Normal file
41
handlers/home.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"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"]
|
||||||
|
|
||||||
|
stack := r.URL.Query().Get("stack")
|
||||||
|
s := services.GetSession(sessionId)
|
||||||
|
if stack != "" {
|
||||||
|
go deployStack(s, stack)
|
||||||
|
}
|
||||||
|
http.ServeFile(w, r, "./www/index.html")
|
||||||
|
}
|
||||||
|
|
||||||
|
func deployStack(s *services.Session, stack string) {
|
||||||
|
i, err := services.NewInstance(s, services.InstanceConfig{})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error creating instance for stack [%s]: %s\n", stack, err)
|
||||||
|
}
|
||||||
|
err = i.UploadFromURL("https://raw.githubusercontent.com/play-with-docker/stacks/master" + stack)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error uploading stack file [%s]: %s\n", stack, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fileName := path.Base(stack)
|
||||||
|
code, err := services.Exec(i.Name, []string{"docker-compose", "-f", "/var/run/pwd/uploads/" + fileName, "up", "-d"})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error executing stack [%s]: %s\n", stack, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Stack execution finished with code %d\n", code)
|
||||||
|
}
|
||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
|
||||||
|
|
||||||
"github.com/play-with-docker/play-with-docker/config"
|
"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/services"
|
||||||
@@ -27,16 +26,24 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
|
|||||||
reqDur := req.Form.Get("session-duration")
|
reqDur := req.Form.Get("session-duration")
|
||||||
stack := req.Form.Get("stack")
|
stack := req.Form.Get("stack")
|
||||||
|
|
||||||
|
if stack != "" {
|
||||||
|
if ok, err := stackExists(stack); err != nil {
|
||||||
|
log.Printf("Error retrieving stack: %s", err)
|
||||||
|
rw.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
} else if !ok {
|
||||||
|
log.Printf("Stack [%s] could not be found", stack)
|
||||||
|
rw.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
duration := services.GetDuration(reqDur)
|
duration := services.GetDuration(reqDur)
|
||||||
s, err := services.NewSession(duration)
|
s, err := services.NewSession(duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
//TODO: Return some error code
|
//TODO: Return some error code
|
||||||
} else {
|
} else {
|
||||||
s.StackFile = stack
|
|
||||||
if stack != "" {
|
|
||||||
go deployStack(s)
|
|
||||||
}
|
|
||||||
hostname := fmt.Sprintf("%s.%s", config.PWDCName, req.Host)
|
hostname := fmt.Sprintf("%s.%s", config.PWDCName, req.Host)
|
||||||
// If request is not a form, return sessionId in the body
|
// If request is not a form, return sessionId in the body
|
||||||
if req.Header.Get("X-Requested-With") == "XMLHttpRequest" {
|
if req.Header.Get("X-Requested-With") == "XMLHttpRequest" {
|
||||||
@@ -45,25 +52,22 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
|
|||||||
json.NewEncoder(rw).Encode(resp)
|
json.NewEncoder(rw).Encode(resp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if stack != "" {
|
||||||
|
http.Redirect(rw, req, fmt.Sprintf("http://%s/p/%s?stack=%s", hostname, s.Id, stack), http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
http.Redirect(rw, req, fmt.Sprintf("http://%s/p/%s", hostname, s.Id), http.StatusFound)
|
http.Redirect(rw, req, fmt.Sprintf("http://%s/p/%s", hostname, s.Id), http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func deployStack(s *services.Session) {
|
func stackExists(stack string) (bool, error) {
|
||||||
i, err := services.NewInstance(s, services.InstanceConfig{})
|
resp, err := http.Head("https://raw.githubusercontent.com/play-with-docker/stacks/master" + stack)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error creating instance for stack [%s]: %s\n", s.StackFile, err)
|
return false, err
|
||||||
}
|
|
||||||
err = i.UploadFromURL("https://raw.githubusercontent.com/play-with-docker/stacks/master" + s.StackFile)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error uploading stack file [%s]: %s\n", s.StackFile, err)
|
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
fileName := path.Base(s.StackFile)
|
return resp.StatusCode == 200, nil
|
||||||
code, err := services.Exec(i.Name, []string{"docker-compose", "-f", "/var/run/pwd/uploads/" + fileName, "up", "-d"})
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error executing stack [%s]: %s\n", s.StackFile, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Stack execution finished with code %d\n", code)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,7 +56,6 @@ type Session struct {
|
|||||||
scheduled bool `json:"-"`
|
scheduled bool `json:"-"`
|
||||||
ticker *time.Ticker `json:"-"`
|
ticker *time.Ticker `json:"-"`
|
||||||
PwdIpAddress string `json:"pwd_ip_address"`
|
PwdIpAddress string `json:"pwd_ip_address"`
|
||||||
StackFile string `json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) Lock() {
|
func (s *Session) Lock() {
|
||||||
|
|||||||
Reference in New Issue
Block a user