2017-08-30 03:25:50 +02:00
|
|
|
// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
|
|
|
|
|
2017-09-22 04:18:21 +02:00
|
|
|
package cmd
|
2017-08-30 03:25:50 +02:00
|
|
|
|
|
|
|
import (
|
2017-10-24 05:37:40 +02:00
|
|
|
"fmt"
|
2017-08-30 03:25:50 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-10-24 05:37:40 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
2017-08-30 03:25:50 +02:00
|
|
|
|
2017-08-31 19:28:20 +02:00
|
|
|
"github.com/pkg/errors"
|
2017-09-22 04:18:21 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/encoding"
|
2017-10-21 18:31:01 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/engine"
|
2017-10-19 00:37:18 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/resource/config"
|
2017-09-22 04:18:21 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/resource/deploy"
|
2017-10-16 21:04:35 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/resource/stack"
|
2017-09-22 04:18:21 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/tokens"
|
|
|
|
"github.com/pulumi/pulumi/pkg/util/contract"
|
|
|
|
"github.com/pulumi/pulumi/pkg/workspace"
|
2017-08-30 03:25:50 +02:00
|
|
|
)
|
|
|
|
|
2017-10-19 00:37:18 +02:00
|
|
|
type localStackProvider struct {
|
|
|
|
decrypter config.ValueDecrypter
|
|
|
|
}
|
2017-08-30 03:25:50 +02:00
|
|
|
|
2017-10-16 21:04:35 +02:00
|
|
|
func (p localStackProvider) GetTarget(name tokens.QName) (*deploy.Target, error) {
|
2017-10-11 22:23:44 +02:00
|
|
|
contract.Require(name != "", "name")
|
|
|
|
|
2017-10-17 23:19:47 +02:00
|
|
|
config, err := getConfiguration(name)
|
2017-10-17 21:28:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-19 00:37:18 +02:00
|
|
|
decryptedConfig := make(map[tokens.ModuleMember]string)
|
|
|
|
|
|
|
|
for k, v := range config {
|
|
|
|
decrypted, err := v.Value(p.decrypter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not decrypt configuration value")
|
|
|
|
}
|
|
|
|
decryptedConfig[k] = decrypted
|
|
|
|
}
|
2017-10-11 22:23:44 +02:00
|
|
|
|
2017-10-19 00:37:18 +02:00
|
|
|
return &deploy.Target{Name: name, Config: decryptedConfig}, nil
|
2017-10-11 22:23:44 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 21:04:35 +02:00
|
|
|
func (p localStackProvider) GetSnapshot(name tokens.QName) (*deploy.Snapshot, error) {
|
2017-10-11 22:23:44 +02:00
|
|
|
contract.Require(name != "", "name")
|
|
|
|
|
2017-10-19 00:37:18 +02:00
|
|
|
_, _, snapshot, err := getStack(name)
|
2017-10-11 22:23:44 +02:00
|
|
|
|
|
|
|
return snapshot, err
|
|
|
|
}
|
|
|
|
|
2017-10-21 18:31:01 +02:00
|
|
|
type localStackMutation struct {
|
|
|
|
name tokens.QName
|
|
|
|
}
|
|
|
|
|
2017-10-23 19:11:09 +02:00
|
|
|
func (p localStackProvider) BeginMutation(name tokens.QName) (engine.SnapshotMutation, error) {
|
2017-10-21 18:31:01 +02:00
|
|
|
return localStackMutation{name: name}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m localStackMutation) End(snapshot *deploy.Snapshot) error {
|
|
|
|
contract.Assert(m.name == snapshot.Namespace)
|
|
|
|
|
2017-10-19 00:37:18 +02:00
|
|
|
name, config, _, err := getStack(snapshot.Namespace)
|
2017-10-11 22:23:44 +02:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-19 00:37:18 +02:00
|
|
|
return saveStack(name, config, snapshot)
|
2017-10-11 22:23:44 +02:00
|
|
|
}
|
2017-08-30 03:25:50 +02:00
|
|
|
|
2017-10-19 00:37:18 +02:00
|
|
|
func getStack(name tokens.QName) (tokens.QName, map[tokens.ModuleMember]config.Value, *deploy.Snapshot, error) {
|
2017-08-30 03:25:50 +02:00
|
|
|
contract.Require(name != "", "name")
|
2017-10-16 21:04:35 +02:00
|
|
|
file := workspace.StackPath(name)
|
2017-08-30 03:25:50 +02:00
|
|
|
|
|
|
|
// Detect the encoding of the file so we can do our initial unmarshaling.
|
|
|
|
m, ext := encoding.Detect(file)
|
|
|
|
if m == nil {
|
2017-10-19 00:37:18 +02:00
|
|
|
return "", nil, nil, errors.Errorf("resource deserialization failed; illegal markup extension: '%v'", ext)
|
2017-08-30 03:25:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now read the whole file into a byte blob.
|
|
|
|
b, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2017-10-19 00:37:18 +02:00
|
|
|
return "", nil, nil, err
|
2017-08-30 03:25:50 +02:00
|
|
|
}
|
2017-10-19 00:37:18 +02:00
|
|
|
return "", nil, nil, err
|
2017-08-30 03:25:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal the contents into a checkpoint structure.
|
2017-10-16 21:04:35 +02:00
|
|
|
var checkpoint stack.Checkpoint
|
2017-08-30 03:25:50 +02:00
|
|
|
if err = m.Unmarshal(b, &checkpoint); err != nil {
|
2017-10-19 00:37:18 +02:00
|
|
|
return "", nil, nil, err
|
2017-08-30 03:25:50 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 00:37:18 +02:00
|
|
|
_, config, snapshot, err := stack.DeserializeCheckpoint(&checkpoint)
|
2017-10-22 22:39:21 +02:00
|
|
|
if err != nil {
|
2017-10-19 00:37:18 +02:00
|
|
|
return "", nil, nil, err
|
2017-10-22 22:39:21 +02:00
|
|
|
}
|
2017-10-19 00:37:18 +02:00
|
|
|
|
|
|
|
return name, config, snapshot, nil
|
2017-08-30 03:25:50 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 00:37:18 +02:00
|
|
|
func saveStack(name tokens.QName, config map[tokens.ModuleMember]config.Value, snap *deploy.Snapshot) error {
|
|
|
|
file := workspace.StackPath(name)
|
2017-08-30 03:25:50 +02:00
|
|
|
|
2017-10-16 21:04:35 +02:00
|
|
|
// Make a serializable stack and then use the encoder to encode it.
|
2017-08-30 03:25:50 +02:00
|
|
|
m, ext := encoding.Detect(file)
|
|
|
|
if m == nil {
|
2017-08-31 19:28:20 +02:00
|
|
|
return errors.Errorf("resource serialization failed; illegal markup extension: '%v'", ext)
|
2017-08-30 03:25:50 +02:00
|
|
|
}
|
|
|
|
if filepath.Ext(file) == "" {
|
|
|
|
file = file + ext
|
|
|
|
}
|
2017-10-19 00:37:18 +02:00
|
|
|
dep := stack.SerializeCheckpoint(name, config, snap)
|
2017-08-30 03:25:50 +02:00
|
|
|
b, err := m.Marshal(dep)
|
|
|
|
if err != nil {
|
2017-08-31 19:28:20 +02:00
|
|
|
return errors.Wrap(err, "An IO error occurred during the current operation")
|
2017-08-30 03:25:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Back up the existing file if it already exists.
|
|
|
|
backupTarget(file)
|
|
|
|
|
|
|
|
// Ensure the directory exists.
|
|
|
|
if err = os.MkdirAll(filepath.Dir(file), 0700); err != nil {
|
2017-08-31 19:27:41 +02:00
|
|
|
return errors.Wrap(err, "An IO error occurred during the current operation")
|
2017-08-30 03:25:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// And now write out the new snapshot file, overwriting that location.
|
|
|
|
if err = ioutil.WriteFile(file, b, 0600); err != nil {
|
2017-08-31 19:27:41 +02:00
|
|
|
return errors.Wrap(err, "An IO error occurred during the current operation")
|
2017-08-30 03:25:50 +02:00
|
|
|
}
|
|
|
|
|
2017-10-24 05:37:40 +02:00
|
|
|
// And if we are retaining historical checkpoint information, write it out again
|
|
|
|
if isTruthy(os.Getenv("PULUMI_RETAIN_CHECKPOINTS")) {
|
|
|
|
if err = ioutil.WriteFile(fmt.Sprintf("%v.%v", file, time.Now().UnixNano()), b, 0600); err != nil {
|
|
|
|
return errors.Wrap(err, "An IO error occurred during the current operation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 03:25:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-24 05:37:40 +02:00
|
|
|
func isTruthy(s string) bool {
|
|
|
|
return s == "1" || strings.EqualFold(s, "true")
|
|
|
|
}
|
|
|
|
|
2017-10-19 00:37:18 +02:00
|
|
|
func removeStack(name tokens.QName) error {
|
|
|
|
contract.Require(name != "", "name")
|
2017-08-30 03:25:50 +02:00
|
|
|
// Just make a backup of the file and don't write out anything new.
|
2017-10-19 00:37:18 +02:00
|
|
|
file := workspace.StackPath(name)
|
2017-08-30 03:25:50 +02:00
|
|
|
backupTarget(file)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// backupTarget makes a backup of an existing file, in preparation for writing a new one. Instead of a copy, it
|
|
|
|
// simply renames the file, which is simpler, more efficient, etc.
|
|
|
|
func backupTarget(file string) {
|
|
|
|
contract.Require(file != "", "file")
|
|
|
|
err := os.Rename(file, file+".bak")
|
|
|
|
contract.IgnoreError(err) // ignore errors.
|
|
|
|
// IDEA: consider multiple backups (.bak.bak.bak...etc).
|
|
|
|
}
|