Skip to content

Commit a9a3a6f

Browse files
author
Moritz Clasmeier
committed
New shell command
1 parent 059c3e3 commit a9a3a6f

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

cmd/shell.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"os"
8+
"os/exec"
9+
"time"
10+
11+
"github.com/spf13/cobra"
12+
"github.com/stackrox/roxie/internal/env"
13+
"github.com/stackrox/roxie/internal/logger"
14+
"github.com/stackrox/roxie/internal/manifest"
15+
)
16+
17+
func newShellCmd() *cobra.Command {
18+
cmd := &cobra.Command{
19+
Use: "shell [-- command [args...]]",
20+
Short: "Open a subshell for an existing ACS Central deployment",
21+
Long: `Open an interactive subshell with ACS environment variables
22+
set for an existing ACS Central deployment.
23+
24+
This command reads the roxie manifest secret from the cluster,
25+
re-fetches the CA certificate, and spawns an interactive subshell
26+
with the environment variables set.
27+
28+
If a command is given after "--", it is executed in the modified environment
29+
instead of spawning a subshell.
30+
31+
Examples:
32+
roxie shel
33+
roxie shell -- roxctl central whoami
34+
roxie shell -- bash -c 'echo $ROX_ENDPOINT'`,
35+
Run: func(cmd *cobra.Command, args []string) {
36+
err := runShell(cmd, args)
37+
if err != nil {
38+
if exitErr, ok := errors.AsType[*exec.ExitError](err); ok {
39+
// Propagate exit error from the child process.
40+
os.Exit(exitErr.ExitCode())
41+
}
42+
os.Exit(1)
43+
}
44+
},
45+
DisableFlagParsing: false,
46+
}
47+
48+
cmd.Flags().StringVar(&shell, "shell", "", "Shell to spawn")
49+
50+
return cmd
51+
}
52+
53+
func runShell(cmd *cobra.Command, args []string) error {
54+
log := logger.New()
55+
if err := env.Initialize(log); err != nil {
56+
return err
57+
}
58+
59+
if os.Getenv("ROXIE_SHELL") != "" {
60+
return errors.New("already in a roxie sub-shell (ROXIE_SHELL environment variable is set), please exit the shell and try again")
61+
}
62+
63+
log.Info("Loading manifest from cluster...")
64+
65+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
66+
defer cancel()
67+
68+
m, err := manifest.LoadManifestSecret(ctx, log)
69+
if err != nil {
70+
return fmt.Errorf("failed to load roxie manifest: %w", err)
71+
}
72+
log.Dim("roxie manifest loaded")
73+
74+
// We need this for the setup of the CA cert.
75+
tempDir, err := os.MkdirTemp("", "roxie-shell-*")
76+
if err != nil {
77+
return fmt.Errorf("failed to create temp dir: %w", err)
78+
}
79+
defer os.RemoveAll(tempDir)
80+
81+
centralDeploymentInfo, err := manifest.ManifestToCentralDeploymentInfo(ctx, log, tempDir, m)
82+
if err != nil {
83+
return fmt.Errorf("extracting central deployment info from manifest: %w", err)
84+
}
85+
86+
return runCommandOrSubshell(centralDeploymentInfo, log, args)
87+
}

0 commit comments

Comments
 (0)