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

Skip to content

feat: add autostart/autostop show, show autostart/autostop schedule in ls output #1436

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 2 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 40 additions & 0 deletions cli/autostart.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,52 @@ func autostart() *cobra.Command {
Example: "coder autostart enable my-workspace --minute 30 --hour 9 --days 1-5 --tz Europe/Dublin",
}

autostartCmd.AddCommand(autostartShow())
autostartCmd.AddCommand(autostartEnable())
autostartCmd.AddCommand(autostartDisable())

return autostartCmd
}

func autostartShow() *cobra.Command {
cmd := &cobra.Command{
Use: "show <workspace_name>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return err
}

if workspace.AutostartSchedule == "" {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "not enabled\n")
return nil
}

validSchedule, err := schedule.Weekly(workspace.AutostartSchedule)
if err != nil {
// This should never happen.
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "invalid autostart schedule %q for workspace %s: %s\n", workspace.AutostartSchedule, workspace.Name, err.Error())
return nil
}

_, _ = fmt.Fprintf(cmd.OutOrStdout(), "schedule: %s\nnext: %s\n", workspace.AutostartSchedule, validSchedule.Next(time.Now()))

return nil
},
}
return cmd
}

func autostartEnable() *cobra.Command {
// yes some of these are technically numbers but the cron library will do that work
var autostartMinute string
Expand Down
32 changes: 32 additions & 0 deletions cli/autostart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,43 @@ import (

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
)

func TestAutostart(t *testing.T) {
t.Parallel()

t.Run("ShowOK", func(t *testing.T) {
t.Parallel()

var (
ctx = context.Background()
client = coderdtest.New(t, nil)
_ = coderdtest.NewProvisionerDaemon(t, client)
user = coderdtest.CreateFirstUser(t, client)
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID)
cmdArgs = []string{"autostart", "show", workspace.Name}
sched = "CRON_TZ=Europe/Dublin 30 17 * * 1-5"
stdoutBuf = &bytes.Buffer{}
)

err := client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
Schedule: sched,
})
require.NoError(t, err)

cmd, root := clitest.New(t, cmdArgs...)
clitest.SetupConfig(t, client, root)
cmd.SetOut(stdoutBuf)

err = cmd.Execute()
require.NoError(t, err, "unexpected error")
require.Contains(t, stdoutBuf.String(), "schedule: "+sched)
})

t.Run("EnableDisableOK", func(t *testing.T) {
t.Parallel()

Expand Down
49 changes: 45 additions & 4 deletions cli/autostop.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,59 @@ The default autostop schedule is at 18:00 in your local timezone (TZ env, UTC by

func autostop() *cobra.Command {
autostopCmd := &cobra.Command{
Use: "autostop enable <workspace>",
Short: "schedule a workspace to automatically stop at a regular time",
Long: autostopDescriptionLong,
Example: "coder autostop enable my-workspace --minute 0 --hour 18 --days 1-5 -tz Europe/Dublin",
Annotations: workspaceCommand,
Use: "autostop enable <workspace>",
Short: "schedule a workspace to automatically stop at a regular time",
Long: autostopDescriptionLong,
Example: "coder autostop enable my-workspace --minute 0 --hour 18 --days 1-5 -tz Europe/Dublin",
}

autostopCmd.AddCommand(autostopShow())
autostopCmd.AddCommand(autostopEnable())
autostopCmd.AddCommand(autostopDisable())

return autostopCmd
}

func autostopShow() *cobra.Command {
cmd := &cobra.Command{
Use: "show <workspace_name>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return err
}

if workspace.AutostopSchedule == "" {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "not enabled\n")
return nil
}

validSchedule, err := schedule.Weekly(workspace.AutostopSchedule)
if err != nil {
// This should never happen.
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "invalid autostop schedule %q for workspace %s: %s\n", workspace.AutostopSchedule, workspace.Name, err.Error())
return nil
}

_, _ = fmt.Fprintf(cmd.OutOrStdout(), "schedule: %s\nnext: %s\n", workspace.AutostopSchedule, validSchedule.Next(time.Now()))

return nil
},
}
return cmd
}

func autostopEnable() *cobra.Command {
// yes some of these are technically numbers but the cron library will do that work
var autostopMinute string
Expand Down
32 changes: 32 additions & 0 deletions cli/autostop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,43 @@ import (

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
)

func TestAutostop(t *testing.T) {
t.Parallel()

t.Run("ShowOK", func(t *testing.T) {
t.Parallel()

var (
ctx = context.Background()
client = coderdtest.New(t, nil)
_ = coderdtest.NewProvisionerDaemon(t, client)
user = coderdtest.CreateFirstUser(t, client)
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID)
cmdArgs = []string{"autostop", "show", workspace.Name}
sched = "CRON_TZ=Europe/Dublin 30 17 * * 1-5"
stdoutBuf = &bytes.Buffer{}
)

err := client.UpdateWorkspaceAutostop(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostopRequest{
Schedule: sched,
})
require.NoError(t, err)

cmd, root := clitest.New(t, cmdArgs...)
clitest.SetupConfig(t, client, root)
cmd.SetOut(stdoutBuf)

err = cmd.Execute()
require.NoError(t, err, "unexpected error")
require.Contains(t, stdoutBuf.String(), "schedule: "+sched)
})

t.Run("EnableDisableOK", func(t *testing.T) {
t.Parallel()

Expand Down
14 changes: 13 additions & 1 deletion cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func list() *cobra.Command {
}

tableWriter := cliui.Table()
header := table.Row{"workspace", "template", "status", "last built", "outdated"}
header := table.Row{"workspace", "template", "status", "last built", "outdated", "autostart", "autostop"}
tableWriter.AppendHeader(header)
tableWriter.SortBy([]table.SortBy{{
Name: "workspace",
Expand Down Expand Up @@ -108,13 +108,25 @@ func list() *cobra.Command {
durationDisplay = durationDisplay[:len(durationDisplay)-2]
}

autostartDisplay := "not enabled"
if workspace.AutostartSchedule != "" {
autostartDisplay = workspace.AutostartSchedule
}

autostopDisplay := "not enabled"
if workspace.AutostopSchedule != "" {
autostopDisplay = workspace.AutostopSchedule
}

user := usersByID[workspace.OwnerID]
tableWriter.AppendRow(table.Row{
user.Username + "/" + workspace.Name,
workspace.TemplateName,
status,
durationDisplay,
workspace.Outdated,
autostartDisplay,
autostopDisplay,
})
}
_, err = fmt.Fprintln(cmd.OutOrStdout(), tableWriter.Render())
Expand Down