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

feat: add default autostart and ttl for new workspaces#1632

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
johnstcn merged 12 commits intomainfromcj/default-autostart-ttl
May 23, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
12 commits
Select commitHold shift + click to select a range
5c092be
Dockerfile: add tzdata
johnstcnMay 20, 2022
391f527
database: add autostart_schedule and ttl to InsertWorkspace; make gen
johnstcnMay 20, 2022
98561bf
coderd: workspaces: consume additional fields of CreateWorkspaceRequest
johnstcnMay 20, 2022
0169315
fixup! coderd: workspaces: consume additional fields of CreateWorkspa…
johnstcnMay 20, 2022
dea541f
fixup! fixup! coderd: workspaces: consume additional fields of Create…
johnstcnMay 20, 2022
d621272
autobuild: fix unit tests
johnstcnMay 20, 2022
2f7b939
cli: update: add support for TTL and autostart_schedule
johnstcnMay 20, 2022
813a64f
cli: create: add unit tests
johnstcnMay 20, 2022
1fbc0f2
lint
johnstcnMay 20, 2022
142ad37
coder: use embedded tzdata instead
johnstcnMay 20, 2022
0f348cd
autobuild: fix unit test that only runs with a real db
johnstcnMay 20, 2022
32a86f1
address PR comments
johnstcnMay 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletionscli/create.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,14 +10,20 @@ import (

"github.com/coder/coder/cli/cliflag"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/coderd/autobuild/schedule"
"github.com/coder/coder/codersdk"
)

