Skip to content

Commit

Permalink
Add the option to customize kopia cmd log level (#2107)
Browse files Browse the repository at this point in the history
Add the option to run kopia commands with arbitrary log level values.
All default log level settings remain unchanged if no option is
provided.
  • Loading branch information
redgoat650 committed Jun 13, 2023
1 parent b7a4e02 commit 0b3f846
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 33 deletions.
4 changes: 2 additions & 2 deletions pkg/kopia/command/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type BlobListCommandArgs struct {

// BlobList returns the kopia command for listing blobs in the repository with their sizes
func BlobList(cmdArgs BlobListCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(blobSubCommand, listSubCommand)

return stringSliceCommand(args)
Expand All @@ -32,7 +32,7 @@ type BlobStatsCommandArgs struct {

// BlobStats returns the kopia command to get the blob stats
func BlobStats(cmdArgs BlobStatsCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(blobSubCommand, statsSubCommand, rawFlag)

return stringSliceCommand(args)
Expand Down
16 changes: 11 additions & 5 deletions pkg/kopia/command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type CommandArgs struct {
RepoPassword string
ConfigFilePath string
LogDirectory string
LogLevel string
}

func bashCommand(args logsafe.Cmd) []string {
Expand All @@ -38,22 +39,27 @@ func stringSliceCommand(args logsafe.Cmd) []string {
return args.StringSliceCMD()
}

func commonArgs(cmdArgs *CommandArgs, requireInfoLevel bool) logsafe.Cmd {
func commonArgs(cmdArgs *CommandArgs) logsafe.Cmd {
c := logsafe.NewLoggable(kopiaCommand)
if requireInfoLevel {
c = c.AppendLoggable(logLevelInfoFlag)

if cmdArgs.LogLevel != "" {
c = c.AppendLoggableKV(logLevelFlag, cmdArgs.LogLevel)
} else {
c = c.AppendLoggable(logLevelErrorFlag)
c = c.AppendLoggableKV(logLevelFlag, LogLevelError)
}

if cmdArgs.ConfigFilePath != "" {
c = c.AppendLoggableKV(configFileFlag, cmdArgs.ConfigFilePath)
}

if cmdArgs.LogDirectory != "" {
c = c.AppendLoggableKV(logDirectoryFlag, cmdArgs.LogDirectory)
}

if cmdArgs.RepoPassword != "" {
c = c.AppendRedactedKV(passwordFlag, cmdArgs.RepoPassword)
}

return c
}

Expand All @@ -67,7 +73,7 @@ func addTags(tags []string, args logsafe.Cmd) logsafe.Cmd {

// ExecKopiaArgs returns the basic Argv for executing kopia with the given config file path.
func ExecKopiaArgs(configFilePath string) []string {
return commonArgs(&CommandArgs{ConfigFilePath: configFilePath}, false).StringSliceCMD()
return commonArgs(&CommandArgs{ConfigFilePath: configFilePath}).StringSliceCMD()
}

const (
Expand Down
5 changes: 3 additions & 2 deletions pkg/kopia/command/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ const (
globalFlag = "--global"
jsonFlag = "--json"
logDirectoryFlag = "--log-dir"
logLevelErrorFlag = "--log-level=error"
logLevelInfoFlag = "--log-level=info"
logLevelFlag = "--log-level"
LogLevelError = "error"
LogLevelInfo = "info"
noGrpcFlag = "--no-grpc"
parallelFlag = "--parallel"
passwordFlag = "--password"
Expand Down
2 changes: 1 addition & 1 deletion pkg/kopia/command/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type GeneralCommandArgs struct {
// contains subcommands, loggable flags, loggable key value pairs and
// redacted key value pairs
func GeneralCommand(cmdArgs GeneralCommandArgs) logsafe.Cmd {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
for _, subCmd := range cmdArgs.SubCommands {
args = args.AppendLoggable(subCmd)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/kopia/command/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type MaintenanceInfoCommandArgs struct {

// MaintenanceInfo returns the kopia command to get maintenance info
func MaintenanceInfo(cmdArgs MaintenanceInfoCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(maintenanceSubCommand, infoSubCommand)
if cmdArgs.GetJsonOutput {
args = args.AppendLoggable(jsonFlag)
Expand All @@ -37,7 +37,7 @@ type MaintenanceSetOwnerCommandArgs struct {

// MaintenanceSetOwner returns the kopia command for setting custom maintenance owner
func MaintenanceSetOwner(cmdArgs MaintenanceSetOwnerCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(maintenanceSubCommand, setSubCommand)
args = args.AppendLoggableKV(ownerFlag, cmdArgs.CustomOwner)
return stringSliceCommand(args)
Expand All @@ -49,7 +49,7 @@ type MaintenanceRunCommandArgs struct {

// MaintenanceRunCommand returns the kopia command to run manual maintenance
func MaintenanceRunCommand(cmdArgs MaintenanceRunCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(maintenanceSubCommand, runSubCommand)

return stringSliceCommand(args)
Expand Down
20 changes: 20 additions & 0 deletions pkg/kopia/command/maintenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ func (kMaintenance *KopiaMaintenanceTestSuite) TestMaintenanceCommands(c *C) {
},
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key maintenance run",
},
{
f: func() []string {
args := MaintenanceRunCommandArgs{
CommandArgs: commandArgs,
}
args.CommandArgs.LogLevel = LogLevelInfo
return MaintenanceRunCommand(args)
},
expectedLog: "kopia --log-level=info --config-file=path/kopia.config --log-dir=cache/log --password=encr-key maintenance run",
},
{
f: func() []string {
args := MaintenanceRunCommandArgs{
CommandArgs: commandArgs,
}
args.CommandArgs.LogLevel = LogLevelError
return MaintenanceRunCommand(args)
},
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key maintenance run",
},
} {
cmd := strings.Join(tc.f(), " ")
c.Check(cmd, Equals, tc.expectedLog)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kopia/command/policy_set_global.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type PolicySetGlobalCommandArgs struct {

// PolicySetGlobal returns the kopia command for modifying the global policy
func PolicySetGlobal(cmdArgs PolicySetGlobalCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(policySubCommand, setSubCommand, globalFlag)
for field, val := range cmdArgs.Modifications {
args = args.AppendLoggableKV(field, val)
Expand Down
16 changes: 12 additions & 4 deletions pkg/kopia/command/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type RepositoryCommandArgs struct {

// RepositoryConnectCommand returns the kopia command for connecting to an existing repo
func RepositoryConnectCommand(cmdArgs RepositoryCommandArgs) ([]string, error) {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(repositorySubCommand, connectSubCommand, noCheckForUpdatesFlag)

args = kopiaCacheArgs(args, cmdArgs.CacheDirectory, cmdArgs.ContentCacheMB, cmdArgs.MetadataCacheMB)
Expand Down Expand Up @@ -70,7 +70,7 @@ func RepositoryConnectCommand(cmdArgs RepositoryCommandArgs) ([]string, error) {

// RepositoryCreateCommand returns the kopia command for creation of a repo
func RepositoryCreateCommand(cmdArgs RepositoryCommandArgs) ([]string, error) {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(repositorySubCommand, createSubCommand, noCheckForUpdatesFlag)

args = kopiaCacheArgs(args, cmdArgs.CacheDirectory, cmdArgs.ContentCacheMB, cmdArgs.MetadataCacheMB)
Expand Down Expand Up @@ -116,7 +116,7 @@ func RepositoryConnectServerCommand(cmdArgs RepositoryServerCommandArgs) []strin
RepoPassword: cmdArgs.UserPassword,
ConfigFilePath: cmdArgs.ConfigFilePath,
LogDirectory: cmdArgs.LogDirectory,
}, false)
})
args = args.AppendLoggable(repositorySubCommand, connectSubCommand, serverSubCommand, noCheckForUpdatesFlag, noGrpcFlag)

args = kopiaCacheArgs(args, cmdArgs.CacheDirectory, cmdArgs.ContentCacheMB, cmdArgs.MetadataCacheMB)
Expand All @@ -141,7 +141,15 @@ type RepositoryStatusCommandArgs struct {

// RepositoryStatusCommand returns the kopia command for checking status of the Kopia repository
func RepositoryStatusCommand(cmdArgs RepositoryStatusCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, true)
// Default to info log level unless specified otherwise.
if cmdArgs.LogLevel == "" {
// Make a copy of the common command args, set the log level to info.
common := *cmdArgs.CommandArgs
common.LogLevel = LogLevelInfo
cmdArgs.CommandArgs = &common
}

args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(repositorySubCommand, statusSubCommand)
return stringSliceCommand(args)
}
2 changes: 1 addition & 1 deletion pkg/kopia/command/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type RestoreCommandArgs struct {

// Restore returns the kopia command for restoring root of a snapshot with given root ID
func Restore(cmdArgs RestoreCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(restoreSubCommand, cmdArgs.RootID, cmdArgs.TargetPath)
if cmdArgs.IgnorePermissionErrors {
args = args.AppendLoggable(ignorePermissionsError)
Expand Down
12 changes: 6 additions & 6 deletions pkg/kopia/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ServerStartCommandArgs struct {

// ServerStart returns the kopia command for starting the Kopia API Server
func ServerStart(cmdArgs ServerStartCommandArgs) []string {
args := commonArgs(&CommandArgs{ConfigFilePath: cmdArgs.ConfigFilePath, LogDirectory: cmdArgs.LogDirectory}, false)
args := commonArgs(&CommandArgs{ConfigFilePath: cmdArgs.ConfigFilePath, LogDirectory: cmdArgs.LogDirectory})

if cmdArgs.AutoGenerateCert {
args = args.AppendLoggable(serverSubCommand, startSubCommand, tlsGenerateCertFlag)
Expand Down Expand Up @@ -65,7 +65,7 @@ type ServerRefreshCommandArgs struct {
// ServerRefresh returns the kopia command for refreshing the Kopia API Server
// This helps allow new users to be able to connect to the Server instead of waiting for auto-refresh
func ServerRefresh(cmdArgs ServerRefreshCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(serverSubCommand, refreshSubCommand)
args = args.AppendRedactedKV(serverCertFingerprint, cmdArgs.Fingerprint)
args = args.AppendLoggableKV(addressFlag, cmdArgs.ServerAddress)
Expand All @@ -85,7 +85,7 @@ type ServerStatusCommandArgs struct {

// ServerStatus returns the kopia command for checking status of the Kopia API Server
func ServerStatus(cmdArgs ServerStatusCommandArgs) []string {
args := commonArgs(&CommandArgs{ConfigFilePath: cmdArgs.ConfigFilePath, LogDirectory: cmdArgs.LogDirectory}, false)
args := commonArgs(&CommandArgs{ConfigFilePath: cmdArgs.ConfigFilePath, LogDirectory: cmdArgs.LogDirectory})
args = args.AppendLoggable(serverSubCommand, statusSubCommand)
args = args.AppendLoggableKV(addressFlag, cmdArgs.ServerAddress)
args = args.AppendRedactedKV(serverCertFingerprint, cmdArgs.Fingerprint)
Expand All @@ -101,7 +101,7 @@ type ServerListUserCommmandArgs struct {

// ServerListUser returns the kopia command to list users from the Kopia API Server
func ServerListUser(cmdArgs ServerListUserCommmandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(serverSubCommand, userSubCommand, listSubCommand, jsonFlag)

return stringSliceCommand(args)
Expand All @@ -115,7 +115,7 @@ type ServerSetUserCommandArgs struct {

// ServerSetUser returns the kopia command setting password for existing user for the Kopia API Server
func ServerSetUser(cmdArgs ServerSetUserCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(serverSubCommand, userSubCommand, setSubCommand, cmdArgs.NewUsername)
args = args.AppendRedactedKV(userPasswordFlag, cmdArgs.UserPassword)

Expand All @@ -130,7 +130,7 @@ type ServerAddUserCommandArgs struct {

// ServerAddUser returns the kopia command adding a new user to the Kopia API Server
func ServerAddUser(cmdArgs ServerAddUserCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(serverSubCommand, userSubCommand, addSubCommand, cmdArgs.NewUsername)
args = args.AppendRedactedKV(userPasswordFlag, cmdArgs.UserPassword)

Expand Down
23 changes: 15 additions & 8 deletions pkg/kopia/command/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
)

const (
requireLogLevelInfo = true
manifestTypeSnapshotFilter = "type:snapshot"
)

Expand All @@ -36,8 +35,16 @@ type SnapshotCreateCommandArgs struct {

// SnapshotCreate returns the kopia command for creation of a snapshot
func SnapshotCreate(cmdArgs SnapshotCreateCommandArgs) []string {
// Default to info log level unless specified otherwise.
if cmdArgs.LogLevel == "" {
// Make a copy of the common command args, set the log level to info.
common := *cmdArgs.CommandArgs
common.LogLevel = LogLevelInfo
cmdArgs.CommandArgs = &common
}

parallelismStr := strconv.Itoa(cmdArgs.Parallelism)
args := commonArgs(cmdArgs.CommandArgs, requireLogLevelInfo)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(snapshotSubCommand, createSubCommand, cmdArgs.PathToBackup, jsonFlag)
args = args.AppendLoggableKV(parallelFlag, parallelismStr)
args = addTags(cmdArgs.Tags, args)
Expand All @@ -63,7 +70,7 @@ type SnapshotRestoreCommandArgs struct {

// SnapshotRestore returns kopia command restoring snapshots with given snap ID
func SnapshotRestore(cmdArgs SnapshotRestoreCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(snapshotSubCommand, restoreSubCommand, cmdArgs.SnapID, cmdArgs.TargetPath)
if cmdArgs.IgnorePermissionErrors {
args = args.AppendLoggable(ignorePermissionsError)
Expand All @@ -84,7 +91,7 @@ type SnapshotDeleteCommandArgs struct {

// SnapshotDelete returns the kopia command for deleting a snapshot with given snapshot ID
func SnapshotDelete(cmdArgs SnapshotDeleteCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(snapshotSubCommand, deleteSubCommand, cmdArgs.SnapID, unsafeIgnoreSourceFlag)

return stringSliceCommand(args)
Expand All @@ -98,7 +105,7 @@ type SnapshotExpireCommandArgs struct {

// SnapshotExpire returns the kopia command for removing snapshots with given root ID
func SnapshotExpire(cmdArgs SnapshotExpireCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(snapshotSubCommand, expireSubCommand, cmdArgs.RootID)
if cmdArgs.MustDelete {
args = args.AppendLoggable(deleteFlag)
Expand All @@ -113,7 +120,7 @@ type SnapListAllCommandArgs struct {

// SnapListAll returns the kopia command for listing all snapshots in the repository with their sizes
func SnapListAll(cmdArgs SnapListAllCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(
snapshotSubCommand,
listSubCommand,
Expand All @@ -132,7 +139,7 @@ type SnapListAllWithSnapIDsCommandArgs struct {

// SnapListAllWithSnapIDs returns the kopia command for listing all snapshots in the repository with snapshotIDs
func SnapListAllWithSnapIDs(cmdArgs SnapListAllWithSnapIDsCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(manifestSubCommand, listSubCommand, jsonFlag)
args = args.AppendLoggableKV(filterFlag, manifestTypeSnapshotFilter)

Expand All @@ -145,7 +152,7 @@ type SnapListByTagsCommandArgs struct {
}

func SnapListByTags(cmdArgs SnapListByTagsCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs, false)
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(
snapshotSubCommand,
listSubCommand,
Expand Down

0 comments on commit 0b3f846

Please sign in to comment.