-
Notifications
You must be signed in to change notification settings - Fork 887
feat: allow bumping workspace deadline #1828
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
39a53c2
feat: cli: add coder bump command for autostop extension
johnstcn f764e61
fix: cli/ssh: read autostop deadline from latestbuild.Deadline instea…
johnstcn 2a74312
fix: autobuild/executor: reduce logging
johnstcn 6f2bef5
cli/bump: ensure minimum duration
johnstcn dcd57cf
cli/ssh: notify relative time instead
johnstcn c31b9dd
fixup! cli/ssh: notify relative time instead
johnstcn 23247d4
revert accidental change
johnstcn 41306aa
Update cli/bump.go
johnstcn d9a795f
Update cli/ssh.go
johnstcn 3ecb323
fix: coderd: add missing deadline field
johnstcn 4b868d2
fix: cli/list: show extension in list output
johnstcn cfeb274
fix: less rounding of deadlines
johnstcn b5be82c
Apply suggestions from code review
johnstcn 9a45186
Apply suggestions from code review
johnstcn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
const ( | ||
bumpDescriptionLong = `To extend the autostop deadline for a workspace. | ||
If no unit is specified in the duration, we assume minutes.` | ||
defaultBumpDuration = 90 * time.Minute | ||
) | ||
|
||
func bump() *cobra.Command { | ||
bumpCmd := &cobra.Command{ | ||
Args: cobra.RangeArgs(1, 2), | ||
Annotations: workspaceCommand, | ||
Use: "bump <workspace-name> [duration]", | ||
Short: "Extend the autostop deadline for a workspace.", | ||
Long: bumpDescriptionLong, | ||
Example: "coder bump my-workspace 90m", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
bumpDuration := defaultBumpDuration | ||
if len(args) > 1 { | ||
d, err := tryParseDuration(args[1]) | ||
if err != nil { | ||
return err | ||
} | ||
bumpDuration = d | ||
} | ||
|
||
if bumpDuration < time.Minute { | ||
return xerrors.New("minimum bump duration is 1 minute") | ||
} | ||
|
||
client, err := createClient(cmd) | ||
if err != nil { | ||
return xerrors.Errorf("create client: %w", err) | ||
} | ||
organization, err := currentOrganization(cmd, client) | ||
if err != nil { | ||
return xerrors.Errorf("get current org: %w", err) | ||
} | ||
|
||
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0]) | ||
if err != nil { | ||
return xerrors.Errorf("get workspace: %w", err) | ||
} | ||
|
||
if workspace.LatestBuild.Deadline.IsZero() { | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "no deadline set\n") | ||
return nil | ||
} | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
newDeadline := workspace.LatestBuild.Deadline.Add(bumpDuration) | ||
if err := client.PutExtendWorkspace(cmd.Context(), workspace.ID, codersdk.PutExtendWorkspaceRequest{ | ||
Deadline: newDeadline, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Workspace %q will now stop at %s\n", workspace.Name, newDeadline.Format(time.RFC3339)) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return bumpCmd | ||
} | ||
|
||
func tryParseDuration(raw string) (time.Duration, error) { | ||
// If the user input a raw number, assume minutes | ||
if isDigit(raw) { | ||
raw = raw + "m" | ||
} | ||
d, err := time.ParseDuration(raw) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return d, nil | ||
} | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func isDigit(s string) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would also return true for the empty string, |
||
return strings.IndexFunc(s, func(c rune) bool { | ||
return c < '0' || c > '9' | ||
}) == -1 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
package cli_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
func TestBump(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("BumpOKDefault", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Given: we have a workspace | ||
var ( | ||
err error | ||
ctx = context.Background() | ||
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) | ||
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) | ||
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID) | ||
cmdArgs = []string{"bump", workspace.Name} | ||
stdoutBuf = &bytes.Buffer{} | ||
) | ||
|
||
// Given: we wait for the workspace to be built | ||
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID) | ||
workspace, err = client.Workspace(ctx, workspace.ID) | ||
require.NoError(t, err) | ||
expectedDeadline := workspace.LatestBuild.Deadline.Add(90 * time.Minute) | ||
|
||
// Assert test invariant: workspace build has a deadline set equal to now plus ttl | ||
require.WithinDuration(t, workspace.LatestBuild.Deadline, time.Now().Add(*workspace.TTL), time.Minute) | ||
require.NoError(t, err) | ||
|
||
cmd, root := clitest.New(t, cmdArgs...) | ||
clitest.SetupConfig(t, client, root) | ||
cmd.SetOut(stdoutBuf) | ||
|
||
// When: we execute `coder bump <workspace>` | ||
err = cmd.ExecuteContext(ctx) | ||
require.NoError(t, err, "unexpected error") | ||
|
||
// Then: the deadline of the latest build is updated | ||
updated, err := client.Workspace(ctx, workspace.ID) | ||
require.NoError(t, err) | ||
require.WithinDuration(t, expectedDeadline, updated.LatestBuild.Deadline, time.Minute) | ||
}) | ||
|
||
t.Run("BumpSpecificDuration", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Given: we have a workspace | ||
var ( | ||
err error | ||
ctx = context.Background() | ||
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) | ||
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) | ||
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID) | ||
cmdArgs = []string{"bump", workspace.Name, "30"} | ||
stdoutBuf = &bytes.Buffer{} | ||
) | ||
|
||
// Given: we wait for the workspace to be built | ||
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID) | ||
workspace, err = client.Workspace(ctx, workspace.ID) | ||
require.NoError(t, err) | ||
expectedDeadline := workspace.LatestBuild.Deadline.Add(30 * time.Minute) | ||
|
||
// Assert test invariant: workspace build has a deadline set equal to now plus ttl | ||
require.WithinDuration(t, workspace.LatestBuild.Deadline, time.Now().Add(*workspace.TTL), time.Minute) | ||
require.NoError(t, err) | ||
|
||
cmd, root := clitest.New(t, cmdArgs...) | ||
clitest.SetupConfig(t, client, root) | ||
cmd.SetOut(stdoutBuf) | ||
|
||
// When: we execute `coder bump workspace <number without units>` | ||
err = cmd.ExecuteContext(ctx) | ||
require.NoError(t, err) | ||
|
||
// Then: the deadline of the latest build is updated assuming the units are minutes | ||
updated, err := client.Workspace(ctx, workspace.ID) | ||
require.NoError(t, err) | ||
require.WithinDuration(t, expectedDeadline, updated.LatestBuild.Deadline, time.Minute) | ||
}) | ||
|
||
t.Run("BumpInvalidDuration", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Given: we have a workspace | ||
var ( | ||
err error | ||
ctx = context.Background() | ||
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) | ||
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) | ||
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID) | ||
cmdArgs = []string{"bump", workspace.Name, "kwyjibo"} | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stdoutBuf = &bytes.Buffer{} | ||
) | ||
|
||
// Given: we wait for the workspace to be built | ||
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID) | ||
workspace, err = client.Workspace(ctx, workspace.ID) | ||
require.NoError(t, err) | ||
|
||
// Assert test invariant: workspace build has a deadline set equal to now plus ttl | ||
require.WithinDuration(t, workspace.LatestBuild.Deadline, time.Now().Add(*workspace.TTL), time.Minute) | ||
require.NoError(t, err) | ||
|
||
cmd, root := clitest.New(t, cmdArgs...) | ||
clitest.SetupConfig(t, client, root) | ||
cmd.SetOut(stdoutBuf) | ||
|
||
// When: we execute `coder bump workspace <not a number>` | ||
err = cmd.ExecuteContext(ctx) | ||
// Then: the command fails | ||
require.ErrorContains(t, err, "invalid duration") | ||
}) | ||
|
||
t.Run("BumpNoDeadline", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Given: we have a workspace with no deadline set | ||
var ( | ||
err error | ||
ctx = context.Background() | ||
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) | ||
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) | ||
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID, func(cwr *codersdk.CreateWorkspaceRequest) { | ||
cwr.TTL = nil | ||
}) | ||
cmdArgs = []string{"bump", workspace.Name} | ||
stdoutBuf = &bytes.Buffer{} | ||
) | ||
|
||
// Given: we wait for the workspace to build | ||
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID) | ||
workspace, err = client.Workspace(ctx, workspace.ID) | ||
require.NoError(t, err) | ||
|
||
// Assert test invariant: workspace has no TTL set | ||
require.Zero(t, workspace.LatestBuild.Deadline) | ||
require.NoError(t, err) | ||
|
||
cmd, root := clitest.New(t, cmdArgs...) | ||
clitest.SetupConfig(t, client, root) | ||
cmd.SetOut(stdoutBuf) | ||
|
||
// When: we execute `coder bump workspace`` | ||
err = cmd.ExecuteContext(ctx) | ||
require.NoError(t, err) | ||
|
||
// Then: nothing happens and the deadline remains unset | ||
updated, err := client.Workspace(ctx, workspace.ID) | ||
require.NoError(t, err) | ||
require.Zero(t, updated.LatestBuild.Deadline) | ||
}) | ||
|
||
t.Run("BumpMinimumDuration", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Given: we have a workspace with no deadline set | ||
var ( | ||
err error | ||
ctx = context.Background() | ||
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) | ||
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) | ||
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID) | ||
cmdArgs = []string{"bump", workspace.Name, "59s"} | ||
stdoutBuf = &bytes.Buffer{} | ||
) | ||
|
||
// Given: we wait for the workspace to build | ||
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID) | ||
workspace, err = client.Workspace(ctx, workspace.ID) | ||
require.NoError(t, err) | ||
|
||
// Assert test invariant: workspace build has a deadline set equal to now plus ttl | ||
require.WithinDuration(t, workspace.LatestBuild.Deadline, time.Now().Add(*workspace.TTL), time.Minute) | ||
require.NoError(t, err) | ||
|
||
cmd, root := clitest.New(t, cmdArgs...) | ||
clitest.SetupConfig(t, client, root) | ||
cmd.SetOut(stdoutBuf) | ||
|
||
// When: we execute `coder bump workspace 59s` | ||
err = cmd.ExecuteContext(ctx) | ||
require.ErrorContains(t, err, "minimum bump duration is 1 minute") | ||
|
||
// Then: an error is reported and the deadline remains as before | ||
updated, err := client.Workspace(ctx, workspace.ID) | ||
require.NoError(t, err) | ||
require.WithinDuration(t, workspace.LatestBuild.Deadline, updated.LatestBuild.Deadline, time.Minute) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we have a default, should it just be a flag?
--duration=90m
or something?Cobra supports
duration
stringsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the way that looks, without the flag is less typing!