Skip to content

Commit 4bf019b

Browse files
committed
lifecycle shorthand commands
1 parent 799067f commit 4bf019b

File tree

3 files changed

+167
-4
lines changed

3 files changed

+167
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ hypeman exec -it my-app /bin/sh
5757

5858
# VM lifecycle
5959
# Turn off the VM
60-
hypeman instances stop --id my-app
60+
hypeman stop my-app
6161
# Boot the VM that was turned off
62-
hypeman instances start --id my-app
62+
hypeman start my-app
6363
# Put the VM to sleep (paused)
64-
hypeman instances standby --id my-app
64+
hypeman standby my-app
6565
# Awaken the VM (resumed)
66-
hypeman instances restore --id my-app
66+
hypeman restore my-app
6767

6868
# Create a reverse proxy ("ingress") from the host to your VM
6969
hypeman ingress create --name my-ingress my-app --hostname my-nginx-app --port 80 --host-port 8081

pkg/cmd/cmd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func init() {
7474
&psCmd,
7575
&logsCmd,
7676
&rmCmd,
77+
&stopCmd,
78+
&startCmd,
79+
&standbyCmd,
80+
&restoreCmd,
7781
&ingressCmd,
7882
{
7983
Name: "health",

pkg/cmd/lifecycle.go

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/onkernel/hypeman-go"
9+
"github.com/onkernel/hypeman-go/option"
10+
"github.com/urfave/cli/v3"
11+
)
12+
13+
var stopCmd = cli.Command{
14+
Name: "stop",
15+
Usage: "Stop a running instance",
16+
ArgsUsage: "<instance>",
17+
Action: handleStop,
18+
HideHelpCommand: true,
19+
}
20+
21+
var startCmd = cli.Command{
22+
Name: "start",
23+
Usage: "Start a stopped instance",
24+
ArgsUsage: "<instance>",
25+
Action: handleStart,
26+
HideHelpCommand: true,
27+
}
28+
29+
var standbyCmd = cli.Command{
30+
Name: "standby",
31+
Usage: "Put an instance into standby (pause and snapshot)",
32+
ArgsUsage: "<instance>",
33+
Action: handleStandby,
34+
HideHelpCommand: true,
35+
}
36+
37+
var restoreCmd = cli.Command{
38+
Name: "restore",
39+
Usage: "Restore an instance from standby",
40+
ArgsUsage: "<instance>",
41+
Action: handleRestore,
42+
HideHelpCommand: true,
43+
}
44+
45+
func handleStop(ctx context.Context, cmd *cli.Command) error {
46+
args := cmd.Args().Slice()
47+
if len(args) < 1 {
48+
return fmt.Errorf("instance name or ID required\nUsage: hypeman stop <instance>")
49+
}
50+
51+
client := hypeman.NewClient(getDefaultRequestOptions(cmd)...)
52+
53+
instanceID, err := ResolveInstance(ctx, &client, args[0])
54+
if err != nil {
55+
return err
56+
}
57+
58+
var opts []option.RequestOption
59+
if cmd.Root().Bool("debug") {
60+
opts = append(opts, debugMiddlewareOption)
61+
}
62+
63+
fmt.Fprintf(os.Stderr, "Stopping %s...\n", args[0])
64+
65+
instance, err := client.Instances.Stop(ctx, instanceID, opts...)
66+
if err != nil {
67+
return err
68+
}
69+
70+
fmt.Fprintf(os.Stderr, "Stopped %s (state: %s)\n", instance.Name, instance.State)
71+
return nil
72+
}
73+
74+
func handleStart(ctx context.Context, cmd *cli.Command) error {
75+
args := cmd.Args().Slice()
76+
if len(args) < 1 {
77+
return fmt.Errorf("instance name or ID required\nUsage: hypeman start <instance>")
78+
}
79+
80+
client := hypeman.NewClient(getDefaultRequestOptions(cmd)...)
81+
82+
instanceID, err := ResolveInstance(ctx, &client, args[0])
83+
if err != nil {
84+
return err
85+
}
86+
87+
var opts []option.RequestOption
88+
if cmd.Root().Bool("debug") {
89+
opts = append(opts, debugMiddlewareOption)
90+
}
91+
92+
fmt.Fprintf(os.Stderr, "Starting %s...\n", args[0])
93+
94+
instance, err := client.Instances.Start(ctx, instanceID, opts...)
95+
if err != nil {
96+
return err
97+
}
98+
99+
fmt.Fprintf(os.Stderr, "Started %s (state: %s)\n", instance.Name, instance.State)
100+
return nil
101+
}
102+
103+
func handleStandby(ctx context.Context, cmd *cli.Command) error {
104+
args := cmd.Args().Slice()
105+
if len(args) < 1 {
106+
return fmt.Errorf("instance name or ID required\nUsage: hypeman standby <instance>")
107+
}
108+
109+
client := hypeman.NewClient(getDefaultRequestOptions(cmd)...)
110+
111+
instanceID, err := ResolveInstance(ctx, &client, args[0])
112+
if err != nil {
113+
return err
114+
}
115+
116+
var opts []option.RequestOption
117+
if cmd.Root().Bool("debug") {
118+
opts = append(opts, debugMiddlewareOption)
119+
}
120+
121+
fmt.Fprintf(os.Stderr, "Putting %s into standby...\n", args[0])
122+
123+
instance, err := client.Instances.Standby(ctx, instanceID, opts...)
124+
if err != nil {
125+
return err
126+
}
127+
128+
fmt.Fprintf(os.Stderr, "Standby %s (state: %s)\n", instance.Name, instance.State)
129+
return nil
130+
}
131+
132+
func handleRestore(ctx context.Context, cmd *cli.Command) error {
133+
args := cmd.Args().Slice()
134+
if len(args) < 1 {
135+
return fmt.Errorf("instance name or ID required\nUsage: hypeman restore <instance>")
136+
}
137+
138+
client := hypeman.NewClient(getDefaultRequestOptions(cmd)...)
139+
140+
instanceID, err := ResolveInstance(ctx, &client, args[0])
141+
if err != nil {
142+
return err
143+
}
144+
145+
var opts []option.RequestOption
146+
if cmd.Root().Bool("debug") {
147+
opts = append(opts, debugMiddlewareOption)
148+
}
149+
150+
fmt.Fprintf(os.Stderr, "Restoring %s from standby...\n", args[0])
151+
152+
instance, err := client.Instances.Restore(ctx, instanceID, opts...)
153+
if err != nil {
154+
return err
155+
}
156+
157+
fmt.Fprintf(os.Stderr, "Restored %s (state: %s)\n", instance.Name, instance.State)
158+
return nil
159+
}

0 commit comments

Comments
 (0)