This change removes the need to `pulumi init` when targeting the local backend. A fair amount of the change lays the foundation that the next set of changes to stop having `pulumi init` be used for cloud stacks as well. Previously, `pulumi init` logically did two things: 1. It created the bookkeeping directory for local stacks, this was stored in `<repository-root>/.pulumi`, where `<repository-root>` was the path to what we belived the "root" of your project was. In the case of git repositories, this was the directory that contained your `.git` folder. 2. It recorded repository information in `<repository-root>/.pulumi/repository.json`. This was used by the cloud backend when computing what project to interact with on Pulumi.com The new identity model will remove the need for (2), since we only need an owner and stack name to fully qualify a stack on pulumi.com, so it's easy enough to stop creating a folder just for that. However, for the local backend, we need to continue to retain some information about stacks (e.g. checkpoints, history, etc). In addition, we need to store our workspace settings (which today just contains the selected stack) somehere. For state stored by the local backend, we change the URL scheme from `local://` to `local://<optional-root-path>`. When `<optional-root-path>` is unset, it defaults to `$HOME`. We create our `.pulumi` folder in that directory. This is important because stack names now must be unique within the backend, but we have some tests using local stacks which use fixed stack names, so each integration test really wants its own "view" of the world. For the workspace settings, we introduce a new `workspaces` directory in `~/.pulumi`. In this folder we write the workspace settings file for each project. The file name is the name of the project, combined with the SHA1 of the path of the project file on disk, to ensure that multiple pulumi programs with the same project name have different workspace settings. This does mean that moving a project's location on disk will cause the CLI to "forget" what the selected stack was, which is unfortunate, but not the end of the world. If this ends up being a big pain point, we can certianly try to play games in the future (for example, if we saw a .git folder in a parent folder, we could store data in there). With respect to compatibility, we don't attempt to migrate older files to their newer locations. For long lived stacks managed using the local backend, we can provide information on where to move things to. For all stacks (regardless of backend) we'll require the user to `pulumi stack select` their stack again, but that seems like the correct trade-off vs writing complicated upgrade code.
168 lines
4.2 KiB
Go
168 lines
4.2 KiB
Go
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
|
|
|
|
package workspace
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/pulumi/pulumi/pkg/resource/config"
|
|
"github.com/pulumi/pulumi/pkg/tokens"
|
|
"github.com/pulumi/pulumi/pkg/util/contract"
|
|
)
|
|
|
|
// W offers functionality for interacting with Pulumi workspaces.
|
|
type W interface {
|
|
Settings() *Settings // returns a mutable pointer to the optional workspace settings info.
|
|
Repository() *Repository // (optional) returns the repository this project belongs to.
|
|
Save() error // saves any modifications to the workspace.
|
|
}
|
|
|
|
type projectWorkspace struct {
|
|
name tokens.PackageName // the package this workspace is associated with.
|
|
project string // the path to the Pulumi.[yaml|json] file for this project.
|
|
settings *Settings // settings for this workspace.
|
|
repo *Repository // the repo this workspace is associated with.
|
|
}
|
|
|
|
// New creates a new workspace using the current working directory.
|
|
func New() (W, error) {
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return NewFrom(cwd)
|
|
}
|
|
|
|
// NewFrom creates a new Pulumi workspace in the given directory. Requires a Pulumi.yaml file be present in the
|
|
// folder hierarchy between dir and the .pulumi folder.
|
|
func NewFrom(dir string) (W, error) {
|
|
repo, err := GetRepository(dir)
|
|
if err == ErrNoRepository {
|
|
repo = nil
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
path, err := DetectProjectPathFrom(dir)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if path == "" {
|
|
return nil, errors.New("no Pulumi.yaml project file found")
|
|
}
|
|
|
|
proj, err := LoadProject(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
w := projectWorkspace{
|
|
name: proj.Name,
|
|
project: path,
|
|
repo: repo,
|
|
}
|
|
|
|
err = w.readSettings()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if w.settings.ConfigDeprecated == nil {
|
|
w.settings.ConfigDeprecated = make(map[tokens.QName]config.Map)
|
|
}
|
|
|
|
return &w, nil
|
|
}
|
|
|
|
func (pw *projectWorkspace) Settings() *Settings {
|
|
return pw.settings
|
|
}
|
|
|
|
func (pw *projectWorkspace) Repository() *Repository {
|
|
return pw.repo
|
|
}
|
|
|
|
func (pw *projectWorkspace) Save() error {
|
|
// let's remove all the empty entries from the config array
|
|
for k, v := range pw.settings.ConfigDeprecated {
|
|
if len(v) == 0 {
|
|
delete(pw.settings.ConfigDeprecated, k)
|
|
}
|
|
}
|
|
|
|
settingsFile := pw.settingsPath()
|
|
|
|
// If the settings file is empty, don't write an new one, and delete the old one if present. Since we put workspaces
|
|
// under ~/.pulumi/workspaces, cleaning them out when possible prevents us from littering a bunch of files in the
|
|
// home directory.
|
|
if pw.settings.IsEmpty() {
|
|
err := os.Remove(settingsFile)
|
|
if err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
err := os.MkdirAll(filepath.Dir(settingsFile), 0700)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
b, err := json.MarshalIndent(pw.settings, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return ioutil.WriteFile(settingsFile, b, 0600)
|
|
}
|
|
|
|
func (pw *projectWorkspace) readSettings() error {
|
|
settingsPath := pw.settingsPath()
|
|
|
|
b, err := ioutil.ReadFile(settingsPath)
|
|
if err != nil && os.IsNotExist(err) {
|
|
// not an error to not have an existing settings file.
|
|
pw.settings = &Settings{}
|
|
return nil
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
|
|
var settings Settings
|
|
|
|
err = json.Unmarshal(b, &settings)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pw.settings = &settings
|
|
return nil
|
|
}
|
|
|
|
func (pw *projectWorkspace) settingsPath() string {
|
|
user, err := user.Current()
|
|
contract.AssertNoErrorf(err, "could not get current user")
|
|
|
|
uniqueFileName := string(pw.name) + "-" + sha1HexString(pw.project) + "-" + WorkspaceFile
|
|
return filepath.Join(user.HomeDir, BookkeepingDir, WorkspaceDir, uniqueFileName)
|
|
}
|
|
|
|
// sha1HexString returns a hex string of the sha1 hash of value.
|
|
func sha1HexString(value string) string {
|
|
h := sha1.New()
|
|
_, err := h.Write([]byte(value))
|
|
contract.AssertNoError(err)
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
// qnameFileName takes a qname and cleans it for use as a filename (by replacing tokens.QNameDelimter with a dash)
|
|
func qnameFileName(nm tokens.QName) string {
|
|
return strings.Replace(string(nm), tokens.QNameDelimiter, "-", -1)
|
|
}
|