Skip to content

Commit

Permalink
feat: adds agent config command that returns the agent configuration
Browse files Browse the repository at this point in the history
- closes #4113
  • Loading branch information
frrist committed Oct 28, 2024
1 parent efddb20 commit 5b0f545
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
40 changes: 40 additions & 0 deletions cmd/cli/agent/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package agent

import (
"fmt"

"github.com/spf13/cobra"

"github.com/bacalhau-project/bacalhau/cmd/util"
"github.com/bacalhau-project/bacalhau/pkg/publicapi/client/v2"
)

func NewConfigCmd() *cobra.Command {
configCmd := &cobra.Command{
Use: "config",
Short: "Get the agent's configuration.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := util.SetupRepoConfig(cmd)
if err != nil {
return fmt.Errorf("failed to setup repo: %w", err)
}
api, err := util.GetAPIClientV2(cmd, cfg)
if err != nil {
return fmt.Errorf("failed to create api client: %w", err)
}
return run(cmd, api)
},
}
return configCmd
}

func run(cmd *cobra.Command, api client.API) error {
ctx := cmd.Context()
response, err := api.Agent().Config(ctx)
if err != nil {
return fmt.Errorf("cannot get agent config: %w", err)
}
cmd.Println(string(response.Config))
return nil
}
1 change: 1 addition & 0 deletions cmd/cli/agent/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ func NewCmd() *cobra.Command {
cmd.AddCommand(NewAliveCmd())
cmd.AddCommand(NewNodeCmd())
cmd.AddCommand(NewVersionCmd())
cmd.AddCommand(NewConfigCmd())
return cmd
}
9 changes: 8 additions & 1 deletion pkg/publicapi/apimodels/agent.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package apimodels

import "github.com/bacalhau-project/bacalhau/pkg/models"
import (
"github.com/bacalhau-project/bacalhau/pkg/models"
)

// IsAliveResponse is the response to the IsAlive request.
type IsAliveResponse struct {
Expand Down Expand Up @@ -30,3 +32,8 @@ type GetAgentNodeResponse struct {
BaseGetResponse
*models.NodeState
}

type GetAgentConfigResponse struct {
BaseGetResponse
Config []byte
}
6 changes: 6 additions & 0 deletions pkg/publicapi/client/v2/api_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ func (c *Agent) Node(ctx context.Context, req *apimodels.GetAgentNodeRequest) (*
err := c.client.Get(ctx, "/api/v1/agent/node", req, &res)
return &res, err
}

func (c *Agent) Config(ctx context.Context) (*apimodels.GetAgentConfigResponse, error) {
var res apimodels.GetAgentConfigResponse
err := c.client.Get(ctx, "/api/v1/agent/config", &apimodels.BaseGetRequest{}, &res)
return &res, err
}
14 changes: 11 additions & 3 deletions pkg/publicapi/endpoint/agent/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package agent

import (
"fmt"
"net/http"

"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"

"github.com/bacalhau-project/bacalhau/pkg/config/types"
"github.com/bacalhau-project/bacalhau/pkg/models"
Expand Down Expand Up @@ -113,16 +115,22 @@ func (e *Endpoint) debug(c echo.Context) error {
return c.JSON(http.StatusOK, debugInfoMap)
}

// debug godoc
// config godoc
//
// @ID agent/config
// @Summary Returns the current configuration of the node.
// @Tags Ops
// @Produce json
// @Success 200 {object} types.Bacalhau
// @Success 200 {object} []byte
// @Failure 500 {object} string
// @Router /api/v1/agent/config [get]
func (e *Endpoint) config(c echo.Context) error {
cfg := e.bacalhauConfig
return c.JSON(http.StatusOK, cfg)
cfgBytes, err := yaml.Marshal(cfg)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to marshal agent config: %s", err))
}
return c.JSON(http.StatusOK, apimodels.GetAgentConfigResponse{
Config: cfgBytes,
})
}

0 comments on commit 5b0f545

Please sign in to comment.