2022-02-10 08:33:27 -06:00
package cli
2022-03-22 13:17:50 -06:00
import (
2023-02-04 14:07:09 -06:00
"bufio"
2022-03-22 13:17:50 -06:00
"fmt"
2023-01-16 14:32:11 -06:00
"io"
2022-07-01 11:49:29 -05:00
"path/filepath"
2023-07-11 13:11:08 +03:00
"strings"
2022-03-22 13:17:50 -06:00
"time"
2022-05-12 14:54:58 +03:00
"github.com/briandowns/spinner"
2022-03-22 13:17:50 -06:00
"golang.org/x/xerrors"
2023-09-07 17:28:22 -04:00
"github.com/coder/pretty"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/provisionersdk"
2022-03-22 13:17:50 -06:00
)
2022-02-10 08:33:27 -06:00
2023-01-16 14:32:11 -06:00
// templateUploadFlags is shared by `templates create` and `templates push`.
type templateUploadFlags struct {
2023-06-20 10:02:44 -05:00
directory string
ignoreLockfile bool
2023-07-11 13:11:08 +03:00
message string
2023-01-16 14:32:11 -06:00
}
2023-06-20 10:02:44 -05:00
func ( pf * templateUploadFlags ) options () [] clibase . Option {
return [] clibase . Option {{
2023-03-23 17:42:20 -05:00
Flag : "directory" ,
FlagShorthand : "d" ,
Description : "Specify the directory to create from, use '-' to read tar from stdin." ,
Default : "." ,
Value : clibase . StringOf ( & pf . directory ),
2023-06-20 10:02:44 -05:00
}, {
Flag : "ignore-lockfile" ,
Description : "Ignore warnings about not having a .terraform.lock.hcl file present in the template." ,
Default : "false" ,
Value : clibase . BoolOf ( & pf . ignoreLockfile ),
2023-07-11 13:11:08 +03:00
}, {
Flag : "message" ,
FlagShorthand : "m" ,
Description : "Specify a message describing the changes in this version of the template. Messages longer than 72 characters will be displayed as truncated." ,
Value : clibase . StringOf ( & pf . message ),
2023-06-20 10:02:44 -05:00
}}
2023-01-16 14:32:11 -06:00
}
2023-04-17 17:58:25 +03:00
func ( pf * templateUploadFlags ) setWorkdir ( wd string ) {
if wd == "" {
return
}
if pf . directory == "" || pf . directory == "." {
pf . directory = wd
} else if ! filepath . IsAbs ( pf . directory ) {
pf . directory = filepath . Join ( wd , pf . directory )
}
}
2023-01-16 14:32:11 -06:00
func ( pf * templateUploadFlags ) stdin () bool {
return pf . directory == "-"
}
2023-03-23 17:42:20 -05:00
func ( pf * templateUploadFlags ) upload ( inv * clibase . Invocation , client * codersdk . Client ) ( * codersdk . UploadResponse , error ) {
2023-02-18 18:32:09 -06:00
var content io . Reader
2023-01-16 14:32:11 -06:00
if pf . stdin () {
2023-03-23 17:42:20 -05:00
content = inv . Stdin
2023-01-16 14:32:11 -06:00
} else {
prettyDir := prettyDirectoryPath ( pf . directory )
2023-03-23 17:42:20 -05:00
_ , err := cliui . Prompt ( inv , cliui . PromptOptions {
2023-01-16 14:32:11 -06:00
Text : fmt . Sprintf ( "Upload %q?" , prettyDir ),
IsConfirm : true ,
Default : cliui . ConfirmYes ,
})
if err != nil {
return nil , err
}
2023-02-04 14:07:09 -06:00
pipeReader , pipeWriter := io . Pipe ()
go func () {
2023-02-06 14:35:48 -06:00
err := provisionersdk . Tar ( pipeWriter , pf . directory , provisionersdk . TemplateArchiveLimit )
_ = pipeWriter . CloseWithError ( err )
2023-02-04 14:07:09 -06:00
}()
defer pipeReader . Close ()
content = pipeReader
2023-01-16 14:32:11 -06:00
}
spin := spinner . New ( spinner . CharSets [ 5 ], 100 * time . Millisecond )
2023-03-23 17:42:20 -05:00
spin . Writer = inv . Stdout
2023-09-07 17:28:22 -04:00
spin . Suffix = pretty . Sprint ( cliui . DefaultStyles . Keyword , " Uploading directory..." )
2023-01-16 14:32:11 -06:00
spin . Start ()
defer spin . Stop ()
2023-03-23 17:42:20 -05:00
resp , err := client . Upload ( inv . Context (), codersdk . ContentTypeTar , bufio . NewReader ( content ))
2023-01-16 14:32:11 -06:00
if err != nil {
return nil , xerrors . Errorf ( "upload: %w" , err )
}
return & resp , nil
}
2023-06-20 10:02:44 -05:00
func ( pf * templateUploadFlags ) checkForLockfile ( inv * clibase . Invocation ) error {
if pf . stdin () || pf . ignoreLockfile {
// Just assume there's a lockfile if reading from stdin.
return nil
}
hasLockfile , err := provisionersdk . DirHasLockfile ( pf . directory )
if err != nil {
return xerrors . Errorf ( "dir has lockfile: %w" , err )
}
if ! hasLockfile {
cliui . Warn ( inv . Stdout , "No .terraform.lock.hcl file found" ,
"When provisioning, Coder will be unable to cache providers without a lockfile and must download them from the internet each time." ,
2023-09-07 17:28:22 -04:00
"Create one by running " + pretty . Sprint ( cliui . DefaultStyles . Code , "terraform init" ) + " in your template directory." ,
2023-06-20 10:02:44 -05:00
)
}
return nil
}
2023-07-11 13:11:08 +03:00
func ( pf * templateUploadFlags ) templateMessage ( inv * clibase . Invocation ) string {
title := strings . SplitN ( pf . message , "\n" , 2 )[ 0 ]
if len ( title ) > 72 {
cliui . Warn ( inv . Stdout , "Template message is longer than 72 characters, it will be displayed as truncated." )
}
if title != pf . message {
cliui . Warn ( inv . Stdout , "Template message contains newlines, only the first line will be displayed." )
}
if pf . message != "" {
return pf . message
}
return "Uploaded from the CLI"
}
2023-01-16 14:32:11 -06:00
func ( pf * templateUploadFlags ) templateName ( args [] string ) ( string , error ) {
if pf . stdin () {
// Can't infer name from directory if none provided.
if len ( args ) == 0 {
return "" , xerrors . New ( "template name argument must be provided" )
}
return args [ 0 ], nil
}
if len ( args ) > 0 {
2023-03-23 17:42:20 -05:00
return args [ 0 ], nil
2023-01-16 14:32:11 -06:00
}
2023-03-27 17:58:20 -05:00
// Have to take absPath to resolve "." and "..".
absPath , err := filepath . Abs ( pf . directory )
if err != nil {
return "" , err
}
2023-03-23 17:42:20 -05:00
// If no name is provided, use the directory name.
2023-03-27 17:58:20 -05:00
return filepath . Base ( absPath ), nil
2023-01-16 14:32:11 -06:00
}
2023-03-23 17:42:20 -05:00
func ( r * RootCmd ) templatePush () * clibase . Cmd {
2022-05-12 14:54:58 +03:00
var (
2022-11-16 16:34:06 -06:00
versionName string
provisioner string
2023-04-17 17:58:25 +03:00
workdir string
2023-02-17 09:07:45 +01:00
variablesFile string
variables [] string
2022-11-16 16:34:06 -06:00
alwaysPrompt bool
provisionerTags [] string
2023-01-16 14:32:11 -06:00
uploadFlags templateUploadFlags
2023-06-03 15:39:00 -07:00
activate bool
2023-07-13 12:58:34 +02:00
create bool
2022-05-12 14:54:58 +03:00
)
2023-03-23 17:42:20 -05:00
client := new ( codersdk . Client )
cmd := & clibase . Cmd {
2022-07-29 15:21:48 -04:00
Use : "push [template]" ,
Short : "Push a new template version from the current directory or as specified by flag" ,
2023-03-23 17:42:20 -05:00
Middleware : clibase . Chain (
clibase . RequireRangeArgs ( 0 , 1 ),
r . InitClient ( client ),
),
Handler : func ( inv * clibase . Invocation ) error {
2023-04-17 17:58:25 +03:00
uploadFlags . setWorkdir ( workdir )
2023-03-23 17:42:20 -05:00
organization , err := CurrentOrganization ( inv , client )
2022-03-22 13:17:50 -06:00
if err != nil {
return err
}
2022-07-01 11:49:29 -05:00
2023-03-23 17:42:20 -05:00
name , err := uploadFlags . templateName ( inv . Args )
2023-01-16 14:32:11 -06:00
if err != nil {
return err
2022-07-01 11:49:29 -05:00
}
2023-07-13 12:58:34 +02:00
var createTemplate bool
2023-03-23 17:42:20 -05:00
template , err := client . TemplateByName ( inv . Context (), organization . ID , name )
2022-03-22 13:17:50 -06:00
if err != nil {
2023-07-13 12:58:34 +02:00
if ! create {
return err
}
createTemplate = true
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 )
2023-03-23 17:42:20 -05:00
resp , err := uploadFlags . upload ( inv , client )
2022-03-22 13:17:50 -06:00
if err != nil {
return err
}
2022-05-12 14:54:58 +03:00
2022-11-16 16:34:06 -06:00
tags , err := ParseProvisionerTags ( provisionerTags )
if err != nil {
return err
}
2023-07-13 12:58:34 +02:00
args := createValidTemplateVersionArgs {
2023-07-11 13:11:08 +03:00
Message : message ,
2022-06-17 12:22:28 -05:00
Client : client ,
Organization : organization ,
2023-09-04 21:42:15 +03:00
Provisioner : codersdk . ProvisionerType ( provisioner ),
2022-10-13 18:02:52 -05:00
FileID : resp . ID ,
2023-07-13 12:58:34 +02:00
ProvisionerTags : tags ,
2023-02-17 09:07:45 +01:00
VariablesFile : variablesFile ,
Variables : variables ,
2023-07-13 12:58:34 +02:00
}
if ! createTemplate {
args . Name = versionName
args . Template = & template
args . ReuseParameters = ! alwaysPrompt
}
job , err := createValidTemplateVersion ( inv , args )
2022-03-22 13:17:50 -06:00
if err != nil {
return err
}
2022-06-17 12:22:28 -05:00
if job . Job . Status != codersdk . ProvisionerJobSucceeded {
return xerrors . Errorf ( "job failed: %s" , job . Job . Status )
2022-03-22 13:17:50 -06:00
}
2023-07-13 12:58:34 +02:00
if createTemplate {
_ , err = client . CreateTemplate ( inv . Context (), organization . ID , codersdk . CreateTemplateRequest {
Name : name ,
VersionID : job . ID ,
})
if err != nil {
return err
}
2023-09-07 17:28:22 -04:00
_ , _ = fmt . Fprintln (
inv . Stdout , "\n" + cliui . Wrap (
"The " + cliui . Keyword ( name ) + " template has been created at " + cliui . Timestamp ( time . Now ()) + "! " +
"Developers can provision a workspace with this template using:" ) + "\n" )
2023-07-13 12:58:34 +02:00
} else if activate {
2023-06-03 15:39:00 -07:00
err = client . UpdateActiveTemplateVersion ( inv . Context (), template . ID , codersdk . UpdateActiveTemplateVersion {
ID : job . ID ,
})
if err != nil {
return err
}
2022-03-22 13:17:50 -06:00
}
2022-06-17 12:22:28 -05:00
2023-09-07 17:28:22 -04:00
_ , _ = fmt . Fprintf ( inv . Stdout , "Updated version at %s!\n" , pretty . Sprint ( cliui . DefaultStyles . DateTimeStamp , time . Now (). Format ( time . Stamp )))
2022-02-10 08:33:27 -06:00
return nil
},
}
2022-05-12 14:54:58 +03:00
2023-03-23 17:42:20 -05:00
cmd . Options = clibase . OptionSet {
{
2023-04-17 17:58:25 +03:00
Flag : "test.provisioner" ,
Description : "Customize the provisioner backend." ,
Default : "terraform" ,
Value : clibase . StringOf ( & provisioner ),
// This is for testing!
Hidden : true ,
},
{
Flag : "test.workdir" ,
Description : "Customize the working directory." ,
Default : "" ,
Value : clibase . StringOf ( & workdir ),
2023-03-23 17:42:20 -05:00
// This is for testing!
Hidden : true ,
},
{
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 ( & variables ),
2023-07-25 09:36:02 -05:00
},
{
Flag : "var" ,
Description : "Alias of --variable." ,
Value : clibase . StringArrayOf ( & variables ),
2023-03-23 17:42:20 -05:00
},
{
Flag : "provisioner-tag" ,
Description : "Specify a set of tags to target provisioner daemons." ,
Value : clibase . StringArrayOf ( & provisionerTags ),
},
{
Flag : "name" ,
Description : "Specify a name for the new template version. It will be automatically generated if not provided." ,
Value : clibase . StringOf ( & versionName ),
},
{
Flag : "always-prompt" ,
Description : "Always prompt all parameters. Does not pull parameter values from active template version." ,
Value : clibase . BoolOf ( & alwaysPrompt ),
},
2023-06-03 15:39:00 -07:00
{
Flag : "activate" ,
Description : "Whether the new template will be marked active." ,
Default : "true" ,
Value : clibase . BoolOf ( & activate ),
},
2023-07-13 12:58:34 +02:00
{
Flag : "create" ,
Description : "Create the template if it does not exist." ,
Default : "false" ,
Value : clibase . BoolOf ( & create ),
},
2023-03-23 17:42:20 -05:00
cliui . SkipPromptOption (),
2022-05-12 14:54:58 +03:00
}
2023-06-20 10:02:44 -05:00
cmd . Options = append ( cmd . Options , uploadFlags . options () ... )
2022-05-12 14:54:58 +03:00
return cmd
2022-02-10 08:33:27 -06:00
}