-
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.
implement log forwarding from client
- Loading branch information
1 parent
ab8f45a
commit d2f32b5
Showing
8 changed files
with
187 additions
and
105 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
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,64 @@ | ||
package agent | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/sonroyaalmerol/pbs-d2d-backup/internal/utils" | ||
) | ||
|
||
var httpClient *http.Client | ||
|
||
func ProxmoxHTTPRequest(method, url string, body io.Reader, respBody any) error { | ||
serverUrl := os.Getenv("PBS_AGENT_SERVER") | ||
req, err := http.NewRequest( | ||
method, | ||
fmt.Sprintf( | ||
"%s%s", | ||
strings.TrimSuffix(serverUrl, "/"), | ||
url, | ||
), | ||
body, | ||
) | ||
|
||
if err != nil { | ||
return fmt.Errorf("ProxmoxHTTPRequest: error creating http request -> %w", err) | ||
} | ||
|
||
req.Header.Add("Content-Type", "application/json") | ||
|
||
if httpClient == nil { | ||
httpClient = &http.Client{ | ||
Timeout: time.Second * 30, | ||
Transport: utils.BaseTransport, | ||
} | ||
} | ||
|
||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("ProxmoxHTTPRequest: error executing http request -> %w", err) | ||
} | ||
defer func() { | ||
_, _ = io.Copy(io.Discard, resp.Body) | ||
resp.Body.Close() | ||
}() | ||
|
||
rawBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf("ProxmoxHTTPRequest: error getting body content -> %w", err) | ||
} | ||
|
||
if respBody != nil { | ||
err = json.Unmarshal(rawBody, respBody) | ||
if err != nil { | ||
return fmt.Errorf("ProxmoxHTTPRequest: error json unmarshal body content (%s) -> %w", string(rawBody), err) | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package serverlog | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/sonroyaalmerol/pbs-d2d-backup/internal/agent" | ||
) | ||
|
||
type Logger struct { | ||
Hostname string | ||
} | ||
|
||
type PingResponse struct { | ||
Pong bool `json:"pong"` | ||
} | ||
|
||
func InitializeLogger() (*Logger, error) { | ||
var pingResp PingResponse | ||
err := agent.ProxmoxHTTPRequest(http.MethodGet, "/api2/json/ping", nil, &pingResp) | ||
if err != nil { | ||
return nil, fmt.Errorf("InitializeLogger: failed to ping server -> %w", err) | ||
} | ||
|
||
hostname, _ := os.Hostname() | ||
return &Logger{Hostname: hostname}, nil | ||
} | ||
|
||
type LogRequest struct { | ||
Hostname string `json:"hostname"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func (l *Logger) Print(v string) { | ||
body, err := json.Marshal(&LogRequest{ | ||
Hostname: l.Hostname, | ||
Message: v, | ||
}) | ||
if err != nil { | ||
log.Println(fmt.Errorf("Print: error marshalling request body -> %w", err).Error()) | ||
} | ||
|
||
err = agent.ProxmoxHTTPRequest(http.MethodPost, "/ap2/json/d2d/agent-log", bytes.NewBuffer(body), nil) | ||
if err != nil { | ||
log.Println(fmt.Errorf("Print: error posting log to server -> %w", err).Error()) | ||
} | ||
} |
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
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
Oops, something went wrong.