/
backend.go
1376 lines (1170 loc) 路 41.8 KB
/
backend.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-2023, 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 diy
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/gofrs/uuid"
user "github.com/tweekmonster/luser"
"gocloud.dev/blob"
_ "gocloud.dev/blob/azureblob" // driver for azblob://
_ "gocloud.dev/blob/fileblob" // driver for file://
"gocloud.dev/blob/gcsblob" // driver for gs://
_ "gocloud.dev/blob/s3blob" // driver for s3://
"gocloud.dev/gcerrors"
"github.com/pulumi/pulumi/pkg/v3/authhelpers"
"github.com/pulumi/pulumi/pkg/v3/backend"
"github.com/pulumi/pulumi/pkg/v3/backend/display"
sdkDisplay "github.com/pulumi/pulumi/pkg/v3/display"
"github.com/pulumi/pulumi/pkg/v3/engine"
"github.com/pulumi/pulumi/pkg/v3/operations"
"github.com/pulumi/pulumi/pkg/v3/resource/deploy"
"github.com/pulumi/pulumi/pkg/v3/resource/edit"
"github.com/pulumi/pulumi/pkg/v3/resource/stack"
"github.com/pulumi/pulumi/pkg/v3/secrets"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors"
"github.com/pulumi/pulumi/sdk/v3/go/common/encoding"
"github.com/pulumi/pulumi/sdk/v3/go/common/env"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
"github.com/pulumi/pulumi/sdk/v3/go/common/slice"
"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/result"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)
// UpgradeOptions customizes the behavior of the upgrade operation.
type UpgradeOptions struct {
// ProjectsForDetachedStacks is an optional function that is able to
// backfill project names for stacks that have no project specified otherwise.
//
// It is called with a list of stack names that have no project specified.
// It should return a list of project names to use for each stack name
// in the same order.
// If a returned name is blank, the stack at that position will be skipped
// in the upgrade process.
//
// The length of 'projects' MUST match the length of 'stacks'.
// If it does not, the upgrade will panic.
//
// If this function is not specified,
// stacks without projects will be skipped during the upgrade.
ProjectsForDetachedStacks func(stacks []tokens.StackName) (projects []tokens.Name, err error)
}
// Backend extends the base backend interface with specific information about diy backends.
type Backend interface {
backend.Backend
diy() // at the moment, no diy specific info, so just use a marker function.
// Upgrade to the latest state store version.
Upgrade(ctx context.Context, opts *UpgradeOptions) error
}
type diyBackend struct {
d diag.Sink
// originalURL is the URL provided when the diyBackend was initialized, for example
// "file://~". url is a canonicalized version that should be used when persisting data.
// (For example, replacing ~ with the home directory, making an absolute path, etc.)
originalURL string
url string
bucket Bucket
mutex sync.Mutex
lockID string
gzip bool
Env env.Env
// The current project, if any.
currentProject atomic.Pointer[workspace.Project]
// The store controls the layout of stacks in the backend.
// We use different layouts based on the version of the backend
// specified in the metadata file.
// If the metadata file is missing, we use the legacy layout.
store referenceStore
}
type diyBackendReference struct {
name tokens.StackName
project tokens.Name
// A thread-safe way to get the current project.
// The function reference or the pointer returned by the function may be nil.
currentProject func() *workspace.Project
// referenceStore that created this reference.
//
// This is necessary because
// the referenceStore for a backend may change over time,
// but the store for this reference should not.
store referenceStore
}
func (r *diyBackendReference) String() string {
// If project is blank this is a legacy non-project scoped stack reference, just return the name.
if r.project == "" {
return r.name.String()
}
// If the user has asked us to fully qualify names, we won't elide any
// information.
if cmdutil.FullyQualifyStackNames {
return fmt.Sprintf("organization/%s/%s", r.project, r.name)
}
if r.currentProject != nil {
proj := r.currentProject()
// For project scoped references when stringifying backend references,
// we take the current project (if present) into account.
// If the project names match, we can elide them.
if proj != nil && string(r.project) == string(proj.Name) {
return r.name.String()
}
}
// Else return a new style fully qualified reference.
return fmt.Sprintf("organization/%s/%s", r.project, r.name)
}
func (r *diyBackendReference) Name() tokens.StackName {
return r.name
}
func (r *diyBackendReference) Project() (tokens.Name, bool) {
return r.project, r.project != ""
}
func (r *diyBackendReference) FullyQualifiedName() tokens.QName {
if r.project == "" {
return r.name.Q()
}
return tokens.QName(fmt.Sprintf("organization/%s/%s", r.project, r.name))
}
// Helper methods that delegate to the underlying referenceStore.
func (r *diyBackendReference) Validate() error { return r.store.ValidateReference(r) }
func (r *diyBackendReference) StackBasePath() string { return r.store.StackBasePath(r) }
func (r *diyBackendReference) HistoryDir() string { return r.store.HistoryDir(r) }
func (r *diyBackendReference) BackupDir() string { return r.store.BackupDir(r) }
func IsDIYBackendURL(urlstr string) bool {
u, err := url.Parse(urlstr)
if err != nil {
return false
}
return blob.DefaultURLMux().ValidBucketScheme(u.Scheme)
}
const FilePathPrefix = "file://"
// New constructs a new diy backend,
// using the given URL as the root for storage.
// The URL must use one of the schemes supported by the go-cloud blob package.
// Thes inclue: file, s3, gs, azblob.
func New(ctx context.Context, d diag.Sink, originalURL string, project *workspace.Project) (Backend, error) {
return newDIYBackend(ctx, d, originalURL, project, nil)
}
type diyBackendOptions struct {
// Env specifies how to get environment variables.
//
// Defaults to env.Global
Env env.Env
}
// newDIYBackend builds a diy backend implementation
// with the given options.
func newDIYBackend(
ctx context.Context, d diag.Sink, originalURL string, project *workspace.Project,
opts *diyBackendOptions,
) (*diyBackend, error) {
if opts == nil {
opts = &diyBackendOptions{}
}
if opts.Env == nil {
opts.Env = env.Global()
}
if !IsDIYBackendURL(originalURL) {
return nil, fmt.Errorf("diy URL %s has an illegal prefix; expected one of: %s",
originalURL, strings.Join(blob.DefaultURLMux().BucketSchemes(), ", "))
}
u, err := massageBlobPath(originalURL)
if err != nil {
return nil, err
}
p, err := url.Parse(u)
if err != nil {
return nil, err
}
blobmux := blob.DefaultURLMux()
// for gcp we want to support additional credentials
// schemes on top of go-cloud's default credentials mux.
if p.Scheme == gcsblob.Scheme {
blobmux, err = authhelpers.GoogleCredentialsMux(ctx)
if err != nil {
return nil, err
}
}
bucket, err := blobmux.OpenBucket(ctx, u)
if err != nil {
return nil, fmt.Errorf("unable to open bucket %s: %w", u, err)
}
if !strings.HasPrefix(u, FilePathPrefix) {
bucketSubDir := strings.TrimLeft(p.Path, "/")
if bucketSubDir != "" {
if !strings.HasSuffix(bucketSubDir, "/") {
bucketSubDir += "/"
}
bucket = blob.PrefixedBucket(bucket, bucketSubDir)
}
}
// Allocate a unique lock ID for this backend instance.
lockID, err := uuid.NewV4()
if err != nil {
return nil, err
}
gzipCompression := opts.Env.GetBool(env.DIYBackendGzip)
wbucket := &wrappedBucket{bucket: bucket}
bucket = nil // prevent accidental use of unwrapped bucket
backend := &diyBackend{
d: d,
originalURL: originalURL,
url: u,
bucket: wbucket,
lockID: lockID.String(),
gzip: gzipCompression,
Env: opts.Env,
}
backend.currentProject.Store(project)
// Read the Pulumi state metadata
// and ensure that it is compatible with this version of the CLI.
// The version in the metadata file informs which store we use.
meta, err := ensurePulumiMeta(ctx, wbucket, opts.Env)
if err != nil {
return nil, err
}
// projectMode tracks whether the current state supports project-scoped stacks.
// Historically, the diy backend did not support this.
// To avoid breaking old stacks, we use legacy mode for existing states.
// We use project mode only if one of the following is true:
//
// - The state has a single .pulumi/meta.yaml file
// and the version is 1 or greater.
// - The state is entirely new
// so there's no risk of breaking old stacks.
//
// All actual logic of project mode vs legacy mode is handled by the referenceStore.
// This boolean just helps us warn users about unmigrated stacks.
var projectMode bool
switch meta.Version {
case 0:
backend.store = newLegacyReferenceStore(wbucket)
case 1:
backend.store = newProjectReferenceStore(wbucket, backend.currentProject.Load)
projectMode = true
default:
return nil, fmt.Errorf(
"state store unsupported: 'meta.yaml' version (%d) is not supported "+
"by this version of the Pulumi CLI", meta.Version)
}
// If we're not in project mode, or we've disabled the warning, we're done.
if !projectMode || opts.Env.GetBool(env.DIYBackendNoLegacyWarning) {
return backend, nil
}
// Otherwise, warn about any old stack files.
// This is possible if a user creates a new stack with a new CLI,
// or migrates it to project mode with `pulumi state upgrade`,
// but someone else interacts with the same state with an old CLI.
refs, err := newLegacyReferenceStore(wbucket).ListReferences(ctx)
if err != nil {
// If there's an error listing don't fail, just don't print the warnings
return backend, nil
}
if len(refs) == 0 {
return backend, nil
}
var msg strings.Builder
msg.WriteString("Found legacy stack files in state store:\n")
for _, ref := range refs {
fmt.Fprintf(&msg, " - %s\n", ref.Name())
}
msg.WriteString("Please run 'pulumi state upgrade' to migrate them to the new format.\n")
msg.WriteString("Set PULUMI_DIY_BACKEND_NO_LEGACY_WARNING=1 to disable this warning.")
d.Warningf(diag.Message("", msg.String()))
return backend, nil
}
func (b *diyBackend) Upgrade(ctx context.Context, opts *UpgradeOptions) error {
if opts == nil {
opts = &UpgradeOptions{}
}
// We don't use the existing b.store because
// this may already be a projectReferenceStore
// with new legacy files introduced to it accidentally.
olds, err := newLegacyReferenceStore(b.bucket).ListReferences(ctx)
if err != nil {
return fmt.Errorf("read old references: %w", err)
}
sort.Slice(olds, func(i, j int) bool {
return olds[i].Name().String() < olds[j].Name().String()
})
// There's no limit to the number of stacks we need to upgrade.
// We don't want to overload the system with too many concurrent upgrades.
// We'll run a fixed pool of goroutines to upgrade stacks.
pool := newWorkerPool(0 /* numWorkers */, len(olds) /* numTasks */)
defer pool.Close()
// Projects for each stack in `olds` in the same order.
// projects[i] is the project name for olds[i].
projects := make([]tokens.Name, len(olds))
for idx, old := range olds {
idx, old := idx, old
pool.Enqueue(func() error {
project, err := b.guessProject(ctx, old)
if err != nil {
return fmt.Errorf("guess stack %s project: %w", old.Name(), err)
}
// No lock necessary;
// projects is pre-allocated.
projects[idx] = project
return nil
})
}
if err := pool.Wait(); err != nil {
return err
}
// If there are any stacks without projects
// and the user provided a callback to fill them,
// use it to fill in the missing projects.
if opts.ProjectsForDetachedStacks != nil {
var (
// Names of stacks in 'olds' that don't have a project
detached []tokens.StackName
// reverseIdx[i] is the index of detached[i]
// in olds and projects.
//
// In other words:
//
// detached[i] == olds[reverseIdx[i]].Name()
// projects[reverseIdx[i]] == ""
reverseIdx []int
)
for i, ref := range olds {
if projects[i] == "" {
detached = append(detached, ref.Name())
reverseIdx = append(reverseIdx, i)
}
}
if len(detached) != 0 {
detachedProjects, err := opts.ProjectsForDetachedStacks(detached)
if err != nil {
return err
}
contract.Assertf(len(detached) == len(detachedProjects),
"ProjectsForDetachedStacks returned the wrong number of projects: "+
"expected %d, got %d", len(detached), len(detachedProjects))
for i, project := range detachedProjects {
projects[reverseIdx[i]] = project
}
}
}
// It's important that we attempt to write the new metadata file
// before we attempt the upgrade.
// This ensures that if permissions are borked for any reason,
// (e.g., we can write to .pulumi/*/*" but not ".pulumi/*.")
// we don't leave the bucket in a completely inaccessible state.
meta := pulumiMeta{Version: 1}
if err := meta.WriteTo(ctx, b.bucket); err != nil {
var s strings.Builder
fmt.Fprintf(&s, "Could not write new state metadata file: %v\n", err)
fmt.Fprintf(&s, "Please verify that the storage is writable, and try again.")
b.d.Errorf(diag.RawMessage("", s.String()))
return errors.New("state upgrade failed")
}
newStore := newProjectReferenceStore(b.bucket, b.currentProject.Load)
var upgraded atomic.Int64 // number of stacks successfully upgraded
for idx, old := range olds {
idx, old := idx, old
pool.Enqueue(func() error {
project := projects[idx]
if project == "" {
b.d.Warningf(diag.Message("", "Skipping stack %q: no project name found"), old)
return nil
}
if err := b.upgradeStack(ctx, newStore, project, old); err != nil {
b.d.Warningf(diag.Message("", "Skipping stack %q: %v"), old, err)
} else {
upgraded.Add(1)
}
return nil
})
}
// We log all errors above. This should never fail.
err = pool.Wait()
contract.AssertNoErrorf(err, "pool.Wait should never return an error")
b.store = newStore
b.d.Infoerrf(diag.Message("", "Upgraded %d stack(s) to project mode"), upgraded.Load())
return nil
}
// guessProject inspects the checkpoint for the given stack and attempts to
// guess the project name for it.
// Returns an empty string if the project name cannot be determined.
func (b *diyBackend) guessProject(ctx context.Context, old *diyBackendReference) (tokens.Name, error) {
contract.Requiref(old.project == "", "old.project", "must be empty")
chk, err := b.getCheckpoint(ctx, old)
if err != nil {
return "", fmt.Errorf("read checkpoint: %w", err)
}
// Try and find the project name from _any_ resource URN
if chk.Latest != nil {
for _, res := range chk.Latest.Resources {
return tokens.Name(res.URN.Project()), nil
}
}
return "", nil
}
// upgradeStack upgrades a single stack to use the provided projectReferenceStore.
func (b *diyBackend) upgradeStack(
ctx context.Context,
newStore *projectReferenceStore,
project tokens.Name,
old *diyBackendReference,
) error {
contract.Requiref(old.project == "", "old.project", "must be empty")
contract.Requiref(project != "", "project", "must not be empty")
new := newStore.newReference(project, old.Name())
if err := b.renameStack(ctx, old, new); err != nil {
return fmt.Errorf("rename to %v: %w", new, err)
}
return nil
}
// massageBlobPath takes the path the user provided and converts it to an appropriate form go-cloud
// can support. Importantly, s3/azblob/gs paths should not be be touched. This will only affect
// file:// paths which have a few oddities around them that we want to ensure work properly.
func massageBlobPath(path string) (string, error) {
if !strings.HasPrefix(path, FilePathPrefix) {
// Not a file:// path. Keep this untouched and pass directly to gocloud.
return path, nil
}
// We need to set no_tmp_dir to a value to avoid using the system temp directory.
// See also https://github.com/pulumi/pulumi/issues/15352
url, err := url.Parse(path)
if err != nil {
return "", fmt.Errorf("parsing the provided URL: %w", err)
}
query := url.Query()
if query.Get("no_tmp_dir") == "" {
query.Set("no_tmp_dir", "true")
} else if query.Get("no_tmp_dir") == "false" {
// If no_tmp_dir is set to false, we strip it out. The library will default to false if
// the parameter is not present, but will consider any value being set as true.
query.Del("no_tmp_dir")
}
queryString := ""
if len(query) > 0 {
queryString = "?" + query.Encode()
}
// Strip off the "file://" portion so we can examine and determine what to do with the rest.
path = strings.TrimPrefix(path, FilePathPrefix)
// Strip off the query parameter, since we're computing that separately.
path = strings.Split(path, "?")[0]
// We need to specially handle ~. The shell doesn't take care of this for us, and later
// functions we run into can't handle this either.
//
// From https://stackoverflow.com/questions/17609732/expand-tilde-to-home-directory
if strings.HasPrefix(path, "~") {
usr, err := user.Current()
if err != nil {
return "", fmt.Errorf("Could not determine current user to resolve `file://~` path.: %w", err)
}
if path == "~" {
path = usr.HomeDir
} else {
path = filepath.Join(usr.HomeDir, path[2:])
}
}
// For file:// backend, ensure a relative path is resolved. fileblob only supports absolute paths.
path, err = filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("An IO error occurred while building the absolute path: %w", err)
}
// Using example from https://godoc.org/gocloud.dev/blob/fileblob#example-package--OpenBucket
// On Windows, convert "\" to "/" and add a leading "/". (See https://gocloud.dev/howto/blob/#local)
path = filepath.ToSlash(path)
if os.PathSeparator != '/' && !strings.HasPrefix(path, "/") {
path = "/" + path
}
return FilePathPrefix + path + queryString, nil
}
func Login(ctx context.Context, d diag.Sink, url string, project *workspace.Project) (Backend, error) {
be, err := New(ctx, d, url, project)
if err != nil {
return nil, err
}
return be, workspace.StoreAccount(be.URL(), workspace.Account{}, true)
}
func (b *diyBackend) getReference(ref backend.StackReference) (*diyBackendReference, error) {
stackRef, ok := ref.(*diyBackendReference)
if !ok {
return nil, errors.New("bad stack reference type")
}
return stackRef, stackRef.Validate()
}
func (b *diyBackend) diy() {}
func (b *diyBackend) Name() string {
name, err := os.Hostname()
contract.IgnoreError(err)
if name == "" {
name = "diy"
}
return name
}
func (b *diyBackend) URL() string {
return b.originalURL
}
func (b *diyBackend) SetCurrentProject(project *workspace.Project) {
b.currentProject.Store(project)
}
func (b *diyBackend) GetPolicyPack(ctx context.Context, policyPack string,
d diag.Sink,
) (backend.PolicyPack, error) {
return nil, errors.New("DIY backend does not support resource policy")
}
func (b *diyBackend) ListPolicyGroups(ctx context.Context, orgName string, _ backend.ContinuationToken) (
apitype.ListPolicyGroupsResponse, backend.ContinuationToken, error,
) {
return apitype.ListPolicyGroupsResponse{}, nil, errors.New("DIY backend does not support resource policy")
}
func (b *diyBackend) ListPolicyPacks(ctx context.Context, orgName string, _ backend.ContinuationToken) (
apitype.ListPolicyPacksResponse, backend.ContinuationToken, error,
) {
return apitype.ListPolicyPacksResponse{}, nil, errors.New("DIY backend does not support resource policy")
}
func (b *diyBackend) SupportsTags() bool {
return false
}
func (b *diyBackend) SupportsOrganizations() bool {
return false
}
func (b *diyBackend) SupportsProgress() bool {
return false
}
func (b *diyBackend) ParseStackReference(stackRef string) (backend.StackReference, error) {
return b.parseStackReference(stackRef)
}
func (b *diyBackend) parseStackReference(stackRef string) (*diyBackendReference, error) {
return b.store.ParseReference(stackRef)
}
// ValidateStackName verifies the stack name is valid for the diy backend.
func (b *diyBackend) ValidateStackName(stackRef string) error {
_, err := b.ParseStackReference(stackRef)
return err
}
func (b *diyBackend) DoesProjectExist(ctx context.Context, _ string, projectName string) (bool, error) {
projStore, ok := b.store.(*projectReferenceStore)
if !ok {
// Legacy stores don't have projects
// so the project does not exist.
return false, nil
}
return projStore.ProjectExists(ctx, projectName)
}
// Confirm the specified stack's project doesn't contradict the meta.yaml of the current project.
// If the CWD is not in a Pulumi project, does not contradict.
// If the project name in Pulumi.yaml is "foo", a stack with a name of bar/foo should not work.
func currentProjectContradictsWorkspace(stack *diyBackendReference) bool {
contract.Requiref(stack != nil, "stack", "is nil")
if stack.project == "" {
return false
}
projPath, err := workspace.DetectProjectPath()
if err != nil {
return false
}
if projPath == "" {
return false
}
proj, err := workspace.LoadProject(projPath)
if err != nil {
return false
}
return proj.Name.String() != stack.project.String()
}
func (b *diyBackend) CreateStack(ctx context.Context, stackRef backend.StackReference,
root string, opts *backend.CreateStackOptions,
) (backend.Stack, error) {
if opts != nil && len(opts.Teams) > 0 {
return nil, backend.ErrTeamsNotSupported
}
diyStackRef, err := b.getReference(stackRef)
if err != nil {
return nil, err
}
err = b.Lock(ctx, stackRef)
if err != nil {
return nil, err
}
defer b.Unlock(ctx, stackRef)
if currentProjectContradictsWorkspace(diyStackRef) {
return nil, fmt.Errorf("provided project name %q doesn't match Pulumi.yaml", diyStackRef.project)
}
stackName := diyStackRef.FullyQualifiedName()
if stackName == "" {
return nil, errors.New("invalid empty stack name")
}
if _, err := b.stackExists(ctx, diyStackRef); err == nil {
return nil, &backend.StackAlreadyExistsError{StackName: string(stackName)}
}
_, err = b.saveStack(ctx, diyStackRef, nil, nil)
if err != nil {
return nil, err
}
stack := newStack(diyStackRef, b)
b.d.Infof(diag.Message("", "Created stack '%s'"), stack.Ref())
return stack, nil
}
func (b *diyBackend) GetStack(ctx context.Context, stackRef backend.StackReference) (backend.Stack, error) {
diyStackRef, err := b.getReference(stackRef)
if err != nil {
return nil, err
}
_, err = b.stackExists(ctx, diyStackRef)
if err != nil {
if errors.Is(err, errCheckpointNotFound) {
return nil, nil
}
return nil, err
}
return newStack(diyStackRef, b), nil
}
func (b *diyBackend) ListStacks(
ctx context.Context, filter backend.ListStacksFilter, _ backend.ContinuationToken) (
[]backend.StackSummary, backend.ContinuationToken, error,
) {
stacks, err := b.getStacks(ctx)
if err != nil {
return nil, nil, err
}
// Note that the provided stack filter is only partially honored, since fields like organizations and tags
// aren't persisted in the diy backend.
results := slice.Prealloc[backend.StackSummary](len(stacks))
for _, stackRef := range stacks {
// We can check for project name filter here, but be careful about legacy stores where project is always blank.
stackProject, hasProject := stackRef.Project()
if filter.Project != nil && hasProject && string(stackProject) != *filter.Project {
continue
}
chk, err := b.getCheckpoint(ctx, stackRef)
if err != nil {
// There is a race between listing stacks and getting their checkpoints. If there's an error getting
// the checkpoint, check if the stack still exists before returning an error.
if _, existsErr := b.stackExists(ctx, stackRef); existsErr == errCheckpointNotFound {
continue
}
return nil, nil, err
}
results = append(results, newDIYStackSummary(stackRef, chk))
}
return results, nil, nil
}
func (b *diyBackend) RemoveStack(ctx context.Context, stack backend.Stack, force bool) (bool, error) {
diyStackRef, err := b.getReference(stack.Ref())
if err != nil {
return false, err
}
err = b.Lock(ctx, diyStackRef)
if err != nil {
return false, err
}
defer b.Unlock(ctx, diyStackRef)
checkpoint, err := b.getCheckpoint(ctx, diyStackRef)
if err != nil {
return false, err
}
// Don't remove stacks that still have resources.
if !force && checkpoint != nil && checkpoint.Latest != nil && len(checkpoint.Latest.Resources) > 0 {
return true, errors.New("refusing to remove stack because it still contains resources")
}
return false, b.removeStack(ctx, diyStackRef)
}
func (b *diyBackend) RenameStack(ctx context.Context, stack backend.Stack,
newName tokens.QName,
) (backend.StackReference, error) {
diyStackRef, err := b.getReference(stack.Ref())
if err != nil {
return nil, err
}
// Ensure the new stack name is valid.
newRef, err := b.parseStackReference(string(newName))
if err != nil {
return nil, err
}
err = b.renameStack(ctx, diyStackRef, newRef)
if err != nil {
return nil, err
}
return newRef, nil
}
func (b *diyBackend) renameStack(ctx context.Context, oldRef *diyBackendReference,
newRef *diyBackendReference,
) error {
err := b.Lock(ctx, oldRef)
if err != nil {
return err
}
defer b.Unlock(ctx, oldRef)
// Ensure the destination stack does not already exist.
hasExisting, err := b.bucket.Exists(ctx, b.stackPath(ctx, newRef))
if err != nil {
return err
}
if hasExisting {
return fmt.Errorf("a stack named %s already exists", newRef.String())
}
// Get the current state from the stack to be renamed.
chk, err := b.getCheckpoint(ctx, oldRef)
if err != nil {
return fmt.Errorf("failed to load checkpoint: %w", err)
}
// If we have a checkpoint, we need to rename the URNs inside it to use the new stack name.
if chk != nil && chk.Latest != nil {
project, has := newRef.Project()
contract.Assertf(has || project == "", "project should be blank for legacy stacks")
if err = edit.RenameStack(chk.Latest, newRef.name, tokens.PackageName(project)); err != nil {
return err
}
}
chkJSON, err := encoding.JSON.Marshal(chk)
if err != nil {
return fmt.Errorf("marshalling checkpoint: %w", err)
}
versionedCheckpoint := &apitype.VersionedCheckpoint{
Version: apitype.DeploymentSchemaVersionCurrent,
Checkpoint: json.RawMessage(chkJSON),
}
// Now save the snapshot with a new name (we pass nil to re-use the existing secrets manager from the snapshot).
if _, _, err = b.saveCheckpoint(ctx, newRef, versionedCheckpoint); err != nil {
return err
}
// To remove the old stack, just make a backup of the file and don't write out anything new.
file := b.stackPath(ctx, oldRef)
backupTarget(ctx, b.bucket, file, false)
// And rename the history folder as well.
if err = b.renameHistory(ctx, oldRef, newRef); err != nil {
return err
}
return err
}
func (b *diyBackend) GetLatestConfiguration(ctx context.Context,
stack backend.Stack,
) (config.Map, error) {
hist, err := b.GetHistory(ctx, stack.Ref(), 1 /*pageSize*/, 1 /*page*/)
if err != nil {
return nil, err
}
if len(hist) == 0 {
return nil, backend.ErrNoPreviousDeployment
}
return hist[0].Config, nil
}
func (b *diyBackend) PackPolicies(
ctx context.Context, policyPackRef backend.PolicyPackReference,
cancellationScopes backend.CancellationScopeSource,
callerEventsOpt chan<- engine.Event,
) result.Result {
return result.Error("DIY backend does not support resource policy")
}
func (b *diyBackend) Preview(ctx context.Context, stack backend.Stack,
op backend.UpdateOperation, events chan<- engine.Event,
) (*deploy.Plan, sdkDisplay.ResourceChanges, result.Result) {
// We can skip PreviewThenPromptThenExecute and just go straight to Execute.
opts := backend.ApplierOptions{
DryRun: true,
ShowLink: true,
}
return b.apply(ctx, apitype.PreviewUpdate, stack, op, opts, events)
}
func (b *diyBackend) Update(ctx context.Context, stack backend.Stack,
op backend.UpdateOperation,
) (sdkDisplay.ResourceChanges, result.Result) {
err := b.Lock(ctx, stack.Ref())
if err != nil {
return nil, result.FromError(err)
}
defer b.Unlock(ctx, stack.Ref())
return backend.PreviewThenPromptThenExecute(ctx, apitype.UpdateUpdate, stack, op, b.apply)
}
func (b *diyBackend) Import(ctx context.Context, stack backend.Stack,
op backend.UpdateOperation, imports []deploy.Import,
) (sdkDisplay.ResourceChanges, result.Result) {
err := b.Lock(ctx, stack.Ref())
if err != nil {
return nil, result.FromError(err)
}
defer b.Unlock(ctx, stack.Ref())
op.Imports = imports
if op.Opts.PreviewOnly {
// We can skip PreviewThenPromptThenExecute, and just go straight to Execute.
opts := backend.ApplierOptions{
DryRun: true,
ShowLink: true,
}
op.Opts.Engine.GeneratePlan = false
_, changes, res := b.apply(
ctx, apitype.ResourceImportUpdate, stack, op, opts, nil /*events*/)
return changes, res
}
return backend.PreviewThenPromptThenExecute(ctx, apitype.ResourceImportUpdate, stack, op, b.apply)
}
func (b *diyBackend) Refresh(ctx context.Context, stack backend.Stack,
op backend.UpdateOperation,
) (sdkDisplay.ResourceChanges, result.Result) {
err := b.Lock(ctx, stack.Ref())
if err != nil {
return nil, result.FromError(err)
}
defer b.Unlock(ctx, stack.Ref())
if op.Opts.PreviewOnly {
// We can skip PreviewThenPromptThenExecute, and just go straight to Execute.
opts := backend.ApplierOptions{
DryRun: true,
ShowLink: true,
}
op.Opts.Engine.GeneratePlan = false
_, changes, res := b.apply(
ctx, apitype.RefreshUpdate, stack, op, opts, nil /*events*/)
return changes, res
}
return backend.PreviewThenPromptThenExecute(ctx, apitype.RefreshUpdate, stack, op, b.apply)
}
func (b *diyBackend) Destroy(ctx context.Context, stack backend.Stack,
op backend.UpdateOperation,
) (sdkDisplay.ResourceChanges, result.Result) {
err := b.Lock(ctx, stack.Ref())
if err != nil {
return nil, result.FromError(err)