Squashed commit of the following:

commit f5baab4e88
Author: DaneEveritt <dane@daneeveritt.com>
Date:   Sat Jul 9 17:50:53 2022 -0400

    Finalize activity event sending logic and cron config

commit 9830387f21
Author: DaneEveritt <dane@daneeveritt.com>
Date:   Sat Jul 9 16:26:13 2022 -0400

    Send power events in a more usable format

commit 49f3a61d16
Author: DaneEveritt <dane@daneeveritt.com>
Date:   Sat Jul 9 15:47:24 2022 -0400

    Configure cron to actually send to endpoint

commit 28137c4c14
Author: DaneEveritt <dane@daneeveritt.com>
Date:   Sat Jul 9 15:42:29 2022 -0400

    Copy the body buffer otherwise subsequent backoff attempts will not have a buffer to send

commit 20e44bdc55
Author: DaneEveritt <dane@daneeveritt.com>
Date:   Sat Jul 9 14:38:41 2022 -0400

    Add internal logic to process activity events and send them to the panel

commit 0380488cd2
Author: DaneEveritt <dane@daneeveritt.com>
Date:   Mon Jul 4 17:55:17 2022 -0400

    Track power events

commit 9eab08b92f
Author: DaneEveritt <dane@daneeveritt.com>
Date:   Mon Jul 4 17:36:03 2022 -0400

    Initial logic to support logging activity on Wings to send back to the panel
This commit is contained in:
DaneEveritt
2022-07-09 17:51:19 -04:00
parent 9864a0fe34
commit ed330fa6be
17 changed files with 463 additions and 14 deletions

View File

@@ -30,6 +30,7 @@ type Client interface {
SetInstallationStatus(ctx context.Context, uuid string, successful bool) error
SetTransferStatus(ctx context.Context, uuid string, successful bool) error
ValidateSftpCredentials(ctx context.Context, request SftpAuthRequest) (SftpAuthResponse, error)
SendActivityLogs(ctx context.Context, activity []json.RawMessage) error
}
type client struct {
@@ -128,10 +129,19 @@ func (c *client) requestOnce(ctx context.Context, method, path string, body io.R
// and adds the required authentication headers to the request that is being
// created. Errors returned will be of the RequestError type if there was some
// type of response from the API that can be parsed.
func (c *client) request(ctx context.Context, method, path string, body io.Reader, opts ...func(r *http.Request)) (*Response, error) {
func (c *client) request(ctx context.Context, method, path string, body *bytes.Buffer, opts ...func(r *http.Request)) (*Response, error) {
var res *Response
err := backoff.Retry(func() error {
r, err := c.requestOnce(ctx, method, path, body, opts...)
var b bytes.Buffer
if body != nil {
// We have to create a copy of the body, otherwise attempting this request again will
// send no data if there was initially a body since the "requestOnce" method will read
// the whole buffer, thus leaving it empty at the end.
if _, err := b.Write(body.Bytes()); err != nil {
return backoff.Permanent(errors.Wrap(err, "http: failed to copy body buffer"))
}
}
r, err := c.requestOnce(ctx, method, path, &b, opts...)
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return backoff.Permanent(err)

View File

@@ -3,6 +3,7 @@ package remote
import (
"context"
"fmt"
"github.com/goccy/go-json"
"strconv"
"sync"
@@ -178,6 +179,16 @@ func (c *client) SendRestorationStatus(ctx context.Context, backup string, succe
return nil
}
// SendActivityLogs sends activity logs back to the Panel for processing.
func (c *client) SendActivityLogs(ctx context.Context, activity []json.RawMessage) error {
resp, err := c.Post(ctx, "/activity", d{"data": activity})
if err != nil {
return errors.WithStackIf(err)
}
_ = resp.Body.Close()
return nil
}
// getServersPaged returns a subset of servers from the Panel API using the
// pagination query parameters.
func (c *client) getServersPaged(ctx context.Context, page, limit int) ([]RawServerData, Pagination, error) {

View File

@@ -2,11 +2,10 @@ package remote
import (
"bytes"
"regexp"
"strings"
"github.com/apex/log"
"github.com/goccy/go-json"
"regexp"
"strings"
"github.com/pterodactyl/wings/parser"
)