pkg/cmd/cli/cmd/status.go
94df0e0f
 package cmd
 
 import (
73597be5
 	"errors"
94df0e0f
 	"fmt"
 	"io"
 
a0b058a4
 	"github.com/gonum/graph/encoding/dot"
94df0e0f
 	"github.com/spf13/cobra"
 
e7296533
 	kapi "k8s.io/kubernetes/pkg/api"
95ec120f
 	kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
94df0e0f
 
 	"github.com/openshift/origin/pkg/cmd/cli/describe"
6267dded
 	"github.com/openshift/origin/pkg/cmd/templates"
94df0e0f
 	"github.com/openshift/origin/pkg/cmd/util/clientcmd"
4f77d599
 	dotutil "github.com/openshift/origin/pkg/util/dot"
94df0e0f
 )
 
73597be5
 // StatusRecommendedName is the recommended command name.
 const StatusRecommendedName = "status"
a0b058a4
 
6267dded
 var (
 	statusLong = templates.LongDesc(`
 		Show a high level overview of the current project
c124965f
 
6267dded
 		This command will show services, deployment configs, build configurations, and active deployments.
 		If you have any misconfigured components information about them will be shown. For more information
 		about individual items, use the describe command (e.g. %[1]s describe buildConfig,
 		%[1]s describe deploymentConfig, %[1]s describe service).
a0b058a4
 
6267dded
 		You can specify an output format of "-o dot" to have this command output the generated status
 		graph in DOT format that is suitable for use by the "dot" command.`)
f3fd2835
 
6267dded
 	statusExample = templates.Examples(`
 		# See an overview of the current project.
 	  %[1]s
73597be5
 
6267dded
 	  # Export the overview of the current project in an svg file.
 	  %[1]s -o dot | dot -T svg -o project.svg
73597be5
 
6267dded
 	  # See an overview of the current project including details for any identified issues.
 	  %[1]s -v`)
1558f2d9
 )
94df0e0f
 
73597be5
 // StatusOptions contains all the necessary options for the Openshift cli status command.
 type StatusOptions struct {
e7296533
 	namespace     string
 	allNamespaces bool
 	outputFormat  string
 	describer     *describe.ProjectStatusDescriber
 	out           io.Writer
 	verbose       bool
1de3e6e8
 
 	logsCommandName             string
 	securityPolicyCommandFormat string
b2de2910
 	setProbeCommandName         string
bd7e128a
 	patchCommandName            string
73597be5
 }
 
 // NewCmdStatus implements the OpenShift cli status command.
5913cbe3
 // baseCLIName is the path from root cmd to the parent of this cmd.
 func NewCmdStatus(name, baseCLIName, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
73597be5
 	opts := &StatusOptions{}
a0b058a4
 
94df0e0f
 	cmd := &cobra.Command{
73597be5
 		Use:     fmt.Sprintf("%s [-o dot | -v ]", StatusRecommendedName),
1558f2d9
 		Short:   "Show an overview of the current project",
5913cbe3
 		Long:    fmt.Sprintf(statusLong, baseCLIName),
c36413f1
 		Example: fmt.Sprintf(statusExample, fullName),
94df0e0f
 		Run: func(cmd *cobra.Command, args []string) {
bcf8d9bb
 			err := opts.Complete(f, cmd, baseCLIName, args, out)
95ec120f
 			kcmdutil.CheckErr(err)
73597be5
 
 			if err := opts.Validate(); err != nil {
95ec120f
 				kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
a0b058a4
 			}
 
73597be5
 			err = opts.RunStatus()
95ec120f
 			kcmdutil.CheckErr(err)
94df0e0f
 		},
 	}
a0b058a4
 
73597be5
 	cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", opts.outputFormat, "Output format. One of: dot.")
 	cmd.Flags().BoolVarP(&opts.verbose, "verbose", "v", opts.verbose, "See details for resolving issues.")
e7296533
 	cmd.Flags().BoolVar(&opts.allNamespaces, "all-namespaces", false, "Display status for all namespaces (must have cluster admin)")
a0b058a4
 
94df0e0f
 	return cmd
 }
 
73597be5
 // Complete completes the options for the Openshift cli status command.
bcf8d9bb
 func (o *StatusOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, baseCLIName string, args []string, out io.Writer) error {
73597be5
 	if len(args) > 0 {
95ec120f
 		return kcmdutil.UsageError(cmd, "no arguments should be provided")
73597be5
 	}
 
b2de2910
 	o.logsCommandName = fmt.Sprintf("%s logs", cmd.Parent().CommandPath())
1de3e6e8
 	o.securityPolicyCommandFormat = "oadm policy add-scc-to-user anyuid -n %s -z %s"
b2de2910
 	o.setProbeCommandName = fmt.Sprintf("%s set probe", cmd.Parent().CommandPath())
1de3e6e8
 
94df0e0f
 	client, kclient, err := f.Clients()
 	if err != nil {
 		return err
 	}
 
5ebad032
 	config, err := f.OpenShiftClientConfig.ClientConfig()
 	if err != nil {
 		return err
 	}
 
e7296533
 	if o.allNamespaces {
 		o.namespace = kapi.NamespaceAll
 	} else {
 		namespace, _, err := f.DefaultNamespace()
 		if err != nil {
 			return err
 		}
 		o.namespace = namespace
94df0e0f
 	}
 
bcf8d9bb
 	if baseCLIName == "" {
 		baseCLIName = "oc"
 	}
 
1de3e6e8
 	o.describer = &describe.ProjectStatusDescriber{
 		K:       kclient,
 		C:       client,
 		Server:  config.Host,
 		Suggest: o.verbose,
 
bcf8d9bb
 		CommandBaseName: baseCLIName,
 
de867b9e
 		// TODO: Remove these and reference them inside the markers using constants.
1de3e6e8
 		LogsCommandName:             o.logsCommandName,
 		SecurityPolicyCommandFormat: o.securityPolicyCommandFormat,
b2de2910
 		SetProbeCommandName:         o.setProbeCommandName,
1de3e6e8
 	}
73597be5
 
 	o.out = out
94df0e0f
 
 	return nil
 }
a0b058a4
 
73597be5
 // Validate validates the options for the Openshift cli status command.
 func (o StatusOptions) Validate() error {
 	if len(o.outputFormat) != 0 && o.outputFormat != "dot" {
 		return fmt.Errorf("invalid output format provided: %s", o.outputFormat)
a0b058a4
 	}
73597be5
 	if len(o.outputFormat) > 0 && o.verbose {
 		return errors.New("cannot provide suggestions when output format is dot")
a0b058a4
 	}
73597be5
 	return nil
 }
a0b058a4
 
73597be5
 // RunStatus contains all the necessary functionality for the OpenShift cli status command.
 func (o StatusOptions) RunStatus() error {
 	var (
 		s   string
 		err error
 	)
 
 	switch o.outputFormat {
 	case "":
 		s, err = o.describer.Describe(o.namespace, "")
 		if err != nil {
 			return err
 		}
 	case "dot":
 		g, _, err := o.describer.MakeGraph(o.namespace)
 		if err != nil {
 			return err
 		}
4f77d599
 		data, err := dot.Marshal(g, dotutil.Quote(o.namespace), "", "  ", false)
73597be5
 		if err != nil {
 			return err
 		}
 		s = string(data)
 	default:
 		return fmt.Errorf("invalid output format provided: %s", o.outputFormat)
a0b058a4
 	}
 
73597be5
 	fmt.Fprintf(o.out, s)
a0b058a4
 	return nil
 }