Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
0cc46c8
fix: make template push a superset of template create
f0ssel Dec 20, 2023
93da7d0
add back work dir
f0ssel Dec 20, 2023
3c409f6
add groupacl
f0ssel Dec 20, 2023
4b763d7
Add back workdir
f0ssel Dec 20, 2023
d178cc7
combine edit flags
f0ssel Dec 20, 2023
5474942
fix edit
f0ssel Dec 20, 2023
2b9bcf6
unify edit and push
f0ssel Dec 20, 2023
44f264b
make gen
f0ssel Dec 20, 2023
d58cf18
fix test
f0ssel Dec 21, 2023
9b32922
make gen
f0ssel Dec 21, 2023
c6ef6f4
update golden
f0ssel Dec 21, 2023
e1e1653
fix tests:
f0ssel Dec 21, 2023
0cdefbc
fix merge
f0ssel Dec 21, 2023
d2a7866
add test
f0ssel Dec 22, 2023
63d57fd
remove test
f0ssel Dec 22, 2023
ca65869
add unset test
f0ssel Dec 22, 2023
9866235
Add deprecation warning
f0ssel Dec 22, 2023
35c7adf
fix
f0ssel Dec 22, 2023
4abf179
rename functions
f0ssel Jan 2, 2024
a3fdb76
test removing flags
f0ssel Jan 3, 2024
7599090
revert
f0ssel Jan 3, 2024
e958e1a
cleanup
f0ssel Jan 3, 2024
c8cae6c
add back flag
f0ssel Jan 3, 2024
1b03e89
remove create command
f0ssel Jan 4, 2024
8918cda
make gen
f0ssel Jan 4, 2024
7d2a7ac
fix gen
f0ssel Jan 4, 2024
5b2792d
add private flag to template edit
f0ssel Jan 4, 2024
b85a73c
fix test
f0ssel Jan 4, 2024
7655bc9
fix golden
f0ssel Jan 4, 2024
2f2911a
update mentions of templates create command
f0ssel Jan 4, 2024
5334e8e
update golden
f0ssel Jan 4, 2024
79c0c02
add removal comment
f0ssel Jan 4, 2024
2dfb98e
Add back create
f0ssel Jan 4, 2024
cb0aec4
fix formatting
f0ssel Jan 4, 2024
d1f13d1
add disableeveryonegroupaccess test
f0ssel Jan 4, 2024
5792279
update test
f0ssel Jan 5, 2024
2e54259
fix test lint
f0ssel Jan 5, 2024
de78f4b
pr comments
f0ssel Jan 5, 2024
0c859bc
fix text formatting
f0ssel Jan 5, 2024
b65ab81
Add deprecation to help text
f0ssel Jan 5, 2024
77c9edf
fix template push wording
f0ssel Jan 5, 2024
13794fd
golden
f0ssel Jan 5, 2024
8355850
fix gen again
f0ssel Jan 5, 2024
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
Add back create
  • Loading branch information
f0ssel committed Jan 5, 2024
commit 2dfb98e280d7ed4ee2af31e330aeba9cdc17c643
251 changes: 241 additions & 10 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,259 @@ package cli

import (
"fmt"
"net/http"
"time"
"unicode/utf8"

"golang.org/x/xerrors"

"github.com/coder/pretty"

"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/pretty"
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/coder/v2/codersdk"
)

// TODO(f0ssel): This should be removed a few versions after coder 2.7.0 has been released.
func (*RootCmd) templateCreate() *clibase.Cmd {
func (r *RootCmd) templateCreate() *clibase.Cmd {
var (
provisioner string
provisionerTags []string
variablesFile string
commandLineVariables []string
disableEveryone bool
requireActiveVersion bool

defaultTTL time.Duration
failureTTL time.Duration
dormancyThreshold time.Duration
dormancyAutoDeletion time.Duration
maxTTL time.Duration

uploadFlags templateUploadFlags
)
client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: "create [name]",
Short: "Create a template from the current directory or as specified by flag",
Hidden: true,
Use: "create [name]",
Short: "Create a template from the current directory or as specified by flag",
Middleware: clibase.Chain(
clibase.RequireRangeArgs(0, 1),
r.InitClient(client),
),
Handler: func(inv *clibase.Invocation) error {
_, _ = fmt.Fprintln(inv.Stdout, "\n"+pretty.Sprint(cliui.DefaultStyles.Wrap,
pretty.Sprint(
cliui.DefaultStyles.Error,
"ERROR: The `coder templates create` command has been removed. "+
"Use the `coder templates push` command to create and update templates. "+
"Use the `coder templates edit` command to change template settings.")))
cliui.DefaultStyles.Warn,
"DEPRECATION WARNING: Use `coder templates push` command for creating and updating templates. "+
"Use `coder templates edit` command for editing template settings."+
"This command will be removed in a future release."+
"Waiting 1 second...")+"\n"))
time.Sleep(1 * time.Second)

