-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
74 changed files
with
10,397 additions
and
1,747 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/stackvista/stackstate-cli/cmd/agent" | ||
"github.com/stackvista/stackstate-cli/internal/di" | ||
) | ||
|
||
func AgentCommand(deps *di.Deps) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "agent", | ||
Short: "Manage the StackState agents", | ||
Long: "Manage the StackState agents.", | ||
} | ||
|
||
cmd.AddCommand(agent.ListCommand(deps)) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package agent | ||
|
||
import ( | ||
"cmp" | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"github.com/stackvista/stackstate-cli/generated/stackstate_api" | ||
"github.com/stackvista/stackstate-cli/internal/common" | ||
"github.com/stackvista/stackstate-cli/internal/di" | ||
"github.com/stackvista/stackstate-cli/internal/printer" | ||
"slices" | ||
"time" | ||
) | ||
|
||
func ListCommand(deps *di.Deps) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "List all registered agents", | ||
Long: "List all registered agents", | ||
RunE: deps.CmdRunEWithApi(RunListCommand), | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func RunListCommand(cmd *cobra.Command, cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo) common.CLIError { | ||
agents, resp, err := api.AgentRegistrationsApi.AllAgentRegistrations(cli.Context).Execute() | ||
|
||
if err != nil { | ||
return common.NewResponseError(err, resp) | ||
} | ||
|
||
agentList := agents.Agents | ||
|
||
slices.SortFunc(agentList, func(a, b stackstate_api.AgentRegistration) int { | ||
if n := cmp.Compare(a.Lease, b.Lease); n != 0 { | ||
return n | ||
} | ||
// If leases are equal, order by registration moment | ||
return cmp.Compare(a.RegisteredEpochMs, b.RegisteredEpochMs) | ||
}) | ||
|
||
var active = 0 | ||
var limited = 0 | ||
var stale = 0 | ||
|
||
for _, agent := range agentList { | ||
if agent.Lease == stackstate_api.AGENTLEASE_ACTIVE { | ||
active++ | ||
} else if agent.Lease == stackstate_api.AGENTLEASE_LIMITED { | ||
limited++ | ||
} else { | ||
stale++ | ||
} | ||
} | ||
|
||
if cli.IsJson() { | ||
cli.Printer.PrintJson(map[string]interface{}{ | ||
"agents": agentList, | ||
}) | ||
} else { | ||
data := make([][]interface{}, len(agentList)) | ||
currentTime := time.Now() | ||
|
||
for i, agent := range agentList { | ||
data[i] = []interface{}{agent.AgentId, agent.Lease, currentTime.Sub(time.UnixMilli(agent.RegisteredEpochMs)).String(), time.UnixMilli(agent.GetLeaseUntilEpochMs()).String()} | ||
} | ||
cli.Printer.Table(printer.TableData{ | ||
Header: []string{"Host", "Lease", "Age", "Last Lease"}, | ||
Data: data, | ||
MissingTableDataMsg: printer.NotFoundMsg{Types: "topics"}, | ||
}) | ||
|
||
cli.Printer.PrintLn("") | ||
cli.Printer.PrintLn(fmt.Sprintf("Totals: %d active, %d limited, %d stale", active, limited, stale)) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
{ | ||
description = "StackState CLI"; | ||
|
||
nixConfig.bash-prompt = "STS CLI 2 $ "; | ||
|
||
inputs = { | ||
nixpkgs.url = "github:nixos/nixpkgs"; | ||
flake-utils.url = "github:numtide/flake-utils"; | ||
}; | ||
|
||
outputs = { self, nixpkgs, flake-utils }: | ||
flake-utils.lib.eachDefaultSystem (system: | ||
let | ||
pkgs = import nixpkgs { inherit system; overlays = [ ]; }; | ||
pkgs-linux = import nixpkgs { system = "x86_64-linux"; overlays = [ ]; }; | ||
|
||
# Dependencies used for both development and CI/CD | ||
sharedDeps = pkgs: (with pkgs; [ | ||
bash | ||
go_1_19 | ||
gotools | ||
diffutils # Required for golangci-lint | ||
golangci-lint | ||
openapi-generator-cli | ||
]); | ||
|
||
# Dependencies used only by CI/CD | ||
ciDeps = pkgs: (with pkgs; [ | ||
git | ||
cacert | ||
gcc | ||
coreutils-full | ||
goreleaser | ||
awscli | ||
docker | ||
]); | ||
|
||
darwinDevShellExtraDeps = pkgs: pkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs.darwin.apple_sdk_11_0; [ | ||
Libsystem | ||
IOKit | ||
]); | ||
in { | ||
|
||
devShells = { | ||
dev = pkgs.mkShell { | ||
buildInputs = sharedDeps(pkgs) ++ darwinDevShellExtraDeps(pkgs); | ||
}; | ||
|
||
ci = pkgs.mkShell { | ||
buildInputs = sharedDeps(pkgs) ++ ciDeps(pkgs); | ||
}; | ||
}; | ||
|
||
devShell = self.devShells."${system}".dev; | ||
|
||
packages = { | ||
sts = pkgs.buildGo119Module { | ||
pname = "sts"; | ||
version = "2.0.0"; | ||
|
||
src = ./.; | ||
|
||
# This hash locks the dependencies of this package. | ||
# Change it to the provided when the go dependencies change. | ||
# See https://www.tweag.io/blog/2021-03-04-gomod2nix/ for details. | ||
# | ||
# NOTE In case if your build fails due to incosistency in vendor modules | ||
# Comment out the real hash and uncomment the fake one then on next `nix build .` run | ||
# you will get a new real hash which can be used here. | ||
# | ||
# vendorSha256 = pkgs.lib.fakeSha256; | ||
vendorSha256 = "sha256-aXTDHT1N+4Qpkuxb8vvBvP2VPyS5ofCgX6XFhJ5smUQ="; | ||
|
||
postInstall = '' | ||
mv $out/bin/stackstate-cli2 $out/bin/sts | ||
''; | ||
}; | ||
|
||
ci-image = pkgs.dockerTools.buildImage { | ||
name = "stackstate-cli2-ci"; | ||
tag = "latest"; | ||
created = "now"; | ||
|
||
contents = sharedDeps(pkgs-linux) ++ ciDeps(pkgs-linux); | ||
|
||
config = { | ||
Env = [ | ||
"GIT_SSL_CAINFO=/etc/ssl/certs/ca-bundle.crt" | ||
"SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt" | ||
]; | ||
|
||
# Required to make golangci-lint work. | ||
Volumes = { | ||
"/tmp" = {}; | ||
}; | ||
}; | ||
}; | ||
|
||
default = self.packages."${system}".sts; | ||
}; | ||
|
||
apps = { | ||
sts = { | ||
type = "app"; | ||
program = "${self.packages."${system}".sts}/bin/sts"; | ||
}; | ||
|
||
default = self.apps."${system}".sts; | ||
}; | ||
}); | ||
} |
Oops, something went wrong.