2026-04-06 15:48:37 +05:30
|
|
|
// Copyright IBM Corp. 2020, 2026
|
2023-11-03 14:22:55 -07:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2024-05-22 11:11:48 -07:00
|
|
|
package cache
|
2023-11-03 14:22:55 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
stderr "errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/boundary/api"
|
2023-11-13 13:36:28 -08:00
|
|
|
"github.com/hashicorp/boundary/internal/clientcache/internal/client"
|
2023-11-03 14:22:55 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/clientcache/internal/daemon"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/cmd/base"
|
2023-11-13 13:36:28 -08:00
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
2023-11-03 14:22:55 -07:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
|
"github.com/posener/complete"
|
2024-01-04 12:51:21 -08:00
|
|
|
"golang.org/x/text/cases"
|
|
|
|
|
"golang.org/x/text/language"
|
2023-11-03 14:22:55 -07:00
|
|
|
)
|
|
|
|
|
|
2024-05-22 11:11:48 -07:00
|
|
|
var errCacheNotRunning = stderr.New("The cache process is not running.")
|
2023-11-03 14:22:55 -07:00
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
_ cli.Command = (*AddTokenCommand)(nil)
|
|
|
|
|
_ cli.CommandAutocomplete = (*AddTokenCommand)(nil)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type StatusCommand struct {
|
|
|
|
|
*base.Command
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *StatusCommand) Synopsis() string {
|
2026-10-12 10:20:06 -07:00
|
|
|
return "Get the status information of the running threshold cache"
|
2023-11-03 14:22:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *StatusCommand) Help() string {
|
|
|
|
|
helpText := `
|
2026-10-12 10:20:06 -07:00
|
|
|
Usage: threshold cache status [options]
|
2023-11-03 14:22:55 -07:00
|
|
|
|
2026-10-12 10:20:06 -07:00
|
|
|
Get the status of the threshold cache:
|
2023-11-03 14:22:55 -07:00
|
|
|
|
2026-10-12 10:20:06 -07:00
|
|
|
$ threshold cache status
|
2023-11-03 14:22:55 -07:00
|
|
|
|
|
|
|
|
For a full list of examples, please see the documentation.
|
|
|
|
|
|
|
|
|
|
` + c.Flags().Help()
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *StatusCommand) Flags() *base.FlagSets {
|
2024-01-26 15:56:41 -08:00
|
|
|
set := c.FlagSet(base.FlagSetOutputFormat)
|
|
|
|
|
f := set.NewFlagSet("Client Options")
|
|
|
|
|
|
|
|
|
|
f.BoolVar(&base.BoolVar{
|
|
|
|
|
Name: "output-curl-string",
|
|
|
|
|
Target: &c.FlagOutputCurlString,
|
|
|
|
|
Usage: "Instead of executing the request, print an equivalent cURL command string and exit.",
|
|
|
|
|
})
|
|
|
|
|
|
2023-11-03 14:22:55 -07:00
|
|
|
return set
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *StatusCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
|
return complete.PredictNothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *StatusCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
|
return c.Flags().Completions()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *StatusCommand) Run(args []string) int {
|
|
|
|
|
ctx := c.Context
|
|
|
|
|
f := c.Flags()
|
|
|
|
|
if err := f.Parse(args); err != nil {
|
|
|
|
|
c.PrintCliError(err)
|
|
|
|
|
return base.CommandUserError
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 13:36:28 -08:00
|
|
|
resp, result, apiErr, err := c.Status(ctx)
|
2023-11-03 14:22:55 -07:00
|
|
|
if err != nil {
|
|
|
|
|
c.PrintCliError(err)
|
|
|
|
|
return base.CommandCliError
|
|
|
|
|
}
|
|
|
|
|
if apiErr != nil {
|
2024-05-22 11:11:48 -07:00
|
|
|
c.PrintApiError(apiErr, "Error from cache when getting the status")
|
2023-11-03 14:22:55 -07:00
|
|
|
return base.CommandApiError
|
|
|
|
|
}
|
2023-11-13 13:36:28 -08:00
|
|
|
|
2023-11-03 14:22:55 -07:00
|
|
|
switch base.Format(c.UI) {
|
|
|
|
|
case "json":
|
2023-11-13 13:36:28 -08:00
|
|
|
if ok := c.PrintJsonItem(resp); !ok {
|
|
|
|
|
return base.CommandCliError
|
|
|
|
|
}
|
2023-11-03 14:22:55 -07:00
|
|
|
default:
|
2023-11-13 13:36:28 -08:00
|
|
|
c.UI.Output(printStatusTable(result))
|
2023-11-03 14:22:55 -07:00
|
|
|
}
|
|
|
|
|
return base.CommandSuccess
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 13:36:28 -08:00
|
|
|
func (c *StatusCommand) Status(ctx context.Context) (*api.Response, *daemon.StatusResult, *api.Error, error) {
|
2023-11-03 14:22:55 -07:00
|
|
|
dotPath, err := DefaultDotDirectory(ctx)
|
|
|
|
|
if err != nil {
|
2023-11-13 13:36:28 -08:00
|
|
|
return nil, nil, nil, err
|
2023-11-03 14:22:55 -07:00
|
|
|
}
|
2023-11-15 14:40:05 -08:00
|
|
|
var opts []client.Option
|
|
|
|
|
if c.FlagOutputCurlString {
|
|
|
|
|
opts = append(opts, client.WithOutputCurlString())
|
|
|
|
|
}
|
2023-11-03 14:22:55 -07:00
|
|
|
|
2023-11-15 14:40:05 -08:00
|
|
|
return status(ctx, dotPath, opts...)
|
2023-11-03 14:22:55 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-15 14:40:05 -08:00
|
|
|
func status(ctx context.Context, daemonPath string, opt ...client.Option) (*api.Response, *daemon.StatusResult, *api.Error, error) {
|
2024-05-22 11:11:48 -07:00
|
|
|
const op = "cache.status"
|
2024-01-02 11:15:45 -08:00
|
|
|
addr := daemon.SocketAddress(daemonPath)
|
|
|
|
|
_, err := os.Stat(addr.Path)
|
2023-11-03 14:59:42 -07:00
|
|
|
if addr.Scheme == "unix" && err != nil {
|
2024-05-22 11:11:48 -07:00
|
|
|
return nil, nil, nil, errCacheNotRunning
|
2023-11-03 14:22:55 -07:00
|
|
|
}
|
2023-11-13 13:36:28 -08:00
|
|
|
c, err := client.New(ctx, addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, nil, err
|
2023-11-03 14:22:55 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-15 14:40:05 -08:00
|
|
|
resp, err := c.Get(ctx, "/v1/status", nil, opt...)
|
2023-11-03 14:22:55 -07:00
|
|
|
if err != nil {
|
2023-11-13 13:36:28 -08:00
|
|
|
return nil, nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("client do failed"))
|
2023-11-03 14:22:55 -07:00
|
|
|
}
|
2023-11-13 13:36:28 -08:00
|
|
|
|
|
|
|
|
res := &daemon.StatusResult{}
|
|
|
|
|
apiErr, err := resp.Decode(&res)
|
2023-11-03 14:22:55 -07:00
|
|
|
if err != nil {
|
2024-05-22 11:11:48 -07:00
|
|
|
return nil, nil, nil, fmt.Errorf("Error when sending request to the cache: %w.", err)
|
2023-11-13 13:36:28 -08:00
|
|
|
}
|
|
|
|
|
if apiErr != nil {
|
|
|
|
|
return resp, nil, apiErr, nil
|
2023-11-03 14:22:55 -07:00
|
|
|
}
|
2023-11-13 13:36:28 -08:00
|
|
|
return resp, res, nil, nil
|
2023-11-03 14:22:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func printStatusTable(status *daemon.StatusResult) string {
|
|
|
|
|
nonAttributeMap := map[string]any{
|
|
|
|
|
"User Count": len(status.Users),
|
|
|
|
|
}
|
|
|
|
|
if len(status.SocketAddress) > 0 {
|
|
|
|
|
nonAttributeMap["Domain Socket"] = status.SocketAddress
|
|
|
|
|
}
|
2024-01-22 08:42:19 -08:00
|
|
|
if len(status.LogLocation) > 0 {
|
|
|
|
|
nonAttributeMap["Log Location"] = status.LogLocation
|
|
|
|
|
}
|
2024-01-24 16:26:15 -08:00
|
|
|
if len(status.Version) > 0 {
|
|
|
|
|
nonAttributeMap["Version"] = status.Version
|
|
|
|
|
}
|
2023-11-03 14:22:55 -07:00
|
|
|
if status.Uptime > 0 {
|
|
|
|
|
nonAttributeMap["Uptime"] = status.Uptime.Round(time.Second)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
|
|
|
|
|
|
|
|
|
|
ret := []string{
|
|
|
|
|
"",
|
|
|
|
|
"Status:",
|
|
|
|
|
base.WrapMap(2, maxLength+2, nonAttributeMap),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(status.Users) > 0 {
|
|
|
|
|
ret = append(ret, printUsersTable(status.Users)...)
|
|
|
|
|
}
|
|
|
|
|
return base.WrapForHelpText(ret)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func printUsersTable(us []daemon.UserStatus) []string {
|
|
|
|
|
ret := []string{
|
|
|
|
|
"",
|
|
|
|
|
}
|
|
|
|
|
for _, u := range us {
|
|
|
|
|
ret = append(ret, " User:")
|
|
|
|
|
nonAttributeMap := map[string]any{
|
|
|
|
|
"Id": u.Id,
|
2023-12-20 11:07:38 -08:00
|
|
|
"Address": u.BoundaryInstance.Address,
|
2023-11-03 14:22:55 -07:00
|
|
|
"AuthToken Count": len(u.AuthTokens),
|
2023-12-20 11:07:38 -08:00
|
|
|
"Search Support": u.BoundaryInstance.CacheSupport,
|
|
|
|
|
}
|
|
|
|
|
if u.BoundaryInstance.LastSupportCheck > 0 {
|
|
|
|
|
nonAttributeMap["Since Search Support Check"] = u.BoundaryInstance.LastSupportCheck.Round(time.Second)
|
2023-11-03 14:22:55 -07:00
|
|
|
}
|
|
|
|
|
maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
|
|
|
|
|
ret = append(ret,
|
|
|
|
|
base.WrapMap(4, maxLength+4, nonAttributeMap),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for _, at := range u.AuthTokens {
|
|
|
|
|
nonAttributeMap := map[string]any{
|
|
|
|
|
"Id": at.Id,
|
|
|
|
|
}
|
|
|
|
|
if at.KeyringReferences > 0 {
|
|
|
|
|
nonAttributeMap["In Keyring"] = "true"
|
|
|
|
|
}
|
|
|
|
|
maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
|
|
|
|
|
ret = append(ret,
|
|
|
|
|
" AuthToken:",
|
|
|
|
|
base.WrapMap(6, maxLength+6, nonAttributeMap),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, r := range u.Resources {
|
|
|
|
|
nonAttributeMap := map[string]any{
|
|
|
|
|
"Count": r.Count,
|
|
|
|
|
}
|
|
|
|
|
if r.RefreshToken != nil {
|
|
|
|
|
nonAttributeMap["Since Last Refresh"] = r.RefreshToken.LastUsed.Round(time.Second)
|
2023-12-20 11:07:38 -08:00
|
|
|
nonAttributeMap["Since Initial Fetch"] = r.RefreshToken.Age.Round(time.Second)
|
2023-11-03 14:22:55 -07:00
|
|
|
}
|
|
|
|
|
if r.LastError != nil {
|
|
|
|
|
nonAttributeMap["Since Last Error"] = r.LastError.LastReturned.Round(time.Second)
|
|
|
|
|
nonAttributeMap["Last Error Message"] = r.LastError.Error
|
|
|
|
|
}
|
|
|
|
|
maxLength := base.MaxAttributesLength(nonAttributeMap, nil, nil)
|
|
|
|
|
ret = append(ret,
|
2024-01-04 12:51:21 -08:00
|
|
|
fmt.Sprintf(" %s:", cases.Title(language.English).String(r.Name)),
|
2023-11-03 14:22:55 -07:00
|
|
|
base.WrapMap(6, maxLength+6, nonAttributeMap),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
}
|