pulumi/pkg/backend/cloud/stack.go

162 lines
5.6 KiB
Go
Raw Normal View History

// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
package cloud
import (
"fmt"
"github.com/pkg/errors"
2018-01-11 00:04:55 +01:00
"github.com/pulumi/pulumi/pkg/apitype"
"github.com/pulumi/pulumi/pkg/backend"
"github.com/pulumi/pulumi/pkg/operations"
"github.com/pulumi/pulumi/pkg/resource"
"github.com/pulumi/pulumi/pkg/resource/config"
"github.com/pulumi/pulumi/pkg/resource/deploy"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/workspace"
)
// Stack is a cloud stack. This simply adds some cloud-specific properties atop the standard backend stack interface.
type Stack interface {
backend.Stack
CloudURL() string // the URL to the cloud containing this stack.
OrgName() string // the organization that owns this stack.
CloudName() string // the PPC in which this stack is running.
RunLocally() bool // true if previews/updates/destroys targeting this stack run locally.
ConsoleURL() (string, error) // the URL to view the stack's information on Pulumi.com
}
// cloudStack is a cloud stack descriptor.
type cloudStack struct {
name backend.StackReference // the stack's name.
cloudURL string // the URL to the cloud containing this stack.
orgName string // the organization that owns this stack.
cloudName string // the PPC in which this stack is running.
config config.Map // the stack's config bag.
snapshot *deploy.Snapshot // a snapshot representing the latest deployment state.
b *cloudBackend // a pointer to the backend this stack belongs to.
}
type cloudBackendReference struct {
name tokens.QName
owner string
b *cloudBackend
}
func (c cloudBackendReference) String() string {
2018-04-18 20:25:16 +02:00
curUser, err := c.b.client.GetPulumiAccountName()
if err != nil {
curUser = ""
}
if c.owner == curUser {
return string(c.name)
}
return fmt.Sprintf("%s/%s", c.owner, c.name)
}
2018-04-18 20:25:16 +02:00
func (c cloudBackendReference) StackName() tokens.QName {
return c.name
}
func newStack(apistack apitype.Stack, b *cloudBackend) Stack {
// Create a fake snapshot out of this stack.
// TODO[pulumi/pulumi-service#249]: add time, version, etc. info to the manifest.
stackName := apistack.StackName
var resources []*resource.State
for _, res := range apistack.Resources {
resources = append(resources, resource.NewState(
res.Type,
res.URN,
res.Custom,
false,
res.ID,
resource.NewPropertyMapFromMap(res.Inputs),
resource.NewPropertyMapFromMap(res.Outputs),
res.Parent,
res.Protect,
// TODO(swgillespie) provide an actual list of dependencies
[]resource.URN{},
))
}
snapshot := deploy.NewSnapshot(stackName, deploy.Manifest{}, resources)
// Now assemble all the pieces into a stack structure.
return &cloudStack{
name: cloudBackendReference{
owner: apistack.OrgName,
name: stackName,
b: b,
},
cloudURL: b.CloudURL(),
orgName: apistack.OrgName,
cloudName: apistack.CloudName,
config: nil, // TODO[pulumi/pulumi-service#249]: add the config variables.
snapshot: snapshot,
b: b,
}
}
// managedCloudName is the name used to refer to the cloud in the Pulumi Service that owns all of an organization's
// managed stacks. All engine operations for a managed stack--previews, updates, destroys, etc.--run locally.
const managedCloudName = "pulumi"
func (s *cloudStack) Name() backend.StackReference { return s.name }
func (s *cloudStack) Config() config.Map { return s.config }
func (s *cloudStack) Snapshot() *deploy.Snapshot { return s.snapshot }
func (s *cloudStack) Backend() backend.Backend { return s.b }
func (s *cloudStack) CloudURL() string { return s.cloudURL }
func (s *cloudStack) OrgName() string { return s.orgName }
func (s *cloudStack) CloudName() string { return s.cloudName }
func (s *cloudStack) RunLocally() bool { return s.cloudName == managedCloudName }
func (s *cloudStack) Remove(force bool) (bool, error) {
return backend.RemoveStack(s, force)
}
func (s *cloudStack) Preview(proj *workspace.Project, root string, m backend.UpdateMetadata,
opts backend.UpdateOptions, scopes backend.CancellationScopeSource) error {
return backend.PreviewStack(s, proj, root, m, opts, scopes)
}
Revise the way previews are controlled I found the flag --force to be a strange name for skipping a preview, since that name is usually reserved for operations that might be harmful and yet you're coercing a tool to do it anyway, knowing there's a chance you're going to shoot yourself in the foot. I also found that what I almost always want in the situation where --force was being used is to actually just run a preview and have the confirmation auto-accepted. Going straight to --force isn't the right thing in a CI scenario, where you actually want to run a preview first, just to ensure there aren't any issues, before doing the update. In a sense, there are four options here: 1. Run a preview, ask for confirmation, then do an update (the default). 2. Run a preview, auto-accept, and then do an update (the CI scenario). 3. Just run a preview with neither a confirmation nor an update (dry run). 4. Just do an update, without performing a preview beforehand (rare). This change enables all four workflows in our CLI. Rather than have an explosion of flags, we have a single flag, --preview, which can specify the mode that we're operating in. The following are the values which correlate to the above four modes: 1. "": default (no --preview specified) 2. "auto": auto-accept preview confirmation 3. "only": only run a preview, don't confirm or update 4. "skip": skip the preview altogether As part of this change, I redid a bit of how the preview modes were specified. Rather than booleans, which had some illegal combinations, this change introduces a new enum type. Furthermore, because the engine is wholly ignorant of these flags -- and only the backend understands them -- it was confusing to me that engine.UpdateOptions stored this flag, especially given that all interesting engine options _also_ accepted a dryRun boolean. As of this change, the backend.PreviewBehavior controls the preview options.
2018-04-28 23:50:17 +02:00
func (s *cloudStack) Update(proj *workspace.Project, root string, m backend.UpdateMetadata,
opts backend.UpdateOptions, scopes backend.CancellationScopeSource) error {
return backend.UpdateStack(s, proj, root, m, opts, scopes)
}
Revise the way previews are controlled I found the flag --force to be a strange name for skipping a preview, since that name is usually reserved for operations that might be harmful and yet you're coercing a tool to do it anyway, knowing there's a chance you're going to shoot yourself in the foot. I also found that what I almost always want in the situation where --force was being used is to actually just run a preview and have the confirmation auto-accepted. Going straight to --force isn't the right thing in a CI scenario, where you actually want to run a preview first, just to ensure there aren't any issues, before doing the update. In a sense, there are four options here: 1. Run a preview, ask for confirmation, then do an update (the default). 2. Run a preview, auto-accept, and then do an update (the CI scenario). 3. Just run a preview with neither a confirmation nor an update (dry run). 4. Just do an update, without performing a preview beforehand (rare). This change enables all four workflows in our CLI. Rather than have an explosion of flags, we have a single flag, --preview, which can specify the mode that we're operating in. The following are the values which correlate to the above four modes: 1. "": default (no --preview specified) 2. "auto": auto-accept preview confirmation 3. "only": only run a preview, don't confirm or update 4. "skip": skip the preview altogether As part of this change, I redid a bit of how the preview modes were specified. Rather than booleans, which had some illegal combinations, this change introduces a new enum type. Furthermore, because the engine is wholly ignorant of these flags -- and only the backend understands them -- it was confusing to me that engine.UpdateOptions stored this flag, especially given that all interesting engine options _also_ accepted a dryRun boolean. As of this change, the backend.PreviewBehavior controls the preview options.
2018-04-28 23:50:17 +02:00
func (s *cloudStack) Refresh(proj *workspace.Project, root string, m backend.UpdateMetadata,
opts backend.UpdateOptions, scopes backend.CancellationScopeSource) error {
return backend.RefreshStack(s, proj, root, m, opts, scopes)
}
Revise the way previews are controlled I found the flag --force to be a strange name for skipping a preview, since that name is usually reserved for operations that might be harmful and yet you're coercing a tool to do it anyway, knowing there's a chance you're going to shoot yourself in the foot. I also found that what I almost always want in the situation where --force was being used is to actually just run a preview and have the confirmation auto-accepted. Going straight to --force isn't the right thing in a CI scenario, where you actually want to run a preview first, just to ensure there aren't any issues, before doing the update. In a sense, there are four options here: 1. Run a preview, ask for confirmation, then do an update (the default). 2. Run a preview, auto-accept, and then do an update (the CI scenario). 3. Just run a preview with neither a confirmation nor an update (dry run). 4. Just do an update, without performing a preview beforehand (rare). This change enables all four workflows in our CLI. Rather than have an explosion of flags, we have a single flag, --preview, which can specify the mode that we're operating in. The following are the values which correlate to the above four modes: 1. "": default (no --preview specified) 2. "auto": auto-accept preview confirmation 3. "only": only run a preview, don't confirm or update 4. "skip": skip the preview altogether As part of this change, I redid a bit of how the preview modes were specified. Rather than booleans, which had some illegal combinations, this change introduces a new enum type. Furthermore, because the engine is wholly ignorant of these flags -- and only the backend understands them -- it was confusing to me that engine.UpdateOptions stored this flag, especially given that all interesting engine options _also_ accepted a dryRun boolean. As of this change, the backend.PreviewBehavior controls the preview options.
2018-04-28 23:50:17 +02:00
func (s *cloudStack) Destroy(proj *workspace.Project, root string, m backend.UpdateMetadata,
opts backend.UpdateOptions, scopes backend.CancellationScopeSource) error {
return backend.DestroyStack(s, proj, root, m, opts, scopes)
}
func (s *cloudStack) GetLogs(query operations.LogQuery) ([]operations.LogEntry, error) {
return backend.GetStackLogs(s, query)
}
func (s *cloudStack) ExportDeployment() (*apitype.UntypedDeployment, error) {
return backend.ExportStackDeployment(s)
}
func (s *cloudStack) ImportDeployment(deployment *apitype.UntypedDeployment) error {
return backend.ImportStackDeployment(s, deployment)
}
func (s *cloudStack) ConsoleURL() (string, error) {
path, err := s.b.StackConsoleURL(s.Name())
if err != nil {
return "", nil
}
url := s.b.CloudConsoleURL(path)
if url == "" {
return "", errors.New("could not determine clould console URL")
}
return url, nil
}