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

added FetchContainerLogs method #214

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Fula.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Fula' # Name for your pod
s.version = '1.43.0'
s.version = '1.44.0'
s.summary = 'Go-fula for iOS'
s.homepage = 'https://github.com/functionland/go-fula'

Expand Down
3 changes: 3 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@
actionEraseBlData: func(from peer.ID, w http.ResponseWriter, r *http.Request) {
bl.handleEraseBlData(r.Context(), from, w, r)
},
actionFetchContainerLogs: func(from peer.ID, w http.ResponseWriter, r *http.Request) {
bl.handleFetchContainerLogs(r.Context(), from, w, r)
},
}

// Look up the function in the map and call it
Expand Down Expand Up @@ -1014,7 +1017,7 @@
err = bl.p.Start(ctx)
if err != nil {
log.Errorw("Error when starting the Ping Server", "PeerID", user.PeerID, "err", err)
} else {

Check failure on line 1020 in blockchain/blockchain.go

View workflow job for this annotation

GitHub Actions / All

empty branch (SA9003)

Check failure on line 1020 in blockchain/blockchain.go

View workflow job for this annotation

GitHub Actions / All

empty branch (SA9003)
// TODO: THIS METHOD BELOW NEEDS TO RE_INITIALIZE ANNONCEMENTS WITH NEW TOPIC ND START IT FIRST
/*
log.Debugw("Found self peerID and ran Ping Server and announcing pooljoinrequest now", "peer", user.PeerID)
Expand Down
75 changes: 75 additions & 0 deletions blockchain/blox.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,43 @@

}

func (bl *FxBlockchain) FetchContainerLogs(ctx context.Context, to peer.ID, r wifi.FetchContainerLogsRequest) ([]byte, error) {

if bl.allowTransientConnection {
ctx = network.WithUseTransient(ctx, "fx.blockchain")
}

var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(r); err != nil {
return nil, err
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://"+to.String()+".invalid/"+actionFetchContainerLogs, &buf)
if err != nil {
return nil, err
}
resp, err := bl.c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
switch {
case err != nil:
return nil, err
case resp.StatusCode != http.StatusAccepted:
// Attempt to parse the body as JSON.
if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil {
// If we can't parse the JSON, return the original body in the error.
return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b))
}
// Return the parsed error message and description.
return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description)
default:
return b, nil
}
}

func (bl *FxBlockchain) DeleteWifi(ctx context.Context, to peer.ID, r wifi.DeleteWifiRequest) ([]byte, error) {

if bl.allowTransientConnection {
Expand Down Expand Up @@ -281,3 +318,41 @@
return b, nil
}
}

func (bl *FxBlockchain) handleFetchContainerLogs(ctx context.Context, from peer.ID, w http.ResponseWriter, r *http.Request) {
log := log.With("action", actionFetchContainerLogs, "from", from)

// Parse the JSON body of the request into the DeleteWifiRequest struct
var req wifi.FetchContainerLogsRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
log.Error("failed to decode request: %v", err)
http.Error(w, "failed to decode request", http.StatusBadRequest)
return
}
log.Debugw("handleFetchContainerLogs received", "req", req)

out := wifi.FetchContainerLogsResponse{

Check failure on line 334 in blockchain/blox.go

View workflow job for this annotation

GitHub Actions / All

this value of out is never used (SA4006)

Check failure on line 334 in blockchain/blox.go

View workflow job for this annotation

GitHub Actions / All

this value of out is never used (SA4006)
Status: true,
Msg: "",
}
res, err := wifi.FetchContainerLogs(ctx, req)
if err != nil {
out = wifi.FetchContainerLogsResponse{
Status: false,
Msg: err.Error(),
}
} else {
out = wifi.FetchContainerLogsResponse{
Status: true,
Msg: res,
}
}

w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(out); err != nil {
log.Error("failed to write response: %v", err)
http.Error(w, "failed to write response", http.StatusInternalServerError)
return
}

}
58 changes: 31 additions & 27 deletions blockchain/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ const (
actionManifestRemoveStored = "fula-manifest-remove_storing_manifest"

//Hardware
actionBloxFreeSpace = "blox-free-space"
actionEraseBlData = "erase-blockchain-data"
actionWifiRemoveall = "wifi-removeall"
actionReboot = "reboot"
actionPartition = "partition"
actionDeleteFulaConfig = "delete-fula-config"
actionDeleteWifi = "delete-wifi"
actionDisconnectWifi = "disconnect-wifi"
actionGetAccount = "get-account"
actionBloxFreeSpace = "blox-free-space"
actionEraseBlData = "erase-blockchain-data"
actionWifiRemoveall = "wifi-removeall"
actionReboot = "reboot"
actionPartition = "partition"
actionDeleteFulaConfig = "delete-fula-config"
actionDeleteWifi = "delete-wifi"
actionDisconnectWifi = "disconnect-wifi"
actionGetAccount = "get-account"
actionFetchContainerLogs = "fetch-container-logs"
)

type LinkWithLimit struct {
Expand Down Expand Up @@ -354,6 +355,7 @@ type Blockchain interface {
Partition(context.Context, peer.ID) ([]byte, error)
DeleteFulaConfig(context.Context, peer.ID) ([]byte, error)
GetAccount(context.Context, peer.ID) ([]byte, error)
FetchContainerLogs(context.Context, peer.ID, wifi.FetchContainerLogsRequest) ([]byte, error)
}

var requestTypes = map[string]reflect.Type{
Expand Down Expand Up @@ -381,15 +383,16 @@ var requestTypes = map[string]reflect.Type{
actionTransferToMumbai: reflect.TypeOf(TransferToFulaRequest{}),

//Hardware
actionBloxFreeSpace: reflect.TypeOf(wifi.BloxFreeSpaceRequest{}),
actionEraseBlData: reflect.TypeOf(wifi.EraseBlDataRequest{}),
actionWifiRemoveall: reflect.TypeOf(wifi.WifiRemoveallRequest{}),
actionReboot: reflect.TypeOf(wifi.RebootRequest{}),
actionPartition: reflect.TypeOf(wifi.PartitionRequest{}),
actionDeleteFulaConfig: reflect.TypeOf(wifi.DeleteFulaConfigRequest{}),
actionDeleteWifi: reflect.TypeOf(wifi.DeleteWifiRequest{}),
actionDisconnectWifi: reflect.TypeOf(wifi.DeleteWifiRequest{}),
actionGetAccount: reflect.TypeOf(GetAccountRequest{}),
actionBloxFreeSpace: reflect.TypeOf(wifi.BloxFreeSpaceRequest{}),
actionEraseBlData: reflect.TypeOf(wifi.EraseBlDataRequest{}),
actionWifiRemoveall: reflect.TypeOf(wifi.WifiRemoveallRequest{}),
actionReboot: reflect.TypeOf(wifi.RebootRequest{}),
actionPartition: reflect.TypeOf(wifi.PartitionRequest{}),
actionDeleteFulaConfig: reflect.TypeOf(wifi.DeleteFulaConfigRequest{}),
actionDeleteWifi: reflect.TypeOf(wifi.DeleteWifiRequest{}),
actionDisconnectWifi: reflect.TypeOf(wifi.DeleteWifiRequest{}),
actionGetAccount: reflect.TypeOf(GetAccountRequest{}),
actionFetchContainerLogs: reflect.TypeOf(wifi.FetchContainerLogsRequest{}),
}

var responseTypes = map[string]reflect.Type{
Expand Down Expand Up @@ -417,13 +420,14 @@ var responseTypes = map[string]reflect.Type{
actionTransferToMumbai: reflect.TypeOf(TransferToFulaResponse{}),

//Hardware
actionBloxFreeSpace: reflect.TypeOf(wifi.BloxFreeSpaceResponse{}),
actionEraseBlData: reflect.TypeOf(wifi.EraseBlDataResponse{}),
actionWifiRemoveall: reflect.TypeOf(wifi.WifiRemoveallResponse{}),
actionReboot: reflect.TypeOf(wifi.RebootResponse{}),
actionPartition: reflect.TypeOf(wifi.PartitionResponse{}),
actionDeleteFulaConfig: reflect.TypeOf(wifi.DeleteFulaConfigResponse{}),
actionDeleteWifi: reflect.TypeOf(wifi.DeleteWifiResponse{}),
actionDisconnectWifi: reflect.TypeOf(wifi.DeleteWifiResponse{}),
actionGetAccount: reflect.TypeOf(GetAccountResponse{}),
actionBloxFreeSpace: reflect.TypeOf(wifi.BloxFreeSpaceResponse{}),
actionEraseBlData: reflect.TypeOf(wifi.EraseBlDataResponse{}),
actionWifiRemoveall: reflect.TypeOf(wifi.WifiRemoveallResponse{}),
actionReboot: reflect.TypeOf(wifi.RebootResponse{}),
actionPartition: reflect.TypeOf(wifi.PartitionResponse{}),
actionDeleteFulaConfig: reflect.TypeOf(wifi.DeleteFulaConfigResponse{}),
actionDeleteWifi: reflect.TypeOf(wifi.DeleteWifiResponse{}),
actionDisconnectWifi: reflect.TypeOf(wifi.DeleteWifiResponse{}),
actionGetAccount: reflect.TypeOf(GetAccountResponse{}),
actionFetchContainerLogs: reflect.TypeOf(wifi.FetchContainerLogsResponse{}),
}
7 changes: 7 additions & 0 deletions mobile/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,10 @@ func (c *Client) GetAccount() ([]byte, error) {
ctx := context.TODO()
return c.bl.GetAccount(ctx, c.bloxPid)
}

// GetAccount requests blox at Config.BloxAddr to get the balance of the account.
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) FetchContainerLogs(ContainerName string, TailCount string) ([]byte, error) {
ctx := context.TODO()
return c.bl.FetchContainerLogs(ctx, c.bloxPid, wifi.FetchContainerLogsRequest{ContainerName: ContainerName, TailCount: TailCount})
}
42 changes: 42 additions & 0 deletions wap/pkg/wifi/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"os/exec"
"strconv"
"strings"
Expand All @@ -28,6 +29,15 @@ type BloxFreeSpaceResponse struct {
UsedPercentage float32 `json:"used_percentage"`
}

type FetchContainerLogsResponse struct {
Status bool `json:"status"`
Msg string `json:"msg"`
}
type FetchContainerLogsRequest struct {
ContainerName string
TailCount string
}

type DockerInfo struct {
Image string `json:"image"`
Version string `json:"version"`
Expand All @@ -42,6 +52,13 @@ type Config struct {
// other fields
}

type SyncInfo struct {
Best string
Target string
Finalized string
Speed string
}

const (
B = 1
KB = 1024 * B
Expand Down Expand Up @@ -203,3 +220,28 @@ func GetContainerInfo(containerName string) (DockerInfo, error) {

return info, nil
}

func FetchContainerLogs(ctx context.Context, req FetchContainerLogsRequest) (string, error) {
cli, err := client.NewClientWithOpts(client.WithAPIVersionNegotiation(), client.WithHost("unix:///var/run/docker.sock"))
if err != nil {
return "", fmt.Errorf("creating Docker client: %w", err)
}

options := types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Tail: req.TailCount, // Adjust the number of lines as needed
}
logs, err := cli.ContainerLogs(ctx, req.ContainerName, options)
if err != nil {
return "", fmt.Errorf("getting container logs: %w", err)
}
defer logs.Close()

logBytes, err := io.ReadAll(logs)
if err != nil {
return "", fmt.Errorf("reading container logs: %w", err)
}

return string(logBytes), nil
}
2 changes: 1 addition & 1 deletion wap/props.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"connection":"9a:09:32:25:44:e8","country_code":"GB","password":"hhio618123.","ssid":"9a:09:32:25:44:e8"}
{"connection":"9a:09:32:25:44:e8","country_code":"GB","password":".","ssid":"9a:09:32:25:44:e8"}
Loading