This change supports displaying manifest information for a stack and changes the way we handle Snapshots in our backend. Previously, every call to GetStack would synthesize a Snapshot by taking the set of resources returned from the `/api/stacks/<owner>/<name>` endpoint, combined with an empty manfiest (since the service was not returning the manifest). This wasn't great for two reasons: 1. We didn't have manifest information, so we couldn't display any of its information (most important the last updated time). 2. This strategy required that the service return all the resources for a stack anytime GetStack was called. While the CLI did not often need this detailed information the fact that we forced the Service to produce it (which in the case of stack managed PPC would require the service to talk to yet another service) creates a bunch of work that we end up ignoring. I've refactored the code such that `backend.Stack`'s `Snapshot()` method now lazily requests the information from the service such that we can construct a `Snapshot()` on demand and only pay the cost when we actually need it. I think making more of this stuff lazy is the long term direction we want to follow. Unfortunately, right now, it means in cases where we do need this data we end up fetching it twice. The service does it once when we call GetStack and then we do it again when we actually need to get at the Snapshot. However, once we land this change, we can update the service to no longer return resources on the apistack.Stack type. The CLI no longer needs this property. We'll likely want to continue in a direction where `apistack.Stack` can be created quickly by the service (without expensive database queries or fetching remote resources) and just add additional endpoints that let us get at the specific information we want in the specific cases when we want it instead of forcing us to return a bunch of data that we often ignore. Fixes pulumi/pulumi-service#371
195 lines
5.7 KiB
Go
195 lines
5.7 KiB
Go
// Copyright 2016-2018, 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 cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
|
|
"github.com/dustin/go-humanize"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/pulumi/pulumi/pkg/backend"
|
|
"github.com/pulumi/pulumi/pkg/backend/cloud"
|
|
"github.com/pulumi/pulumi/pkg/backend/state"
|
|
"github.com/pulumi/pulumi/pkg/tokens"
|
|
"github.com/pulumi/pulumi/pkg/util/cmdutil"
|
|
"github.com/pulumi/pulumi/pkg/util/contract"
|
|
"github.com/pulumi/pulumi/pkg/workspace"
|
|
)
|
|
|
|
func newStackLsCmd() *cobra.Command {
|
|
var allStacks bool
|
|
cmd := &cobra.Command{
|
|
Use: "ls",
|
|
Short: "List all known stacks",
|
|
Args: cmdutil.NoArgs,
|
|
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
|
// Ensure we are in a project; if not, we will fail.
|
|
projPath, err := workspace.DetectProjectPath()
|
|
if err != nil {
|
|
return errors.Wrapf(err, "could not detect current project")
|
|
} else if projPath == "" {
|
|
return errors.New("no Pulumi.yaml found; please run this command in a project directory")
|
|
}
|
|
|
|
proj, err := workspace.LoadProject(projPath)
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not load current project")
|
|
}
|
|
|
|
// Get a list of all known backends, as we will query them all.
|
|
b, err := currentBackend()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Get the current stack so we can print a '*' next to it.
|
|
var current string
|
|
if s, _ := state.CurrentStack(commandContext(), b); s != nil {
|
|
// If we couldn't figure out the current stack, just don't print the '*' later on instead of failing.
|
|
current = s.Name().String()
|
|
}
|
|
|
|
var packageFilter *tokens.PackageName
|
|
if !allStacks {
|
|
packageFilter = &proj.Name
|
|
}
|
|
|
|
// Now produce a list of summaries, and enumerate them sorted by name.
|
|
var result error
|
|
var stackNames []string
|
|
stacks := make(map[string]backend.Stack)
|
|
bs, err := b.ListStacks(commandContext(), packageFilter)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
showPPCColumn, maxPPC := hasAnyPPCStacks(bs)
|
|
_, showURLColumn := b.(cloud.Backend)
|
|
|
|
for _, stack := range bs {
|
|
name := stack.Name().String()
|
|
stacks[name] = stack
|
|
stackNames = append(stackNames, name)
|
|
}
|
|
sort.Strings(stackNames)
|
|
|
|
// Devote 48 characters to the name width, unless there is a longer name.
|
|
maxname := 48
|
|
for _, name := range stackNames {
|
|
if len(name) > maxname {
|
|
maxname = len(name)
|
|
}
|
|
}
|
|
|
|
// We have to fault in snapshots for all the stacks we are going to list here, because that's the easiest
|
|
// way to get the last update time and the resource count. Since this is an expensive operation, we'll
|
|
// do it before printing any output so the latency happens all at once instead of line by line.
|
|
//
|
|
// TODO[pulumi/pulumi-service#1530]: We need a lighterweight way of fetching just the specific information
|
|
// we want to display here.
|
|
for _, name := range stackNames {
|
|
stack := stacks[name]
|
|
_, err := stack.Snapshot(commandContext())
|
|
contract.IgnoreError(err) // If we couldn't get snapshot for the stack don't fail the overall listing.
|
|
}
|
|
|
|
formatDirective := "%-" + strconv.Itoa(maxname) + "s %-24s %-18s"
|
|
headers := []interface{}{"NAME", "LAST UPDATE", "RESOURCE COUNT"}
|
|
|
|
if showPPCColumn {
|
|
formatDirective += " %-" + strconv.Itoa(maxPPC) + "s"
|
|
headers = append(headers, "PPC")
|
|
}
|
|
if showURLColumn {
|
|
formatDirective += " %s"
|
|
headers = append(headers, "URL")
|
|
}
|
|
|
|
formatDirective = formatDirective + "\n"
|
|
|
|
fmt.Printf(formatDirective, headers...)
|
|
for _, name := range stackNames {
|
|
// Mark the name as current '*' if we've selected it.
|
|
stack := stacks[name]
|
|
if name == current {
|
|
name += "*"
|
|
}
|
|
|
|
// Get last deployment info, provided that it exists.
|
|
none := "n/a"
|
|
lastUpdate := none
|
|
resourceCount := none
|
|
snap, err := stack.Snapshot(commandContext())
|
|
contract.IgnoreError(err) // If we couldn't get snapshot for the stack don't fail the overall listing.
|
|
|
|
if snap != nil {
|
|
if t := snap.Manifest.Time; !t.IsZero() {
|
|
lastUpdate = humanize.Time(t)
|
|
}
|
|
resourceCount = strconv.Itoa(len(snap.Resources))
|
|
}
|
|
|
|
values := []interface{}{name, lastUpdate, resourceCount}
|
|
if showPPCColumn {
|
|
// Print out the PPC name.
|
|
var cloudInfo string
|
|
if cs, ok := stack.(cloud.Stack); ok && !cs.RunLocally() {
|
|
cloudInfo = cs.CloudName()
|
|
} else {
|
|
cloudInfo = none
|
|
}
|
|
values = append(values, cloudInfo)
|
|
}
|
|
if showURLColumn {
|
|
var url string
|
|
if cs, ok := stack.(cloud.Stack); ok {
|
|
if u, urlErr := cs.ConsoleURL(); urlErr == nil {
|
|
url = u
|
|
}
|
|
}
|
|
if url == "" {
|
|
url = none
|
|
}
|
|
values = append(values, url)
|
|
}
|
|
|
|
fmt.Printf(formatDirective, values...)
|
|
}
|
|
|
|
return result
|
|
}),
|
|
}
|
|
cmd.PersistentFlags().BoolVarP(
|
|
&allStacks, "all", "a", false, "List all stacks instead of just stacks for the current project")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func hasAnyPPCStacks(stacks []backend.Stack) (bool, int) {
|
|
res, maxLen := false, 0
|
|
for _, s := range stacks {
|
|
if cs, ok := s.(cloud.Stack); ok {
|
|
if !cs.RunLocally() {
|
|
res = true
|
|
maxLen = len(cs.CloudName())
|
|
}
|
|
}
|
|
}
|
|
|
|
return res, maxLen
|
|
}
|