Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add saucectl imagerunner logs command #735

Merged
merged 7 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/saucectl/saucectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/saucelabs/saucectl/internal/cmd/completion"
"github.com/saucelabs/saucectl/internal/cmd/configure"
"github.com/saucelabs/saucectl/internal/cmd/doctor"
"github.com/saucelabs/saucectl/internal/cmd/hto"
"github.com/saucelabs/saucectl/internal/cmd/ini"
"github.com/saucelabs/saucectl/internal/cmd/jobs"
"github.com/saucelabs/saucectl/internal/cmd/new"
Expand Down Expand Up @@ -71,6 +72,7 @@ func main() {
storage.Command(cmd.PersistentPreRun),
artifacts.Command(cmd.PersistentPreRun),
jobs.Command(cmd.PersistentPreRun),
hto.Command(cmd.PersistentPreRun),
)

if err := cmd.Execute(); err != nil {
Expand Down
48 changes: 48 additions & 0 deletions internal/cmd/hto/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package hto

import (
"errors"
"time"

"github.com/saucelabs/saucectl/internal/credentials"
"github.com/saucelabs/saucectl/internal/http"
"github.com/saucelabs/saucectl/internal/region"
"github.com/spf13/cobra"
)

var (
imagerunnerClient http.ImageRunner
)

func Command(preRun func(cmd *cobra.Command, args []string)) *cobra.Command {
var regio string

cmd := &cobra.Command{
Use: "hto",
Short: "Commands for interacting with Hosted Test Orchestration (HTO) runs",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if preRun != nil {
preRun(cmd, args)
}

if region.FromString(regio) == region.None {
return errors.New("invalid region")
}

creds := credentials.Get()
url := region.FromString(regio).APIBaseURL()

imagerunnerClient = http.NewImageRunner(url, creds, 15*time.Minute)

return nil
},
}

flags := cmd.PersistentFlags()
flags.StringVarP(&regio, "region", "r", "us-west-1", "The Sauce Labs region. Options: us-west-1, eu-central-1.")

cmd.AddCommand(
LogsCommand(),
)
return cmd
}
45 changes: 45 additions & 0 deletions internal/cmd/hto/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package hto

import (
"context"
"fmt"

cmds "github.com/saucelabs/saucectl/internal/cmd"
"github.com/saucelabs/saucectl/internal/segment"
"github.com/saucelabs/saucectl/internal/usage"
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)

func LogsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "logs <runID>",
Short: "Fetch the logs for an HTO run",
PreRun: func(cmd *cobra.Command, args []string) {
tracker := segment.DefaultTracker

go func() {
tracker.Collect(
cases.Title(language.English).String(cmds.FullName(cmd)),
usage.Properties{}.SetFlags(cmd.Flags()),
)
_ = tracker.Close()
}()
},
RunE: func(cmd *cobra.Command, args []string) error {
return exec(args[0])
},
}

return cmd
}

func exec(runID string) error {
log, err := imagerunnerClient.GetLogs(context.Background(), runID)
if err != nil {
return err
}
fmt.Println(log)
return nil
}
2 changes: 1 addition & 1 deletion internal/cmd/storage/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Command(preRun func(cmd *cobra.Command, args []string)) *cobra.Command {

cmd := &cobra.Command{
Use: "storage",
Short: "Interact with Sauce Storage.",
Short: "Interact with Sauce Storage",
TraverseChildren: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if preRun != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/http/imagerunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ func (c *ImageRunner) DownloadArtifacts(ctx context.Context, id string) (io.Read
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusNotFound {
return nil, imagerunner.LogURLNotFoundError{
RunID: id,
}
}
if resp.StatusCode != http.StatusOK {
b, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("unexpected server response (%d): %s", resp.StatusCode, b)
Expand Down Expand Up @@ -169,6 +174,11 @@ func (c *ImageRunner) GetLogs(ctx context.Context, id string) (string, error) {
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusNotFound {
return "", imagerunner.LogURLNotFoundError{
mhan83 marked this conversation as resolved.
Show resolved Hide resolved
RunID: id,
}
}
if resp.StatusCode != http.StatusOK {
b, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("unexpected server response (%d): %s", resp.StatusCode, b)
Expand Down
13 changes: 12 additions & 1 deletion internal/imagerunner/imagerunner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package imagerunner

import "errors"
import (
"errors"
"fmt"
)

// The different states that a runner can be in.
const (
Expand Down Expand Up @@ -31,6 +34,14 @@ func Done(status string) bool {

var ErrResourceNotFound = errors.New("resource not found")

type LogURLNotFoundError struct {
RunID string
}

func (e LogURLNotFoundError) Error() string {
return fmt.Sprintf("Could not find log URL for run with ID %s", e.RunID)
mhan83 marked this conversation as resolved.
Show resolved Hide resolved
}

type RunnerSpec struct {
Container Container `json:"container,omitempty"`
EntryPoint string `json:"entrypoint,omitempty"`
Expand Down