From eee76951b70e1216f25f3dc5aef2e705ec12909a Mon Sep 17 00:00:00 2001 From: "Jonathan Leibiusky @xetorthio" Date: Wed, 21 Jun 2017 10:02:25 -0300 Subject: [PATCH 1/2] Add multipart upload to instances --- handlers/file_upload.go | 33 +++++++++++++++++++++++++-------- pwd/instance.go | 12 ++++++++++++ pwd/pwd.go | 2 ++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/handlers/file_upload.go b/handlers/file_upload.go index 4241b62..f261d9e 100644 --- a/handlers/file_upload.go +++ b/handlers/file_upload.go @@ -1,6 +1,7 @@ package handlers import ( + "io" "log" "net/http" @@ -28,18 +29,34 @@ func FileUpload(rw http.ResponseWriter, req *http.Request) { rw.WriteHeader(http.StatusOK) return } else { - // This is for multipart upload - log.Println("Not implemented yet") - - /* - err := req.ParseMultipartForm(32 << 20) + red, err := req.MultipartReader() + if err != nil { + log.Println(err) + rw.WriteHeader(http.StatusBadRequest) + return + } + for { + p, err := red.NextPart() + if err == io.EOF { + break + } if err != nil { log.Println(err) - rw.WriteHeader(http.StatusBadRequest) + continue + } + + if p.FileName() == "" { + continue + } + err = core.InstanceUploadFromReader(i, p.FileName(), p) + if err != nil { + log.Println(err) + rw.WriteHeader(http.StatusInternalServerError) return } - */ - rw.WriteHeader(http.StatusInternalServerError) + log.Printf("Uploaded [%s] to [%s]\n", p.FileName(), i.Name) + } + rw.WriteHeader(http.StatusOK) return } diff --git a/pwd/instance.go b/pwd/instance.go index 072098b..5bb064c 100644 --- a/pwd/instance.go +++ b/pwd/instance.go @@ -133,6 +133,18 @@ func (p *pwd) InstanceUploadFromUrl(instance *Instance, url string) error { return nil } +func (p *pwd) InstanceUploadFromReader(instance *Instance, fileName string, reader io.Reader) error { + defer observeAction("InstanceUploadFromReader", time.Now()) + + copyErr := p.docker.CopyToContainer(instance.Name, "/var/run/pwd/uploads", fileName, reader) + + if copyErr != nil { + return fmt.Errorf("Error while uploading file [%s]. Error: %s\n", fileName, copyErr) + } + + return nil +} + func (p *pwd) InstanceGet(session *Session, name string) *Instance { defer observeAction("InstanceGet", time.Now()) return session.Instances[name] diff --git a/pwd/pwd.go b/pwd/pwd.go index 183e9f3..8a0a736 100644 --- a/pwd/pwd.go +++ b/pwd/pwd.go @@ -1,6 +1,7 @@ package pwd import ( + "io" "sync" "time" @@ -65,6 +66,7 @@ type PWDApi interface { InstanceResizeTerminal(instance *Instance, cols, rows uint) error InstanceAttachTerminal(instance *Instance) error InstanceUploadFromUrl(instance *Instance, url string) error + InstanceUploadFromReader(instance *Instance, filename string, reader io.Reader) error InstanceGet(session *Session, name string) *Instance InstanceFindByIP(ip string) *Instance InstanceFindByAlias(sessionPrefix, alias string) *Instance From 32dbb5ca99566774bda341fb7652357c8f371463 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Wed, 21 Jun 2017 11:08:14 -0300 Subject: [PATCH 2/2] Add keys to allow transparent sshing between hosts --- Dockerfile.dind | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Dockerfile.dind b/Dockerfile.dind index d768cb4..2b7378f 100644 --- a/Dockerfile.dind +++ b/Dockerfile.dind @@ -41,7 +41,11 @@ ENV DOCKER_STORAGE_DRIVER=$docker_storage_driver # Move to our home WORKDIR /root -RUN mkdir -p /var/run/pwd/certs && mkdir -p /var/run/pwd/uploads +# Setup certs and ssh keys +RUN mkdir -p /var/run/pwd/certs && mkdir -p /var/run/pwd/uploads \ + && ssh-keygen -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key >/dev/null \ + && mkdir ~/.ssh && ssh-keygen -N "" -t rsa -f ~/.ssh/id_rsa \ + && cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys # Remove IPv6 alias for localhost and start docker in the background ... CMD cat /etc/hosts >/etc/hosts.bak && \ @@ -53,7 +57,7 @@ CMD cat /etc/hosts >/etc/hosts.bak && \ sed -i "s/\DOCKER_TLSCERT/$DOCKER_TLSCERT/" /etc/docker/daemon.json && \ sed -i "s/\DOCKER_TLSKEY/$DOCKER_TLSKEY/" /etc/docker/daemon.json && \ umount /var/lib/docker && mount -t securityfs none /sys/kernel/security && \ - echo "root:root" | chpasswd &> /dev/null && ssh-keygen -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key >/dev/null && \ + echo "root:root" | chpasswd &> /dev/null && \ /usr/sbin/sshd -o PermitRootLogin=yes -o PrintMotd=no 2>/dev/null && \ dockerd &>/docker.log & \ while true ; do script -q -c "/bin/bash -l" /dev/null ; done