/
stack.go
1159 lines (1022 loc) 路 36.4 KB
/
stack.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-2020, 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.
// Package auto contains the Pulumi Automation API, the programmatic interface for driving Pulumi programs
// without the CLI.
// Generally this can be thought of as encapsulating the functionality of the CLI (`pulumi up`, `pulumi preview`,
// pulumi destroy`, `pulumi stack init`, etc.) but with more flexibility. This still requires a
// CLI binary to be installed and available on your $PATH.
//
// In addition to fine-grained building blocks, Automation API provides three out of the box ways to work with Stacks:
//
// 1. Programs locally available on-disk and addressed via a filepath (NewStackLocalSource)
// stack, err := NewStackLocalSource(ctx, "myOrg/myProj/myStack", filepath.Join("..", "path", "to", "project"))
//
// 2. Programs fetched from a Git URL (NewStackRemoteSource)
// stack, err := NewStackRemoteSource(ctx, "myOrg/myProj/myStack", GitRepo{
// URL: "https://github.com/pulumi/test-repo.git",
// ProjectPath: filepath.Join("project", "path", "repo", "root", "relative"),
// })
//
// 3. Programs defined as a function alongside your Automation API code (NewStackInlineSource)
// stack, err := NewStackInlineSource(ctx, "myOrg/myProj/myStack", func(pCtx *pulumi.Context) error {
// bucket, err := s3.NewBucket(pCtx, "bucket", nil)
// if err != nil {
// return err
// }
// pCtx.Export("bucketName", bucket.Bucket)
// return nil
// })
//
// Each of these creates a stack with access to the full range of Pulumi lifecycle methods
// (up/preview/refresh/destroy), as well as methods for managing config, stack, and project settings.
//
// err := stack.SetConfig(ctx, "key", ConfigValue{ Value: "value", Secret: true })
// preRes, err := stack.Preview(ctx)
// // detailed info about results
// fmt.Println(preRes.prev.Steps[0].URN)
//
// The Automation API provides a natural way to orchestrate multiple stacks,
// feeding the output of one stack as an input to the next as shown in the package-level example below.
// The package can be used for a number of use cases:
//
// - Driving pulumi deployments within CI/CD workflows
//
// - Integration testing
//
// - Multi-stage deployments such as blue-green deployment patterns
//
// - Deployments involving application code like database migrations
//
// - Building higher level tools, custom CLIs over pulumi, etc
//
// - Using pulumi behind a REST or GRPC API
//
// - Debugging Pulumi programs (by using a single main entrypoint with "inline" programs)
//
// To enable a broad range of runtime customization the API defines a `Workspace` interface.
// A Workspace is the execution context containing a single Pulumi project, a program, and multiple stacks.
// Workspaces are used to manage the execution environment, providing various utilities such as plugin
// installation, environment configuration ($PULUMI_HOME), and creation, deletion, and listing of Stacks.
// Every Stack including those in the above examples are backed by a Workspace which can be accessed via:
//
// w = stack.Workspace()
// err := w.InstallPlugin("aws", "v3.2.0")
//
// Workspaces can be explicitly created and customized beyond the three Stack creation helpers noted above:
//
// w, err := NewLocalWorkspace(ctx, WorkDir(filepath.Join(".", "project", "path"), PulumiHome("~/.pulumi"))
// s := NewStack(ctx, "org/proj/stack", w)
//
// A default implementation of workspace is provided as `LocalWorkspace`. This implementation relies on Pulumi.yaml
// and Pulumi.<stack>.yaml as the intermediate format for Project and Stack settings. Modifying ProjectSettings will
// alter the Workspace Pulumi.yaml file, and setting config on a Stack will modify the Pulumi.<stack>.yaml file.
// This is identical to the behavior of Pulumi CLI driven workspaces. Custom Workspace
// implementations can be used to store Project and Stack settings as well as Config in a different format,
// such as an in-memory data structure, a shared persistent SQL database, or cloud object storage. Regardless of
// the backing Workspace implementation, the Pulumi SaaS Console will still be able to display configuration
// applied to updates as it does with the local version of the Workspace today.
//
// The Automation API also provides error handling utilities to detect common cases such as concurrent update
// conflicts:
//
// uRes, err :=stack.Up(ctx)
// if err != nil && IsConcurrentUpdateError(err) { /* retry logic here */ }
package auto
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
pbempty "github.com/golang/protobuf/ptypes/empty"
"github.com/nxadm/tail"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/pulumi/pulumi/sdk/v3/go/auto/debug"
"github.com/pulumi/pulumi/sdk/v3/go/auto/events"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optdestroy"
"github.com/pulumi/pulumi/sdk/v3/go/auto/opthistory"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optpreview"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optrefresh"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optup"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/pulumi/pulumi/sdk/v3/go/common/constant"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/rpcutil"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
)
// Stack is an isolated, independently configurable instance of a Pulumi program.
// Stack exposes methods for the full pulumi lifecycle (up/preview/refresh/destroy), as well as managing configuration.
// Multiple Stacks are commonly used to denote different phases of development
// (such as development, staging and production) or feature branches (such as feature-x-dev, jane-feature-x-dev).
type Stack struct {
workspace Workspace
stackName string
}
// FullyQualifiedStackName returns a stack name formatted with the greatest possible specificity:
// org/project/stack or user/project/stack
// Using this format avoids ambiguity in stack identity guards creating or selecting the wrong stack.
// Note that filestate backends (local file, S3, Azure Blob) do not support stack names in this
// format, and instead only use the stack name without an org/user or project to qualify it.
// See: https://github.com/pulumi/pulumi/issues/2522
func FullyQualifiedStackName(org, project, stack string) string {
return fmt.Sprintf("%s/%s/%s", org, project, stack)
}
// NewStack creates a new stack using the given workspace, and stack name.
// It fails if a stack with that name already exists
func NewStack(ctx context.Context, stackName string, ws Workspace) (Stack, error) {
var s Stack
s = Stack{
workspace: ws,
stackName: stackName,
}
err := ws.CreateStack(ctx, stackName)
if err != nil {
return s, err
}
return s, nil
}
// SelectStack selects stack using the given workspace, and stack name.
// It returns an error if the given Stack does not exist.
func SelectStack(ctx context.Context, stackName string, ws Workspace) (Stack, error) {
var s Stack
s = Stack{
workspace: ws,
stackName: stackName,
}
err := ws.SelectStack(ctx, stackName)
if err != nil {
return s, err
}
return s, nil
}
// UpsertStack tries to create a new stack using the given workspace and
// stack name if the stack does not already exist,
// or falls back to selecting the existing stack. If the stack does not exist,
// it will be created and selected.
func UpsertStack(ctx context.Context, stackName string, ws Workspace) (Stack, error) {
s, err := NewStack(ctx, stackName, ws)
// error for all failures except if the stack already exists, as we'll
// just select the stack if it exists.
if err != nil && !IsCreateStack409Error(err) {
return s, err
}
err = ws.SelectStack(ctx, stackName)
if err != nil {
return s, err
}
return s, nil
}
// Name returns the stack name
func (s *Stack) Name() string {
return s.stackName
}
// Workspace returns the underlying Workspace backing the Stack.
// This handles state associated with the Project and child Stacks including
// settings, configuration, and environment.
func (s *Stack) Workspace() Workspace {
return s.workspace
}
// Preview preforms a dry-run update to a stack, returning pending changes.
// https://www.pulumi.com/docs/reference/cli/pulumi_preview/
func (s *Stack) Preview(ctx context.Context, opts ...optpreview.Option) (PreviewResult, error) {
var res PreviewResult
preOpts := &optpreview.Options{}
for _, o := range opts {
o.ApplyOption(preOpts)
}
var sharedArgs []string
sharedArgs = debug.AddArgs(&preOpts.DebugLogOpts, sharedArgs)
if preOpts.Message != "" {
sharedArgs = append(sharedArgs, fmt.Sprintf("--message=%q", preOpts.Message))
}
if preOpts.ExpectNoChanges {
sharedArgs = append(sharedArgs, "--expect-no-changes")
}
if preOpts.Diff {
sharedArgs = append(sharedArgs, "--diff")
}
for _, rURN := range preOpts.Replace {
sharedArgs = append(sharedArgs, fmt.Sprintf("--replace=%s", rURN))
}
for _, tURN := range preOpts.Target {
sharedArgs = append(sharedArgs, fmt.Sprintf("--target=%s", tURN))
}
for _, pack := range preOpts.PolicyPacks {
sharedArgs = append(sharedArgs, fmt.Sprintf("--policy-pack=%s", pack))
}
for _, packConfig := range preOpts.PolicyPackConfigs {
sharedArgs = append(sharedArgs, fmt.Sprintf("--policy-pack-config=%s", packConfig))
}
if preOpts.TargetDependents {
sharedArgs = append(sharedArgs, "--target-dependents")
}
if preOpts.Parallel > 0 {
sharedArgs = append(sharedArgs, fmt.Sprintf("--parallel=%d", preOpts.Parallel))
}
if preOpts.UserAgent != "" {
sharedArgs = append(sharedArgs, fmt.Sprintf("--exec-agent=%s", preOpts.UserAgent))
}
if preOpts.Color != "" {
sharedArgs = append(sharedArgs, fmt.Sprintf("--color=%s", preOpts.Color))
}
if preOpts.Plan != "" {
sharedArgs = append(sharedArgs, fmt.Sprintf("--save-plan=%s", preOpts.Plan))
}
kind, args := constant.ExecKindAutoLocal, []string{"preview"}
if program := s.Workspace().Program(); program != nil {
server, err := startLanguageRuntimeServer(program)
if err != nil {
return res, err
}
defer contract.IgnoreClose(server)
kind, args = constant.ExecKindAutoInline, append(args, "--client="+server.address)
}
args = append(args, fmt.Sprintf("--exec-kind=%s", kind))
args = append(args, sharedArgs...)
var summaryEvents []apitype.SummaryEvent
eventChannel := make(chan events.EngineEvent)
eventsDone := make(chan bool)
go func() {
for {
event, ok := <-eventChannel
if !ok {
close(eventsDone)
return
}
if event.SummaryEvent != nil {
summaryEvents = append(summaryEvents, *event.SummaryEvent)
}
}
}()
eventChannels := []chan<- events.EngineEvent{eventChannel}
eventChannels = append(eventChannels, preOpts.EventStreams...)
t, err := tailLogs("preview", eventChannels)
if err != nil {
return res, errors.Wrap(err, "failed to tail logs")
}
defer t.Close()
args = append(args, "--event-log", t.Filename)
stdout, stderr, code, err := s.runPulumiCmdSync(
ctx,
preOpts.ProgressStreams, /* additionalOutput */
preOpts.ErrorProgressStreams, /* additionalErrorOutput */
args...,
)
if err != nil {
return res, newAutoError(errors.Wrap(err, "failed to run preview"), stdout, stderr, code)
}
// Close the file watcher wait for all events to send
t.Close()
<-eventsDone
if len(summaryEvents) == 0 {
return res, newAutoError(errors.New("failed to get preview summary"), stdout, stderr, code)
}
if len(summaryEvents) > 1 {
return res, newAutoError(errors.New("got multiple preview summaries"), stdout, stderr, code)
}
res.StdOut = stdout
res.StdErr = stderr
res.ChangeSummary = summaryEvents[0].ResourceChanges
return res, nil
}
// Up creates or updates the resources in a stack by executing the program in the Workspace.
// https://www.pulumi.com/docs/reference/cli/pulumi_up/
func (s *Stack) Up(ctx context.Context, opts ...optup.Option) (UpResult, error) {
var res UpResult
upOpts := &optup.Options{}
for _, o := range opts {
o.ApplyOption(upOpts)
}
var sharedArgs []string
sharedArgs = debug.AddArgs(&upOpts.DebugLogOpts, sharedArgs)
if upOpts.Message != "" {
sharedArgs = append(sharedArgs, fmt.Sprintf("--message=%q", upOpts.Message))
}
if upOpts.ExpectNoChanges {
sharedArgs = append(sharedArgs, "--expect-no-changes")
}
if upOpts.Diff {
sharedArgs = append(sharedArgs, "--diff")
}
for _, rURN := range upOpts.Replace {
sharedArgs = append(sharedArgs, fmt.Sprintf("--replace=%s", rURN))
}
for _, tURN := range upOpts.Target {
sharedArgs = append(sharedArgs, fmt.Sprintf("--target=%s", tURN))
}
for _, pack := range upOpts.PolicyPacks {
sharedArgs = append(sharedArgs, fmt.Sprintf("--policy-pack=%s", pack))
}
for _, packConfig := range upOpts.PolicyPackConfigs {
sharedArgs = append(sharedArgs, fmt.Sprintf("--policy-pack-config=%s", packConfig))
}
if upOpts.TargetDependents {
sharedArgs = append(sharedArgs, "--target-dependents")
}
if upOpts.Parallel > 0 {
sharedArgs = append(sharedArgs, fmt.Sprintf("--parallel=%d", upOpts.Parallel))
}
if upOpts.UserAgent != "" {
sharedArgs = append(sharedArgs, fmt.Sprintf("--exec-agent=%s", upOpts.UserAgent))
}
if upOpts.Color != "" {
sharedArgs = append(sharedArgs, fmt.Sprintf("--color=%s", upOpts.Color))
}
if upOpts.Plan != "" {
sharedArgs = append(sharedArgs, fmt.Sprintf("--plan=%s", upOpts.Plan))
}
kind, args := constant.ExecKindAutoLocal, []string{"up", "--yes", "--skip-preview"}
if program := s.Workspace().Program(); program != nil {
server, err := startLanguageRuntimeServer(program)
if err != nil {
return res, err
}
defer contract.IgnoreClose(server)
kind, args = constant.ExecKindAutoInline, append(args, "--client="+server.address)
}
args = append(args, fmt.Sprintf("--exec-kind=%s", kind))
if len(upOpts.EventStreams) > 0 {
eventChannels := upOpts.EventStreams
t, err := tailLogs("up", eventChannels)
if err != nil {
return res, errors.Wrap(err, "failed to tail logs")
}
defer t.Close()
args = append(args, "--event-log", t.Filename)
}
args = append(args, sharedArgs...)
stdout, stderr, code, err := s.runPulumiCmdSync(ctx, upOpts.ProgressStreams, upOpts.ErrorProgressStreams, args...)
if err != nil {
return res, newAutoError(errors.Wrap(err, "failed to run update"), stdout, stderr, code)
}
outs, err := s.Outputs(ctx)
if err != nil {
return res, err
}
historyOpts := []opthistory.Option{}
if upOpts.ShowSecrets != nil {
historyOpts = append(historyOpts, opthistory.ShowSecrets(*upOpts.ShowSecrets))
}
history, err := s.History(ctx, 1 /*pageSize*/, 1 /*page*/, historyOpts...)
if err != nil {
return res, err
}
res = UpResult{
Outputs: outs,
StdOut: stdout,
StdErr: stderr,
}
if len(history) > 0 {
res.Summary = history[0]
}
return res, nil
}
// Refresh compares the current stack鈥檚 resource state with the state known to exist in the actual
// cloud provider. Any such changes are adopted into the current stack.
func (s *Stack) Refresh(ctx context.Context, opts ...optrefresh.Option) (RefreshResult, error) {
var res RefreshResult
refreshOpts := &optrefresh.Options{}
for _, o := range opts {
o.ApplyOption(refreshOpts)
}
var args []string
args = debug.AddArgs(&refreshOpts.DebugLogOpts, args)
args = append(args, "refresh", "--yes", "--skip-preview")
if refreshOpts.Message != "" {
args = append(args, fmt.Sprintf("--message=%q", refreshOpts.Message))
}
if refreshOpts.ExpectNoChanges {
args = append(args, "--expect-no-changes")
}
for _, tURN := range refreshOpts.Target {
args = append(args, fmt.Sprintf("--target=%s", tURN))
}
if refreshOpts.Parallel > 0 {
args = append(args, fmt.Sprintf("--parallel=%d", refreshOpts.Parallel))
}
if refreshOpts.UserAgent != "" {
args = append(args, fmt.Sprintf("--exec-agent=%s", refreshOpts.UserAgent))
}
if refreshOpts.Color != "" {
args = append(args, fmt.Sprintf("--color=%s", refreshOpts.Color))
}
execKind := constant.ExecKindAutoLocal
if s.Workspace().Program() != nil {
execKind = constant.ExecKindAutoInline
}
args = append(args, fmt.Sprintf("--exec-kind=%s", execKind))
if len(refreshOpts.EventStreams) > 0 {
eventChannels := refreshOpts.EventStreams
t, err := tailLogs("refresh", eventChannels)
if err != nil {
return res, errors.Wrap(err, "failed to tail logs")
}
defer t.Close()
args = append(args, "--event-log", t.Filename)
}
stdout, stderr, code, err := s.runPulumiCmdSync(
ctx,
refreshOpts.ProgressStreams, /* additionalOutputs */
refreshOpts.ErrorProgressStreams, /* additionalErrorOutputs */
args...,
)
if err != nil {
return res, newAutoError(errors.Wrap(err, "failed to refresh stack"), stdout, stderr, code)
}
historyOpts := []opthistory.Option{}
if showSecrets := refreshOpts.ShowSecrets; showSecrets != nil {
historyOpts = append(historyOpts, opthistory.ShowSecrets(*showSecrets))
}
history, err := s.History(ctx, 1 /*pageSize*/, 1 /*page*/, historyOpts...)
if err != nil {
return res, errors.Wrap(err, "failed to refresh stack")
}
var summary UpdateSummary
if len(history) > 0 {
summary = history[0]
}
res = RefreshResult{
Summary: summary,
StdOut: stdout,
StdErr: stderr,
}
return res, nil
}
// Destroy deletes all resources in a stack, leaving all history and configuration intact.
func (s *Stack) Destroy(ctx context.Context, opts ...optdestroy.Option) (DestroyResult, error) {
var res DestroyResult
destroyOpts := &optdestroy.Options{}
for _, o := range opts {
o.ApplyOption(destroyOpts)
}
var args []string
args = debug.AddArgs(&destroyOpts.DebugLogOpts, args)
args = append(args, "destroy", "--yes", "--skip-preview")
if destroyOpts.Message != "" {
args = append(args, fmt.Sprintf("--message=%q", destroyOpts.Message))
}
for _, tURN := range destroyOpts.Target {
args = append(args, fmt.Sprintf("--target=%s", tURN))
}
if destroyOpts.TargetDependents {
args = append(args, "--target-dependents")
}
if destroyOpts.Parallel > 0 {
args = append(args, fmt.Sprintf("--parallel=%d", destroyOpts.Parallel))
}
if destroyOpts.UserAgent != "" {
args = append(args, fmt.Sprintf("--exec-agent=%s", destroyOpts.UserAgent))
}
if destroyOpts.Color != "" {
args = append(args, fmt.Sprintf("--color=%s", destroyOpts.Color))
}
execKind := constant.ExecKindAutoLocal
if s.Workspace().Program() != nil {
execKind = constant.ExecKindAutoInline
}
args = append(args, fmt.Sprintf("--exec-kind=%s", execKind))
if len(destroyOpts.EventStreams) > 0 {
eventChannels := destroyOpts.EventStreams
t, err := tailLogs("destroy", eventChannels)
if err != nil {
return res, errors.Wrap(err, "failed to tail logs")
}
defer t.Close()
args = append(args, "--event-log", t.Filename)
}
stdout, stderr, code, err := s.runPulumiCmdSync(
ctx,
destroyOpts.ProgressStreams, /* additionalOutputs */
destroyOpts.ErrorProgressStreams, /* additionalErrorOutputs */
args...,
)
if err != nil {
return res, newAutoError(errors.Wrap(err, "failed to destroy stack"), stdout, stderr, code)
}
historyOpts := []opthistory.Option{}
if showSecrets := destroyOpts.ShowSecrets; showSecrets != nil {
historyOpts = append(historyOpts, opthistory.ShowSecrets(*showSecrets))
}
history, err := s.History(ctx, 1 /*pageSize*/, 1 /*page*/, historyOpts...)
if err != nil {
return res, errors.Wrap(err, "failed to destroy stack")
}
var summary UpdateSummary
if len(history) > 0 {
summary = history[0]
}
res = DestroyResult{
Summary: summary,
StdOut: stdout,
StdErr: stderr,
}
return res, nil
}
// Outputs get the current set of Stack outputs from the last Stack.Up().
func (s *Stack) Outputs(ctx context.Context) (OutputMap, error) {
return s.Workspace().StackOutputs(ctx, s.Name())
}
// History returns a list summarizing all previous and current results from Stack lifecycle operations
// (up/preview/refresh/destroy).
func (s *Stack) History(ctx context.Context,
pageSize int, page int, opts ...opthistory.Option) ([]UpdateSummary, error) {
err := s.Workspace().SelectStack(ctx, s.Name())
if err != nil {
return nil, errors.Wrap(err, "failed to get stack history")
}
var options opthistory.Options
for _, opt := range opts {
opt.ApplyOption(&options)
}
showSecrets := true
if options.ShowSecrets != nil {
showSecrets = *options.ShowSecrets
}
args := []string{"stack", "history", "--json"}
if showSecrets {
args = append(args, "--show-secrets")
}
if pageSize > 0 {
// default page=1 if unset when pageSize is set
if page < 1 {
page = 1
}
args = append(args, "--page-size", fmt.Sprintf("%d", pageSize), "--page", fmt.Sprintf("%d", page))
}
stdout, stderr, errCode, err := s.runPulumiCmdSync(
ctx,
nil, /* additionalOutputs */
nil, /* additionalErrorOutputs */
args...,
)
if err != nil {
return nil, newAutoError(errors.Wrap(err, "failed to get stack history"), stdout, stderr, errCode)
}
var history []UpdateSummary
err = json.Unmarshal([]byte(stdout), &history)
if err != nil {
return nil, errors.Wrap(err, "unable to unmarshal history result")
}
return history, nil
}
// GetConfig returns the config value associated with the specified key.
func (s *Stack) GetConfig(ctx context.Context, key string) (ConfigValue, error) {
return s.Workspace().GetConfig(ctx, s.Name(), key)
}
// GetAllConfig returns the full config map.
func (s *Stack) GetAllConfig(ctx context.Context) (ConfigMap, error) {
return s.Workspace().GetAllConfig(ctx, s.Name())
}
// SetConfig sets the specified config key-value pair.
func (s *Stack) SetConfig(ctx context.Context, key string, val ConfigValue) error {
return s.Workspace().SetConfig(ctx, s.Name(), key, val)
}
// SetAllConfig sets all values in the provided config map.
func (s *Stack) SetAllConfig(ctx context.Context, config ConfigMap) error {
return s.Workspace().SetAllConfig(ctx, s.Name(), config)
}
// RemoveConfig removes the specified config key-value pair.
func (s *Stack) RemoveConfig(ctx context.Context, key string) error {
return s.Workspace().RemoveConfig(ctx, s.Name(), key)
}
// RemoveAllConfig removes all values in the provided list of keys.
func (s *Stack) RemoveAllConfig(ctx context.Context, keys []string) error {
return s.Workspace().RemoveAllConfig(ctx, s.Name(), keys)
}
// RefreshConfig gets and sets the config map used with the last Update.
func (s *Stack) RefreshConfig(ctx context.Context) (ConfigMap, error) {
return s.Workspace().RefreshConfig(ctx, s.Name())
}
// Info returns a summary of the Stack including its URL.
func (s *Stack) Info(ctx context.Context) (StackSummary, error) {
var info StackSummary
err := s.Workspace().SelectStack(ctx, s.Name())
if err != nil {
return info, errors.Wrap(err, "failed to fetch stack info")
}
summary, err := s.Workspace().Stack(ctx)
if err != nil {
return info, errors.Wrap(err, "failed to fetch stack info")
}
if summary != nil {
info = *summary
}
return info, nil
}
// Cancel stops a stack's currently running update. It returns an error if no update is currently running.
// Note that this operation is _very dangerous_, and may leave the stack in an inconsistent state
// if a resource operation was pending when the update was canceled.
// This command is not supported for local backends.
func (s *Stack) Cancel(ctx context.Context) error {
stdout, stderr, errCode, err := s.runPulumiCmdSync(
ctx,
nil, /* additionalOutput */
nil, /* additionalErrorOutput */
"cancel", "--yes")
if err != nil {
return newAutoError(errors.Wrap(err, "failed to cancel update"), stdout, stderr, errCode)
}
return nil
}
// Export exports the deployment state of the stack.
// This can be combined with Stack.Import to edit a stack's state (such as recovery from failed deployments).
func (s *Stack) Export(ctx context.Context) (apitype.UntypedDeployment, error) {
return s.Workspace().ExportStack(ctx, s.Name())
}
// Import imports the specified deployment state into the stack.
// This can be combined with Stack.Export to edit a stack's state (such as recovery from failed deployments).
func (s *Stack) Import(ctx context.Context, state apitype.UntypedDeployment) error {
return s.Workspace().ImportStack(ctx, s.Name(), state)
}
// UpdateSummary provides a summary of a Stack lifecycle operation (up/preview/refresh/destroy).
type UpdateSummary struct {
Version int `json:"version"`
Kind string `json:"kind"`
StartTime string `json:"startTime"`
Message string `json:"message"`
Environment map[string]string `json:"environment"`
Config ConfigMap `json:"config"`
Result string `json:"result,omitempty"`
// These values are only present once the update finishes
EndTime *string `json:"endTime,omitempty"`
ResourceChanges *map[string]int `json:"resourceChanges,omitempty"`
}
// OutputValue models a Pulumi Stack output, providing the plaintext value and a boolean indicating secretness.
type OutputValue struct {
Value interface{}
Secret bool
}
// UpResult contains information about a Stack.Up operation,
// including Outputs, and a summary of the deployed changes.
type UpResult struct {
StdOut string
StdErr string
Outputs OutputMap
Summary UpdateSummary
}
// GetPermalink returns the permalink URL in the Pulumi Console for the update operation.
func (ur *UpResult) GetPermalink() (string, error) {
return GetPermalink(ur.StdOut)
}
// ErrParsePermalinkFailed occurs when the the generated permalink URL can't be found in the op result
var ErrParsePermalinkFailed = errors.New("failed to get permalink")
// GetPermalink returns the permalink URL in the Pulumi Console for the update
// or refresh operation. This will error for alternate, local backends.
func GetPermalink(stdout string) (string, error) {
const permalinkSearchStr = "View Live: |Permalink: "
startRegex := regexp.MustCompile(permalinkSearchStr)
endRegex := regexp.MustCompile("\n")
// Find the start of the permalink in the output.
start := startRegex.FindStringIndex(stdout)
if start == nil {
return "", ErrParsePermalinkFailed
}
permalinkStart := stdout[start[1]:]
// Find the end of the permalink.
end := endRegex.FindStringIndex(permalinkStart)
if end == nil {
return "", ErrParsePermalinkFailed
}
permalink := permalinkStart[:end[1]-1]
return permalink, nil
}
// OutputMap is the output result of running a Pulumi program
type OutputMap map[string]OutputValue
// PreviewStep is a summary of the expected state transition of a given resource based on running the current program.
type PreviewStep struct {
// Op is the kind of operation being performed.
Op string `json:"op"`
// URN is the resource being affected by this operation.
URN resource.URN `json:"urn"`
// Provider is the provider that will perform this step.
Provider string `json:"provider,omitempty"`
// OldState is the old state for this resource, if appropriate given the operation type.
OldState *apitype.ResourceV3 `json:"oldState,omitempty"`
// NewState is the new state for this resource, if appropriate given the operation type.
NewState *apitype.ResourceV3 `json:"newState,omitempty"`
// DiffReasons is a list of keys that are causing a diff (for updating steps only).
DiffReasons []resource.PropertyKey `json:"diffReasons,omitempty"`
// ReplaceReasons is a list of keys that are causing replacement (for replacement steps only).
ReplaceReasons []resource.PropertyKey `json:"replaceReasons,omitempty"`
// DetailedDiff is a structured diff that indicates precise per-property differences.
DetailedDiff map[string]PropertyDiff `json:"detailedDiff"`
}
// PropertyDiff contains information about the difference in a single property value.
type PropertyDiff struct {
// Kind is the kind of difference.
Kind string `json:"kind"`
// InputDiff is true if this is a difference between old and new inputs instead of old state and new inputs.
InputDiff bool `json:"inputDiff"`
}
// PreviewResult is the output of Stack.Preview() describing the expected set of changes from the next Stack.Up()
type PreviewResult struct {
StdOut string
StdErr string
ChangeSummary map[apitype.OpType]int
}
// GetPermalink returns the permalink URL in the Pulumi Console for the preview operation.
func (pr *PreviewResult) GetPermalink() (string, error) {
return GetPermalink(pr.StdOut)
}
// RefreshResult is the output of a successful Stack.Refresh operation
type RefreshResult struct {
StdOut string
StdErr string
Summary UpdateSummary
}
// GetPermalink returns the permalink URL in the Pulumi Console for the refresh operation.
func (rr *RefreshResult) GetPermalink() (string, error) {
return GetPermalink(rr.StdOut)
}
// DestroyResult is the output of a successful Stack.Destroy operation
type DestroyResult struct {
StdOut string
StdErr string
Summary UpdateSummary
}
// GetPermalink returns the permalink URL in the Pulumi Console for the destroy operation.
func (dr *DestroyResult) GetPermalink() (string, error) {
return GetPermalink(dr.StdOut)
}
// secretSentinel represents the CLI response for an output marked as "secret"
const secretSentinel = "[secret]"
func (s *Stack) runPulumiCmdSync(
ctx context.Context,
additionalOutput []io.Writer,
additionalErrorOutput []io.Writer,
args ...string,
) (string, string, int, error) {
var env []string
debugEnv := fmt.Sprintf("%s=%s", "PULUMI_DEBUG_COMMANDS", "true")
env = append(env, debugEnv)
if s.Workspace().PulumiHome() != "" {
homeEnv := fmt.Sprintf("%s=%s", pulumiHomeEnv, s.Workspace().PulumiHome())
env = append(env, homeEnv)
}
if envvars := s.Workspace().GetEnvVars(); envvars != nil {
for k, v := range envvars {
e := []string{k, v}
env = append(env, strings.Join(e, "="))
}
}
additionalArgs, err := s.Workspace().SerializeArgsForOp(ctx, s.Name())
if err != nil {
return "", "", -1, errors.Wrap(err, "failed to exec command, error getting additional args")
}
args = append(args, additionalArgs...)
args = append(args, "--stack", s.Name())
stdout, stderr, errCode, err := runPulumiCommandSync(
ctx,
s.Workspace().WorkDir(),
additionalOutput,
additionalErrorOutput,
env,
args...,
)
if err != nil {
return stdout, stderr, errCode, err
}
err = s.Workspace().PostCommandCallback(ctx, s.Name())
if err != nil {
return stdout, stderr, errCode, errors.Wrap(err, "command ran successfully, but error running PostCommandCallback")
}
return stdout, stderr, errCode, nil
}
const (
stateWaiting = iota
stateRunning
stateCanceled
stateFinished
)
type languageRuntimeServer struct {
m sync.Mutex
c *sync.Cond
fn pulumi.RunFunc
address string
state int
cancel chan bool
done chan error
}
// isNestedInvocation returns true if pulumi.RunWithContext is on the stack.
func isNestedInvocation() bool {
depth, callers := 0, make([]uintptr, 32)
for {
n := runtime.Callers(depth, callers)
if n == 0 {
return false
}
depth += n
frames := runtime.CallersFrames(callers)
for f, more := frames.Next(); more; f, more = frames.Next() {
if f.Function == "github.com/pulumi/pulumi/sdk/v3/go/pulumi.RunWithContext" {
return true
}
}
}
}
func startLanguageRuntimeServer(fn pulumi.RunFunc) (*languageRuntimeServer, error) {
if isNestedInvocation() {
return nil, errors.New("nested stack operations are not supported https://github.com/pulumi/pulumi/issues/5058")
}
s := &languageRuntimeServer{
fn: fn,
cancel: make(chan bool),
}
s.c = sync.NewCond(&s.m)
port, done, err := rpcutil.Serve(0, s.cancel, []func(*grpc.Server) error{
func(srv *grpc.Server) error {
pulumirpc.RegisterLanguageRuntimeServer(srv, s)
return nil
},
}, nil)
if err != nil {
return nil, err
}
s.address, s.done = fmt.Sprintf("127.0.0.1:%d", port), done
return s, nil
}
func (s *languageRuntimeServer) Close() error {
s.m.Lock()
switch s.state {
case stateCanceled:
s.m.Unlock()
return nil
case stateWaiting:
// Not started yet; go ahead and cancel
default:
for s.state != stateFinished {
s.c.Wait()
}
}
s.state = stateCanceled
s.m.Unlock()
s.cancel <- true
close(s.cancel)
return <-s.done
}
func (s *languageRuntimeServer) GetRequiredPlugins(ctx context.Context,
req *pulumirpc.GetRequiredPluginsRequest) (*pulumirpc.GetRequiredPluginsResponse, error) {
return &pulumirpc.GetRequiredPluginsResponse{}, nil
}