-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_bundle.go
60 lines (51 loc) · 1.73 KB
/
instance_bundle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package handlers
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/replicatedhq/kurl-testgrid/tgapi/pkg/crypto"
"github.com/replicatedhq/kurl-testgrid/tgapi/pkg/logger"
"github.com/replicatedhq/kurl-testgrid/tgapi/pkg/persistence"
)
func InstanceBundle(passpharse string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("DEBUG: Start %s %s handler", r.Method, r.URL.Path)
defer log.Printf("DEBUG: End %s %s handler", r.Method, r.URL.Path)
bucket := os.Getenv("SUPPORT_BUNDLE_BUCKET")
if bucket == "" {
w.WriteHeader(http.StatusNotImplemented)
return
}
instanceID := mux.Vars(r)["instanceId"]
encrypted, err := crypto.StreamEncrypt(passpharse, r.Body)
if err != nil {
logger.Error(errors.Errorf("Failed to encrypt bundle for instance %s: %v", instanceID, err))
w.WriteHeader(http.StatusInternalServerError)
return
}
defer encrypted.Close()
key := fmt.Sprintf("%s-%d/bundle.tgz.age", instanceID, time.Now().Unix())
input := &s3manager.UploadInput{
Body: aws.ReadSeekCloser(encrypted),
Bucket: aws.String(bucket),
Key: aws.String(key),
}
s3Uploader := persistence.GetS3Uploader()
_, err = s3Uploader.UploadWithContext(context.Background(), input)
if err != nil {
logger.Error(errors.Errorf("Failed to upload bundle to s3 for instance %s: %v", instanceID, err))
w.WriteHeader(http.StatusInternalServerError)
return
}
bundleURL := fmt.Sprintf("https://%s.s3.amazonaws.com/%s", bucket, key)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(bundleURL + "\n"))
}
}