Esempio n. 1
0
// BatchUpdate updates a list of paths to the instance. Only update/create
// events should be included.
func BatchUpdate(container *schemas.Container, paths map[string]string, errorChan chan error) error {
	body, gzipWriter, tarWriter := tar.NewTarGZ()

	for full, rel := range paths {
		info, err := os.Lstat(full)
		if err != nil {
			if os.IsNotExist(err) {
				errorChan <- &BatchError{Path: full, Err: err}
				continue
			}

			return err
		}

		err = tar.WritePath(tarWriter, info, full, rel)
		if err != nil {
			if os.IsNotExist(err) {
				errorChan <- &BatchError{Path: full, Err: err}
				continue
			}

			return err
		}
	}
	tarWriter.Close()
	gzipWriter.Close()

	addr := net.JoinHostPort(container.Address, config.DelanceyProdPort)
	req, err := http.NewRequest("PATCH", "http://"+addr+"/batch", body)
	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.StatusUpdated {
		// If the error matches return var.
		if resData.Error() == ErrNotInUse.Error() {
			return ErrNotInUse
		}

		return resData
	}

	return nil
}
Esempio n. 2
0
// 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)
}