From f82286d1d1c2aadcb3bce3c74420ade7aaf3ea5d Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Fri, 28 Jul 2017 18:01:23 -0300 Subject: [PATCH 01/12] Add new UCP image --- pwd/instance.go | 11 +++++++++-- pwd/session.go | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pwd/instance.go b/pwd/instance.go index 4482728..a9c1660 100644 --- a/pwd/instance.go +++ b/pwd/instance.go @@ -283,12 +283,19 @@ func (p *pwd) InstanceAllowedImages() []string { return []string{ config.GetDindImageName(), "franela/dind:overlay2-dev", - "franela/ucp:2.4.1", + "franela/ucp:2.1.5", } } func (p *pwd) InstanceExec(instance *types.Instance, cmd []string) (int, error) { defer observeAction("InstanceExec", time.Now()) - return p.docker.Exec(instance.Name, cmd) + b := bytes.NewBufferString("") + if c, err := p.docker.ExecAttach(instance.Name, cmd, b); c > 0 { + log.Println(b.String()) + return c, fmt.Errorf("Error %d running command [%s]", c, cmd) + } else if err != nil { + return -1, err + } + return 0, nil } diff --git a/pwd/session.go b/pwd/session.go index fedf2f1..d8eb0b1 100644 --- a/pwd/session.go +++ b/pwd/session.go @@ -186,6 +186,10 @@ func (p *pwd) SessionGet(sessionId string) *types.Session { s, _ := p.storage.SessionGet(sessionId) + if s == nil { + return nil + } + if err := p.prepareSession(s); err != nil { log.Println(err) return nil From 3f4c96286e328f83f9ccdb166b2f6707e889dc8a Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Mon, 31 Jul 2017 10:10:48 -0300 Subject: [PATCH 02/12] Avoid to have unnecessary opened connections --- handlers/ws.go | 1 + www/assets/app.js | 1 + 2 files changed, 2 insertions(+) diff --git a/handlers/ws.go b/handlers/ws.go index 37e8b8f..746d2bc 100644 --- a/handlers/ws.go +++ b/handlers/ws.go @@ -21,6 +21,7 @@ func WS(so socketio.Socket) { session := core.SessionGet(sessionId) if session == nil { log.Printf("Session with id [%s] does not exist!\n", sessionId) + so.Disconnect() return } diff --git a/www/assets/app.js b/www/assets/app.js index c04d774..9596109 100644 --- a/www/assets/app.js +++ b/www/assets/app.js @@ -181,6 +181,7 @@ socket.on('session end', function() { $scope.showAlert('Session timed out!', 'Your session has expired and all of your instances have been deleted.', '#sessionEnd') $scope.isAlive = false; + socket.close(); }); socket.on('viewport', function(rows, cols) { From fa3b56ecfe1df59d8cf535b6429f8bd22ac167e2 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Mon, 31 Jul 2017 12:00:49 -0300 Subject: [PATCH 03/12] Check docker daemon is alive in ping endpoint --- handlers/ping.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/handlers/ping.go b/handlers/ping.go index ccf5cec..5e621f9 100644 --- a/handlers/ping.go +++ b/handlers/ping.go @@ -1,9 +1,12 @@ package handlers import ( + "context" "log" "net/http" + "time" + "github.com/docker/docker/client" "github.com/play-with-docker/play-with-docker/config" "github.com/shirou/gopsutil/load" ) @@ -11,6 +14,21 @@ import ( func Ping(rw http.ResponseWriter, req *http.Request) { // Get system load average of the last 5 minutes and compare it against a threashold. + c, err := client.NewEnvClient() + + if err != nil { + rw.WriteHeader(http.StatusInternalServerError) + return + } + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Microsecond) + defer cancel() + + if _, err := c.Info(ctx); err != nil && err == context.DeadlineExceeded { + log.Printf("Docker info took to long to respond\n") + rw.WriteHeader(http.StatusGatewayTimeout) + } + a, err := load.Avg() if err != nil { log.Println("Cannot get system load average!", err) From 7a49c557e011e785d36f898d82532f3d29459063 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Mon, 31 Jul 2017 18:20:27 -0300 Subject: [PATCH 04/12] Return 404 for WS handler when session doesnt exist --- api.go | 4 +--- handlers/bootstrap.go | 8 ++++---- handlers/ping.go | 2 +- handlers/ws.go | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/api.go b/api.go index 313e2bf..fcb762d 100644 --- a/api.go +++ b/api.go @@ -41,8 +41,6 @@ func main() { } }() - server := handlers.Broadcast.GetHandler() - r := mux.NewRouter() corsRouter := mux.NewRouter() @@ -77,7 +75,7 @@ func main() { http.ServeFile(rw, r, "www/sdk.js") }) - corsRouter.Handle("/sessions/{sessionId}/ws/", server) + corsRouter.HandleFunc("/sessions/{sessionId}/ws/", handlers.WebSocket) r.Handle("/metrics", promhttp.Handler()) // Generic routes diff --git a/handlers/bootstrap.go b/handlers/bootstrap.go index 9e8e90b..74a8282 100644 --- a/handlers/bootstrap.go +++ b/handlers/bootstrap.go @@ -12,7 +12,7 @@ import ( ) var core pwd.PWDApi -var Broadcast pwd.BroadcastApi +var broadcast pwd.BroadcastApi func Bootstrap() { c, err := client.NewEnvClient() @@ -22,18 +22,18 @@ func Bootstrap() { d := docker.NewDocker(c) - Broadcast, err = pwd.NewBroadcast(WS, WSError) + broadcast, err = pwd.NewBroadcast(WS, WSError) if err != nil { log.Fatal(err) } - t := pwd.NewScheduler(Broadcast, d) + t := pwd.NewScheduler(broadcast, d) s, err := storage.NewFileStorage(config.SessionsFile) if err != nil && !os.IsNotExist(err) { log.Fatal("Error decoding sessions from disk ", err) } - core = pwd.NewPWD(d, t, Broadcast, s) + core = pwd.NewPWD(d, t, broadcast, s) } diff --git a/handlers/ping.go b/handlers/ping.go index 5e621f9..49cfdd1 100644 --- a/handlers/ping.go +++ b/handlers/ping.go @@ -21,7 +21,7 @@ func Ping(rw http.ResponseWriter, req *http.Request) { return } - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Microsecond) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if _, err := c.Info(ctx); err != nil && err == context.DeadlineExceeded { diff --git a/handlers/ws.go b/handlers/ws.go index 746d2bc..c8b938b 100644 --- a/handlers/ws.go +++ b/handlers/ws.go @@ -3,6 +3,7 @@ package handlers import ( "fmt" "log" + "net/http" "github.com/googollee/go-socket.io" "github.com/gorilla/mux" @@ -52,3 +53,16 @@ func WS(so socketio.Socket) { func WSError(so socketio.Socket) { log.Println("error ws") } + +func WebSocket(rw http.ResponseWriter, req *http.Request) { + vars := mux.Vars(req) + sessionId := vars["sessionId"] + + session := core.SessionGet(sessionId) + if session == nil { + rw.WriteHeader(http.StatusNotFound) + return + } + + broadcast.GetHandler().ServeHTTP(rw, req) +} From a6e7ee41a9c30214b8b1d702034700a977184579 Mon Sep 17 00:00:00 2001 From: Mano Marks Date: Mon, 7 Aug 2017 18:10:11 -0700 Subject: [PATCH 05/12] updating README to suggest using docker stack deploy instead of docker-compose up --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6f92fe8..a6e843b 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,9 @@ Start the Docker daemon on your machine and run `docker pull franela/dind`. 2) `go get -v -d -t ./...` -3) Start PWD as a container with docker-compose up. +3) Start PWD as a container with `docker stack deploy -c docker-compose.yml pwd`. -5) Point to http://localhost and click "New Instance" +5) Point to http://localhost and click "New Instance". Notes: From 12872e3dd6229fcfc5a68a05ba9046ab00faf065 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Tue, 8 Aug 2017 09:00:41 -0300 Subject: [PATCH 06/12] =?UTF-8?q?Revert=20"updating=20README=20to=20sugges?= =?UTF-8?q?t=20using=20docker=20stack=20deploy=20instead=20of=20docke?= =?UTF-8?q?=E2=80=A6"=20(#178)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a6e843b..6f92fe8 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,9 @@ Start the Docker daemon on your machine and run `docker pull franela/dind`. 2) `go get -v -d -t ./...` -3) Start PWD as a container with `docker stack deploy -c docker-compose.yml pwd`. +3) Start PWD as a container with docker-compose up. -5) Point to http://localhost and click "New Instance". +5) Point to http://localhost and click "New Instance" Notes: From e0f3186591539a5046d38e46bcbd9dc8d3b5650b Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Sat, 16 Sep 2017 04:25:18 -0300 Subject: [PATCH 07/12] Update to 17.07 dind --- Dockerfile.dind | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile.dind b/Dockerfile.dind index 4c58bc3..db193fd 100644 --- a/Dockerfile.dind +++ b/Dockerfile.dind @@ -1,4 +1,4 @@ -ARG VERSION=docker:17.06.0-ce-dind +ARG VERSION=docker:17.07-dind FROM ${VERSION} RUN apk add --no-cache git tmux py2-pip apache2-utils vim build-base gettext-dev curl bash-completion bash util-linux jq openssh qemu-img qemu-system-x86_64 From 05045424b03397574557c7e7954b32ea31e643f7 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Fri, 6 Oct 2017 13:33:39 -0300 Subject: [PATCH 08/12] Use latest dind stable image --- Dockerfile.dind | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile.dind b/Dockerfile.dind index db193fd..4276471 100644 --- a/Dockerfile.dind +++ b/Dockerfile.dind @@ -1,4 +1,4 @@ -ARG VERSION=docker:17.07-dind +ARG VERSION=docker:stable-dind FROM ${VERSION} RUN apk add --no-cache git tmux py2-pip apache2-utils vim build-base gettext-dev curl bash-completion bash util-linux jq openssh qemu-img qemu-system-x86_64 From bf398281604e34d8d9cbc992bbfd5ebd320b2f24 Mon Sep 17 00:00:00 2001 From: Valentin Vieriu Date: Wed, 18 Oct 2017 10:35:43 +0200 Subject: [PATCH 09/12] Update README.md (#197) As #128 was merged, the url structure is different --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6f92fe8..07ac93d 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ If you want to make changes to the `dind` image being used, make your changes to ### How can I connect to a published port from the outside world? -If you need to access your services from outside, use the following URL pattern `http://pwd-..labs.play-with-docker.com` (i.e: http://pwd10_2_135_3-80.host3.labs.play-with-docker.com/). +If you need to access your services from outside, use the following URL pattern `http://pwd-..labs.play-with-docker.com` (i.e: http://pwd10-2-135-3-80.host3.labs.play-with-docker.com/). ### Why is PWD running in ports 80 and 443?, Can I change that?. From 4f9e58584b4eec63b2d9138d062640e9fd2b9c8e Mon Sep 17 00:00:00 2001 From: leigh schrandt Date: Sun, 22 Oct 2017 12:40:24 +0200 Subject: [PATCH 10/12] Update README.md (#199) More URL format bikeshedding --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 07ac93d..c48d26a 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ If you want to make changes to the `dind` image being used, make your changes to ### How can I connect to a published port from the outside world? -If you need to access your services from outside, use the following URL pattern `http://pwd-..labs.play-with-docker.com` (i.e: http://pwd10-2-135-3-80.host3.labs.play-with-docker.com/). +If you need to access your services from outside, use the following URL pattern `http://pwd-..labs.play-with-docker.com` (i.e: http://pwd10-2-135-3-80.host3.labs.play-with-docker.com/). ### Why is PWD running in ports 80 and 443?, Can I change that?. From 70fb34d3d51a98b8603c04e58344f77596219526 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Tue, 24 Oct 2017 15:29:18 -0300 Subject: [PATCH 11/12] Disable image pulling and update docker cli --- docker/docker.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/docker/docker.go b/docker/docker.go index 08876c7..bf595cf 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -275,18 +275,18 @@ func (d *docker) CreateContainer(opts CreateContainerOpts) (string, error) { container, err := d.c.ContainerCreate(context.Background(), cf, h, networkConf, opts.ContainerName) if err != nil { - if client.IsErrImageNotFound(err) { - log.Printf("Unable to find image '%s' locally\n", opts.Image) - if err = d.pullImage(context.Background(), opts.Image); err != nil { - return "", err - } - container, err = d.c.ContainerCreate(context.Background(), cf, h, networkConf, opts.ContainerName) - if err != nil { - return "", err - } - } else { - return "", err - } + //if client.IsErrImageNotFound(err) { + //log.Printf("Unable to find image '%s' locally\n", opts.Image) + //if err = d.pullImage(context.Background(), opts.Image); err != nil { + //return "", err + //} + //container, err = d.c.ContainerCreate(context.Background(), cf, h, networkConf, opts.ContainerName) + //if err != nil { + //return "", err + //} + //} else { + return "", err + //} } if err := d.copyIfSet(opts.ServerCert, "cert.pem", containerCertDir, opts.ContainerName); err != nil { @@ -346,7 +346,7 @@ func (d *docker) ExecAttach(instanceName string, command []string, out io.Writer if err != nil { return 0, err } - resp, err := d.c.ContainerExecAttach(context.Background(), e.ID, types.ExecConfig{AttachStdout: true, AttachStderr: true, Tty: true}) + resp, err := d.c.ContainerExecAttach(context.Background(), e.ID, types.ExecStartCheck{Tty: true}) if err != nil { return 0, err } From db6e23e1fae76466b99b357bd5e72c543806a852 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Wed, 25 Oct 2017 15:15:12 -0300 Subject: [PATCH 12/12] Add sudo shim --- Dockerfile.dind | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile.dind b/Dockerfile.dind index 4276471..602afd2 100644 --- a/Dockerfile.dind +++ b/Dockerfile.dind @@ -39,7 +39,7 @@ RUN mkdir /etc/bash_completion.d \ RUN rm /sbin/modprobe && echo '#!/bin/true' >/sbin/modprobe && chmod +x /sbin/modprobe # Install a nice vimrc file and prompt (by soulshake) -COPY ["docker-prompt","/usr/local/bin/"] +COPY ["docker-prompt","sudo","/usr/local/bin/"] COPY [".vimrc",".profile", ".inputrc", ".gitconfig", "./root/"] COPY ["motd", "/etc/motd"] COPY ["daemon.json", "/etc/docker/"]