/
new.go
1109 lines (972 loc) · 33.8 KB
/
new.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// nolint: goconst
package main
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"unicode"
"gopkg.in/yaml.v3"
survey "github.com/AlecAivazis/survey/v2"
surveycore "github.com/AlecAivazis/survey/v2/core"
"github.com/opentracing/opentracing-go"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/v3/backend"
"github.com/pulumi/pulumi/pkg/v3/backend/display"
"github.com/pulumi/pulumi/pkg/v3/backend/state"
"github.com/pulumi/pulumi/pkg/v3/engine"
"github.com/pulumi/pulumi/pkg/v3/util/yamlutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)
type promptForValueFunc func(yes bool, valueType string, defaultValue string, secret bool,
isValidFn func(value string) error, opts display.Options) (string, error)
type newArgs struct {
configArray []string
configPath bool
description string
dir string
force bool
generateOnly bool
interactive bool
name string
offline bool
prompt promptForValueFunc
secretsProvider string
stack string
templateNameOrURL string
yes bool
listTemplates bool
}
func runNew(ctx context.Context, args newArgs) error {
if !args.interactive && !args.yes {
return errors.New("--yes must be passed in to proceed when running in non-interactive mode")
}
// Prepare options.
opts := display.Options{
Color: cmdutil.GetGlobalColorization(),
IsInteractive: args.interactive,
}
// Validate name (if specified) before further prompts/operations.
if args.name != "" && workspace.ValidateProjectName(args.name) != nil {
return fmt.Errorf("'%s' is not a valid project name. %w", args.name, workspace.ValidateProjectName(args.name))
}
// Validate secrets provider type
if err := validateSecretsProvider(args.secretsProvider); err != nil {
return err
}
// Get the current working directory.
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("getting the working directory: %w", err)
}
originalCwd := cwd
// If dir was specified, ensure it exists and use it as the
// current working directory.
if args.dir != "" {
cwd, err = useSpecifiedDir(args.dir)
if err != nil {
return err
}
}
// Return an error if the directory isn't empty.
if !args.force {
if err = errorIfNotEmptyDirectory(cwd); err != nil {
return err
}
}
// If we're going to be creating a stack, get the current backend, which
// will kick off the login flow (if not already logged-in).
if !args.generateOnly {
if _, err = currentBackend(ctx, opts); err != nil {
return err
}
}
// Ensure the project doesn't already exist.
if args.name != "" {
if err := validateProjectName(ctx, args.name, args.generateOnly, opts); err != nil {
return err
}
}
// Retrieve the template repo.
repo, err := workspace.RetrieveTemplates(args.templateNameOrURL, args.offline, workspace.TemplateKindPulumiProject)
if err != nil {
return err
}
defer func() {
contract.IgnoreError(repo.Delete())
}()
// List the templates from the repo.
templates, err := repo.Templates()
if err != nil {
return err
}
var template workspace.Template
if len(templates) == 0 {
return errors.New("no templates")
} else if len(templates) == 1 {
template = templates[0]
} else {
if template, err = chooseTemplate(templates, opts); err != nil {
return err
}
}
// Do a dry run, if we're not forcing files to be overwritten.
if !args.force {
if err = workspace.CopyTemplateFilesDryRun(template.Dir, cwd, args.name); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("template '%s' not found: %w", args.templateNameOrURL, err)
}
return err
}
}
// If a stack was specified via --stack, see if it already exists.
// Only do the lookup for fully-qualified stack names `org/project/stack` because
// otherwise `getStack` will fail to detect the project folder and fail.
// The main purpose of this lookup is getting a proper start with a project
// created via the web app.
var s backend.Stack
if args.stack != "" && strings.Count(args.stack, "/") == 2 {
stackName, err := buildStackName(args.stack)
if err != nil {
return err
}
existingStack, existingName, existingDesc, err := getStack(ctx, stackName, opts)
if err != nil {
return err
}
s = existingStack
if args.name == "" {
args.name = existingName
}
if args.description == "" {
args.description = existingDesc
}
}
// Show instructions, if we're going to show at least one prompt.
hasAtLeastOnePrompt := (args.name == "") || (args.description == "") || (!args.generateOnly && args.stack == "")
if !args.yes && hasAtLeastOnePrompt {
fmt.Println("This command will walk you through creating a new Pulumi project.")
fmt.Println()
fmt.Println(
opts.Color.Colorize(
colors.Highlight("Enter a value or leave blank to accept the (default), and press <ENTER>.",
"<ENTER>", colors.BrightCyan+colors.Bold)))
fmt.Println(
opts.Color.Colorize(
colors.Highlight("Press ^C at any time to quit.", "^C", colors.BrightCyan+colors.Bold)))
fmt.Println()
}
// Prompt for the project name, if it wasn't already specified.
if args.name == "" {
defaultValue := workspace.ValueOrSanitizedDefaultProjectName(args.name, template.ProjectName, filepath.Base(cwd))
if err := validateProjectName(ctx, defaultValue, args.generateOnly, opts); err != nil {
// If --yes is given error out now that the default value is invalid. If we allow prompt to catch
// this case it can lead to a confusing error message because we set the defaultValue to "" below.
// See https://github.com/pulumi/pulumi/issues/8747.
if args.yes {
return fmt.Errorf("'%s' is not a valid project name. %w", defaultValue, err)
}
// Do not suggest an invalid or existing name as the default project name.
defaultValue = ""
}
validate := func(s string) error { return validateProjectName(ctx, s, args.generateOnly, opts) }
args.name, err = args.prompt(args.yes, "project name", defaultValue, false, validate, opts)
if err != nil {
return err
}
}
// Prompt for the project description, if it wasn't already specified.
if args.description == "" {
defaultValue := workspace.ValueOrDefaultProjectDescription(
args.description, template.ProjectDescription, template.Description)
args.description, err = args.prompt(
args.yes, "project description", defaultValue, false, workspace.ValidateProjectDescription, opts)
if err != nil {
return err
}
}
// Actually copy the files.
if err = workspace.CopyTemplateFiles(template.Dir, cwd, args.force, args.name, args.description); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("template '%s' not found: %w", args.templateNameOrURL, err)
}
return err
}
fmt.Printf("Created project '%s'\n", args.name)
fmt.Println()
// Load the project, update the name & description, remove the template section, and save it.
proj, path, err := readProjectWithPath()
root := filepath.Dir(path)
if err != nil {
return err
}
if filepath.Ext(path) == ".yaml" {
filedata, err := os.ReadFile(path)
if err != nil {
return err
}
var workspaceDocument yaml.Node
err = yaml.Unmarshal(filedata, &workspaceDocument)
if err != nil {
return err
}
proj.Name = tokens.PackageName(args.name)
err = yamlutil.Insert(&workspaceDocument, "name", args.name)
if err != nil {
return err
}
proj.Description = &args.description
err = yamlutil.Insert(&workspaceDocument, "description", args.description)
if err != nil {
return err
}
proj.Template = nil
err = yamlutil.Delete(&workspaceDocument, "template")
if err != nil {
return err
}
if proj.Runtime.Name() == "python" {
// If the template does give virtualenv use it, else default to "venv"
if len(proj.Runtime.Options()) == 0 {
proj.Runtime.SetOption("virtualenv", "venv")
err = yamlutil.Insert(&workspaceDocument, "runtime", strings.TrimSpace(`
name: python
options:
virtualenv: venv
`))
if err != nil {
return err
}
}
}
contract.Assert(len(workspaceDocument.Content) == 1)
projFile, err := yaml.Marshal(workspaceDocument.Content[0])
if err != nil {
return err
}
err = os.WriteFile(path, projFile, 0600)
if err != nil {
return err
}
}
appendFileName := "Pulumi.yaml.append"
appendFile := filepath.Join(root, appendFileName)
os.Remove(appendFile)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
// Create the stack, if needed.
if !args.generateOnly && s == nil {
if s, err = promptAndCreateStack(ctx, args.prompt,
args.stack, args.name, true /*setCurrent*/, args.yes, opts, args.secretsProvider); err != nil {
return err
}
// The backend will print "Created stack '<stack>'" on success.
fmt.Println()
}
// Prompt for config values (if needed) and save.
if !args.generateOnly {
err = handleConfig(ctx, s, args.templateNameOrURL, template, args.configArray, args.yes, args.configPath, opts)
if err != nil {
return err
}
}
// Ensure the stack is selected.
if !args.generateOnly && s != nil {
contract.IgnoreError(state.SetCurrentStack(s.Ref().String()))
}
// Install dependencies.
if !args.generateOnly {
span := opentracing.SpanFromContext(ctx)
projinfo := &engine.Projinfo{Proj: proj, Root: root}
pwd, _, pluginCtx, err := engine.ProjectInfoContext(
projinfo,
nil,
cmdutil.Diag(),
cmdutil.Diag(),
false,
span,
)
if err != nil {
return err
}
defer pluginCtx.Close()
if err := installDependencies(pluginCtx, &proj.Runtime, pwd); err != nil {
return err
}
}
fmt.Println(
opts.Color.Colorize(
colors.BrightGreen+colors.Bold+"Your new project is ready to go!"+colors.Reset) +
" " + cmdutil.EmojiOr("✨", ""))
fmt.Println()
// Print out next steps.
printNextSteps(proj, originalCwd, cwd, args.generateOnly, opts)
if template.Quickstart != "" {
fmt.Println(template.Quickstart)
}
return nil
}
// Ensure the directory exists and uses it as the current working
// directory.
func useSpecifiedDir(dir string) (string, error) {
// Ensure the directory exists.
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return "", fmt.Errorf("creating the directory: %w", err)
}
// Change the working directory to the specified directory.
if err := os.Chdir(dir); err != nil {
return "", fmt.Errorf("changing the working directory: %w", err)
}
// Get the new working directory.
var cwd string
var err error
if cwd, err = os.Getwd(); err != nil {
return "", fmt.Errorf("getting the working directory: %w", err)
}
return cwd, nil
}
// newNewCmd creates a New command with default dependencies.
// Intentionally disabling here for cleaner err declaration/assignment.
// nolint: vetshadow
func newNewCmd() *cobra.Command {
var args = newArgs{
interactive: cmdutil.Interactive(),
prompt: promptForValue,
}
getTemplates := func() ([]workspace.Template, error) {
// Attempt to retrieve available templates.
repo, err := workspace.RetrieveTemplates("", false /*offline*/, workspace.TemplateKindPulumiProject)
if err != nil {
logging.Warningf("could not retrieve templates: %v", err)
return []workspace.Template{}, err
}
// Get the list of templates.
return repo.Templates()
}
cmd := &cobra.Command{
Use: "new [template|url]",
SuggestFor: []string{"init", "create"},
Short: "Create a new Pulumi project",
Long: "Create a new Pulumi project and stack from a template.\n" +
"\n" +
"To create a project from a specific template, pass the template name (such as `aws-typescript`\n" +
"or `azure-python`). If no template name is provided, a list of suggested templates will be presented\n" +
"which can be selected interactively.\n" +
"\n" +
"By default, a stack created using the pulumi.com backend will use the pulumi.com secrets\n" +
"provider and a stack created using the local or cloud object storage backend will use the\n" +
"`passphrase` secrets provider. A different secrets provider can be selected by passing the\n" +
"`--secrets-provider` flag.\n" +
"\n" +
"To use the `passphrase` secrets provider with the pulumi.com backend, use:\n" +
"* `pulumi new --secrets-provider=passphrase`\n" +
"\n" +
"To use a cloud secrets provider with any backend, use one of the following:\n" +
"* `pulumi new --secrets-provider=\"awskms://alias/ExampleAlias?region=us-east-1\"`\n" +
"* `pulumi new --secrets-provider=\"awskms://1234abcd-12ab-34cd-56ef-1234567890ab?region=us-east-1\"`\n" +
"* `pulumi new --secrets-provider=\"azurekeyvault://mykeyvaultname.vault.azure.net/keys/mykeyname\"`\n" +
"* `pulumi new --secrets-provider=\"gcpkms://projects/p/locations/l/keyRings/r/cryptoKeys/k\"`\n" +
"* `pulumi new --secrets-provider=\"hashivault://mykey\"`" +
"\n\n" +
"To create a project from a specific source control location, pass the url as follows e.g.\n" +
"* `pulumi new https://gitlab.com/<user>/<repo>`\n" +
"* `pulumi new https://bitbucket.org/<user>/<repo>`\n" +
"* `pulumi new https://github.com/<user>/<repo>`\n" +
"\n" +
"To create the project from a branch of a specific source control location, pass the url to the branch, e.g.\n" +
"* `pulumi new https://gitlab.com/<user>/<repo>/tree/<branch>`\n" +
"* `pulumi new https://bitbucket.org/<user>/<repo>/tree/<branch>`\n" +
"* `pulumi new https://github.com/<user>/<repo>/tree/<branch>`\n",
Args: cmdutil.MaximumNArgs(1),
Run: cmdutil.RunFunc(func(cmd *cobra.Command, cliArgs []string) error {
ctx := commandContext()
if len(cliArgs) > 0 {
args.templateNameOrURL = cliArgs[0]
}
if args.listTemplates {
templates, err := getTemplates()
if err != nil {
logging.Warningf("could not list templates: %v", err)
return err
}
available, _ := templatesToOptionArrayAndMap(templates, true)
fmt.Println("")
fmt.Println("Available Templates:")
for _, t := range available {
fmt.Printf(" %s\n", t)
}
return nil
}
args.yes = args.yes || skipConfirmations()
return runNew(ctx, args)
}),
}
// Add additional help that includes a list of available templates.
defaultHelp := cmd.HelpFunc()
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
// Show default help.
defaultHelp(cmd, args)
templates, err := getTemplates()
if err != nil {
logging.Warningf("could not list templates: %v", err)
return
}
// If we have any templates, show them.
if len(templates) > 0 {
fmt.Println()
fmt.Printf("There are %d locally installed templates.\n", len(templates))
}
})
cmd.PersistentFlags().StringArrayVarP(
&args.configArray, "config", "c", []string{},
"Config to save")
cmd.PersistentFlags().BoolVar(
&args.configPath, "config-path", false,
"Config keys contain a path to a property in a map or list to set")
cmd.PersistentFlags().StringVarP(
&args.description, "description", "d", "",
"The project description; if not specified, a prompt will request it")
cmd.PersistentFlags().StringVar(
&args.dir, "dir", "",
"The location to place the generated project; if not specified, the current directory is used")
cmd.PersistentFlags().BoolVarP(
&args.force, "force", "f", false,
"Forces content to be generated even if it would change existing files")
cmd.PersistentFlags().BoolVarP(
&args.generateOnly, "generate-only", "g", false,
"Generate the project only; do not create a stack, save config, or install dependencies")
cmd.PersistentFlags().StringVarP(
&args.name, "name", "n", "",
"The project name; if not specified, a prompt will request it")
cmd.PersistentFlags().BoolVarP(
&args.offline, "offline", "o", false,
"Use locally cached templates without making any network requests")
cmd.PersistentFlags().StringVarP(
&args.stack, "stack", "s", "",
"The stack name; either an existing stack or stack to create; if not specified, a prompt will request it")
cmd.PersistentFlags().BoolVarP(
&args.yes, "yes", "y", false,
"Skip prompts and proceed with default values")
cmd.PersistentFlags().StringVar(
&args.secretsProvider, "secrets-provider", "default", "The type of the provider that should be used to encrypt and "+
"decrypt secrets (possible choices: default, passphrase, awskms, azurekeyvault, gcpkms, hashivault)")
cmd.PersistentFlags().BoolVarP(
&args.listTemplates, "list-templates", "l", false,
"List locally installed templates and exit")
return cmd
}
// errorIfNotEmptyDirectory returns an error if path is not empty.
func errorIfNotEmptyDirectory(path string) error {
infos, err := os.ReadDir(path)
if err != nil {
return err
}
if len(infos) > 0 {
return fmt.Errorf("%s is not empty; "+
"rerun in an empty directory, pass the path to an empty directory to --dir, or use --force", path)
}
return nil
}
func validateProjectName(ctx context.Context, projectName string, generateOnly bool, opts display.Options) error {
err := workspace.ValidateProjectName(projectName)
if err != nil {
return err
}
if !generateOnly {
b, err := currentBackend(ctx, opts)
if err != nil {
return err
}
exists, err := b.DoesProjectExist(ctx, projectName)
if err != nil {
return err
}
if exists {
return errors.New("A project with this name already exists")
}
}
return nil
}
// getStack gets a stack and the project name & description, or returns nil if the stack doesn't exist.
func getStack(ctx context.Context, stack string, opts display.Options) (backend.Stack, string, string, error) {
b, err := currentBackend(ctx, opts)
if err != nil {
return nil, "", "", err
}
stackRef, err := b.ParseStackReference(stack)
if err != nil {
return nil, "", "", err
}
s, err := b.GetStack(ctx, stackRef)
if err != nil {
return nil, "", "", err
}
name := ""
description := ""
if s != nil {
tags := s.Tags()
// Tags might be nil/empty, but if it has name and description use them
name = tags[apitype.ProjectNameTag]
description = tags[apitype.ProjectDescriptionTag]
}
return s, name, description, nil
}
// promptAndCreateStack creates and returns a new stack (prompting for the name as needed).
func promptAndCreateStack(ctx context.Context, prompt promptForValueFunc,
stack string, projectName string, setCurrent bool, yes bool, opts display.Options,
secretsProvider string) (backend.Stack, error) {
b, err := currentBackend(ctx, opts)
if err != nil {
return nil, err
}
if stack != "" {
stackName, err := buildStackName(stack)
if err != nil {
return nil, err
}
s, err := stackInit(ctx, b, stackName, setCurrent, secretsProvider)
if err != nil {
return nil, err
}
return s, nil
}
if b.SupportsOrganizations() {
fmt.Print("Please enter your desired stack name.\n" +
"To create a stack in an organization, " +
"use the format <org-name>/<stack-name> (e.g. `acmecorp/dev`).\n")
}
for {
stackName, err := prompt(yes, "stack name", "dev", false, b.ValidateStackName, opts)
if err != nil {
return nil, err
}
formattedStackName, err := buildStackName(stackName)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
s, err := stackInit(ctx, b, formattedStackName, setCurrent, secretsProvider)
if err != nil {
if !yes {
// Let the user know about the error and loop around to try again.
fmt.Printf("Sorry, could not create stack '%s': %v\n", stackName, err)
continue
}
return nil, err
}
return s, nil
}
}
// stackInit creates the stack.
func stackInit(
ctx context.Context, b backend.Backend, stackName string,
setCurrent bool, secretsProvider string) (backend.Stack, error) {
stackRef, err := b.ParseStackReference(stackName)
if err != nil {
return nil, err
}
return createStack(ctx, b, stackRef, nil, setCurrent, secretsProvider)
}
// saveConfig saves the config for the stack.
func saveConfig(stack backend.Stack, c config.Map) error {
project, _, err := readProject()
if err != nil {
return err
}
ps, err := loadProjectStack(project, stack)
if err != nil {
return err
}
for k, v := range c {
ps.Config[k] = v
}
return saveProjectStack(stack, ps)
}
// installDependencies will install dependencies for the project, e.g. by running `npm install` for nodejs projects.
func installDependencies(ctx *plugin.Context, runtime *workspace.ProjectRuntimeInfo, directory string) error {
// First make sure the language plugin is present. We need this to load the required resource plugins.
// TODO: we need to think about how best to version this. For now, it always picks the latest.
lang, err := ctx.Host.LanguageRuntime(ctx.Root, ctx.Pwd, runtime.Name(), runtime.Options())
if err != nil {
return fmt.Errorf("failed to load language plugin %s: %w", runtime.Name(), err)
}
if err = lang.InstallDependencies(directory); err != nil {
return fmt.Errorf("installing dependencies failed; rerun manually to try again, "+
"then run `pulumi up` to perform an initial deployment: %w", err)
}
return nil
}
// printNextSteps prints out a series of commands that the user needs to run before their stack is able to be updated.
func printNextSteps(proj *workspace.Project, originalCwd, cwd string, generateOnly bool, opts display.Options) {
var commands []string
// If the target working directory is not the same as our current WD, tell the user to
// CD to the target directory.
if originalCwd != cwd {
// If we can determine a relative path, use that, otherwise use the full path.
var cd string
if rel, err := filepath.Rel(originalCwd, cwd); err == nil {
cd = rel
} else {
cd = cwd
}
// Surround the path with double quotes if it contains whitespace.
if containsWhiteSpace(cd) {
cd = fmt.Sprintf("\"%s\"", cd)
}
cd = fmt.Sprintf("cd %s", cd)
commands = append(commands, cd)
}
if generateOnly {
// We didn't install dependencies, so instruct the user to do so.
if strings.EqualFold(proj.Runtime.Name(), "nodejs") {
commands = append(commands, "npm install")
} else if strings.EqualFold(proj.Runtime.Name(), "python") {
commands = append(commands, pythonCommands()...)
}
// We didn't create a stack so show that as a command to run before `pulumi up`.
commands = append(commands, "pulumi stack init")
}
if len(commands) == 0 { // No additional commands need to be run.
deployMsg := "To perform an initial deployment, run `pulumi up`"
deployMsg = colors.Highlight(deployMsg, "pulumi up", colors.BrightBlue+colors.Bold)
fmt.Println(opts.Color.Colorize(deployMsg))
fmt.Println()
return
}
if len(commands) == 1 { // Only one additional command need to be run.
deployMsg := fmt.Sprintf("To perform an initial deployment, run '%s', then, run `pulumi up`", commands[0])
deployMsg = colors.Highlight(deployMsg, commands[0], colors.BrightBlue+colors.Bold)
deployMsg = colors.Highlight(deployMsg, "pulumi up", colors.BrightBlue+colors.Bold)
fmt.Println(opts.Color.Colorize(deployMsg))
fmt.Println()
return
}
// One or more additional commands needs to be run.
fmt.Println("To perform an initial deployment, run the following commands:")
fmt.Println()
for i, cmd := range commands {
cmdColors := colors.BrightBlue + colors.Bold + cmd + colors.Reset
fmt.Printf(" %d. %s\n", i+1, opts.Color.Colorize(cmdColors))
}
fmt.Println()
upMsg := colors.Highlight("Then, run `pulumi up`", "pulumi up", colors.BrightBlue+colors.Bold)
fmt.Println(opts.Color.Colorize(upMsg))
fmt.Println()
}
// pythonCommands returns the set of Python commands to create a virtual environment, activate it, and
// install dependencies.
func pythonCommands() []string {
var commands []string
// Create the virtual environment.
switch runtime.GOOS {
case "windows":
commands = append(commands, "python -m venv venv")
default:
commands = append(commands, "python3 -m venv venv")
}
// Activate the virtual environment. Only active in the user's current shell, so we can't
// just run it for the user here.
switch runtime.GOOS {
case "windows":
commands = append(commands, "venv\\Scripts\\activate")
default:
commands = append(commands, "source venv/bin/activate")
}
// Update pip, setuptools, and wheel within the virtualenv.
commands = append(commands, "python -m pip install --upgrade pip setuptools wheel")
// Install dependencies within the virtualenv.
commands = append(commands, "python -m pip install -r requirements.txt")
return commands
}
// chooseTemplate will prompt the user to choose amongst the available templates.
func chooseTemplate(templates []workspace.Template, opts display.Options) (workspace.Template, error) {
const chooseTemplateErr = "no template selected; please use `pulumi new` to choose one"
if !opts.IsInteractive {
return workspace.Template{}, errors.New(chooseTemplateErr)
}
// Customize the prompt a little bit (and disable color since it doesn't match our scheme).
surveycore.DisableColor = true
var selectedOption workspace.Template
for {
options, optionToTemplateMap := templatesToOptionArrayAndMap(templates, true)
nopts := len(options)
pageSize := optimalPageSize(optimalPageSizeOpts{nopts: nopts})
message := fmt.Sprintf("\rPlease choose a template (%d/%d shown):\n", pageSize, nopts)
message = opts.Color.Colorize(colors.SpecPrompt + message + colors.Reset)
var option string
if err := survey.AskOne(&survey.Select{
Message: message,
Options: options,
PageSize: pageSize,
}, &option, surveyIcons(opts.Color)); err != nil {
return workspace.Template{}, errors.New(chooseTemplateErr)
}
var has bool
selectedOption, has = optionToTemplateMap[option]
if has {
break
}
}
return selectedOption, nil
}
// parseConfig parses the config values passed via command line flags.
// These are passed as `-c aws:region=us-east-1 -c foo:bar=blah` and end up
// in configArray as ["aws:region=us-east-1", "foo:bar=blah"].
// This function converts the array into a config.Map.
func parseConfig(configArray []string, path bool) (config.Map, error) {
configMap := make(config.Map)
for _, c := range configArray {
kvp := strings.SplitN(c, "=", 2)
key, err := parseConfigKey(kvp[0])
if err != nil {
return nil, err
}
value := config.NewValue("")
if len(kvp) == 2 {
value = config.NewValue(kvp[1])
}
if err = configMap.Set(key, value, path); err != nil {
return nil, err
}
}
return configMap, nil
}
// promptForConfig will go through each config key needed by the template and prompt for a value.
// If a config value exists in commandLineConfig, it will be used without prompting.
// If stackConfig is non-nil and a config value exists in stackConfig, it will be used as the default
// value when prompting instead of the default value specified in templateConfig.
func promptForConfig(
ctx context.Context,
stack backend.Stack,
templateConfig map[string]workspace.ProjectTemplateConfigValue,
commandLineConfig config.Map,
stackConfig config.Map,
yes bool,
opts display.Options) (config.Map, error) {
// Convert `string` keys to `config.Key`. If a string key is missing a delimiter,
// the project name will be prepended.
parsedTemplateConfig := make(map[config.Key]workspace.ProjectTemplateConfigValue)
for k, v := range templateConfig {
parsedKey, parseErr := parseConfigKey(k)
if parseErr != nil {
return nil, parseErr
}
parsedTemplateConfig[parsedKey] = v
}
// Sort keys. Note that we use the fully qualified module member here instead of a `prettyKey` so that
// all config values for the current program are prompted one after another.
var keys config.KeyArray
for k := range parsedTemplateConfig {
keys = append(keys, k)
}
sort.Sort(keys)
sm, err := getStackSecretsManager(stack)
if err != nil {
return nil, err
}
encrypter, err := sm.Encrypter()
if err != nil {
return nil, err
}
decrypter, err := sm.Decrypter()
if err != nil {
return nil, err
}
c := make(config.Map)
for _, k := range keys {
// If it was passed as a command line flag, use it without prompting.
if val, ok := commandLineConfig[k]; ok {
c[k] = val
continue
}
templateConfigValue := parsedTemplateConfig[k]
// Prepare a default value.
var defaultValue string
var secret bool
if stackConfig != nil {
// Use the stack's existing value as the default.
if val, ok := stackConfig[k]; ok {
// It's OK to pass a nil or non-nil crypter for non-secret values.
value, err := val.Value(decrypter)
if err != nil {
return nil, err
}
defaultValue = value
}
}
if defaultValue == "" {
defaultValue = templateConfigValue.Default
}
if !secret {
secret = templateConfigValue.Secret
}
// Prepare the prompt.
prompt := prettyKey(k)
if templateConfigValue.Description != "" {
prompt = prompt + ": " + templateConfigValue.Description
}
// Prompt.
value, err := promptForValue(yes, prompt, defaultValue, secret, nil, opts)
if err != nil {
return nil, err
}
// Encrypt the value if needed.
var v config.Value
if secret {
enc, err := encrypter.EncryptValue(ctx, value)
if err != nil {
return nil, err
}
v = config.NewSecureValue(enc)
} else {
v = config.NewValue(value)
}
// Save it.
c[k] = v
}
// Add any other config values from the command line.
for k, v := range commandLineConfig {
if _, ok := c[k]; !ok {
c[k] = v
}
}
return c, nil
}
// promptForValue prompts the user for a value with a defaultValue preselected. Hitting enter accepts the
// default. If yes is true, defaultValue is returned without prompting. isValidFn is an optional parameter;
// when specified, it will be run to validate that value entered. When this function returns a non nil error
// validation is assumed to have failed and an error is printed. The error returned by isValidFn is also displayed
// to provide information about why the validation failed. A period is appended to this message. `promptForValue` then
// prompts again.
func promptForValue(
yes bool, valueType string, defaultValue string, secret bool,
isValidFn func(value string) error, opts display.Options) (string, error) {
var value string
for {
// If we are auto-accepting the default (--yes), just set it and move on to validating.
// Otherwise, prompt the user interactively for a value.