Skip to content

Commit

Permalink
iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
directionless committed Oct 24, 2023
1 parent 414e713 commit d43bff9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
46 changes: 31 additions & 15 deletions cmd/launcher/flare.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/kit/ulid"
"github.com/kolide/launcher/pkg/agent/flags"
"github.com/kolide/launcher/pkg/agent/knapsack"
Expand Down Expand Up @@ -50,10 +52,6 @@ func runFlare(args []string) error {
return fmt.Errorf("parsing flags: %w", err)
}

if *flSave != "local" && *flSave != "upload" {
return fmt.Errorf("invalid save option: %s, expected local or upload", *flSave)
}

// were passing an empty array here just to get the default options
opts, err := launcher.ParseOptions("flareupload", make([]string, 0))
if err != nil {
Expand All @@ -67,23 +65,41 @@ func runFlare(args []string) error {
k := knapsack.New(nil, flagController, nil)
ctx := context.Background()

if *flSave == "upload" {
type flareDestinationTyp interface {
io.WriteCloser
Name() string
}
var flareDest flareDestinationTyp

switch *flSave {
case "upload":
shipper, err := shipper.New(k, shipper.WithNote(strings.Join(flagset.Args(), " ")), shipper.WithUploadRequestURL(*flUploadRequestURL))
if err != nil {
return err
}
return checkups.RunFlare(ctx, k, shipper, checkups.StandaloneEnviroment)
}
flareDest = shipper
case "local":
reportName := fmt.Sprintf("kolide_agent_flare_report_%s.zip", ulid.New())
reportPath := filepath.Join(*flOutputDir, reportName)

flareFile, err := os.Create(reportPath)
if err != nil {
return fmt.Errorf("creating flare file (%s): %w", reportPath, err)
}
defer flareFile.Close()
flareDest = flareFile
default:
return fmt.Errorf(`invalid save option: %s, expected "local" or "upload"`, *flSave)

// saving flare locally
reportName := fmt.Sprintf("kolide_agent_flare_report_%s", ulid.New())
reportPath := fmt.Sprintf("%s.zip", filepath.Join(*flOutputDir, reportName))
}

flareFile, err := os.Create(reportPath)
if err != nil {
return fmt.Errorf("creating flare file (%s): %w", reportPath, err)
if err := checkups.RunFlare(ctx, k, flareDest, checkups.StandaloneEnviroment); err != nil {
return err
}
defer flareFile.Close()

return checkups.RunFlare(ctx, k, flareFile, checkups.StandaloneEnviroment)
if flareDest.Name() == "" {
level.Info(logger).Log("msg", "Flare completed")
}
level.Info(logger).Log("msg", "Flare completed", "file", flareDest.Name())
return nil
}
9 changes: 8 additions & 1 deletion pkg/debug/shipper/shipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type shipper struct {
writer io.WriteCloser
knapsack types.Knapsack

uploadName string
uploadRequestURL string
uploadRequest *http.Request
uploadRequestStarted bool
Expand Down Expand Up @@ -91,6 +92,10 @@ func New(knapsack types.Knapsack, opts ...shipperOption) (*shipper, error) {
return s, nil
}

func (s *shipper) Name() string {
return s.uploadName
}

func (s *shipper) Write(p []byte) (n int, err error) {
if s.uploadRequestStarted {
return s.writer.Write(p)
Expand Down Expand Up @@ -164,7 +169,8 @@ func (s *shipper) signedUrl() (string, error) {
defer signedUrlResponse.Body.Close()

responseData := struct {
URL string `json:"URL"`
URL string `json:"URL"`
Name string `json:"name"`
}{}

if err := json.NewDecoder(signedUrlResponse.Body).Decode(&responseData); err != nil {
Expand All @@ -175,6 +181,7 @@ func (s *shipper) signedUrl() (string, error) {
return "", fmt.Errorf("got %s status in signed url response", signedUrlResponse.Status)
}

s.uploadName = responseData.Name
return responseData.URL, nil
}

Expand Down

0 comments on commit d43bff9

Please sign in to comment.