Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds agent config command that returns the agent configuration #4671

Merged
merged 7 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
})
}
Loading