Skip to content

Commit

Permalink
Add error to GetSession return values
Browse files Browse the repository at this point in the history
- Add button to copy instance SSH access
  • Loading branch information
marcosnils committed Dec 14, 2017
1 parent 3ffaaaf commit dd6a332
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 26 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@
[[constraint]]
branch = "master"
name = "github.com/satori/go.uuid"

[[constraint]]
name = "github.com/zbindenren/negroni-prometheus"
version = "0.1.1"
26 changes: 22 additions & 4 deletions handlers/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/play-with-docker/play-with-docker/config"
"github.com/play-with-docker/play-with-docker/event"
"github.com/play-with-docker/play-with-docker/pwd"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/negroni"
)
Expand All @@ -27,8 +28,19 @@ var core pwd.PWDApi
var e event.EventApi
var landings = map[string][]byte{}

var latencyHistogramVec = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "pwd_handlers_duration_ms",
Help: "How long it took to process a specific handler, in a specific host",
Buckets: []float64{300, 1200, 5000},
}, []string{"action"})

type HandlerExtender func(h *mux.Router)

func init() {
prometheus.MustRegister(latencyHistogramVec)

}

func Bootstrap(c pwd.PWDApi, ev event.EventApi) {
core = c
e = ev
Expand All @@ -53,6 +65,10 @@ func Register(extend HandlerExtender) {
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}", DeleteInstance).Methods("DELETE")
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/exec", Exec).Methods("POST")

r.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/editor.html", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "www/editor.html")
})

r.HandleFunc("/ooc", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "./www/ooc.html")
}).Methods("GET")
Expand All @@ -64,9 +80,6 @@ func Register(extend HandlerExtender) {
r.HandleFunc("/robots.txt", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "www/robots.txt")
})
r.HandleFunc("/sdk.js", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "www/sdk.js")
})

corsRouter.HandleFunc("/sessions/{sessionId}/ws/", WSH)
r.Handle("/metrics", promhttp.Handler())
Expand All @@ -90,6 +103,7 @@ func Register(extend HandlerExtender) {
}

n := negroni.Classic()

r.PathPrefix("/").Handler(negroni.New(negroni.Wrap(corsHandler(corsRouter))))
n.UseHandler(r)

Expand Down Expand Up @@ -128,7 +142,11 @@ func Register(extend HandlerExtender) {
rr.HandleFunc("/ping", Ping).Methods("GET")
rr.Handle("/metrics", promhttp.Handler())
rr.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
http.Redirect(rw, r, fmt.Sprintf("https://%s", r.Host), http.StatusMovedPermanently)
target := fmt.Sprintf("https://%s%s", r.Host, r.URL.Path)
if len(r.URL.RawQuery) > 0 {
target += "?" + r.URL.RawQuery
}
http.Redirect(rw, r, target, http.StatusMovedPermanently)
})
nr := negroni.Classic()
nr.UseHandler(rr)
Expand Down
8 changes: 6 additions & 2 deletions handlers/close_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import (
"net/http"

"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/storage"
)

