UI modifications for stack creation

This commit is contained in:
Jonathan Leibiusky @xetorthio
2017-05-16 17:37:34 -03:00
parent bfa90865a1
commit 1634200ef7
6 changed files with 169 additions and 28 deletions

View File

@@ -1,9 +1,7 @@
package handlers
import (
"log"
"net/http"
"path"
"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/services"
@@ -13,29 +11,9 @@ 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)
if s.Stack != "" {
go s.DeployStack()
}
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)
}

View File

@@ -39,7 +39,7 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
}
duration := services.GetDuration(reqDur)
s, err := services.NewSession(duration)
s, err := services.NewSession(duration, stack)
if err != nil {
log.Println(err)
//TODO: Return some error code
@@ -54,7 +54,7 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
}
if stack != "" {
http.Redirect(rw, req, fmt.Sprintf("http://%s/p/%s?stack=%s", hostname, s.Id, stack), http.StatusFound)
http.Redirect(rw, req, fmt.Sprintf("http://%s/p/%s", hostname, s.Id), http.StatusFound)
return
}
http.Redirect(rw, req, fmt.Sprintf("http://%s/p/%s", hostname, s.Id), http.StatusFound)