2022-02-10 08:33:27 -06:00
package cli
import (
"fmt"
2023-05-10 12:57:11 -07:00
"net/http"
2022-02-10 08:33:27 -06:00
"time"
2022-09-07 13:01:18 -07:00
"unicode/utf8"
2022-02-10 08:33:27 -06:00
"golang.org/x/xerrors"
2023-09-07 17:28:22 -04:00
"github.com/coder/pretty"
2024-03-15 11:24:38 -05:00
"github.com/coder/serpent"
2023-09-07 17:28:22 -04:00
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/coder/v2/codersdk"
2022-02-10 08:33:27 -06:00
)
2024-03-17 09:45:26 -05:00
func ( r * RootCmd ) templateCreate () * serpent . Command {
2022-02-12 13:34:04 -06:00
var (
2023-10-19 17:16:15 -05:00
provisioner string
provisionerTags [] string
variablesFile string
2023-12-15 14:55:24 +01:00
commandLineVariables [] string
2023-10-19 17:16:15 -05:00
disableEveryone bool
requireActiveVersion bool
2023-08-25 11:00:38 -05:00
2023-11-21 18:20:01 -06:00
defaultTTL time . Duration
failureTTL time . Duration
dormancyThreshold time . Duration
dormancyAutoDeletion time . Duration
2023-01-16 14:32:11 -06:00
uploadFlags templateUploadFlags
2024-07-01 11:04:45 -10:00
orgContext = NewOrganizationContext ()
2022-02-12 13:34:04 -06:00
)
2023-03-23 17:42:20 -05:00
client := new ( codersdk . Client )
2024-03-17 09:45:26 -05:00
cmd := & serpent . Command {
2022-04-14 13:23:20 -04:00
Use : "create [name]" ,
2024-01-05 16:04:14 -05:00
Short : "DEPRECATED: Create a template from the current directory or as specified by flag" ,
2024-03-15 11:24:38 -05:00
Middleware : serpent . Chain (
serpent . RequireRangeArgs ( 0 , 1 ),
2024-01-05 16:04:14 -05:00
cliui . DeprecationWarning (
"Use `coder templates push` command for creating and updating templates. \n" +
"Use `coder templates edit` command for editing template settings. " ,
),
2023-03-23 17:42:20 -05:00
r . InitClient ( client ),
),
2024-03-15 11:24:38 -05:00
Handler : func ( inv * serpent . Invocation ) error {
2024-03-20 10:37:57 -05:00
isTemplateSchedulingOptionsSet := failureTTL != 0 || dormancyThreshold != 0 || dormancyAutoDeletion != 0
2023-10-19 17:16:15 -05:00
if isTemplateSchedulingOptionsSet || requireActiveVersion {
2023-05-10 12:57:11 -07:00
entitlements , err := client . Entitlements ( inv . Context ())
2023-10-19 17:16:15 -05:00
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" )
2023-05-10 12:57:11 -07:00
} else if err != nil {
return xerrors . Errorf ( "get entitlements: %w" , err )
}
2023-10-19 17:16:15 -05:00
if isTemplateSchedulingOptionsSet {
if ! entitlements . Features [ codersdk . FeatureAdvancedTemplateScheduling ]. Enabled {
2024-03-20 10:37:57 -05:00
return xerrors . Errorf ( "your license is not entitled to use advanced template scheduling, so you cannot set --failure-ttl, or --inactivity-ttl" )
2023-10-19 17:16:15 -05:00
}
}
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" )
}
2023-05-10 12:57:11 -07:00
}
}
2024-07-01 11:04:45 -10:00
organization , err := orgContext . Selected ( inv , client )
2022-02-10 08:33:27 -06:00
if err != nil {
return err
}
2022-04-14 13:23:20 -04:00
2024-09-12 14:36:20 +10:00
templateName , err := uploadFlags . templateName ( inv )
2023-01-16 14:32:11 -06:00
if err != nil {
return err
2022-03-22 13:17:50 -06:00
}
2022-04-14 13:23:20 -04:00
2024-01-30 12:47:10 +02:00
if utf8 . RuneCountInString ( templateName ) > 32 {
return xerrors . Errorf ( "Template name must be no more than 32 characters" )
2022-09-07 13:01:18 -07:00
}
2023-03-23 17:42:20 -05:00
_ , err = client . TemplateByName ( inv . Context (), organization . ID , templateName )
2022-03-22 13:17:50 -06:00
if err == nil {
2022-04-06 12:42:40 -05:00
return xerrors . Errorf ( "A template already exists named %q!" , templateName )
2022-03-22 13:17:50 -06:00
}
2023-06-20 10:02:44 -05:00
err = uploadFlags . checkForLockfile ( inv )
if err != nil {
return xerrors . Errorf ( "check for lockfile: %w" , err )
}
2023-07-11 13:11:08 +03:00
message := uploadFlags . templateMessage ( inv )
2024-01-12 15:08:23 +01:00
var varsFiles [] string
2024-09-12 14:36:20 +10:00
if ! uploadFlags . stdin ( inv ) {
2024-07-24 14:11:29 +10:00
varsFiles , err = codersdk . DiscoverVarsFiles ( uploadFlags . directory )
2024-01-12 15:08:23 +01:00
if err != nil {
return err
}
if len ( varsFiles ) > 0 {
_ , _ = fmt . Fprintln ( inv . Stdout , "Auto-discovered Terraform tfvars files. Make sure to review and clean up any unused files." )
}
}
2022-05-12 14:54:58 +03:00
// Confirm upload of the directory.
2023-03-23 17:42:20 -05:00
resp , err := uploadFlags . upload ( inv , client )
2022-04-11 18:54:30 -05:00
if err != nil {
return err
}
2022-11-16 16:34:06 -06:00
tags , err := ParseProvisionerTags ( provisionerTags )
if err != nil {
return err
}
2024-07-24 14:11:29 +10:00
userVariableValues , err := codersdk . ParseUserVariableValues (
2024-01-12 15:08:23 +01:00
varsFiles ,
2023-12-15 14:55:24 +01:00
variablesFile ,
commandLineVariables )
if err != nil {
return err
}
2023-06-02 11:16:46 +02:00
job , err := createValidTemplateVersion ( inv , createValidTemplateVersionArgs {
2023-12-15 14:55:24 +01:00
Message : message ,
Client : client ,
Organization : organization ,
Provisioner : codersdk . ProvisionerType ( provisioner ),
FileID : resp . ID ,
ProvisionerTags : tags ,
UserVariableValues : userVariableValues ,
2022-06-17 12:22:28 -05:00
})
2022-03-22 13:17:50 -06:00
if err != nil {
return err
}
2024-09-12 14:36:20 +10:00
if ! uploadFlags . stdin ( inv ) {
2023-03-23 17:42:20 -05:00
_ , err = cliui . Prompt ( inv , cliui . PromptOptions {
2023-01-16 14:32:11 -06:00
Text : "Confirm create?" ,
IsConfirm : true ,
})
if err != nil {
return err
}
2022-02-10 08:33:27 -06:00
}
2022-02-12 13:34:04 -06:00
2022-06-07 13:37:45 +01:00
createReq := codersdk . CreateTemplateRequest {
2023-11-21 18:20:01 -06:00
Name : templateName ,
VersionID : job . ID ,
DefaultTTLMillis : ptr . Ref ( defaultTTL . Milliseconds ()),
FailureTTLMillis : ptr . Ref ( failureTTL . Milliseconds ()),
TimeTilDormantMillis : ptr . Ref ( dormancyThreshold . Milliseconds ()),
TimeTilDormantAutoDeleteMillis : ptr . Ref ( dormancyAutoDeletion . Milliseconds ()),
DisableEveryoneGroupAccess : disableEveryone ,
RequireActiveVersion : requireActiveVersion ,
2022-06-07 13:37:45 +01:00
}
2024-07-12 10:47:28 -10:00
template , err := client . CreateTemplate ( inv . Context (), organization . ID , createReq )
2022-02-12 13:34:04 -06:00
if err != nil {
return err
2022-02-10 08:33:27 -06:00
}
2023-09-07 17:28:22 -04:00
_ , _ = 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 )) + "! " +
2022-04-11 18:54:30 -05:00
"Developers can provision a workspace with this template using:" ) + "\n" )
2024-07-12 10:47:28 -10:00
_ , _ = fmt . Fprintln ( inv . Stdout , " " + pretty . Sprint ( cliui . DefaultStyles . Code , fmt . Sprintf ( "coder create --template=%q --org=%q [workspace name]" , templateName , template . OrganizationName )))
2023-03-23 17:42:20 -05:00
_ , _ = fmt . Fprintln ( inv . Stdout )
2022-04-11 18:54:30 -05:00
2022-02-10 08:33:27 -06:00
return nil
},
}
2024-03-15 11:24:38 -05:00
cmd . Options = serpent . OptionSet {
2023-06-14 11:08:58 -05:00
{
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." ,
2024-03-15 11:24:38 -05:00
Value : serpent . BoolOf ( & disableEveryone ),
2023-06-14 11:08:58 -05:00
},
2023-03-23 17:42:20 -05:00
{
Flag : "variables-file" ,
Description : "Specify a file path with values for Terraform-managed variables." ,
2024-03-15 11:24:38 -05:00
Value : serpent . StringOf ( & variablesFile ),
2023-03-23 17:42:20 -05:00
},
{
Flag : "variable" ,
Description : "Specify a set of values for Terraform-managed variables." ,
2024-03-15 11:24:38 -05:00
Value : serpent . StringArrayOf ( & commandLineVariables ),
2023-03-23 17:42:20 -05:00
},
2023-07-25 09:36:02 -05:00
{
Flag : "var" ,
Description : "Alias of --variable." ,
2024-03-15 11:24:38 -05:00
Value : serpent . StringArrayOf ( & commandLineVariables ),
2023-07-25 09:36:02 -05:00
},
2023-03-23 17:42:20 -05:00
{
Flag : "provisioner-tag" ,
Description : "Specify a set of tags to target provisioner daemons." ,
2024-03-15 11:24:38 -05:00
Value : serpent . StringArrayOf ( & provisionerTags ),
2023-03-23 17:42:20 -05:00
},
{
Flag : "default-ttl" ,
2023-08-25 13:39:12 -04:00
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." ,
2023-03-23 17:42:20 -05:00
Default : "24h" ,
2024-03-15 11:24:38 -05:00
Value : serpent . DurationOf ( & defaultTTL ),
2023-03-23 17:42:20 -05:00
},
2023-05-10 12:57:11 -07:00
{
Flag : "failure-ttl" ,
2023-08-25 13:39:12 -04:00
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." ,
2023-05-10 12:57:11 -07:00
Default : "0h" ,
2024-03-15 11:24:38 -05:00
Value : serpent . DurationOf ( & failureTTL ),
2023-05-10 12:57:11 -07:00
},
{
2023-11-21 18:20:01 -06:00
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." ,
2023-05-10 12:57:11 -07:00
Default : "0h" ,
2024-03-15 11:24:38 -05:00
Value : serpent . DurationOf ( & dormancyThreshold ),
2023-05-10 12:57:11 -07:00
},
2023-11-21 18:20:01 -06:00
{
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" ,
2024-03-15 11:24:38 -05:00
Value : serpent . DurationOf ( & dormancyAutoDeletion ),
2023-11-21 18:20:01 -06:00
},
2023-03-23 17:42:20 -05:00
{
Flag : "test.provisioner" ,
Description : "Customize the provisioner backend." ,
Default : "terraform" ,
2024-03-15 11:24:38 -05:00
Value : serpent . StringOf ( & provisioner ),
2023-03-23 17:42:20 -05:00
Hidden : true ,
},
2023-10-19 17:16:15 -05:00
{
Flag : "require-active-version" ,
2024-11-01 15:43:51 -04:00
Description : "Requires workspace builds to use the active template version. This setting does not apply to template admins. This is an enterprise-only feature. See https://coder.com/docs/admin/templates/managing-templates#require-automatic-updates-enterprise for more details." ,
2024-03-15 11:24:38 -05:00
Value : serpent . BoolOf ( & requireActiveVersion ),
2023-10-19 17:16:15 -05:00
Default : "false" ,
},
2023-03-23 17:42:20 -05:00
cliui . SkipPromptOption (),
2022-02-12 13:34:04 -06:00
}
2024-07-12 10:47:28 -10:00
orgContext . AttachOptions ( cmd )
2023-06-20 10:02:44 -05:00
cmd . Options = append ( cmd . Options , uploadFlags . options () ... )
2022-02-12 13:34:04 -06:00
return cmd
}