func CloseSession(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
sessionId := vars["sessionId"]

session := core.SessionGet(sessionId)
if session == nil {
session, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
rw.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}

if err := core.SessionClose(session); err != nil {
Expand Down
10 changes: 7 additions & 3 deletions handlers/delete_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@ import (
"net/http"

"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/storage"
)

func DeleteInstance(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
sessionId := vars["sessionId"]
instanceName := vars["instanceName"]

s := core.SessionGet(sessionId)
s, err := core.SessionGet(sessionId)
if s != nil {
i := core.InstanceGet(s, instanceName)
err := core.InstanceDelete(s, i)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
} else {
rw.WriteHeader(http.StatusNotFound)
} else if err == storage.NotFoundError {
rw.WriteHeader(http.StatusInternalServerError)
return
} else if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
}
2 changes: 1 addition & 1 deletion handlers/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Exec(rw http.ResponseWriter, req *http.Request) {
return
}

s := core.SessionGet(sessionId)
s, _ := core.SessionGet(sessionId)
if s == nil {
rw.WriteHeader(http.StatusNotFound)
return
Expand Down
10 changes: 9 additions & 1 deletion handlers/file_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ import (
"path/filepath"

"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/storage"
)

func FileUpload(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
sessionId := vars["sessionId"]
instanceName := vars["instanceName"]

s := core.SessionGet(sessionId)
s, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
rw.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
i := core.InstanceGet(s, instanceName)

// allow up to 32 MB which is the default
Expand Down
8 changes: 6 additions & 2 deletions handlers/get_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/play-with-docker/play-with-docker/storage"
)

type SessionInfo struct {
Expand All @@ -18,8 +19,11 @@ func GetSession(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
sessionId := vars["sessionId"]

session := core.SessionGet(sessionId)
if session == nil {
session, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
rw.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
rw.WriteHeader(http.StatusNotFound)
return
}
Expand Down
8 changes: 6 additions & 2 deletions handlers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ import (
"path/filepath"

"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/storage"
)

func Home(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
sessionId := vars["sessionId"]

s := core.SessionGet(sessionId)
if s == nil {
s, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
// Session doesn't exist (can happen if closing the sessions an reloading the page, or similar).
w.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if s.Stack != "" {
go core.SessionDeployStack(s)
Expand Down
6 changes: 5 additions & 1 deletion handlers/new_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ func NewInstance(rw http.ResponseWriter, req *http.Request) {

json.NewDecoder(req.Body).Decode(&body)

s := core.SessionGet(sessionId)
s, err := core.SessionGet(sessionId)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}

playground := core.PlaygroundGet(s.PlaygroundId)
if playground == nil {
Expand Down
2 changes: 2 additions & 0 deletions handlers/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func Ping(rw http.ResponseWriter, req *http.Request) {
defer latencyHistogramVec.WithLabelValues("ping").Observe(float64(time.Since(time.Now()).Nanoseconds()) / 1000000)
// Get system load average of the last 5 minutes and compare it against a threashold.

c, err := docker.NewEnvClient()
Expand All @@ -27,6 +28,7 @@ func Ping(rw http.ResponseWriter, req *http.Request) {
if _, err := c.Info(ctx); err != nil && err == context.DeadlineExceeded {
log.Printf("Docker info took to long to respond\n")
rw.WriteHeader(http.StatusGatewayTimeout)
return
}

a, err := load.Avg()
Expand Down
8 changes: 6 additions & 2 deletions handlers/session_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ func SessionSetup(rw http.ResponseWriter, req *http.Request) {

json.NewDecoder(req.Body).Decode(&body)

s := core.SessionGet(sessionId)
s, err := core.SessionGet(sessionId)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}

err := core.SessionSetup(s, body)
err = core.SessionSetup(s, body)
if err != nil {
if pwd.SessionNotEmpty(err) {
log.Println("Cannot setup a session that contains instances")
Expand Down
5 changes: 3 additions & 2 deletions handlers/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/play-with-docker/play-with-docker/event"
"github.com/play-with-docker/play-with-docker/storage"
"github.com/satori/go.uuid"
)

Expand Down Expand Up @@ -144,8 +145,8 @@ func ws(so *socket) {

sessionId := vars["sessionId"]

session := core.SessionGet(sessionId)
if session == nil {
session, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
log.Printf("Session with id [%s] does not exist!\n", sessionId)
return
}
Expand Down
2 changes: 1 addition & 1 deletion pwd/pwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type PWDApi interface {
SessionClose(session *types.Session) error
SessionGetSmallestViewPort(sessionId string) types.ViewPort
SessionDeployStack(session *types.Session) error
SessionGet(id string) *types.Session
SessionGet(id string) (*types.Session, error)
SessionSetup(session *types.Session, conf SessionSetupConf) error

InstanceNew(session *types.Session, conf types.InstanceConfig) (*types.Instance, error)
Expand Down
6 changes: 3 additions & 3 deletions pwd/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,17 @@ func (p *pwd) SessionDeployStack(s *types.Session) error {
return nil
}

func (p *pwd) SessionGet(sessionId string) *types.Session {
func (p *pwd) SessionGet(sessionId string) (*types.Session, error) {
defer observeAction("SessionGet", time.Now())

s, err := p.storage.SessionGet(sessionId)

if err != nil {
log.Println(err)
return nil
return nil, err
}

return s
return s, nil
}

func (p *pwd) SessionSetup(session *types.Session, sconf SessionSetupConf) error {
Expand Down
2 changes: 1 addition & 1 deletion www/assets/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function() {
'use strict';

var app = angular.module('DockerPlay', ['ngMaterial', 'ngFileUpload']);
var app = angular.module('DockerPlay', ['ngMaterial', 'ngFileUpload', 'ngclipboard']);

// Automatically redirects user to a new session when bypassing captcha.
// Controller keeps code/logic separate from the HTML
Expand Down
5 changes: 5 additions & 0 deletions www/default/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ <h4>{{instance.hostname}}</h4>
</md-card-content>
<md-card-actions>
<md-button class="md-warn md-raised" ng-click="deleteInstance(instance)" ng-disabled="isInstanceBeingDeleted">{{deleteInstanceBtnText}}</md-button>
<md-button class="md-raised" ngclipboard data-clipboard-text="ssh {{instance.proxy_host}}@direct.{{instance.session_host}}">SSH
<md-tooltip md-direction="top">Copied</md-tooltip>
</md-button>
</md-card-actions>
</md-card>
<md-card flex md-theme="default" md-theme-watch >
Expand Down Expand Up @@ -291,6 +294,8 @@ <h2>Settings</h2>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>
<script src="https://cdn.rawgit.com/zenorocha/clipboard.js/master/dist/clipboard.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ngclipboard/1.1.2/ngclipboard.min.js"></script>


<script src="https://cdnjs.cloudflare.com/ajax/libs/danialfarid-angular-file-upload/12.2.13/ng-file-upload-all.min.js" integrity="sha256-LrZq3efIkFX0BooX7x/rjWyYDvMKfFV2HJpy6HBw7cE=" crossorigin="anonymous"></script>
Expand Down

0 comments on commit dd6a332

Please sign in to comment.