Skip to content

Commit

Permalink
SplitByPlugin for logs so it's easier to troubleshoot and do dev + ha…
Browse files Browse the repository at this point in the history
…ndling in_progress so we can use it to check if the registration happened in a certain time and work around #40 failures and retry
  • Loading branch information
NorseGaud committed Dec 11, 2024
1 parent c4ab9d9 commit e365436
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 61 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ Your config.yml file must define the database in one of the following ways:

### Plugin Setup and Usage Guides

You can control the location plugins are stored on the host by setting the `plugins_path` in the `config.yml` file. If not set, it will default to `~/.config/anklet/plugins/`.
You can control the location plugins are stored on the host by setting the `plugins_path` in the `config.yml` file. If not set, it will default to `~/.config/anklet/plugins/`.

**NOTE: Plugin names MUST be unique across all hosts.**

#### Github Actions

Expand Down
41 changes: 33 additions & 8 deletions internal/anka/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,40 @@ func (cli *Cli) AnkaStart(pluginCtx context.Context) error {
return nil
}

func (cli *Cli) AnkaCopy(pluginCtx context.Context, filesToCopyIn ...string) error {
if pluginCtx.Err() != nil {
return fmt.Errorf("context canceled before AnkaCopy")
func (cli *Cli) AnkaCopyOutOfVM(ctx context.Context, objectToCopyOut string, hostLevelDestination string) error {
if ctx.Err() != nil {
return fmt.Errorf("context canceled before AnkaCopyOutOfVM")
}
logger, err := logging.GetLoggerFromContext(pluginCtx)
logger, err := logging.GetLoggerFromContext(ctx)
if err != nil {
return err
}
vm, err := GetAnkaVmFromContext(pluginCtx)
vm, err := GetAnkaVmFromContext(ctx)
if err != nil {
return err
}
copyOutput, err := cli.ExecuteParseJson(ctx, "anka", "-j", "cp", "-a", fmt.Sprintf("%s:%s", vm.Name, objectToCopyOut), hostLevelDestination)
if err != nil {
return err
}
if copyOutput.Status != "OK" {
return fmt.Errorf("error copying into vm: %s", copyOutput.Message)
}
logger.DebugContext(ctx, "copy output", "std", copyOutput)
logger.InfoContext(ctx, "successfully copied %s out of vm", "object", objectToCopyOut, "stdout", copyOutput.Message)

return nil
}

func (cli *Cli) AnkaCopyIntoVM(ctx context.Context, filesToCopyIn ...string) error {
if ctx.Err() != nil {
return fmt.Errorf("context canceled before AnkaCopyIntoVM")
}
logger, err := logging.GetLoggerFromContext(ctx)
if err != nil {
return err
}
vm, err := GetAnkaVmFromContext(ctx)
if err != nil {
return err
}
Expand All @@ -345,15 +370,15 @@ func (cli *Cli) AnkaCopy(pluginCtx context.Context, filesToCopyIn ...string) err
return fmt.Errorf("error evaluating symlink for %s: %w", hostLevelFile, err)
}
hostLevelFile = realPath
copyOutput, err := cli.ExecuteParseJson(pluginCtx, "anka", "-j", "cp", "-a", hostLevelFile, fmt.Sprintf("%s:", vm.Name))
copyOutput, err := cli.ExecuteParseJson(ctx, "anka", "-j", "cp", "-a", hostLevelFile, fmt.Sprintf("%s:", vm.Name))
if err != nil {
return err
}
if copyOutput.Status != "OK" {
return fmt.Errorf("error copying into vm: %s", copyOutput.Message)
}
logger.DebugContext(pluginCtx, "copy output", "std", copyOutput)
logger.InfoContext(pluginCtx, "successfully copied file into vm", "file", hostLevelFile, "stdout", copyOutput.Message)
logger.DebugContext(ctx, "copy output", "std", copyOutput)
logger.InfoContext(ctx, "successfully copied file into vm", "file", hostLevelFile, "stdout", copyOutput.Message)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type Config struct {
}

type Log struct {
FileDir string `yaml:"file_dir"`
FileDir string `yaml:"file_dir"`
SplitByPlugin bool `yaml:"split_by_plugin"`
}

type Metrics struct {
Expand Down
8 changes: 4 additions & 4 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func UpdateLoggerToFile(logger *slog.Logger, filePath string, suffix string) (*s
return nil, "", err
}

options := &slog.HandlerOptions{Level: slog.LevelInfo}
options := &slog.HandlerOptions{Level: slog.LevelDebug}
handler := &ContextHandler{Handler: slog.NewJSONHandler(file, options)}

// Copy existing logger attributes to the new logger
Expand Down Expand Up @@ -109,13 +109,13 @@ func Panic(workerCtx context.Context, pluginCtx context.Context, errorMessage st
panic(errorMessage)
}

func DevContext(pluginCtx context.Context, errorMessage string) {
func DevContext(ctx context.Context, message string) {
if strings.ToUpper(os.Getenv("LOG_LEVEL")) == "DEV" {
logger, err := GetLoggerFromContext(pluginCtx)
logger, err := GetLoggerFromContext(ctx)
if err != nil {
panic(err)
}
logger.DebugContext(pluginCtx, errorMessage)
logger.DebugContext(ctx, message)
}
}

Expand Down
Loading

0 comments on commit e365436

Please sign in to comment.