func init() { Env = "testing" err := os.MkdirAll("test", os.ModePerm|os.ModeDir) if err != nil { panic(err) } // If the tar file exists don't create it. _, err = os.Stat(uploadPath) if err == nil { return } // Create a gzipped tar file in test. file, err := os.Create(filepath.Join("test", "upload.tar.gz")) if err != nil { panic(err) } defer file.Close() contents, err := tar.Tar(".", nil) if err != nil { panic(err) } _, err = io.Copy(file, contents) if err != nil { panic(err) } }
// Upload sends the paths contents to the service compressed. func (watcher *Watcher) Upload() error { var ( err error file *os.File ) path := watcher.uploadPath() i := 0 ignores, err := db.GetIgnores(watcher.Path) if err != nil { return watcher.wrapErr(err) } if watcher.Path != "" { upload, err := tar.Tar(watcher.Path, ignores) if err != nil { return watcher.wrapErr(err) } // Write the tgz file to read from. file, err = os.Create(path) if err != nil { return watcher.wrapErr(err) } defer os.RemoveAll(path) defer file.Close() // Copy the contents to the file. _, err = io.Copy(file, upload) if err != nil { return watcher.wrapErr(err) } } for i < 1000 { // If we've failed once, wait a bit. if err != nil { <-time.After(time.Millisecond * 50) } // Make sure we're at the beginning of the file. if watcher.Path != "" { _, err = file.Seek(0, os.SEEK_SET) if err != nil { return watcher.wrapErr(err) } } // Attempt to upload the file to the services satellite. err = delancey.Upload(watcher.Service.SatelliteAddr, watcher.Service.Name, file) if err == nil { return nil } i++ } return watcher.wrapErr(err) }
// UploadSSH sends the .ssh directory to the container func UploadSSH(container *schemas.Container, path string) error { contents, err := tar.Tar(path, []string{}) if err != nil { return err } addr := net.JoinHostPort(container.Address, config.DelanceyProdPort) req, err := http.NewRequest("PUT", "http://"+addr+"/ssh", contents) if err != nil { return err } res, err := http.DefaultClient.Do(req) if err != nil { return err } defer res.Body.Close() resData := new(requests.Res) decoder := json.NewDecoder(res.Body) err = decoder.Decode(resData) if err != nil { return err } if resData.Status != requests.StatusSuccess { // If the error matches return var. if resData.Error() == ErrNotInUse.Error() { return ErrNotInUse } return resData } return nil }
// GET /, Retrieve the containers code. func indexHandler(rw http.ResponseWriter, req *http.Request) { var ( contents io.Reader err error ) empty, gzipWriter, tarWriter := tar.NewTarGZ() tarWriter.Close() gzipWriter.Close() // Require a container to exist. if currentContainer == nil { renderer.JSON(rw, http.StatusBadRequest, map[string]string{ "status": requests.StatusFailed, "error": delancey.ErrNotInUse.Error(), }) return } // Tar the contents of the container. contents, err = tar.Tar(currentContainer.RemotePath, []string{}) if err != nil && !os.IsNotExist(err) { renderer.JSON(rw, http.StatusInternalServerError, map[string]string{ "status": requests.StatusFailed, "error": err.Error(), }) return } // If the path didn't exist, just provide an empty targz stream. if err != nil { contents = empty } rw.WriteHeader(http.StatusOK) io.Copy(rw, contents) }