Skip to content

Commit

Permalink
Copy logging config if doesn't exist, and set telemetry name based on…
Browse files Browse the repository at this point in the history
… env var (#10)
  • Loading branch information
hsoerensen authored Aug 16, 2024
1 parent 53cdaaf commit 60eeaee
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 18 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ WORKDIR /
COPY ./tools/ /tools
COPY Makefile /
COPY go.mod /
COPY go.sum /

RUN make all

Expand Down
2 changes: 2 additions & 0 deletions Dockerfile.participation
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ WORKDIR /
COPY ./tools/ /tools
COPY Makefile /
COPY go.mod /
COPY go.sum /

RUN make all

Expand All @@ -31,5 +32,6 @@ COPY --from=algorand --chown=0:0 /node/bin/kmd /node/bin/kmd
COPY --from=algorand --chown=0:0 /node/bin/diagcfg /node/bin/diagcfg
COPY --from=builder /build/ /node/bin/
COPY configuration /algod/configuration
COPY configuration/logging.config /algod/data/logging.config

CMD ["/node/bin/start-node"]
1 change: 1 addition & 0 deletions configuration/logging.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Enable":false,"SendToLog":false,"URI":"","Name":"","GUID":"","FilePath":"","UserName":"","Password":"","MinLogLevel":3,"ReportHistoryLevel":3}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module github.com/voinetwork/voi-node

go 1.22.5
go 1.22.5

require (
"github.com/google/uuid" v1.6.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
33 changes: 16 additions & 17 deletions tools/start-node/start-node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package main

import (
"flag"
"fmt"
"github.com/voinetwork/voi-node/tools/utils"
"log"
"os"
"time"
)

const (
envCatchupVar = "VOINETWORK_CATCHUP"
algodDataDir = "/algod/data"
algodLogConfig = "/algod/data/logging.config"
algodCmd = "/node/bin/algod"
catchupCmd = "/node/bin/catch-catchpoint"
goalCmd = "/node/bin/goal"
envCatchupVar = "VOINETWORK_CATCHUP"
algodDataDir = "/algod/data"
algodLogConfig = "/algod/data/logging.config"
algodLogConfigDefault = "/algod/configuration/logging.config"
algodCmd = "/node/bin/algod"
catchupCmd = "/node/bin/catch-catchpoint"
goalCmd = "/node/bin/goal"
)

var network string
Expand Down Expand Up @@ -46,6 +46,12 @@ func main() {
profile = envProfile
}

fu := utils.FileUtils{}
err := fu.CopyFile(algodLogConfigDefault, algodLogConfig, false)
if err != nil {
log.Fatalf("Failed to copy logging configuration: %v", err)
}

log.Printf("Network: %s", network)
log.Printf("Profile: %s", profile)
log.Printf("Overwrite Config: %t", overwriteConfig)
Expand Down Expand Up @@ -73,18 +79,11 @@ func main() {
} else {
telemetryName, telemetrySet := nu.GetTelemetryNameFromEnv()

if telemetrySet && profile == "participation" {
fu := utils.FileUtils{}
err := fu.UpdateJSONAttribute(algodLogConfig, "Enable", true)
if profile == "participation" {
err := fu.SetTelemetryState(algodLogConfig, telemetryName, telemetrySet)
if err != nil {
fmt.Errorf("failed to enable telemetry: %v", err)
log.Fatalf("Failed to set telemetry state: %v", err)
}
err = fu.UpdateJSONAttribute(algodLogConfig, "Name", telemetryName)
if err != nil {
fmt.Errorf("failed to set telemetry name: %v", err)
}

log.Printf("Telemetry enabled. Telemetry Name: %s", telemetryName)
}

done = pu.StartProcess(algodCmd, "-d", algodDataDir)
Expand Down
57 changes: 57 additions & 0 deletions tools/utils/file_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"io"
"log"
"os"
Expand Down Expand Up @@ -94,6 +95,62 @@ func (fu FileUtils) UpdateJSONAttribute(filePath, attributeName string, newValue
return nil
}

func (fu FileUtils) EnsureGUIDExists(filePath string) error {
// Open and read the JSON file
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open JSON file: %v", err)
}
defer file.Close()

// Parse the JSON content
var data map[string]interface{}
decoder := json.NewDecoder(file)
if err := decoder.Decode(&data); err != nil {
return fmt.Errorf("failed to decode JSON content: %v", err)
}

// Check if the GUID attribute is empty and generate a new GUID if necessary
if guid, ok := data["GUID"].(string); ok && guid == "" {
newGUID := uuid.New().String()
data["GUID"] = newGUID
}

// Write the updated JSON content back to the file
updatedData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal updated JSON content: %v", err)
}

if err := os.WriteFile(filePath, updatedData, 0644); err != nil {
return fmt.Errorf("failed to write updated JSON content to file: %v", err)
}

return nil
}

func (fu FileUtils) SetTelemetryState(filePath, telemetryName string, enabled bool) error {
err := fu.UpdateJSONAttribute(filePath, "Enable", enabled)
if err != nil {
log.Printf("failed to set telemetry enabled state: %v", err)
return err
}
err = fu.UpdateJSONAttribute(filePath, "Name", telemetryName)
if err != nil {
log.Printf("failed to set telemetry name: %v", err)
return err
}

err = fu.EnsureGUIDExists(filePath)
if err != nil {
log.Printf("failed to ensure GUID exists: %v", err)
return err
}

log.Printf("Telemetry %s. Telemetry Name: %s", map[bool]string{true: "enabled", false: "disabled"}[enabled], telemetryName)
return nil
}

func fileExists(filePath string) bool {
_, err := os.Stat(filePath)
return !os.IsNotExist(err)
Expand Down

0 comments on commit 60eeaee

Please sign in to comment.