funccreate()*cobra.Command {
var (
workspaceNamestring
templateNamestring
parameterFilestring
autostartMinutestring
autostartHourstring
autostartDowstring
parameterFilestring
templateNamestring
ttl time.Duration
tzNamestring
workspaceNamestring
)
cmd:=&cobra.Command{
Annotations:workspaceCommand,
Expand DownExpand Up@@ -54,6 +60,20 @@ func create() *cobra.Command {
}
}

tz,err:=time.LoadLocation(tzName)
iferr!=nil {
returnxerrors.Errorf("Invalid workspace autostart timezone: %w",err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

nit: Error strings should not be capitalized:https://github.com/golang/go/wiki/CodeReviewComments#error-strings

f0ssel and johnstcn reacted with thumbs up emoji
}
schedSpec:=fmt.Sprintf("CRON_TZ=%s %s %s * * %s",tz.String(),autostartMinute,autostartHour,autostartDow)
_,err=schedule.Weekly(schedSpec)
iferr!=nil {
returnxerrors.Errorf("invalid workspace autostart schedule: %w",err)
}

ifttl==0 {
returnxerrors.Errorf("TTL must be at least 1 minute")
}

_,err=client.WorkspaceByOwnerAndName(cmd.Context(),organization.ID,codersdk.Me,workspaceName)
iferr==nil {
returnxerrors.Errorf("A workspace already exists named %q!",workspaceName)
Expand DownExpand Up@@ -174,9 +194,11 @@ func create() *cobra.Command {

before:=time.Now()
workspace,err:=client.CreateWorkspace(cmd.Context(),organization.ID, codersdk.CreateWorkspaceRequest{
TemplateID:template.ID,
Name:workspaceName,
ParameterValues:parameters,
TemplateID:template.ID,
Name:workspaceName,
AutostartSchedule:&schedSpec,
TTL:&ttl,
ParameterValues:parameters,
})
iferr!=nil {
returnerr
Expand DownExpand Up@@ -207,5 +229,10 @@ func create() *cobra.Command {
cliui.AllowSkipPrompt(cmd)
cliflag.StringVarP(cmd.Flags(),&templateName,"template","t","CODER_TEMPLATE_NAME","","Specify a template name.")
cliflag.StringVarP(cmd.Flags(),&parameterFile,"parameter-file","","CODER_PARAMETER_FILE","","Specify a file path with parameter values.")
cliflag.StringVarP(cmd.Flags(),&autostartMinute,"autostart-minute","","CODER_WORKSPACE_AUTOSTART_MINUTE","0","Specify the minute(s) at which the workspace should autostart (e.g. 0).")
cliflag.StringVarP(cmd.Flags(),&autostartHour,"autostart-hour","","CODER_WORKSPACE_AUTOSTART_HOUR","9","Specify the hour(s) at which the workspace should autostart (e.g. 9).")
cliflag.StringVarP(cmd.Flags(),&autostartDow,"autostart-day-of-week","","CODER_WORKSPACE_AUTOSTART_DOW","MON-FRI","Specify the days(s) on which the workspace should autostart (e.g. MON,TUE,WED,THU,FRI)")
cliflag.StringVarP(cmd.Flags(),&tzName,"tz","","TZ","","Specify your timezone location for workspace autostart (e.g. US/Central).")
cliflag.DurationVarP(cmd.Flags(),&ttl,"ttl","","CODER_WORKSPACE_TTL",8*time.Hour,"Specify a time-to-live (TTL) for the workspace (e.g. 8h).")
returncmd
}
67 changes: 66 additions & 1 deletioncli/create_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
Expand All@@ -25,7 +26,17 @@ func TestCreate(t *testing.T) {
version:=coderdtest.CreateTemplateVersion(t,client,user.OrganizationID,nil)
coderdtest.AwaitTemplateVersionJob(t,client,version.ID)
template:=coderdtest.CreateTemplate(t,client,user.OrganizationID,version.ID)
cmd,root:=clitest.New(t,"create","my-workspace","--template",template.Name)
args:= []string{
"create",
"my-workspace",
"--template",template.Name,
"--tz","US/Central",
"--autostart-minute","0",
"--autostart-hour","*/2",
"--autostart-day-of-week","MON-FRI",
"--ttl","8h",
}
cmd,root:=clitest.New(t,args...)
clitest.SetupConfig(t,client,root)
doneChan:=make(chanstruct{})
pty:=ptytest.New(t)
Expand All@@ -48,6 +59,60 @@ func TestCreate(t *testing.T) {
<-doneChan
})

t.Run("CreateErrInvalidTz",func(t*testing.T) {
t.Parallel()
client:=coderdtest.New(t,&coderdtest.Options{IncludeProvisionerD:true})
user:=coderdtest.CreateFirstUser(t,client)
version:=coderdtest.CreateTemplateVersion(t,client,user.OrganizationID,nil)
coderdtest.AwaitTemplateVersionJob(t,client,version.ID)
template:=coderdtest.CreateTemplate(t,client,user.OrganizationID,version.ID)
args:= []string{
"create",
"my-workspace",
"--template",template.Name,
"--tz","invalid",
}
cmd,root:=clitest.New(t,args...)
clitest.SetupConfig(t,client,root)
doneChan:=make(chanstruct{})
pty:=ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
gofunc() {
deferclose(doneChan)
err:=cmd.Execute()
assert.EqualError(t,err,"Invalid workspace autostart timezone: unknown time zone invalid")
}()
<-doneChan
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

As we were hunting this style today, should we eliminate them here as well? 😄

johnstcn reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

For these ones, I'm going to just useassert instead ofrequire.

mafredri reacted with thumbs up emoji
})

t.Run("CreateErrInvalidTTL",func(t*testing.T) {
t.Parallel()
client:=coderdtest.New(t,&coderdtest.Options{IncludeProvisionerD:true})
user:=coderdtest.CreateFirstUser(t,client)
version:=coderdtest.CreateTemplateVersion(t,client,user.OrganizationID,nil)
coderdtest.AwaitTemplateVersionJob(t,client,version.ID)
template:=coderdtest.CreateTemplate(t,client,user.OrganizationID,version.ID)
args:= []string{
"create",
"my-workspace",
"--template",template.Name,
"--ttl","0s",
}
cmd,root:=clitest.New(t,args...)
clitest.SetupConfig(t,client,root)
doneChan:=make(chanstruct{})
pty:=ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
gofunc() {
deferclose(doneChan)
err:=cmd.Execute()
assert.EqualError(t,err,"TTL must be at least 1 minute")
}()
<-doneChan
})

t.Run("CreateFromListWithSkip",func(t*testing.T) {
t.Parallel()
client:=coderdtest.New(t,&coderdtest.Options{IncludeProvisionerD:true})
Expand Down
3 changes: 2 additions & 1 deletioncli/parameter.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,9 +6,10 @@ import (
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"

"github.com/spf13/cobra"

"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
"github.com/spf13/cobra"
)

// Reads a YAML file and populates a string -> string map.
Expand Down
1 change: 1 addition & 0 deletionscmd/coder/main.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ import (
"os/exec"
"path/filepath"
"strings"
_ "time/tzdata"

"github.com/coder/coder/cli"
"github.com/coder/coder/cli/cliui"
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp