Skip to content

Commit 72d9691

Browse files
committed
labctl playground lifetime
1 parent 84beab0 commit 72d9691

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎api/plays.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ type Play struct {
6464

6565
ExpiresIn int `json:"expiresIn" yaml:"expiresIn"`
6666

67+
MaxPlayTime string `json:"maxPlayTime,omitempty" yaml:"maxPlayTime,omitempty"`
68+
6769
Status *PlayStatus `json:"status,omitempty" yaml:"status,omitempty"`
6870

6971
Playground Playground `json:"playground" yaml:"playground"`
@@ -370,6 +372,19 @@ func (c *Client) PersistPlay(ctx context.Context, id string) error {
370372
return c.PostInto(ctx, "/plays/"+id+"/actions", nil, nil, body, &p)
371373
}
372374

375+
func (c *Client) SetPlayMaxPlayTime(ctx context.Context, id string, maxPlayTimeMinutes int) (*Play, error) {
376+
body, err := toJSONBody(map[string]any{
377+
"action": "set_max_play_time",
378+
"maxPlayTimeMinutes": maxPlayTimeMinutes,
379+
})
380+
if err != nil {
381+
return nil, err
382+
}
383+
384+
var p Play
385+
return &p, c.PostInto(ctx, "/plays/"+id+"/actions", nil, nil, body, &p)
386+
}
387+
373388
func (c *Client) RebootPlayMachine(ctx context.Context, id, machine string) (*Play, error) {
374389
body, err := toJSONBody(map[string]any{"action": "machine.reboot", "machine": machine})
375390
if err != nil {

‎cmd/playground/lifetime.go‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package playground
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/spf13/cobra"
9+
10+
"github.com/iximiuz/labctl/internal/completion"
11+
"github.com/iximiuz/labctl/internal/labcli"
12+
)
13+
14+
type lifetimeOptions struct {
15+
playID string
16+
lifetime time.Duration
17+
}
18+
19+
func newLifetimeCommand(cli labcli.CLI) *cobra.Command {
20+
var opts lifetimeOptions
21+
22+
cmd := &cobra.Command{
23+
Use: "lifetime <playground-id> [new-lifetime]",
24+
Short: `Show or set a playground's lifetime - the total session duration counted from the playground's start (e.g., 90m, 3h, 2h30m)`,
25+
Args: cobra.RangeArgs(1, 2),
26+
ValidArgsFunction: completion.ActivePlays(cli),
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
opts.playID = args[0]
29+
30+
if len(args) == 1 {
31+
return labcli.WrapStatusError(runShowPlaygroundLifetime(cmd.Context(), cli, &opts))
32+
}
33+
34+
lifetime, err := time.ParseDuration(args[1])
35+
if err != nil {
36+
return labcli.WrapStatusError(fmt.Errorf("invalid lifetime %q: %w", args[1], err))
37+
}
38+
opts.lifetime = lifetime
39+
40+
return labcli.WrapStatusError(runSetPlaygroundLifetime(cmd.Context(), cli, &opts))
41+
},
42+
}
43+
44+
return cmd
45+
}
46+
47+
func runShowPlaygroundLifetime(ctx context.Context, cli labcli.CLI, opts *lifetimeOptions) error {
48+
play, err := cli.Client().GetPlay(ctx, opts.playID)
49+
if err != nil {
50+
return fmt.Errorf("couldn't get playground: %w", err)
51+
}
52+
53+
if play.MaxPlayTime == "" {
54+
return labcli.NewStatusError(1, "playground %s has no lifetime information", opts.playID)
55+
}
56+
57+
lifetime, err := time.ParseDuration(play.MaxPlayTime)
58+
if err != nil {
59+
cli.PrintOut("%s\n", play.MaxPlayTime)
60+
return nil
61+
}
62+
63+
if play.ExpiresIn > 0 {
64+
cli.PrintOut("%s (expires in %s)\n", lifetime, (time.Duration(play.ExpiresIn) * time.Millisecond).Round(time.Second))
65+
} else {
66+
cli.PrintOut("%s\n (expired)", lifetime)
67+
}
68+
69+
return nil
70+
}
71+
72+
func runSetPlaygroundLifetime(ctx context.Context, cli labcli.CLI, opts *lifetimeOptions) error {
73+
minutes := int(opts.lifetime.Minutes())
74+
if minutes < 1 {
75+
return fmt.Errorf("lifetime must be at least 1 minute")
76+
}
77+
78+
cli.PrintAux("Setting lifetime of playground %s to %s...\n", opts.playID, opts.lifetime)
79+
80+
play, err := cli.Client().SetPlayMaxPlayTime(ctx, opts.playID, minutes)
81+
if err != nil {
82+
return fmt.Errorf("couldn't set the playground lifetime: %w", err)
83+
}
84+
85+
if play.ExpiresIn > 0 {
86+
cli.PrintAux("Playground will now expire in %s.\n", (time.Duration(play.ExpiresIn) * time.Millisecond).Round(time.Second))
87+
} else {
88+
cli.PrintAux("Playground lifetime is set.\n")
89+
}
90+
91+
return nil
92+
}

‎cmd/playground/playground.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func NewCommand(cli labcli.CLI) *cobra.Command {
2727
newRestartCommand(cli),
2828
newDestroyCommand(cli),
2929
newPersistCommand(cli),
30+
newLifetimeCommand(cli),
3031
newMachinesCommand(cli),
3132
newMachineCommand(cli),
3233
newCreateCommand(cli),

0 commit comments

Comments
 (0)