github.com/strongmonkey/helm@v2.7.2+incompatible/cmd/helm/status.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"regexp"
    23  	"text/tabwriter"
    24  
    25  	"github.com/gosuri/uitable"
    26  	"github.com/gosuri/uitable/util/strutil"
    27  	"github.com/spf13/cobra"
    28  
    29  	"k8s.io/helm/pkg/helm"
    30  	"k8s.io/helm/pkg/proto/hapi/release"
    31  	"k8s.io/helm/pkg/proto/hapi/services"
    32  	"k8s.io/helm/pkg/timeconv"
    33  )
    34  
    35  var statusHelp = `
    36  This command shows the status of a named release.
    37  The status consists of:
    38  - last deployment time
    39  - k8s namespace in which the release lives
    40  - state of the release (can be: UNKNOWN, DEPLOYED, DELETED, SUPERSEDED, FAILED or DELETING)
    41  - list of resources that this release consists of, sorted by kind
    42  - details on last test suite run, if applicable
    43  - additional notes provided by the chart
    44  `
    45  
    46  type statusCmd struct {
    47  	release string
    48  	out     io.Writer
    49  	client  helm.Interface
    50  	version int32
    51  }
    52  
    53  func newStatusCmd(client helm.Interface, out io.Writer) *cobra.Command {
    54  	status := &statusCmd{
    55  		out:    out,
    56  		client: client,
    57  	}
    58  
    59  	cmd := &cobra.Command{
    60  		Use:     "status [flags] RELEASE_NAME",
    61  		Short:   "displays the status of the named release",
    62  		Long:    statusHelp,
    63  		PreRunE: setupConnection,
    64  		RunE: func(cmd *cobra.Command, args []string) error {
    65  			if len(args) == 0 {
    66  				return errReleaseRequired
    67  			}
    68  			status.release = args[0]
    69  			if status.client == nil {
    70  				status.client = newClient()
    71  			}
    72  			return status.run()
    73  		},
    74  	}
    75  
    76  	cmd.PersistentFlags().Int32Var(&status.version, "revision", 0, "if set, display the status of the named release with revision")
    77  
    78  	return cmd
    79  }
    80  
    81  func (s *statusCmd) run() error {
    82  	res, err := s.client.ReleaseStatus(s.release, helm.StatusReleaseVersion(s.version))
    83  	if err != nil {
    84  		return prettyError(err)
    85  	}
    86  
    87  	PrintStatus(s.out, res)
    88  	return nil
    89  }
    90  
    91  // PrintStatus prints out the status of a release. Shared because also used by
    92  // install / upgrade
    93  func PrintStatus(out io.Writer, res *services.GetReleaseStatusResponse) {
    94  	if res.Info.LastDeployed != nil {
    95  		fmt.Fprintf(out, "LAST DEPLOYED: %s\n", timeconv.String(res.Info.LastDeployed))
    96  	}
    97  	fmt.Fprintf(out, "NAMESPACE: %s\n", res.Namespace)
    98  	fmt.Fprintf(out, "STATUS: %s\n", res.Info.Status.Code)
    99  	fmt.Fprintf(out, "\n")
   100  	if len(res.Info.Status.Resources) > 0 {
   101  		re := regexp.MustCompile("  +")
   102  
   103  		w := tabwriter.NewWriter(out, 0, 0, 2, ' ', tabwriter.TabIndent)
   104  		fmt.Fprintf(w, "RESOURCES:\n%s\n", re.ReplaceAllString(res.Info.Status.Resources, "\t"))
   105  		w.Flush()
   106  	}
   107  	if res.Info.Status.LastTestSuiteRun != nil {
   108  		lastRun := res.Info.Status.LastTestSuiteRun
   109  		fmt.Fprintf(out, "TEST SUITE:\n%s\n%s\n\n%s\n",
   110  			fmt.Sprintf("Last Started: %s", timeconv.String(lastRun.StartedAt)),
   111  			fmt.Sprintf("Last Completed: %s", timeconv.String(lastRun.CompletedAt)),
   112  			formatTestResults(lastRun.Results))
   113  	}
   114  
   115  	if len(res.Info.Status.Notes) > 0 {
   116  		fmt.Fprintf(out, "NOTES:\n%s\n", res.Info.Status.Notes)
   117  	}
   118  }
   119  
   120  func formatTestResults(results []*release.TestRun) string {
   121  	tbl := uitable.New()
   122  	tbl.MaxColWidth = 50
   123  	tbl.AddRow("TEST", "STATUS", "INFO", "STARTED", "COMPLETED")
   124  	for i := 0; i < len(results); i++ {
   125  		r := results[i]
   126  		n := r.Name
   127  		s := strutil.PadRight(r.Status.String(), 10, ' ')
   128  		i := r.Info
   129  		ts := timeconv.String(r.StartedAt)
   130  		tc := timeconv.String(r.CompletedAt)
   131  		tbl.AddRow(n, s, i, ts, tc)
   132  	}
   133  	return tbl.String()
   134  }