-
Notifications
You must be signed in to change notification settings - Fork 1
CLI: Update Hypeman Go SDK to 9cd8f5ca0926682c2f1c1148adf3f5d1e289b7f6 #41
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d994913
CLI: Update Hypeman Go SDK to fae578c6868a3ad232e67a8bd323b9fb307451d3
kernel-internal[bot] ebaa09f
CLI: Update hypeman SDK to 40bbd485e7 and add --state/--metadata flag…
kernel-internal[bot] f66ded9
CLI: Update hypeman SDK to 1f34cf2541 and add --cpus/--memory flags t…
kernel-internal[bot] 25ff1e4
CLI: Update Hypeman Go SDK to 9cd8f5ca0926682c2f1c1148adf3f5d1e289b7f6
kernel-internal[bot] cf00700
fix ps filtering and metadata handling
sjmiller609 d39f873
add inspect and fork commands with sdk-based calls
sjmiller609 7130271
Hide envs by default on output
sjmiller609 a7132f6
show id of forked vm
sjmiller609 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,5 @@ dist/ | |
| .env | ||
| hypeman/** | ||
| bin/hypeman | ||
| demos/ | ||
| .DS_Store | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/kernel/hypeman-go" | ||
| "github.com/kernel/hypeman-go/option" | ||
| "github.com/tidwall/gjson" | ||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| var forkCmd = cli.Command{ | ||
| Name: "fork", | ||
| Usage: "Fork an instance into a new instance", | ||
| ArgsUsage: "<source> <name>", | ||
| Flags: []cli.Flag{ | ||
| &cli.BoolFlag{ | ||
| Name: "from-running", | ||
| Usage: "Allow forking from a running source by doing standby -> fork -> restore", | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "target-state", | ||
| Usage: "Target state for the forked instance: Stopped, Standby, or Running", | ||
| }, | ||
| }, | ||
| Action: handleFork, | ||
| HideHelpCommand: true, | ||
| } | ||
|
|
||
| func handleFork(ctx context.Context, cmd *cli.Command) error { | ||
| args := cmd.Args().Slice() | ||
| if len(args) < 2 { | ||
| return fmt.Errorf("source instance and target name required\nUsage: hypeman fork [flags] <source> <name>") | ||
| } | ||
|
|
||
| source := args[0] | ||
| targetName := args[1] | ||
|
|
||
| targetState, err := normalizeForkTargetState(cmd.String("target-state")) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| client := hypeman.NewClient(getDefaultRequestOptions(cmd)...) | ||
|
|
||
| sourceID, err := ResolveInstance(ctx, &client, source) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var opts []option.RequestOption | ||
| if cmd.Root().Bool("debug") { | ||
| opts = append(opts, debugMiddlewareOption) | ||
| } | ||
|
|
||
| params := instanceForkParams{ | ||
| Name: targetName, | ||
| } | ||
| if cmd.IsSet("from-running") { | ||
| fromRunning := cmd.Bool("from-running") | ||
| params.FromRunning = &fromRunning | ||
| } | ||
| if targetState != "" { | ||
| params.TargetState = &targetState | ||
| } | ||
|
|
||
| fmt.Fprintf(os.Stderr, "Forking %s to %s...\n", source, targetName) | ||
|
|
||
| format := cmd.Root().String("format") | ||
| transform := cmd.Root().String("transform") | ||
|
|
||
| var raw []byte | ||
| if format != "auto" { | ||
| opts = append(opts, option.WithResponseBodyInto(&raw)) | ||
| } | ||
|
|
||
| var forked hypeman.Instance | ||
| if err := client.Post(ctx, fmt.Sprintf("instances/%s/fork", sourceID), params, &forked, opts...); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if format != "auto" { | ||
| obj := gjson.ParseBytes(raw) | ||
| return ShowJSON(os.Stdout, "instance fork", obj, format, transform) | ||
| } | ||
|
|
||
| // Output instance ID (useful for scripting) | ||
| fmt.Println(forked.ID) | ||
| fmt.Fprintf(os.Stderr, "Forked %s as %s (state: %s)\n", source, forked.Name, forked.State) | ||
| return nil | ||
| } | ||
|
|
||
| func normalizeForkTargetState(state string) (string, error) { | ||
| switch strings.ToLower(state) { | ||
| case "": | ||
| return "", nil | ||
| case "stopped": | ||
| return "Stopped", nil | ||
| case "standby": | ||
| return "Standby", nil | ||
| case "running": | ||
| return "Running", nil | ||
| default: | ||
| return "", fmt.Errorf("invalid target state: %s (must be Stopped, Standby, or Running)", state) | ||
| } | ||
| } | ||
|
|
||
| type instanceForkParams struct { | ||
| Name string `json:"name"` | ||
| FromRunning *bool `json:"from_running,omitempty"` | ||
| TargetState *string `json:"target_state,omitempty"` | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestNormalizeForkTargetState(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| expected string | ||
| shouldErr bool | ||
| }{ | ||
| { | ||
| name: "empty state", | ||
| input: "", | ||
| expected: "", | ||
| }, | ||
| { | ||
| name: "stopped lowercase", | ||
| input: "stopped", | ||
| expected: "Stopped", | ||
| }, | ||
| { | ||
| name: "standby mixed case", | ||
| input: "StAnDbY", | ||
| expected: "Standby", | ||
| }, | ||
| { | ||
| name: "running title case", | ||
| input: "Running", | ||
| expected: "Running", | ||
| }, | ||
| { | ||
| name: "invalid state", | ||
| input: "paused", | ||
| shouldErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result, err := normalizeForkTargetState(tt.input) | ||
| if tt.shouldErr { | ||
| require.Error(t, err) | ||
| return | ||
| } | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/kernel/hypeman-go" | ||
| "github.com/kernel/hypeman-go/option" | ||
| "github.com/tidwall/gjson" | ||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| var inspectCmd = cli.Command{ | ||
| Name: "inspect", | ||
| Usage: "Get instance details by ID or name", | ||
| ArgsUsage: "<instance>", | ||
| Flags: []cli.Flag{ | ||
| &cli.BoolFlag{ | ||
| Name: "show-env", | ||
| Usage: "Show environment variable values (default: hidden)", | ||
| }, | ||
| }, | ||
| Action: handleInspect, | ||
| HideHelpCommand: true, | ||
| } | ||
|
|
||
| func handleInspect(ctx context.Context, cmd *cli.Command) error { | ||
| args := cmd.Args().Slice() | ||
| if len(args) < 1 { | ||
| return fmt.Errorf("instance ID or name required\nUsage: hypeman inspect <instance>") | ||
| } | ||
|
|
||
| client := hypeman.NewClient(getDefaultRequestOptions(cmd)...) | ||
|
|
||
| instanceID, err := ResolveInstance(ctx, &client, args[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var opts []option.RequestOption | ||
| if cmd.Root().Bool("debug") { | ||
| opts = append(opts, debugMiddlewareOption) | ||
| } | ||
|
|
||
| instance, err := client.Instances.Get(ctx, instanceID, opts...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !cmd.Bool("show-env") { | ||
| instance.Env = redactEnvValues(instance.Env) | ||
| } | ||
|
|
||
| raw, err := json.Marshal(instance) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to encode instance response: %w", err) | ||
| } | ||
|
|
||
| format := cmd.Root().String("format") | ||
| transform := cmd.Root().String("transform") | ||
|
|
||
| obj := gjson.ParseBytes(raw) | ||
| return ShowJSON(os.Stdout, "instance inspect", obj, format, transform) | ||
| } | ||
|
|
||
| func redactEnvValues(env map[string]string) map[string]string { | ||
| if len(env) == 0 { | ||
| return env | ||
| } | ||
|
|
||
| redacted := make(map[string]string, len(env)) | ||
| for key := range env { | ||
| redacted[key] = "[hidden]" | ||
| } | ||
|
|
||
| return redacted | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.