Skip to content

Commit

Permalink
STAC-21470: Add agent list command
Browse files Browse the repository at this point in the history
  • Loading branch information
craffit committed Aug 1, 2024
1 parent 9013c47 commit 7ee284a
Show file tree
Hide file tree
Showing 74 changed files with 10,397 additions and 1,747 deletions.
18 changes: 18 additions & 0 deletions cmd/agent.go
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
}
79 changes: 79 additions & 0 deletions cmd/agent/agent_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package agent

import (
"cmp"
"fmt"

Check failure on line 5 in cmd/agent/agent_list.go

View workflow job for this annotation

GitHub Actions / Linter

File is not `goimports`-ed (goimports)
"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 {

Check failure on line 48 in cmd/agent/agent_list.go

View workflow job for this annotation

GitHub Actions / Linter

ifElseChain: rewrite if-else to switch statement (gocritic)
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
}
1 change: 1 addition & 0 deletions cmd/sts.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func STSCommand(cli *di.Deps) *cobra.Command {
cmd.AddCommand(TopicCommand(cli))
cmd.AddCommand(TopologySyncCommand(cli))
cmd.AddCommand(IngestionApiKeyCommand(cli))
cmd.AddCommand(AgentCommand(cli))

return cmd
}
42 changes: 42 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions flake.nix
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;
};
});
}
Loading

0 comments on commit 7ee284a

Please sign in to comment.