HTTPS and File Uploads (#139)

* Add a few fixes

* Use CopyToContainer instead of bind mounts

* Remove a local compose file

* Changes according to the comments

* Rebase with master
This commit is contained in:
Jonathan Leibiusky
2017-05-12 16:20:09 -03:00
committed by Marcos Nils
parent 61a0bb4db1
commit 8df6373327
8 changed files with 267 additions and 54 deletions

View File

@@ -3,9 +3,12 @@ package services
import (
"context"
"crypto/tls"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
@@ -42,6 +45,14 @@ type Instance struct {
cert *tls.Certificate `json:"-"`
}
type InstanceConfig struct {
ImageName string
Alias string
ServerCert []byte
ServerKey []byte
CACert []byte
}
func (i *Instance) setUsedPort(port uint16) {
rw.Lock()
defer rw.Unlock()
@@ -97,17 +108,17 @@ func getDindImageName() string {
return dindImage
}
func NewInstance(session *Session, imageName, alias string) (*Instance, error) {
if imageName == "" {
imageName = dindImage
func NewInstance(session *Session, conf InstanceConfig) (*Instance, error) {
if conf.ImageName == "" {
conf.ImageName = dindImage
}
log.Printf("NewInstance - using image: [%s]\n", imageName)
instance, err := CreateInstance(session, imageName)
log.Printf("NewInstance - using image: [%s]\n", conf.ImageName)
instance, err := CreateInstance(session, conf)
if err != nil {
return nil, err
}
instance.Alias = alias
instance.Alias = conf.Alias
instance.session = session
@@ -163,6 +174,29 @@ func (i *Instance) Attach() {
case <-i.ctx.Done():
}
}
func (i *Instance) UploadFromURL(url string) error {
log.Printf("Downloading file [%s]\n", url)
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("Could not download file [%s]. Error: %s\n", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("Could not download file [%s]. Status code: %d\n", url, resp.StatusCode)
}
_, fileName := filepath.Split(url)
copyErr := CopyToContainer(i.Name, "/var/run/pwd/uploads", fileName, resp.Body)
if copyErr != nil {
return fmt.Errorf("Error while downloading file [%s]. Error: %s\n", url, copyErr)
}
return nil
}
func GetInstance(session *Session, name string) *Instance {
return session.Instances[name]
}