isTemplateSchedulingOptionsSet := failureTTL != 0 || dormancyThreshold != 0 || dormancyAutoDeletion != 0 || maxTTL != 0

if isTemplateSchedulingOptionsSet || requireActiveVersion {
if failureTTL != 0 || dormancyThreshold != 0 || dormancyAutoDeletion != 0 {
// This call can be removed when workspace_actions is no longer experimental
experiments, exErr := client.Experiments(inv.Context())
if exErr != nil {
return xerrors.Errorf("get experiments: %w", exErr)
}

if !experiments.Enabled(codersdk.ExperimentWorkspaceActions) {
return xerrors.Errorf("--failure-ttl, --dormancy-threshold, and --dormancy-auto-deletion are experimental features. Use the workspace_actions CODER_EXPERIMENTS flag to set these configuration values.")
}
}

entitlements, err := client.Entitlements(inv.Context())
if cerr, ok := codersdk.AsError(err); ok && cerr.StatusCode() == http.StatusNotFound {
return xerrors.Errorf("your deployment appears to be an AGPL deployment, so you cannot set enterprise-only flags")
} else if err != nil {
return xerrors.Errorf("get entitlements: %w", err)
}

if isTemplateSchedulingOptionsSet {
if !entitlements.Features[codersdk.FeatureAdvancedTemplateScheduling].Enabled {
return xerrors.Errorf("your license is not entitled to use advanced template scheduling, so you cannot set --failure-ttl, --inactivity-ttl, or --max-ttl")
}
}

if requireActiveVersion {
if !entitlements.Features[codersdk.FeatureAccessControl].Enabled {
return xerrors.Errorf("your license is not entitled to use enterprise access control, so you cannot set --require-active-version")
}
}
}

organization, err := CurrentOrganization(inv, client)
if err != nil {
return err
}

templateName, err := uploadFlags.templateName(inv.Args)
if err != nil {
return err
}

if utf8.RuneCountInString(templateName) > 31 {
return xerrors.Errorf("Template name must be less than 32 characters")
}

_, err = client.TemplateByName(inv.Context(), organization.ID, templateName)
if err == nil {
return xerrors.Errorf("A template already exists named %q!", templateName)
}

err = uploadFlags.checkForLockfile(inv)
if err != nil {
return xerrors.Errorf("check for lockfile: %w", err)
}

message := uploadFlags.templateMessage(inv)

// Confirm upload of the directory.
resp, err := uploadFlags.upload(inv, client)
if err != nil {
return err
}

tags, err := ParseProvisionerTags(provisionerTags)
if err != nil {
return err
}

userVariableValues, err := ParseUserVariableValues(
variablesFile,
commandLineVariables)
if err != nil {
return err
}

job, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{
Message: message,
Client: client,
Organization: organization,
Provisioner: codersdk.ProvisionerType(provisioner),
FileID: resp.ID,
ProvisionerTags: tags,
UserVariableValues: userVariableValues,
})
if err != nil {
return err
}

if !uploadFlags.stdin() {
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: "Confirm create?",
IsConfirm: true,
})
if err != nil {
return err
}
}

