Skip to content

Commit

Permalink
Enable outputting to a URL if one is provided (#71)
Browse files Browse the repository at this point in the history
* use a url if it is provided

* remove extra printlns

* support bundle yay

* remove extraneous println

* update changelog
  • Loading branch information
gerred committed Dec 1, 2017
1 parent 2e74be1 commit 464b8f0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@

0.9.0 / 2017-11-22
* Kubernetes support

0.9.1 / 2017-12-01
* Support output to a URL. This will do a form post to the provided callback URL.
48 changes: 48 additions & 0 deletions pkg/bundle/generate.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package bundle

import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
Expand All @@ -16,6 +21,14 @@ import (

// Generate a new support bundle and write the results as an archive at pathname
func Generate(tasks []types.Task, timeout time.Duration, pathname string) error {
var isURL bool

callbackURL, err := url.Parse(pathname)
if err == nil && (callbackURL.Scheme == "http" || callbackURL.Scheme == "https") {
isURL = true
pathname = "/tmp/bundle.tar.gz"
}

collectDir, err := ioutil.TempDir(filepath.Dir(pathname), "")
if err != nil {
return errors.Wrap(err, "Creating a temporary directory to store results failed")
Expand Down Expand Up @@ -57,9 +70,44 @@ func Generate(tasks []types.Task, timeout time.Duration, pathname string) error
comp := compressor.NewTgz()
comp.SetTarConfig(compressor.Tar{TruncateLongFiles: true})
// trailing slash keeps the parent directory from being included in archive

if err := comp.Compress(collectDir+"/", pathname); err != nil {
return errors.Wrap(err, "Compressing results directory failed")
}

if isURL {
var b bytes.Buffer
w := multipart.NewWriter(&b)

file, err := os.Open(pathname)
if err != nil {
return errors.Wrap(err, "finding the file that was just compressed")
}
defer file.Close()
fw, err := w.CreateFormFile("file", pathname)
if err != nil {
return errors.Wrap(err, "creating multipart form")
}

if _, err = io.Copy(fw, file); err != nil {
return errors.Wrap(err, "copying buffer")
}

w.Close()

req, err := http.NewRequest("POST", callbackURL.String(), &b)
if err != nil {
return errors.Wrap(err, "making request")
}

req.Header.Set("Content-Type", w.FormDataContentType())

client := &http.Client{}
_, err = client.Do(req)
if err != nil {
return errors.Wrap(err, "completing request")
}
}

return nil
}

0 comments on commit 464b8f0

Please sign in to comment.