Skip to content

Commit

Permalink
Experimental Command runme env store check (#537)
Browse files Browse the repository at this point in the history
Adds an experimental command to check the smart store, it creates a new
session, if the session creation fails, it prints the founded errors.

Issue: #533

---------

Co-authored-by: Sebastian Tiedtke <[email protected]>
  • Loading branch information
pastuxso and sourishkrout authored Mar 17, 2024
1 parent 7796d6a commit 00b3953
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
1 change: 1 addition & 0 deletions internal/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ func setRunnerFlags(cmd *cobra.Command, serverAddr *string) func() ([]client.Run
client.WithInsecure(fInsecure),
client.WithEnableBackgroundProcesses(EnableBackgroundProcesses),
client.WithEnvs([]string{fmt.Sprintf("%s=%d", envStackDepth, stackDepth)}),
client.WithEnvStoreType(runnerv1.SessionEnvStoreType_SESSION_ENV_STORE_TYPE_UNSPECIFIED),
}

switch strings.ToLower(SessionStrategy) {
Expand Down
61 changes: 60 additions & 1 deletion internal/cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"fmt"
"os"
"strings"
"time"
Expand All @@ -15,6 +16,7 @@ import (
"google.golang.org/grpc/credentials"

runnerv1 "github.com/stateful/runme/v3/internal/gen/proto/go/runme/runner/v1"
"github.com/stateful/runme/v3/internal/runner/client"
runmetls "github.com/stateful/runme/v3/internal/tls"
)

Expand Down Expand Up @@ -46,6 +48,7 @@ func storeCmd() *cobra.Command {
}

cmd.AddCommand(storeSnapshotCmd())
cmd.AddCommand(storeCheckCmd())

return &cmd
}
Expand All @@ -71,7 +74,6 @@ func storeSnapshotCmd() *cobra.Command {
}

credentials := credentials.NewTLS(tlsConfig)

conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(credentials))
if err != nil {
return errors.Wrap(err, "failed to connect")
Expand Down Expand Up @@ -110,6 +112,63 @@ func storeSnapshotCmd() *cobra.Command {
return &cmd
}

func storeCheckCmd() *cobra.Command {
var (
serverAddr string
getRunnerOpts func() ([]client.RunnerOption, error)
)

cmd := cobra.Command{
Hidden: true,
Use: "check",
Short: "Validates smart store",
Long: "Connects with a running server to validates smart store, exiting with success or displaying API errors.",
RunE: func(cmd *cobra.Command, args []string) error {
project, err := getProject()
if err != nil {
return err
}

runnerOpts, err := getRunnerOpts()
if err != nil {
return err
}

runnerOpts = append(
runnerOpts,
client.WithinShellMaybe(),
client.WithStdin(cmd.InOrStdin()),
client.WithCleanupSession(true),
client.WithStdout(cmd.OutOrStdout()),
client.WithStderr(cmd.ErrOrStderr()),
client.WithProject(project),
client.WithEnvStoreType(runnerv1.SessionEnvStoreType_SESSION_ENV_STORE_TYPE_OWL),
)

_, err = client.NewRemoteRunner(
cmd.Context(),
serverAddr,
runnerOpts...,
)
if err != nil {
// todo(sebastian): hack
errStr := err.Error()
parts := strings.Split(errStr, "Unknown desc = ")
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Error: %s\n", parts[len(parts)-1])
return nil
}

// _, err = fmt.Printf("session created successfully in %s with id %s\n", project.Root(), runner.GetSessionID())
_, err = fmt.Println("Success")
return err
},
}

getRunnerOpts = setRunnerFlags(&cmd, &serverAddr)

return &cmd
}

func environmentDumpCmd() *cobra.Command {
cmd := cobra.Command{
Use: "dump",
Expand Down
8 changes: 8 additions & 0 deletions internal/runner/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type RunnerSettings struct {
tlsDir string

envs []string

envStoreType runnerv1.SessionEnvStoreType
}

func (rs *RunnerSettings) Clone() *RunnerSettings {
Expand Down Expand Up @@ -218,6 +220,12 @@ func WithTempSettings(rc Runner, opts []RunnerOption, cb func() error) error {
return nil
}

func WithEnvStoreType(EnvStoreType runnerv1.SessionEnvStoreType) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.envStoreType = EnvStoreType
})
}

func ApplyOptions(rc Runner, opts ...RunnerOption) error {
for _, opt := range opts {
if err := opt(rc); err != nil {
Expand Down
15 changes: 11 additions & 4 deletions internal/runner/client/client_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ func (r *RemoteRunner) setupSession(ctx context.Context) error {
return nil
}

resp, err := r.client.CreateSession(ctx, &runnerv1.CreateSessionRequest{
Envs: os.Environ(),
Project: ConvertToRunnerProject(r.project),
})
request := &runnerv1.CreateSessionRequest{
Envs: os.Environ(),
Project: ConvertToRunnerProject(r.project),
EnvStoreType: r.envStoreType,
}

resp, err := r.client.CreateSession(ctx, request)
if err != nil {
return errors.Wrap(err, "failed to create session")
}
Expand Down Expand Up @@ -297,3 +300,7 @@ func (r *RemoteRunner) GetEnvs(ctx context.Context) ([]string, error) {

return resp.Session.Envs, nil
}

func (r *RemoteRunner) GetSessionID() string {
return r.sessionID
}

0 comments on commit 00b3953

Please sign in to comment.