Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf7c19fd

Browse files
johnstcngreyscaled
authored andcommitted
refactor: workspace autostop_schedule -> ttl (#1578)
Co-authored-by: G r e y <grey@coder.com>
1 parentd3817d7 commitf7c19fd

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-
funcGet(usernamestring) (string,error) {
6+
funcGet(_string) (string,error) {
77
returnos.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-
ifworkspace.AutostopSchedule!="" {
120-
ifsched,err:=schedule.Weekly(workspace.AutostopSchedule);err==nil {
121-
autostopDisplay=sched.Cron()
122-
}
119+
ifworkspace.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-
ifws.AutostopSchedule=="" {
272+
ifws.TTL==nil||*ws.TTL==0 {
274273
return time.Time{},nil
275274
}
276275

277-
sched,err:=schedule.Weekly(ws.AutostopSchedule)
278-
iferr!=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
vartitle,bodystring

‎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+
constttlDescriptionLong=`To have your workspace stop automatically after a configurable interval has passed.
14+
Minimum TTL is 1 minute.
15+
`
16+
17+
functtl()*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+
returnttlCmd
31+
}
32+
33+
functtlShow()*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+
iferr!=nil {
40+
returnxerrors.Errorf("create client: %w",err)
41+
}
42+
organization,err:=currentOrganization(cmd,client)
43+
iferr!=nil {
44+
returnxerrors.Errorf("get current org: %w",err)
45+
}
46+
47+
workspace,err:=client.WorkspaceByOwnerAndName(cmd.Context(),organization.ID,codersdk.Me,args[0])
48+
iferr!=nil {
49+
returnxerrors.Errorf("get workspace: %w",err)
50+
}
51+
52+
ifworkspace.TTL==nil {
53+
_,_=fmt.Fprintf(cmd.OutOrStdout(),"not set\n")
54+
returnnil
55+
}
56+
57+
_,_=fmt.Fprintf(cmd.OutOrStdout(),"%s\n",workspace.TTL)
58+
59+
returnnil
60+
},
61+
}
62+
returncmd
63+
}
64+
65+
functtlset()*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+
iferr!=nil {
72+
returnxerrors.Errorf("create client: %w",err)
73+
}
74+
organization,err:=currentOrganization(cmd,client)
75+
iferr!=nil {
76+
returnxerrors.Errorf("get current org: %w",err)
77+
}
78+
79+
workspace,err:=client.WorkspaceByOwnerAndName(cmd.Context(),organization.ID,codersdk.Me,args[0])
80+
iferr!=nil {
81+
returnxerrors.Errorf("get workspace: %w",err)
82+
}
83+
84+
ttl,err:=time.ParseDuration(args[1])
85+
iferr!=nil {
86+
returnxerrors.Errorf("parse ttl: %w",err)
87+
}
88+
89+
truncated:=ttl.Truncate(time.Minute)
90+
91+
iftruncated==0 {
92+
returnxerrors.Errorf("ttl must be at least 1m")
93+
}
94+
95+
iftruncated!=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+
iferr!=nil {
103+
returnxerrors.Errorf("update workspace ttl: %w",err)
104+
}
105+
106+
returnnil
107+
},
108+
}
109+
110+
returncmd
111+
}
112+
113+
functtlunset()*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+
iferr!=nil {
120+
returnxerrors.Errorf("create client: %w",err)
121+
}
122+
organization,err:=currentOrganization(cmd,client)
123+
iferr!=nil {
124+
returnxerrors.Errorf("get current org: %w",err)
125+
}
126+
127+
workspace,err:=client.WorkspaceByOwnerAndName(cmd.Context(),organization.ID,codersdk.Me,args[0])
128+
iferr!=nil {
129+
returnxerrors.Errorf("get workspace: %w",err)
130+
}
131+
132+
err=client.UpdateWorkspaceTTL(cmd.Context(),workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
133+
TTL:nil,
134+
})
135+
iferr!=nil {
136+
returnxerrors.Errorf("update workspace ttl: %w",err)
137+
}
138+
139+
_,_=fmt.Fprint(cmd.OutOrStdout(),"ttl unset\n",workspace.Name)
140+
141+
returnnil
142+
},
143+
}
144+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp