Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f7c19fd

Browse files
johnstcngreyscaled
authored andcommitted
refactor: workspace autostop_schedule -> ttl (#1578)
Co-authored-by: G r e y <[email protected]>
1 parent d3817d7 commit f7c19fd

31 files changed

+530
-471
lines changed

agent/usershell/usershell_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package usershell
33
import "os"
44

55
// Get returns the $SHELL environment variable.
6-
func Get(username string) (string, error) {
6+
func Get(_ string) (string, error) {
77
return os.Getenv("SHELL"), nil
88
}

cli/autostop.go

Lines changed: 0 additions & 167 deletions
This file was deleted.

cli/list.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func list() *cobra.Command {
4949
}
5050

5151
tableWriter := cliui.Table()
52-
header := table.Row{"workspace", "template", "status", "last built", "outdated", "autostart", "autostop"}
52+
header := table.Row{"workspace", "template", "status", "last built", "outdated", "autostart", "ttl"}
5353
tableWriter.AppendHeader(header)
5454
tableWriter.SortBy([]table.SortBy{{
5555
Name: "workspace",
@@ -116,10 +116,8 @@ func list() *cobra.Command {
116116
}
117117

118118
autostopDisplay := "-"
119-
if workspace.AutostopSchedule != "" {
120-
if sched, err := schedule.Weekly(workspace.AutostopSchedule); err == nil {
121-
autostopDisplay = sched.Cron()
122-
}
119+
if workspace.TTL != nil {
120+
autostopDisplay = workspace.TTL.String()
123121
}
124122

125123
user := usersByID[workspace.OwnerID]

cli/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func Root() *cobra.Command {
6262

6363
cmd.AddCommand(
6464
autostart(),
65-
autostop(),
6665
configSSH(),
6766
create(),
6867
delete(),
@@ -78,6 +77,7 @@ func Root() *cobra.Command {
7877
stop(),
7978
ssh(),
8079
templates(),
80+
ttl(),
8181
update(),
8282
users(),
8383
portForward(),

cli/ssh.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/coder/coder/cli/cliflag"
2222
"github.com/coder/coder/cli/cliui"
2323
"github.com/coder/coder/coderd/autobuild/notify"
24-
"github.com/coder/coder/coderd/autobuild/schedule"
2524
"github.com/coder/coder/codersdk"
2625
"github.com/coder/coder/cryptorand"
2726
)
@@ -270,16 +269,11 @@ func notifyCondition(ctx context.Context, client *codersdk.Client, workspaceID u
270269
return time.Time{}, nil
271270
}
272271

273-
if ws.AutostopSchedule == "" {
272+
if ws.TTL == nil || *ws.TTL == 0 {
274273
return time.Time{}, nil
275274
}
276275

277-
sched, err := schedule.Weekly(ws.AutostopSchedule)
278-
if err != nil {
279-
return time.Time{}, nil
280-
}
281-
282-
deadline = sched.Next(now)
276+
deadline = ws.LatestBuild.UpdatedAt.Add(*ws.TTL)
283277
callback = func() {
284278
ttl := deadline.Sub(now)
285279
var title, body string

cli/ttl.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/spf13/cobra"
8+
"golang.org/x/xerrors"
9+
10+
"github.com/coder/coder/codersdk"
11+
)
12+
13+
const ttlDescriptionLong = `To have your workspace stop automatically after a configurable interval has passed.
14+
Minimum TTL is 1 minute.
15+
`
16+
17+
func ttl() *cobra.Command {
18+
ttlCmd := &cobra.Command{
19+
Annotations: workspaceCommand,
20+
Use: "ttl [command]",
21+
Short: "Schedule a workspace to automatically stop after a configurable interval",
22+
Long: ttlDescriptionLong,
23+
Example: "coder ttl set my-workspace 8h30m",
24+
}
25+
26+
ttlCmd.AddCommand(ttlShow())
27+
ttlCmd.AddCommand(ttlset())
28+
ttlCmd.AddCommand(ttlunset())
29+
30+
return ttlCmd
31+
}
32+
33+
func ttlShow() *cobra.Command {
34+
cmd := &cobra.Command{
35+
Use: "show <workspace_name>",
36+
Args: cobra.ExactArgs(1),
37+
RunE: func(cmd *cobra.Command, args []string) error {
38+
client, err := createClient(cmd)
39+
if err != nil {
40+
return xerrors.Errorf("create client: %w", err)
41+
}
42+
organization, err := currentOrganization(cmd, client)
43+
if err != nil {
44+
return xerrors.Errorf("get current org: %w", err)
45+
}
46+
47+
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
48+
if err != nil {
49+
return xerrors.Errorf("get workspace: %w", err)
50+
}
51+
52+
if workspace.TTL == nil {
53+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "not set\n")
54+
return nil
55+
}
56+
57+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", workspace.TTL)
58+
59+
return nil
60+
},
61+
}
62+
return cmd
63+
}
64+
65+
func ttlset() *cobra.Command {
66+
cmd := &cobra.Command{
67+
Use: "set <workspace_name> <ttl>",
68+
Args: cobra.ExactArgs(2),
69+
RunE: func(cmd *cobra.Command, args []string) error {
70+
client, err := createClient(cmd)
71+
if err != nil {
72+
return xerrors.Errorf("create client: %w", err)
73+
}
74+
organization, err := currentOrganization(cmd, client)
75+
if err != nil {
76+
return xerrors.Errorf("get current org: %w", err)
77+
}
78+
79+
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
80+
if err != nil {
81+
return xerrors.Errorf("get workspace: %w", err)
82+
}
83+
84+
ttl, err := time.ParseDuration(args[1])
85+
if err != nil {
86+
return xerrors.Errorf("parse ttl: %w", err)
87+
}
88+
89+
truncated := ttl.Truncate(time.Minute)
90+
91+
if truncated == 0 {
92+
return xerrors.Errorf("ttl must be at least 1m")
93+
}
94+
95+
if truncated != ttl {
96+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "warning: ttl rounded down to %s\n", truncated)
97+
}
98+
99+
err = client.UpdateWorkspaceTTL(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
100+
TTL: &truncated,
101+
})
102+
if err != nil {
103+
return xerrors.Errorf("update workspace ttl: %w", err)
104+
}
105+
106+
return nil
107+
},
108+
}
109+
110+
return cmd
111+
}
112+
113+
func ttlunset() *cobra.Command {
114+
return &cobra.Command{
115+
Use: "unset <workspace_name>",
116+
Args: cobra.ExactArgs(1),
117+
RunE: func(cmd *cobra.Command, args []string) error {
118+
client, err := createClient(cmd)
119+
if err != nil {
120+
return xerrors.Errorf("create client: %w", err)
121+
}
122+
organization, err := currentOrganization(cmd, client)
123+
if err != nil {
124+
return xerrors.Errorf("get current org: %w", err)
125+
}
126+
127+
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
128+
if err != nil {
129+
return xerrors.Errorf("get workspace: %w", err)
130+
}
131+
132+
err = client.UpdateWorkspaceTTL(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
133+
TTL: nil,
134+
})
135+
if err != nil {
136+
return xerrors.Errorf("update workspace ttl: %w", err)
137+
}
138+
139+
_, _ = fmt.Fprint(cmd.OutOrStdout(), "ttl unset\n", workspace.Name)
140+
141+
return nil
142+
},
143+
}
144+
}

0 commit comments

Comments
 (0)