createReq := codersdk.CreateTemplateRequest{
Name: templateName,
VersionID: job.ID,
DefaultTTLMillis: ptr.Ref(defaultTTL.Milliseconds()),
FailureTTLMillis: ptr.Ref(failureTTL.Milliseconds()),
MaxTTLMillis: ptr.Ref(maxTTL.Milliseconds()),
TimeTilDormantMillis: ptr.Ref(dormancyThreshold.Milliseconds()),
TimeTilDormantAutoDeleteMillis: ptr.Ref(dormancyAutoDeletion.Milliseconds()),
DisableEveryoneGroupAccess: disableEveryone,
RequireActiveVersion: requireActiveVersion,
}

_, err = client.CreateTemplate(inv.Context(), organization.ID, createReq)
if err != nil {
return err
}

_, _ = fmt.Fprintln(inv.Stdout, "\n"+pretty.Sprint(cliui.DefaultStyles.Wrap,
"The "+pretty.Sprint(
cliui.DefaultStyles.Keyword, templateName)+" template has been created at "+
pretty.Sprint(cliui.DefaultStyles.DateTimeStamp, time.Now().Format(time.Stamp))+"! "+
"Developers can provision a workspace with this template using:")+"\n")

_, _ = fmt.Fprintln(inv.Stdout, " "+pretty.Sprint(cliui.DefaultStyles.Code, fmt.Sprintf("coder create --template=%q [workspace name]", templateName)))
_, _ = fmt.Fprintln(inv.Stdout)

return nil
},
}
cmd.Options = clibase.OptionSet{
{
Flag: "private",
Description: "Disable the default behavior of granting template access to the 'everyone' group. " +
"The template permissions must be updated to allow non-admin users to use this template.",
Value: clibase.BoolOf(&disableEveryone),
},
{
Flag: "variables-file",
Description: "Specify a file path with values for Terraform-managed variables.",
Value: clibase.StringOf(&variablesFile),
},
{
Flag: "variable",
Description: "Specify a set of values for Terraform-managed variables.",
Value: clibase.StringArrayOf(&commandLineVariables),
},
{
Flag: "var",
Description: "Alias of --variable.",
Value: clibase.StringArrayOf(&commandLineVariables),
},
{
Flag: "provisioner-tag",
Description: "Specify a set of tags to target provisioner daemons.",
Value: clibase.StringArrayOf(&provisionerTags),
},
{
Flag: "default-ttl",
Description: "Specify a default TTL for workspaces created from this template. It is the default time before shutdown - workspaces created from this template default to this value. Maps to \"Default autostop\" in the UI.",
Default: "24h",
Value: clibase.DurationOf(&defaultTTL),
},
{
Flag: "failure-ttl",
Description: "Specify a failure TTL for workspaces created from this template. It is the amount of time after a failed \"start\" build before coder automatically schedules a \"stop\" build to cleanup.This licensed feature's default is 0h (off). Maps to \"Failure cleanup\"in the UI.",
Default: "0h",
Value: clibase.DurationOf(&failureTTL),
},
{
Flag: "dormancy-threshold",
Description: "Specify a duration workspaces may be inactive prior to being moved to the dormant state. This licensed feature's default is 0h (off). Maps to \"Dormancy threshold\" in the UI.",
Default: "0h",
Value: clibase.DurationOf(&dormancyThreshold),
},
{
Flag: "dormancy-auto-deletion",
Description: "Specify a duration workspaces may be in the dormant state prior to being deleted. This licensed feature's default is 0h (off). Maps to \"Dormancy Auto-Deletion\" in the UI.",
Default: "0h",
Value: clibase.DurationOf(&dormancyAutoDeletion),
},

{
Flag: "max-ttl",
Description: "Edit the template maximum time before shutdown - workspaces created from this template must shutdown within the given duration after starting. This is an enterprise-only feature.",
Value: clibase.DurationOf(&maxTTL),
},
{
Flag: "test.provisioner",
Description: "Customize the provisioner backend.",
Default: "terraform",
Value: clibase.StringOf(&provisioner),
Hidden: true,
},
{
Flag: "require-active-version",
Description: "Requires workspace builds to use the active template version. This setting does not apply to template admins. This is an enterprise-only feature.",
Value: clibase.BoolOf(&requireActiveVersion),
Default: "false",
},

cliui.SkipPromptOption(),
}
cmd.Options = append(cmd.Options, uploadFlags.options()...)
return cmd
}
Loading