2022-02-10 14:33:27 +00:00
|
|
|
package cli
|
|
|
|
|
2022-03-22 19:17:50 +00:00
|
|
|
import (
|
2023-02-04 20:07:09 +00:00
|
|
|
"bufio"
|
2022-03-22 19:17:50 +00:00
|
|
|
"fmt"
|
2023-01-16 20:32:11 +00:00
|
|
|
"io"
|
2022-07-01 16:49:29 +00:00
|
|
|
"path/filepath"
|
2022-03-22 19:17:50 +00:00
|
|
|
"time"
|
|
|
|
|
2022-05-12 11:54:58 +00:00
|
|
|
"github.com/briandowns/spinner"
|
2022-03-22 19:17:50 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
"github.com/coder/coder/cli/clibase"
|
2022-05-12 11:54:58 +00:00
|
|
|
"github.com/coder/coder/cli/cliui"
|
2022-06-17 17:22:28 +00:00
|
|
|
"github.com/coder/coder/coderd/database"
|
2022-03-22 19:17:50 +00:00
|
|
|
"github.com/coder/coder/codersdk"
|
|
|
|
"github.com/coder/coder/provisionersdk"
|
|
|
|
)
|
2022-02-10 14:33:27 +00:00
|
|
|
|
2023-01-16 20:32:11 +00:00
|
|
|
// templateUploadFlags is shared by `templates create` and `templates push`.
|
|
|
|
type templateUploadFlags struct {
|
|
|
|
directory string
|
|
|
|
}
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
func (pf *templateUploadFlags) option() clibase.Option {
|
|
|
|
return clibase.Option{
|
|
|
|
Flag: "directory",
|
|
|
|
FlagShorthand: "d",
|
|
|
|
Description: "Specify the directory to create from, use '-' to read tar from stdin.",
|
|
|
|
Default: ".",
|
|
|
|
Value: clibase.StringOf(&pf.directory),
|
|
|
|
}
|
2023-01-16 20:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pf *templateUploadFlags) stdin() bool {
|
|
|
|
return pf.directory == "-"
|
|
|
|
}
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
func (pf *templateUploadFlags) upload(inv *clibase.Invocation, client *codersdk.Client) (*codersdk.UploadResponse, error) {
|
2023-02-19 00:32:09 +00:00
|
|
|
var content io.Reader
|
2023-01-16 20:32:11 +00:00
|
|
|
if pf.stdin() {
|
2023-03-23 22:42:20 +00:00
|
|
|
content = inv.Stdin
|
2023-01-16 20:32:11 +00:00
|
|
|
} else {
|
|
|
|
prettyDir := prettyDirectoryPath(pf.directory)
|
2023-03-23 22:42:20 +00:00
|
|
|
_, err := cliui.Prompt(inv, cliui.PromptOptions{
|
2023-01-16 20:32:11 +00:00
|
|
|
Text: fmt.Sprintf("Upload %q?", prettyDir),
|
|
|
|
IsConfirm: true,
|
|
|
|
Default: cliui.ConfirmYes,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-04 20:07:09 +00:00
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
|
|
|
go func() {
|
2023-02-06 20:35:48 +00:00
|
|
|
err := provisionersdk.Tar(pipeWriter, pf.directory, provisionersdk.TemplateArchiveLimit)
|
|
|
|
_ = pipeWriter.CloseWithError(err)
|
2023-02-04 20:07:09 +00:00
|
|
|
}()
|
|
|
|
defer pipeReader.Close()
|
|
|
|
content = pipeReader
|
2023-01-16 20:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spin := spinner.New(spinner.CharSets[5], 100*time.Millisecond)
|
2023-03-23 22:42:20 +00:00
|
|
|
spin.Writer = inv.Stdout
|
2023-01-16 20:32:11 +00:00
|
|
|
spin.Suffix = cliui.Styles.Keyword.Render(" Uploading directory...")
|
|
|
|
spin.Start()
|
|
|
|
defer spin.Stop()
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
resp, err := client.Upload(inv.Context(), codersdk.ContentTypeTar, bufio.NewReader(content))
|
2023-01-16 20:32:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("upload: %w", err)
|
|
|
|
}
|
|
|
|
return &resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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 22:42:20 +00:00
|
|
|
return args[0], nil
|
2023-01-16 20:32:11 +00:00
|
|
|
}
|
2023-03-23 22:42:20 +00:00
|
|
|
// If no name is provided, use the directory name.
|
|
|
|
return filepath.Base(pf.directory), nil
|
2023-01-16 20:32:11 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
func (r *RootCmd) templatePush() *clibase.Cmd {
|
2022-05-12 11:54:58 +00:00
|
|
|
var (
|
2022-11-16 22:34:06 +00:00
|
|
|
versionName string
|
|
|
|
provisioner string
|
|
|
|
parameterFile string
|
2023-02-17 08:07:45 +00:00
|
|
|
variablesFile string
|
|
|
|
variables []string
|
2022-11-16 22:34:06 +00:00
|
|
|
alwaysPrompt bool
|
|
|
|
provisionerTags []string
|
2023-01-16 20:32:11 +00:00
|
|
|
uploadFlags templateUploadFlags
|
2022-05-12 11:54:58 +00:00
|
|
|
)
|
2023-03-23 22:42:20 +00:00
|
|
|
client := new(codersdk.Client)
|
|
|
|
cmd := &clibase.Cmd{
|
2022-07-29 19:21:48 +00:00
|
|
|
Use: "push [template]",
|
|
|
|
Short: "Push a new template version from the current directory or as specified by flag",
|
2023-03-23 22:42:20 +00:00
|
|
|
Middleware: clibase.Chain(
|
|
|
|
clibase.RequireRangeArgs(0, 1),
|
|
|
|
r.InitClient(client),
|
|
|
|
),
|
|
|
|
Handler: func(inv *clibase.Invocation) error {
|
|
|
|
organization, err := CurrentOrganization(inv, client)
|
2022-03-22 19:17:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-01 16:49:29 +00:00
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
name, err := uploadFlags.templateName(inv.Args)
|
2022-03-22 19:17:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
template, err := client.TemplateByName(inv.Context(), organization.ID, name)
|
2022-03-22 19:17:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-12 11:54:58 +00:00
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
resp, err := uploadFlags.upload(inv, client)
|
2022-03-22 19:17:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-16 22:34:06 +00:00
|
|
|
tags, err := ParseProvisionerTags(provisionerTags)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
job, _, err := createValidTemplateVersion(inv, createValidTemplateVersionArgs{
|
2022-09-24 01:17:36 +00:00
|
|
|
Name: versionName,
|
2022-06-17 17:22:28 +00:00
|
|
|
Client: client,
|
|
|
|
Organization: organization,
|
|
|
|
Provisioner: database.ProvisionerType(provisioner),
|
2022-10-13 23:02:52 +00:00
|
|
|
FileID: resp.ID,
|
2022-06-17 17:22:28 +00:00
|
|
|
ParameterFile: parameterFile,
|
2023-02-17 08:07:45 +00:00
|
|
|
VariablesFile: variablesFile,
|
|
|
|
Variables: variables,
|
2022-06-17 17:22:28 +00:00
|
|
|
Template: &template,
|
|
|
|
ReuseParameters: !alwaysPrompt,
|
2022-11-16 22:34:06 +00:00
|
|
|
ProvisionerTags: tags,
|
2022-03-22 19:17:50 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-17 17:22:28 +00:00
|
|
|
if job.Job.Status != codersdk.ProvisionerJobSucceeded {
|
|
|
|
return xerrors.Errorf("job failed: %s", job.Job.Status)
|
2022-03-22 19:17:50 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{
|
2022-06-17 17:22:28 +00:00
|
|
|
ID: job.ID,
|
2022-03-22 19:17:50 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-17 17:22:28 +00:00
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
_, _ = fmt.Fprintf(inv.Stdout, "Updated version at %s!\n", cliui.Styles.DateTimeStamp.Render(time.Now().Format(time.Stamp)))
|
2022-02-10 14:33:27 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2022-05-12 11:54:58 +00:00
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
cmd.Options = clibase.OptionSet{
|
|
|
|
{
|
|
|
|
Flag: "test.provisioner",
|
|
|
|
FlagShorthand: "p",
|
|
|
|
Description: "Customize the provisioner backend.",
|
|
|
|
Default: "terraform",
|
|
|
|
Value: clibase.StringOf(&provisioner),
|
|
|
|
// This is for testing!
|
|
|
|
Hidden: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Flag: "parameter-file",
|
|
|
|
Description: "Specify a file path with parameter values.",
|
|
|
|
Value: clibase.StringOf(¶meterFile),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
cliui.SkipPromptOption(),
|
|
|
|
uploadFlags.option(),
|
2022-05-12 11:54:58 +00:00
|
|
|
}
|
|
|
|
return cmd
|
2022-02-10 14:33:27 +00:00
|
|
|
}
|