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 RepositorySetParametersCommand #2429

Merged
merged 11 commits into from
Nov 1, 2023
3 changes: 3 additions & 0 deletions pkg/kopia/command/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const (
serverSubCommand = "server"
startSubCommand = "start"
statusSubCommand = "status"
setParametersSubCommand = "set-parameters"
userSubCommand = "user"
addressFlag = "--address"
redirectToDevNull = "> /dev/null 2>&1"
Expand All @@ -85,6 +86,8 @@ const (
pointInTimeConnectionFlag = "--point-in-time"
urlFlag = "--url"
readOnlyFlag = "--readonly"
retentionModeFlag = "--retention-mode"
retentionPeriodFlag = "--retention-period"
)

// List of possible modifications to a policy, expressed as the kopia flag that will modify it
Expand Down
39 changes: 34 additions & 5 deletions pkg/kopia/command/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ import (
type RepositoryCommandArgs struct {
*CommandArgs
CacheArgs
CacheDirectory string
Hostname string
Username string
RepoPathPrefix string
ReadOnly bool
CacheDirectory string
Hostname string
ContentCacheMB int
MetadataCacheMB int
Username string
RepoPathPrefix string
ReadOnly bool
// Only for CreateCommand
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if someone provides this flag with non create command?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fields are not used in RepositoryConnectCommand. The same way PITFlag is used in Connect but not in Create

RetentionMode string
RetentionPeriod time.Duration
// PITFlag is only effective if set while repository connect
PITFlag strfmt.DateTime
Location map[string][]byte
Expand Down Expand Up @@ -87,6 +92,12 @@ func RepositoryCreateCommand(cmdArgs RepositoryCommandArgs) ([]string, error) {
args = args.AppendLoggableKV(overrideUsernameFlag, cmdArgs.Username)
}

// During creation, both should be set. Technically RetentionPeriod should be >= 24 * time.Hour
if cmdArgs.RetentionMode != "" && cmdArgs.RetentionPeriod > 0 {
args = args.AppendLoggableKV(retentionModeFlag, cmdArgs.RetentionMode)
args = args.AppendLoggableKV(retentionPeriodFlag, cmdArgs.RetentionPeriod.String())
}

bsArgs, err := storage.KopiaStorageArgs(&storage.StorageCommandParams{
Location: cmdArgs.Location,
RepoPathPrefix: cmdArgs.RepoPathPrefix,
Expand Down Expand Up @@ -166,3 +177,21 @@ func RepositoryStatusCommand(cmdArgs RepositoryStatusCommandArgs) []string {

return stringSliceCommand(args)
}

type RepositorySetParametersCommandArgs struct {
*CommandArgs
RetentionMode string
RetentionPeriod time.Duration
}

// RepositorySetParametersCommand to cover https://kopia.io/docs/reference/command-line/common/repository-set-parameters/
func RepositorySetParametersCommand(cmdArgs RepositorySetParametersCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(repositorySubCommand, setParametersSubCommand)
// RetentionPeriod can be 0 when wanting to disable blob retention or when changing the mode only
if cmdArgs.RetentionMode != "" {
args = args.AppendLoggableKV(retentionModeFlag, cmdArgs.RetentionMode)
args = args.AppendLoggableKV(retentionPeriodFlag, cmdArgs.RetentionPeriod.String())
}
return stringSliceCommand(args)
}
65 changes: 65 additions & 0 deletions pkg/kopia/command/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package command
import (
"strings"
"testing"
"time"

"github.com/go-openapi/strfmt"
"gopkg.in/check.v1"
Expand All @@ -29,6 +30,8 @@ type RepositoryUtilsSuite struct{}
var _ = check.Suite(&RepositoryUtilsSuite{})

func (s *RepositoryUtilsSuite) TestRepositoryCreateUtil(c *check.C) {
retentionMode := "Locked"
retentionPeriod := 10 * time.Second
for _, tc := range []struct {
cmdArg RepositoryCommandArgs
location map[string]string
Expand Down Expand Up @@ -94,6 +97,46 @@ func (s *RepositoryUtilsSuite) TestRepositoryCreateUtil(c *check.C) {
"--path=/mnt/data/test-prefix/test-path/prefix/",
},
},
{
cmdArg: RepositoryCommandArgs{
CommandArgs: &CommandArgs{
RepoPassword: "pass123",
ConfigFilePath: "/tmp/config.file",
LogDirectory: "/tmp/log.dir",
},
CacheDirectory: "/tmp/cache.dir",
Hostname: "test-hostname",
ContentCacheMB: 0,
MetadataCacheMB: 0,
Username: "test-username",
RepoPathPrefix: "test-path/prefix",
Location: map[string][]byte{
"prefix": []byte("test-prefix"),
"type": []byte("filestore"),
},
RetentionMode: retentionMode,
RetentionPeriod: retentionPeriod,
},
Checker: check.IsNil,
expectedCmd: []string{"kopia",
"--log-level=error",
"--config-file=/tmp/config.file",
"--log-dir=/tmp/log.dir",
"--password=pass123",
"repository",
"create",
"--no-check-for-updates",
"--cache-directory=/tmp/cache.dir",
"--content-cache-size-limit-mb=0",
"--metadata-cache-size-limit-mb=0",
"--override-hostname=test-hostname",
"--override-username=test-username",
"--retention-mode=Locked",
"--retention-period=10s",
"filesystem",
"--path=/mnt/data/test-prefix/test-path/prefix/",
},
},
} {
cmd, err := RepositoryCreateCommand(tc.cmdArg)
c.Assert(err, tc.Checker)
Expand Down Expand Up @@ -255,3 +298,25 @@ func (kRepoStatus *RepositoryUtilsSuite) TestRepositoryStatusCommand(c *check.C)
c.Check(cmd, check.Equals, tc.expectedLog)
}
}

func (s *RepositoryUtilsSuite) TestRepositorySetParametersCommand(c *check.C) {
retentionMode := "Locked"
retentionPeriod := 10 * time.Second
cmd := RepositorySetParametersCommand(RepositorySetParametersCommandArgs{
CommandArgs: &CommandArgs{
ConfigFilePath: "path/kopia.config",
LogDirectory: "cache/log",
},
RetentionMode: retentionMode,
RetentionPeriod: retentionPeriod,
})
c.Assert(cmd, check.DeepEquals, []string{"kopia",
"--log-level=error",
"--config-file=path/kopia.config",
"--log-dir=cache/log",
"repository",
"set-parameters",
"--retention-mode=Locked",
"--retention-period=10s",
})
}