go.ligato.io/vpp-agent/v3@v3.5.0/cmd/agentctl/commands/status.go (about)

     1  //  Copyright (c) 2019 Cisco and/or its affiliates.
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at:
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package commands
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/spf13/cobra"
    21  	"go.ligato.io/cn-infra/v2/health/probe"
    22  
    23  	"go.ligato.io/vpp-agent/v3/cmd/agentctl/api/types"
    24  	agentcli "go.ligato.io/vpp-agent/v3/cmd/agentctl/cli"
    25  )
    26  
    27  func NewStatusCommand(cli agentcli.Cli) *cobra.Command {
    28  	var (
    29  		opts StatusOptions
    30  	)
    31  	cmd := &cobra.Command{
    32  		Use:   "status",
    33  		Short: "Retrieve agent status and version info",
    34  		Args:  cobra.NoArgs,
    35  		RunE: func(cmd *cobra.Command, args []string) error {
    36  			return runStatus(cli, opts)
    37  		},
    38  	}
    39  	flags := cmd.Flags()
    40  	flags.StringVarP(&opts.Format, "format", "f", "", "Format output")
    41  	return cmd
    42  }
    43  
    44  type StatusOptions struct {
    45  	Format string
    46  }
    47  
    48  func runStatus(cli agentcli.Cli, opts StatusOptions) error {
    49  	ctx, cancel := context.WithCancel(context.Background())
    50  	defer cancel()
    51  
    52  	s, err := cli.Client().Status(ctx)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	v, err := cli.Client().AgentVersion(ctx)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	format := opts.Format
    62  	if len(format) == 0 {
    63  		format = defaultFormatStatus
    64  	}
    65  
    66  	data := struct {
    67  		Status  *probe.ExposedStatus
    68  		Version *types.Version
    69  	}{s, v}
    70  
    71  	if err := formatAsTemplate(cli.Out(), format, data); err != nil {
    72  		return err
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  const defaultFormatStatus = `AGENT
    79      App name:    {{.Version.App}}
    80      Version:     {{.Version.Version}}
    81  
    82      State:       {{.Status.AgentStatus.State}}
    83      Started:     {{epoch .Status.AgentStatus.StartTime}} ({{ago (epoch .Status.AgentStatus.StartTime)}} ago)
    84      Last change: {{ago (epoch .Status.AgentStatus.LastChange)}}
    85      Last update: {{ago (epoch .Status.AgentStatus.LastUpdate)}}
    86  
    87      Go version:  {{.Version.GoVersion}}
    88      OS/Arch:     {{.Version.OS}}/{{.Version.Arch}}
    89  
    90      Build Info:
    91          Git commit: {{.Version.GitCommit}}
    92          Git branch: {{.Version.GitBranch}}
    93          User:       {{.Version.BuildUser}}
    94          Host:       {{.Version.BuildHost}}
    95          Built:      {{epoch .Version.BuildTime}}
    96  
    97  PLUGINS
    98  {{- range $name, $plugin := .Status.PluginStatus}}
    99      {{$name}}: {{$plugin.State}}
   100  {{- end}}
   101  `