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

Add setup for sos report #64

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 32 additions & 6 deletions src/logger.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package main

import (
"fmt"
"io"
"os"
"path"

"git.sr.ht/~spc/go-log"
)

var sosReportFolder = "/etc/sos.extras.d"
var sosReportFile = "rhc-worker-logs"

// SetupLogger sets up the logger for the application and returns the log file.
// It creates a log folder if it doesn't exist, opens a log file, sets the log level
// based on the "YGG_LOG_LEVEL" environment variable, configures the log output to
// write to both standard output and the log file, and enables optional log features
// such as date-time, filename, and line number.
// Returns a pointer to an os.File representing the opened log file.
func setupLogger(logFolder string, fileName string) *os.File {
// Check if path exists, if not, create it.
if _, err := os.Stat(logFolder); err != nil {
if err := os.Mkdir(logFolder, os.ModePerm); err != nil {
log.Error(err)
}
if err := checkAndCreateDirectory(logFolder); err != nil {
log.Error(err)
}

logFilePath := path.Join(logFolder, fileName)
// open log file
logFile, err := os.Create(path.Join(logFolder, fileName))
logFile, err := os.Create(logFilePath)
if err != nil {
log.Error(err)
}
Expand All @@ -43,6 +45,9 @@ func setupLogger(logFolder string, fileName string) *os.File {
log.SetLevel(log.LevelInfo)
}

// Initialization for the sosreport extras plugin
setupSosExtrasReport(logFilePath)

// set log output
multWriter := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(multWriter)
Expand All @@ -52,3 +57,24 @@ func setupLogger(logFolder string, fileName string) *os.File {

return logFile
}

// setupSosExtrasReport sets up the sos report file for the sos_extra plugin to
// collect the logs for the worker, which is a special file that points out to
// the current path of the logfile for the worker.
func setupSosExtrasReport(fileContent string) {
if err := checkAndCreateDirectory(sosReportFolder); err != nil {
log.Error(err)
}

// open sosreport file
logFile, err := os.Create(path.Join(sosReportFolder, sosReportFile))
if err != nil {
log.Error(err)
}
defer logFile.Close()

content := fmt.Sprintf(":%s", fileContent)
if _, err := logFile.WriteString(content); err != nil {
log.Error(err)
}
}
29 changes: 29 additions & 0 deletions src/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"fmt"
"os"
"path"
"path/filepath"
"testing"

Expand Down Expand Up @@ -81,3 +83,30 @@ func TestSetupLogger(t *testing.T) {
})
}
}

func TestSetupSosExtrasReport(t *testing.T) {
// FIXME: We are overriding the globals for the below variables, not the
// best approach, but works for now.
sosReportFile = "log-file"
sosReportFolder = t.TempDir()
fileContent := path.Join(sosReportFolder, sosReportFile, "test-file")
expectedFileContent := fmt.Sprintf(":%s", fileContent)

setupSosExtrasReport(fileContent)
if _, err := os.Stat(sosReportFolder); os.IsNotExist(err) {
t.Errorf("Log folder not created: %v", err)
}

logFilePath := filepath.Join(sosReportFolder, sosReportFile)
if _, err := os.Stat(logFilePath); os.IsNotExist(err) {
t.Errorf("SOS report file not created: %v", err)
}

logFile, err := os.ReadFile(logFilePath)
if err != nil {
t.Errorf("Failed to read file: %v", err)
}
if string(logFile) != expectedFileContent {
t.Errorf("File content does not match")
}
}
7 changes: 4 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ const logFileName = "rhc-worker-bash.log"
var yggdDispatchSocketAddr string
var config *Config

// main is the entry point of the application. It initializes values from the environment,
// sets up the logger, establishes a connection with the dispatcher, registers as a handler,
// listens for incoming messages, and starts accepting connections as a Worker service.
// main is the entry point of the application. It initializes values from the
// environment, sets up the logger, establishes a connection with the
// dispatcher, registers as a handler, listens for incoming messages, and
// starts accepting connections as a Worker service.
// Note: The function blocks and runs indefinitely until the server is stopped.
func main() {
var yggSocketAddrExists bool // Has to be separately declared otherwise grpc.Dial doesn't work
Expand Down
14 changes: 14 additions & 0 deletions src/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,17 @@ func loadConfigOrDefault(filePath string) *Config {
setDefaultValues(config)
return config
}

// Helper function to check if a directory exists, if not, create the
// directory, otherwise, return nil. If it fails to create the directory, the
// function will return the error raised by `os.Mkdir` to the caller.
func checkAndCreateDirectory(folder string) error {
// Check if path exists, if not, create it.
if _, err := os.Stat(folder); err != nil {
if err := os.Mkdir(folder, os.ModePerm); err != nil {
return err
}
}

return nil
}
Loading