8000 feat: add default autostart and ttl for new workspaces by johnstcn · Pull Request #1632 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
cli: update: add support for TTL and autostart_schedule
  • Loading branch information
johnstcn committed May 20, 2022
commit 2f7b939912771f0972b5cf79c90c2b2d15871f87
2 changes: 2 additions & 0 deletions cli/autostart.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/coder/coder/codersdk"
)

const defaultAutostartSchedule = "0 9 * * MON-FRI"

const autostartDescriptionLong = `To have your workspace build automatically at a regular time you can enable autostart.
When enabling autostart, provide the minute, hour, and day(s) of week.
The default schedule is at 09:00 in your local timezone (TZ env, UTC by default).
Expand Down
39 changes: 33 additions & 6 deletions cli/create.go
Original file line number Diff line number Diff 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"
)

func create() *cobra.Command {
var (
workspaceName string
templateName string
parameterFile string
autostartMinute string
autostartHour string
autostartDow string
parameterFile string
templateName string
ttl time.Duration
tzName string
workspaceName string
)
cmd := &cobra.Command{
Annotations: workspaceCommand,
Expand Down Expand Up @@ -54,6 +60,20 @@ func create() *cobra.Command {
}
}

tz, err := time.LoadLocation(tzName)
if err != nil {
return xerrors.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

}
schedSpec := fmt.Sprintf("CRON_TZ=%s %s %s * * %s", tz.String(), autostartMinute, autostartHour, autostartDow)
_, err = schedule.Weekly(schedSpec)
if err != nil {
return xerrors.Errorf("Invalid workspace autostart schedule: %w", err)
}

if ttl == 0 {
return xerrors.Errorf("TTL must be at least 1 minute")
}

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

before := time.Now()
workspace, err := client.CreateWorkspace(cmd.Context(), organization.ID, codersdk.CreateWorkspaceRequest{
Templa 8000 teID: template.ID,
Name: workspaceName,
ParameterValues: parameters,
TemplateID: template.ID,
Name: workspaceName,
AutostartSchedule: &schedSpec,
TTL: &ttl,
ParameterValues: parameters,
})
if err != nil {
return err
Expand Down Expand 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", "0", "Specify the minute(s) at which the workspace should autostart.")
cliflag.StringVarP(cmd.Flags(), &autostartHour, "autostart-hour", "", "CODER_WORKSPACE_AUTOSTART", "9", "Specify the hour(s) at which the workspace should autostart.")
cliflag.StringVarP(cmd.Flags(), &autostartDow, "autostart-day-of-week", "", "CODER_WORKSPACE_AUTOSTART", "MON-FRI", "Specify the days(s) on which the workspace should autostart.")
Copy link
Member

Choose a reason for hiding this comment

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

A user might wonder how this should look if they want Monday, Wednesday and Friday (e.g. MON,WED,FRI, multiple flags)? Should we add some examples to illustrate acceptable formats to the user?

cliflag.StringVarP(cmd.Flags(), &tzName, "tz", "", "TZ", "", "Specify your timezone location for workspace autostart.")
cliflag.DurationVarP(cmd.Flags(), &ttl, "ttl", "", "CODER_WORKSPACE_TTL", 8*time.Hour, "Specify a TTL for the workspace.")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
cliflag.DurationVarP(cmd.Flags(), &ttl, "ttl", "", "CODER_WORKSPACE_TTL", 8*time.Hour, "Specify a TTL for the workspace.")
cliflag.DurationVarP(cmd.Flags(), &ttl, "ttl", "", "CODER_WORKSPACE_TTL", 8*time.Hour, "Specify a time-to-live (TTL) for the workspace.")

return cmd
}
3 changes: 2 additions & 1 deletion cli/parameter.go
Original file line number Diff line number Diff 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
0