Skip to content

Commit

Permalink
Merge branch 'next' into mongo_storage
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosnils committed Jul 18, 2017
2 parents 08b64da + a6fdfca commit 8eae9c1
Show file tree
Hide file tree
Showing 19 changed files with 3,969 additions and 3,948 deletions.
2 changes: 2 additions & 0 deletions .profile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export PS1='\e[1m\e[31m[\h] \e[32m($(docker-prompt)) \e[34m\u@$(hostname -i)\e[35m \w\e[0m\n$ '
alias vi='vim'
export PATH=$PATH:/root/go/bin
cat /etc/motd
echo $BASHPID > /var/run/cwd
4 changes: 0 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
FROM golang:1.8

# Copy the runtime dockerfile into the context as Dockerfile
COPY Dockerfile.run /go/bin/Dockerfile
COPY ./www /go/bin/www

COPY . /go/src/github.com/play-with-docker/play-with-docker

WORKDIR /go/src/github.com/play-with-docker/play-with-docker
Expand Down
14 changes: 12 additions & 2 deletions Dockerfile.dind
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
ARG VERSION=docker:17.05.0-ce-dind
ARG VERSION=docker:17.06.0-ce-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
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

ENV GOPATH /root/go
ENV PATH $PATH:$GOPATH

# Use specific moby commit due to vendoring mismatch
ENV MOBY_COMMIT="a73c3d3667f32fd61febcd2e824aa0341a57bafd"

RUN mkdir /root/go && apk add --no-cache go \
&& go get -u -d github.com/moby/tool/cmd/moby && (cd $GOPATH/src/github.com/moby/tool/cmd/moby && git checkout $MOBY_COMMIT && go install) \
&& go get -u github.com/linuxkit/linuxkit/src/cmd/linuxkit \
&& rm -rf /root/go/pkg && rm -rf /root/go/src && rm -rf /usr/lib/go

# Compile and install httping
# (used in orchestration workshop, and very useful anyway)
Expand Down
12 changes: 0 additions & 12 deletions Dockerfile.run

This file was deleted.

2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
var NameFilter = regexp.MustCompile(PWDHostPortGroupRegex)
var AliasFilter = regexp.MustCompile(AliasPortGroupRegex)

var SSLPortNumber, PortNumber, Key, Cert, SessionsFile, PWDContainerName, PWDCName, HashKey
var SSLPortNumber, PortNumber, Key, Cert, SessionsFile, PWDContainerName, PWDCName, HashKey string
var MaxLoadAvg float64

func ParseFlags() {
Expand Down
11 changes: 9 additions & 2 deletions handlers/file_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"log"
"net/http"
"path/filepath"

"github.com/gorilla/mux"
)
Expand All @@ -20,7 +21,10 @@ func FileUpload(rw http.ResponseWriter, req *http.Request) {

// has a url query parameter, ignore body
if url := req.URL.Query().Get("url"); url != "" {
err := core.InstanceUploadFromUrl(i, req.URL.Query().Get("url"))

_, fileName := filepath.Split(url)

err := core.InstanceUploadFromUrl(i, fileName, "", req.URL.Query().Get("url"))
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
Expand All @@ -35,6 +39,8 @@ func FileUpload(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusBadRequest)
return
}
path := req.URL.Query().Get("path")

for {
p, err := red.NextPart()
if err == io.EOF {
Expand All @@ -48,12 +54,13 @@ func FileUpload(rw http.ResponseWriter, req *http.Request) {
if p.FileName() == "" {
continue
}
err = core.InstanceUploadFromReader(i, p.FileName(), p)
err = core.InstanceUploadFromReader(i, p.FileName(), path, p)
if err != nil {
log.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}

log.Printf("Uploaded [%s] to [%s]\n", p.FileName(), i.Name)
}
rw.WriteHeader(http.StatusOK)
Expand Down
2 changes: 1 addition & 1 deletion motd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
###############################################################
# WARNING!!!! #
# This is a sandbox environment. Using personal credentials #
# is HIGHLY! discouraged. Any consequences of doing so, are #
# is HIGHLY! discouraged. Any consequences of doing so are #
# completely the user's responsibilites. #
# #
# The PWD team. #
Expand Down
38 changes: 32 additions & 6 deletions pwd/instance.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pwd

import (
"bytes"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -66,10 +67,11 @@ func (p *pwd) InstanceAttachTerminal(instance *types.Instance) error {
terms[instance.SessionId][instance.Name] = conn
}
io.Copy(encoder.Writer(sw), conn)

return nil
}

func (p *pwd) InstanceUploadFromUrl(instance *types.Instance, url string) error {
func (p *pwd) InstanceUploadFromUrl(instance *types.Instance, fileName, dest string, url string) error {
defer observeAction("InstanceUploadFromUrl", time.Now())
log.Printf("Downloading file [%s]\n", url)
resp, err := http.Get(url)
Expand All @@ -81,9 +83,7 @@ func (p *pwd) InstanceUploadFromUrl(instance *types.Instance, url string) error
return fmt.Errorf("Could not download file [%s]. Status code: %d\n", url, resp.StatusCode)
}

_, fileName := filepath.Split(url)

copyErr := p.docker.CopyToContainer(instance.Name, "/var/run/pwd/uploads", fileName, resp.Body)
copyErr := p.docker.CopyToContainer(instance.Name, dest, fileName, resp.Body)

if copyErr != nil {
return fmt.Errorf("Error while downloading file [%s]. Error: %s\n", url, copyErr)
Expand All @@ -92,10 +92,36 @@ func (p *pwd) InstanceUploadFromUrl(instance *types.Instance, url string) error
return nil
}

func (p *pwd) InstanceUploadFromReader(instance *types.Instance, fileName string, reader io.Reader) error {
func (p *pwd) getInstanceCWD(instance *types.Instance) (string, error) {
b := bytes.NewBufferString("")

if c, err := p.docker.ExecAttach(instance.Name, []string{"bash", "-c", `pwdx $(</var/run/cwd)`}, b); c > 0 {
log.Println(b.String())
return "", fmt.Errorf("Error %d trying to get CWD", c)
} else if err != nil {
return "", err
}

cwd := strings.TrimSpace(strings.Split(b.String(), ":")[1])

return cwd, nil
}

func (p *pwd) InstanceUploadFromReader(instance *types.Instance, fileName, dest string, reader io.Reader) error {
defer observeAction("InstanceUploadFromReader", time.Now())

copyErr := p.docker.CopyToContainer(instance.Name, "/var/run/pwd/uploads", fileName, reader)
var finalDest string
if filepath.IsAbs(dest) {
finalDest = dest
} else {
if cwd, err := p.getInstanceCWD(instance); err != nil {
return err
} else {
finalDest = fmt.Sprintf("%s/%s", cwd, dest)
}
}

copyErr := p.docker.CopyToContainer(instance.Name, finalDest, fileName, reader)

if copyErr != nil {
return fmt.Errorf("Error while uploading file [%s]. Error: %s\n", fileName, copyErr)
Expand Down
4 changes: 2 additions & 2 deletions pwd/pwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ type PWDApi interface {
InstanceNew(session *types.Session, conf InstanceConfig) (*types.Instance, error)
InstanceResizeTerminal(instance *types.Instance, cols, rows uint) error
InstanceAttachTerminal(instance *types.Instance) error
InstanceUploadFromUrl(instance *types.Instance, url string) error
InstanceUploadFromReader(instance *types.Instance, filename string, reader io.Reader) error
InstanceUploadFromUrl(instance *types.Instance, fileName, dest, url string) error
InstanceUploadFromReader(instance *types.Instance, fileName, dest string, reader io.Reader) error
InstanceGet(session *types.Session, name string) *types.Instance
// TODO remove this function when we add the session prefix to the PWD url
InstanceFindByIP(ip string) *types.Instance
Expand Down
7 changes: 5 additions & 2 deletions pwd/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"math"
"path"
"path/filepath"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -154,13 +155,15 @@ func (p *pwd) SessionDeployStack(s *types.Session) error {
log.Printf("Error creating instance for stack [%s]: %s\n", s.Stack, err)
return err
}
err = p.InstanceUploadFromUrl(i, s.Stack)

_, fileName := filepath.Split(s.Stack)
err = p.InstanceUploadFromUrl(i, fileName, "/var/run/pwd/uploads", s.Stack)
if err != nil {
log.Printf("Error uploading stack file [%s]: %s\n", s.Stack, err)
return err
}

fileName := path.Base(s.Stack)
fileName = path.Base(s.Stack)
file := fmt.Sprintf("/var/run/pwd/uploads/%s", fileName)
cmd := fmt.Sprintf("docker swarm init --advertise-addr eth0 && docker-compose -f %s pull && docker stack deploy -c %s %s", file, file, s.StackName)

Expand Down
35 changes: 27 additions & 8 deletions 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']);
var app = angular.module('DockerPlay', ['ngMaterial', 'ngFileUpload']);

// Automatically redirects user to a new session when bypassing captcha.
// Controller keeps code/logic separate from the HTML
Expand All @@ -19,7 +19,7 @@
}
}

app.controller('PlayController', ['$scope', '$log', '$http', '$location', '$timeout', '$mdDialog', '$window', 'TerminalService', 'KeyboardShortcutService', 'InstanceService', 'SessionService', function($scope, $log, $http, $location, $timeout, $mdDialog, $window, TerminalService, KeyboardShortcutService, InstanceService, SessionService) {
app.controller('PlayController', ['$scope', '$log', '$http', '$location', '$timeout', '$mdDialog', '$window', 'TerminalService', 'KeyboardShortcutService', 'InstanceService', 'SessionService', 'Upload', function($scope, $log, $http, $location, $timeout, $mdDialog, $window, TerminalService, KeyboardShortcutService, InstanceService, SessionService, Upload) {
$scope.sessionId = SessionService.getCurrentSessionId();
$scope.instances = [];
$scope.idx = {};
Expand All @@ -31,6 +31,30 @@
$scope.newInstanceBtnText = '+ Add new instance';
$scope.deleteInstanceBtnText = 'Delete';
$scope.isInstanceBeingDeleted = false;
$scope.uploadProgress = 0;


$scope.uploadFiles = function (files, invalidFiles) {
let total = files.length;
let uploadFile = function() {
let file = files.shift();
if (!file){
$scope.uploadMessage = "";
$scope.uploadProgress = 0;
return
}
$scope.uploadMessage = "Uploading file(s) " + (total - files.length) + "/"+ total + " : " + file.name;
let upload = Upload.upload({url: '/sessions/' + $scope.sessionId + '/instances/' + $scope.selectedInstance.name + '/uploads', data: {file: file}, method: 'POST'})
.then(function(){}, function(){}, function(evt) {
$scope.uploadProgress = parseInt(100.0 * evt.loaded / evt.total);
});

// process next file
upload.finally(uploadFile);
}

uploadFile();
}

var selectedKeyboardShortcuts = KeyboardShortcutService.getCurrentShortcuts();

Expand Down Expand Up @@ -105,7 +129,7 @@

if (!state) {
$mdDialog.show({
controller: SessionBuilderModalController,
onComplete: function(){SessionBuilderModalController($mdDialog, $scope)},
contentElement: '#builderDialog',
parent: angular.element(document.body),
clickOutsideToClose: false,
Expand Down Expand Up @@ -273,11 +297,6 @@

$scope.createBuilderTerminal = function() {
var builderTerminalContainer = document.getElementById('builder-terminal');
// For some reason the dialog DOM might not be ready, so we just keep trying
if (!builderTerminalContainer) {
setTimeout($scope.createBuilderTerminal, 100);
return;
}
let term = new Terminal({
cursorBlink: false
});
Expand Down
Loading

0 comments on commit 8eae9c1

Please sign in to comment.