From 64845e223618b31c3ef9eb30ab534f57b49742ee Mon Sep 17 00:00:00 2001 From: shlokchaudhari9 Date: Wed, 13 Jul 2022 14:59:28 +0530 Subject: [PATCH 1/7] Separate out kopia commands from a single file --- pkg/kopia/cmd/blob_list.go | 29 ++ pkg/kopia/cmd/blob_stats.go | 29 ++ pkg/kopia/cmd/commands.go | 483 ------------------ pkg/kopia/cmd/kopia.go | 55 ++ pkg/kopia/cmd/maintenance_info.go | 29 ++ pkg/kopia/cmd/maintenance_owner_set.go | 29 ++ pkg/kopia/cmd/maintenance_run.go | 29 ++ pkg/kopia/cmd/policy_set_global.go | 32 ++ pkg/kopia/cmd/restore.go | 29 ++ pkg/kopia/cmd/server_add_user.go | 48 ++ pkg/kopia/cmd/server_refresh.go | 58 +++ pkg/kopia/cmd/server_start.go | 80 +++ pkg/kopia/cmd/server_status.go | 54 ++ pkg/kopia/cmd/server_user_list.go | 41 ++ pkg/kopia/cmd/server_user_password_set.go | 48 ++ pkg/kopia/cmd/snapshot_create.go | 47 ++ pkg/kopia/cmd/snapshot_delete.go | 29 ++ pkg/kopia/cmd/snapshot_expire.go | 32 ++ pkg/kopia/cmd/snapshot_gc.go | 29 ++ pkg/kopia/cmd/snapshot_list_all.go | 36 ++ .../snapshot_list_all_with_snapshot_ids.go | 33 ++ pkg/kopia/cmd/snapshot_restore.go | 32 ++ 22 files changed, 828 insertions(+), 483 deletions(-) create mode 100644 pkg/kopia/cmd/blob_list.go create mode 100644 pkg/kopia/cmd/blob_stats.go delete mode 100644 pkg/kopia/cmd/commands.go create mode 100644 pkg/kopia/cmd/kopia.go create mode 100644 pkg/kopia/cmd/maintenance_info.go create mode 100644 pkg/kopia/cmd/maintenance_owner_set.go create mode 100644 pkg/kopia/cmd/maintenance_run.go create mode 100644 pkg/kopia/cmd/policy_set_global.go create mode 100644 pkg/kopia/cmd/restore.go create mode 100644 pkg/kopia/cmd/server_add_user.go create mode 100644 pkg/kopia/cmd/server_refresh.go create mode 100644 pkg/kopia/cmd/server_start.go create mode 100644 pkg/kopia/cmd/server_status.go create mode 100644 pkg/kopia/cmd/server_user_list.go create mode 100644 pkg/kopia/cmd/server_user_password_set.go create mode 100644 pkg/kopia/cmd/snapshot_create.go create mode 100644 pkg/kopia/cmd/snapshot_delete.go create mode 100644 pkg/kopia/cmd/snapshot_expire.go create mode 100644 pkg/kopia/cmd/snapshot_gc.go create mode 100644 pkg/kopia/cmd/snapshot_list_all.go create mode 100644 pkg/kopia/cmd/snapshot_list_all_with_snapshot_ids.go create mode 100644 pkg/kopia/cmd/snapshot_restore.go diff --git a/pkg/kopia/cmd/blob_list.go b/pkg/kopia/cmd/blob_list.go new file mode 100644 index 0000000000..48c3a1e4a0 --- /dev/null +++ b/pkg/kopia/cmd/blob_list.go @@ -0,0 +1,29 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// BlobList returns the kopia command for listing blobs in the repository with their sizes +func BlobList(encryptionKey, configFilePath, logDirectory string) []string { + return stringSliceCommand(blobList(encryptionKey, configFilePath, logDirectory)) +} + +func blobList(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(blobSubCommand, listSubCommand) + + return args +} diff --git a/pkg/kopia/cmd/blob_stats.go b/pkg/kopia/cmd/blob_stats.go new file mode 100644 index 0000000000..2bec6761f0 --- /dev/null +++ b/pkg/kopia/cmd/blob_stats.go @@ -0,0 +1,29 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// BlobStats returns the kopia command to get the blob stats +func BlobStats(encryptionKey, configFilePath, logDirectory string) []string { + return stringSliceCommand(blobStats(encryptionKey, configFilePath, logDirectory)) +} + +func blobStats(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(blobSubCommand, statsSubCommand, rawFlag) + + return args +} diff --git a/pkg/kopia/cmd/commands.go b/pkg/kopia/cmd/commands.go deleted file mode 100644 index 06f4708fb4..0000000000 --- a/pkg/kopia/cmd/commands.go +++ /dev/null @@ -1,483 +0,0 @@ -// Copyright 2022 The Kanister Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cmd - -import ( - "strconv" - - "github.com/kanisterio/kanister/pkg/field" - "github.com/kanisterio/kanister/pkg/kopia" - "github.com/kanisterio/kanister/pkg/log" - "github.com/kanisterio/kanister/pkg/logsafe" - "github.com/kanisterio/kanister/pkg/utils" -) - -func bashCommand(args logsafe.Cmd) []string { - log.Info().Print("Kopia Command", field.M{"Command": args.String()}) - return []string{"bash", "-o", "errexit", "-c", args.PlainText()} -} - -func stringSliceCommand(args logsafe.Cmd) []string { - log.Info().Print("Kopia Command", field.M{"Command": args.String()}) - return args.StringSliceCMD() -} - -func kopiaArgs(password, configFilePath, logDirectory string, requireInfoLevel bool) logsafe.Cmd { - c := logsafe.NewLoggable(kopiaCommand) - if requireInfoLevel { - c = c.AppendLoggable(logLevelInfoFlag) - } else { - c = c.AppendLoggable(logLevelErrorFlag) - } - if configFilePath != "" { - c = c.AppendLoggableKV(configFileFlag, configFilePath) - } - if logDirectory != "" { - c = c.AppendLoggableKV(logDirectoryFlag, logDirectory) - } - if password != "" { - c = c.AppendRedactedKV(passwordFlag, password) - } - return c -} - -// ExecKopiaArgs returns the basic Argv for executing kopia with the given config file path. -func ExecKopiaArgs(configFilePath string) []string { - return kopiaArgs("", configFilePath, "", false).StringSliceCMD() -} - -// SnapshotCreateCommand returns the kopia command for creation of a snapshot -// TODO: Have better mechanism to apply global flags -func SnapshotCreateCommand(encryptionKey, pathToBackup, configFilePath, logDirectory string) []string { - return stringSliceCommand(snapshotCreateCommand(encryptionKey, pathToBackup, configFilePath, logDirectory)) -} - -func snapshotCreateCommand(encryptionKey, pathToBackup, configFilePath, logDirectory string) logsafe.Cmd { - const ( - // kube.Exec might timeout after 4h if there is no output from the command - // Setting it to 1h instead of 1000000h so that kopia logs progress once every hour - longUpdateInterval = "1h" - - requireLogLevelInfo = true - ) - - parallelismStr := strconv.Itoa(utils.GetEnvAsIntOrDefault(kopia.DataStoreParallelUploadVarName, kopia.DefaultDataStoreParallelUpload)) - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, requireLogLevelInfo) - args = args.AppendLoggable(snapshotSubCommand, createSubCommand, pathToBackup, jsonFlag) - args = args.AppendLoggableKV(parallelFlag, parallelismStr) - args = args.AppendLoggableKV(progressUpdateIntervalFlag, longUpdateInterval) - - return args -} - -// SnapshotExpireCommand returns the kopia command for removing snapshots with given root ID -func SnapshotExpireCommand(encryptionKey, rootID, configFilePath, logDirectory string, mustDelete bool) []string { - return stringSliceCommand(snapshotExpireCommand(encryptionKey, rootID, configFilePath, logDirectory, mustDelete)) -} - -func snapshotExpireCommand(encryptionKey, rootID, configFilePath, logDirectory string, mustDelete bool) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(snapshotSubCommand, expireSubCommand, rootID) - if mustDelete { - args = args.AppendLoggable(deleteFlag) - } - - return args -} - -// SnapshotRestoreCommand returns kopia command restoring snapshots with given snap ID -func SnapshotRestoreCommand(encryptionKey, snapID, targetPath, configFilePath, logDirectory string, sparseRestore bool) []string { - return stringSliceCommand(snapshotRestoreCommand(encryptionKey, snapID, targetPath, configFilePath, logDirectory, sparseRestore)) -} - -func snapshotRestoreCommand(encryptionKey, snapID, targetPath, configFilePath, logDirectory string, sparseRestore bool) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(snapshotSubCommand, restoreSubCommand, snapID, targetPath) - if sparseRestore { - args = args.AppendLoggable(sparseFlag) - } - - return args -} - -// RestoreCommand returns the kopia command for restoring root of a snapshot with given root ID -func RestoreCommand(encryptionKey, rootID, targetPath, configFilePath, logDirectory string) []string { - return stringSliceCommand(restoreCommand(encryptionKey, rootID, targetPath, configFilePath, logDirectory)) -} - -func restoreCommand(encryptionKey, rootID, targetPath, configFilePath, logDirectory string) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(restoreSubCommand, rootID, targetPath) - - return args -} - -// DeleteCommand returns the kopia command for deleting a snapshot with given snapshot ID -func DeleteCommand(encryptionKey, snapID, configFilePath, logDirectory string) []string { - return stringSliceCommand(deleteCommand(encryptionKey, snapID, configFilePath, logDirectory)) -} - -func deleteCommand(encryptionKey, snapID, configFilePath, logDirectory string) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(snapshotSubCommand, deleteSubCommand, snapID, unsafeIgnoreSourceFlag) - - return args -} - -// SnapshotGCCommand returns the kopia command for issuing kopia snapshot gc -func SnapshotGCCommand(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(snapshotGCCommand(encryptionKey, configFilePath, logDirectory)) -} - -func snapshotGCCommand(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(snapshotSubCommand, gcSubCommand, deleteFlag) - - return args -} - -// MaintenanceSetCommandWithOwner returns the kopia command for setting custom maintenance owner -func MaintenanceSetCommandWithOwner(encryptionKey, configFilePath, logDirectory, customOwner string) []string { - return stringSliceCommand(maintenanceSetOwner(encryptionKey, configFilePath, logDirectory, customOwner)) -} - -func maintenanceSetOwner(encryptionKey, configFilePath, logDirectory, customOwner string) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(maintenanceSubCommand, setSubCommand) - args = args.AppendLoggableKV(ownerFlag, customOwner) - return args -} - -// MaintenanceRunCommand returns the kopia command to run manual maintenance -func MaintenanceRunCommand(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(maintenanceRunCommand(encryptionKey, configFilePath, logDirectory)) -} - -func maintenanceRunCommand(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(maintenanceSubCommand, runSubCommand) - - return args -} - -// MaintenanceInfoCommand returns the kopia command to get maintenance info -func MaintenanceInfoCommand(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(maintenanceInfoCommand(encryptionKey, configFilePath, logDirectory)) -} - -func maintenanceInfoCommand(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(maintenanceSubCommand, infoSubCommand) - - return args -} - -// BlobList returns the kopia command for listing blobs in the repository with their sizes -func BlobList(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(blobList(encryptionKey, configFilePath, logDirectory)) -} - -func blobList(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(blobSubCommand, listSubCommand) - - return args -} - -// BlobStats returns the kopia command to get the blob stats -func BlobStats(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(blobStats(encryptionKey, configFilePath, logDirectory)) -} - -func blobStats(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(blobSubCommand, statsSubCommand, rawFlag) - - return args -} - -// SnapListAll returns the kopia command for listing all snapshots in the repository with their sizes -func SnapListAll(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(snapListAll(encryptionKey, configFilePath, logDirectory)) -} - -func snapListAll(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable( - snapshotSubCommand, - listSubCommand, - allFlag, - deltaFlag, - showIdenticalFlag, - jsonFlag, - ) - - return args -} - -// SnapListAllWithSnapIDs returns the kopia command for listing all snapshots in the repository with snapshotIDs -func SnapListAllWithSnapIDs(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(snapListAllWithSnapIDs(encryptionKey, configFilePath, logDirectory)) -} - -func snapListAllWithSnapIDs(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(manifestSubCommand, listSubCommand, jsonFlag) - args = args.AppendLoggableKV(filterFlag, kopia.ManifestTypeSnapshotFilter) - - return args -} - -// ServerStartCommand returns the kopia command for starting the Kopia API Server -func ServerStartCommand( - configFilePath, - logDirectory, - serverAddress, - tlsCertFile, - tlsKeyFile, - serverUsername, - serverPassword string, - autoGenerateCert, - background bool, -) []string { - return bashCommand(serverStartCommand( - configFilePath, - logDirectory, - serverAddress, - tlsCertFile, - tlsKeyFile, - serverUsername, - serverPassword, - autoGenerateCert, - background, - )) -} - -func serverStartCommand( - configFilePath, - logDirectory, - serverAddress, - tlsCertFile, - tlsKeyFile, - serverUsername, - serverPassword string, - autoGenerateCert, - background bool, -) logsafe.Cmd { - args := kopiaArgs("", configFilePath, logDirectory, false) - - if autoGenerateCert { - args = args.AppendLoggable(serverSubCommand, startSubCommand, tlsGenerateCertFlag) - } else { - args = args.AppendLoggable(serverSubCommand, startSubCommand) - } - args = args.AppendLoggableKV(addressFlag, serverAddress) - args = args.AppendLoggableKV(tlsCertFilePath, tlsCertFile) - args = args.AppendLoggableKV(tlsKeyFilePath, tlsKeyFile) - args = args.AppendLoggableKV(serverUsernameFlag, serverUsername) - args = args.AppendRedactedKV(serverPasswordFlag, serverPassword) - - args = args.AppendLoggableKV(serverControlUsernameFlag, serverUsername) - args = args.AppendRedactedKV(serverControlPasswordFlag, serverPassword) - - // TODO: Remove when GRPC support is added - args = args.AppendLoggable(noGrpcFlag) - - if background { - // To start the server and run in the background - args = args.AppendLoggable(redirectToDevNull, runInBackground) - } - - return args -} - -// ServerStatusCommand returns the kopia command for checking status of the Kopia API Server -func ServerStatusCommand( - configFilePath, - logDirectory, - serverAddress, - serverUsername, - serverPassword, - fingerprint string, -) []string { - return stringSliceCommand(serverStatusCommand( - configFilePath, - logDirectory, - serverAddress, - serverUsername, - serverPassword, - fingerprint, - )) -} - -func serverStatusCommand( - configFilePath, - logDirectory, - serverAddress, - serverUsername, - serverPassword, - fingerprint string, -) logsafe.Cmd { - args := kopiaArgs("", configFilePath, logDirectory, false) - args = args.AppendLoggable(serverSubCommand, statusSubCommand) - args = args.AppendLoggableKV(addressFlag, serverAddress) - args = args.AppendRedactedKV(serverCertFingerprint, fingerprint) - args = args.AppendLoggableKV(serverUsernameFlag, serverUsername) - args = args.AppendRedactedKV(serverPasswordFlag, serverPassword) - - return args -} - -// ServerAddUserCommand returns the kopia command adding a new user to the Kopia API Server -func ServerAddUserCommand( - encryptionKey, - configFilePath, - logDirectory, - newUsername, - userPassword string, -) []string { - return stringSliceCommand(serverAddUserCommand( - encryptionKey, - configFilePath, - logDirectory, - newUsername, - userPassword, - )) -} - -func serverAddUserCommand( - encryptionKey, - configFilePath, - logDirectory, - newUsername, - userPassword string, -) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(serverSubCommand, userSubCommand, addSubCommand, newUsername) - args = args.AppendRedactedKV(userPasswordFlag, userPassword) - - return args -} - -// ServerSetUserCommand returns the kopia command setting password for existing user for the Kopia API Server -func ServerSetUserCommand( - encryptionKey, - configFilePath, - logDirectory, - newUsername, - userPassword string, -) []string { - return stringSliceCommand(serverSetUserCommand( - encryptionKey, - configFilePath, - logDirectory, - newUsername, - userPassword, - )) -} - -func serverSetUserCommand( - encryptionKey, - configFilePath, - logDirectory, - newUsername, - userPassword string, -) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(serverSubCommand, userSubCommand, setSubCommand, newUsername) - args = args.AppendRedactedKV(userPasswordFlag, userPassword) - - return args -} - -// ServerListUserCommand returns the kopia command to list users from the Kopia API Server -func ServerListUserCommand( - encryptionKey, - configFilePath, - logDirectory string, -) []string { - return stringSliceCommand(serverListUserCommand( - encryptionKey, - configFilePath, - logDirectory, - )) -} - -func serverListUserCommand( - encryptionKey, - configFilePath, - logDirectory string, -) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(serverSubCommand, userSubCommand, listSubCommand, jsonFlag) - - return args -} - -// ServerRefreshCommand 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 ServerRefreshCommand( - encryptionKey, - configFilePath, - logDirectory, - serverAddress, - serverUsername, - serverPassword, - fingerprint string, -) []string { - return stringSliceCommand(serverRefreshCommand( - encryptionKey, - configFilePath, - logDirectory, - serverAddress, - serverUsername, - serverPassword, - fingerprint, - )) -} - -func serverRefreshCommand( - encryptionKey, - configFilePath, - logDirectory, - serverAddress, - serverUsername, - serverPassword, - fingerprint string, -) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(serverSubCommand, refreshSubCommand) - args = args.AppendRedactedKV(serverCertFingerprint, fingerprint) - args = args.AppendLoggableKV(addressFlag, serverAddress) - args = args.AppendLoggableKV(serverUsernameFlag, serverUsername) - args = args.AppendRedactedKV(serverPasswordFlag, serverPassword) - - return args -} - -// policySetGlobalCommand returns the kopia command for modifying the global policy -func policySetGlobalCommand(encryptionKey, configFilePath, logDirectory string, modifications policyChanges) []string { - return stringSliceCommand(policySetGlobalCommandSetup(encryptionKey, configFilePath, logDirectory, modifications)) -} - -func policySetGlobalCommandSetup(encryptionKey, configFilePath, logDirectory string, modifications policyChanges) logsafe.Cmd { - args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(policySubCommand, setSubCommand, globalFlag) - for field, val := range modifications { - args = args.AppendLoggableKV(field, val) - } - - return args -} diff --git a/pkg/kopia/cmd/kopia.go b/pkg/kopia/cmd/kopia.go new file mode 100644 index 0000000000..1e3cdf27f3 --- /dev/null +++ b/pkg/kopia/cmd/kopia.go @@ -0,0 +1,55 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "github.com/kanisterio/kanister/pkg/field" + "github.com/kanisterio/kanister/pkg/log" + "github.com/kanisterio/kanister/pkg/logsafe" +) + +func bashCommand(args logsafe.Cmd) []string { + log.Info().Print("Kopia Command", field.M{"Command": args.String()}) + return []string{"bash", "-o", "errexit", "-c", args.PlainText()} +} + +func stringSliceCommand(args logsafe.Cmd) []string { + log.Info().Print("Kopia Command", field.M{"Command": args.String()}) + return args.StringSliceCMD() +} + +func kopiaArgs(password, configFilePath, logDirectory string, requireInfoLevel bool) logsafe.Cmd { + c := logsafe.NewLoggable(kopiaCommand) + if requireInfoLevel { + c = c.AppendLoggable(logLevelInfoFlag) + } else { + c = c.AppendLoggable(logLevelErrorFlag) + } + if configFilePath != "" { + c = c.AppendLoggableKV(configFileFlag, configFilePath) + } + if logDirectory != "" { + c = c.AppendLoggableKV(logDirectoryFlag, logDirectory) + } + if password != "" { + c = c.AppendRedactedKV(passwordFlag, password) + } + return c +} + +// ExecKopiaArgs returns the basic Argv for executing kopia with the given config file path. +func ExecKopiaArgs(configFilePath string) []string { + return kopiaArgs("", configFilePath, "", false).StringSliceCMD() +} diff --git a/pkg/kopia/cmd/maintenance_info.go b/pkg/kopia/cmd/maintenance_info.go new file mode 100644 index 0000000000..80c871ef46 --- /dev/null +++ b/pkg/kopia/cmd/maintenance_info.go @@ -0,0 +1,29 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// MaintenanceInfo returns the kopia command to get maintenance info +func MaintenanceInfo(encryptionKey, configFilePath, logDirectory string) []string { + return stringSliceCommand(maintenanceInfo(encryptionKey, configFilePath, logDirectory)) +} + +func maintenanceInfo(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(maintenanceSubCommand, infoSubCommand) + + return args +} diff --git a/pkg/kopia/cmd/maintenance_owner_set.go b/pkg/kopia/cmd/maintenance_owner_set.go new file mode 100644 index 0000000000..71b10e27f7 --- /dev/null +++ b/pkg/kopia/cmd/maintenance_owner_set.go @@ -0,0 +1,29 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// MaintenanceSetOwner returns the kopia command for setting custom maintenance owner +func MaintenanceSetOwner(encryptionKey, configFilePath, logDirectory, customOwner string) []string { + return stringSliceCommand(maintenanceSetOwner(encryptionKey, configFilePath, logDirectory, customOwner)) +} + +func maintenanceSetOwner(encryptionKey, configFilePath, logDirectory, customOwner string) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(maintenanceSubCommand, setSubCommand) + args = args.AppendLoggableKV(ownerFlag, customOwner) + return args +} diff --git a/pkg/kopia/cmd/maintenance_run.go b/pkg/kopia/cmd/maintenance_run.go new file mode 100644 index 0000000000..b6d3e5f66c --- /dev/null +++ b/pkg/kopia/cmd/maintenance_run.go @@ -0,0 +1,29 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// MaintenanceRunCommand returns the kopia command to run manual maintenance +func MaintenanceRunCommand(encryptionKey, configFilePath, logDirectory string) []string { + return stringSliceCommand(maintenanceRunCommand(encryptionKey, configFilePath, logDirectory)) +} + +func maintenanceRunCommand(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(maintenanceSubCommand, runSubCommand) + + return args +} diff --git a/pkg/kopia/cmd/policy_set_global.go b/pkg/kopia/cmd/policy_set_global.go new file mode 100644 index 0000000000..89ad541b89 --- /dev/null +++ b/pkg/kopia/cmd/policy_set_global.go @@ -0,0 +1,32 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// policySetGlobal returns the kopia command for modifying the global policy +func policySetGlobal(encryptionKey, configFilePath, logDirectory string, modifications policyChanges) []string { + return stringSliceCommand(policySetGlobalSetup(encryptionKey, configFilePath, logDirectory, modifications)) +} + +func policySetGlobalSetup(encryptionKey, configFilePath, logDirectory string, modifications policyChanges) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(policySubCommand, setSubCommand, globalFlag) + for field, val := range modifications { + args = args.AppendLoggableKV(field, val) + } + + return args +} diff --git a/pkg/kopia/cmd/restore.go b/pkg/kopia/cmd/restore.go new file mode 100644 index 0000000000..84816264e1 --- /dev/null +++ b/pkg/kopia/cmd/restore.go @@ -0,0 +1,29 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// Restore returns the kopia command for restoring root of a snapshot with given root ID +func Restore(encryptionKey, rootID, targetPath, configFilePath, logDirectory string) []string { + return stringSliceCommand(restore(encryptionKey, rootID, targetPath, configFilePath, logDirectory)) +} + +func restore(encryptionKey, rootID, targetPath, configFilePath, logDirectory string) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(restoreSubCommand, rootID, targetPath) + + return args +} diff --git a/pkg/kopia/cmd/server_add_user.go b/pkg/kopia/cmd/server_add_user.go new file mode 100644 index 0000000000..0cddaee7dc --- /dev/null +++ b/pkg/kopia/cmd/server_add_user.go @@ -0,0 +1,48 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// ServerAddUser returns the kopia command adding a new user to the Kopia API Server +func ServerAddUser( + encryptionKey, + configFilePath, + logDirectory, + newUsername, + userPassword string, +) []string { + return stringSliceCommand(serverAddUser( + encryptionKey, + configFilePath, + logDirectory, + newUsername, + userPassword, + )) +} + +func serverAddUser( + encryptionKey, + configFilePath, + logDirectory, + newUsername, + userPassword string, +) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(serverSubCommand, userSubCommand, addSubCommand, newUsername) + args = args.AppendRedactedKV(userPasswordFlag, userPassword) + + return args +} diff --git a/pkg/kopia/cmd/server_refresh.go b/pkg/kopia/cmd/server_refresh.go new file mode 100644 index 0000000000..c6769e54f2 --- /dev/null +++ b/pkg/kopia/cmd/server_refresh.go @@ -0,0 +1,58 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// 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( + encryptionKey, + configFilePath, + logDirectory, + serverAddress, + serverUsername, + serverPassword, + fingerprint string, +) []string { + return stringSliceCommand(serverRefresh( + encryptionKey, + configFilePath, + logDirectory, + serverAddress, + serverUsername, + serverPassword, + fingerprint, + )) +} + +func serverRefresh( + encryptionKey, + configFilePath, + logDirectory, + serverAddress, + serverUsername, + serverPassword, + fingerprint string, +) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(serverSubCommand, refreshSubCommand) + args = args.AppendRedactedKV(serverCertFingerprint, fingerprint) + args = args.AppendLoggableKV(addressFlag, serverAddress) + args = args.AppendLoggableKV(serverUsernameFlag, serverUsername) + args = args.AppendRedactedKV(serverPasswordFlag, serverPassword) + + return args +} diff --git a/pkg/kopia/cmd/server_start.go b/pkg/kopia/cmd/server_start.go new file mode 100644 index 0000000000..b8b78cde10 --- /dev/null +++ b/pkg/kopia/cmd/server_start.go @@ -0,0 +1,80 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// ServerStart returns the kopia command for starting the Kopia API Server +func ServerStart( + configFilePath, + logDirectory, + serverAddress, + tlsCertFile, + tlsKeyFile, + serverUsername, + serverPassword string, + autoGenerateCert, + background bool, +) []string { + return bashCommand(serverStart( + configFilePath, + logDirectory, + serverAddress, + tlsCertFile, + tlsKeyFile, + serverUsername, + serverPassword, + autoGenerateCert, + background, + )) +} + +func serverStart( + configFilePath, + logDirectory, + serverAddress, + tlsCertFile, + tlsKeyFile, + serverUsername, + serverPassword string, + autoGenerateCert, + background bool, +) logsafe.Cmd { + args := kopiaArgs("", configFilePath, logDirectory, false) + + if autoGenerateCert { + args = args.AppendLoggable(serverSubCommand, startSubCommand, tlsGenerateCertFlag) + } else { + args = args.AppendLoggable(serverSubCommand, startSubCommand) + } + args = args.AppendLoggableKV(addressFlag, serverAddress) + args = args.AppendLoggableKV(tlsCertFilePath, tlsCertFile) + args = args.AppendLoggableKV(tlsKeyFilePath, tlsKeyFile) + args = args.AppendLoggableKV(serverUsernameFlag, serverUsername) + args = args.AppendRedactedKV(serverPasswordFlag, serverPassword) + + args = args.AppendLoggableKV(serverControlUsernameFlag, serverUsername) + args = args.AppendRedactedKV(serverControlPasswordFlag, serverPassword) + + // TODO: Remove when GRPC support is added + args = args.AppendLoggable(noGrpcFlag) + + if background { + // To start the server and run in the background + args = args.AppendLoggable(redirectToDevNull, runInBackground) + } + + return args +} diff --git a/pkg/kopia/cmd/server_status.go b/pkg/kopia/cmd/server_status.go new file mode 100644 index 0000000000..ef0559be09 --- /dev/null +++ b/pkg/kopia/cmd/server_status.go @@ -0,0 +1,54 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// ServerStatus returns the kopia command for checking status of the Kopia API Server +func ServerStatus( + configFilePath, + logDirectory, + serverAddress, + serverUsername, + serverPassword, + fingerprint string, +) []string { + return stringSliceCommand(serverStatus( + configFilePath, + logDirectory, + serverAddress, + serverUsername, + serverPassword, + fingerprint, + )) +} + +func serverStatus( + configFilePath, + logDirectory, + serverAddress, + serverUsername, + serverPassword, + fingerprint string, +) logsafe.Cmd { + args := kopiaArgs("", configFilePath, logDirectory, false) + args = args.AppendLoggable(serverSubCommand, statusSubCommand) + args = args.AppendLoggableKV(addressFlag, serverAddress) + args = args.AppendRedactedKV(serverCertFingerprint, fingerprint) + args = args.AppendLoggableKV(serverUsernameFlag, serverUsername) + args = args.AppendRedactedKV(serverPasswordFlag, serverPassword) + + return args +} diff --git a/pkg/kopia/cmd/server_user_list.go b/pkg/kopia/cmd/server_user_list.go new file mode 100644 index 0000000000..bb31603f0f --- /dev/null +++ b/pkg/kopia/cmd/server_user_list.go @@ -0,0 +1,41 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// ServerListUser returns the kopia command to list users from the Kopia API Server +func ServerListUser( + encryptionKey, + configFilePath, + logDirectory string, +) []string { + return stringSliceCommand(serverListUser( + encryptionKey, + configFilePath, + logDirectory, + )) +} + +func serverListUser( + encryptionKey, + configFilePath, + logDirectory string, +) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(serverSubCommand, userSubCommand, listSubCommand, jsonFlag) + + return args +} diff --git a/pkg/kopia/cmd/server_user_password_set.go b/pkg/kopia/cmd/server_user_password_set.go new file mode 100644 index 0000000000..5a80773ce8 --- /dev/null +++ b/pkg/kopia/cmd/server_user_password_set.go @@ -0,0 +1,48 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// ServerSetUser returns the kopia command setting password for existing user for the Kopia API Server +func ServerSetUser( + encryptionKey, + configFilePath, + logDirectory, + newUsername, + userPassword string, +) []string { + return stringSliceCommand(serverSetUser( + encryptionKey, + configFilePath, + logDirectory, + newUsername, + userPassword, + )) +} + +func serverSetUser( + encryptionKey, + configFilePath, + logDirectory, + newUsername, + userPassword string, +) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(serverSubCommand, userSubCommand, setSubCommand, newUsername) + args = args.AppendRedactedKV(userPasswordFlag, userPassword) + + return args +} diff --git a/pkg/kopia/cmd/snapshot_create.go b/pkg/kopia/cmd/snapshot_create.go new file mode 100644 index 0000000000..58106f9b22 --- /dev/null +++ b/pkg/kopia/cmd/snapshot_create.go @@ -0,0 +1,47 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "strconv" + + "github.com/kanisterio/kanister/pkg/kopia" + "github.com/kanisterio/kanister/pkg/logsafe" + "github.com/kanisterio/kanister/pkg/utils" +) + +// SnapshotCreate returns the kopia command for creation of a snapshot +// TODO: Have better mechanism to apply global flags +func SnapshotCreate(encryptionKey, pathToBackup, configFilePath, logDirectory string) []string { + return stringSliceCommand(snapshotCreate(encryptionKey, pathToBackup, configFilePath, logDirectory)) +} + +func snapshotCreate(encryptionKey, pathToBackup, configFilePath, logDirectory string) logsafe.Cmd { + const ( + // kube.Exec might timeout after 4h if there is no output from the command + // Setting it to 1h instead of 1000000h so that kopia logs progress once every hour + longUpdateInterval = "1h" + + requireLogLevelInfo = true + ) + + parallelismStr := strconv.Itoa(utils.GetEnvAsIntOrDefault(kopia.DataStoreParallelUploadVarName, kopia.DefaultDataStoreParallelUpload)) + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, requireLogLevelInfo) + args = args.AppendLoggable(snapshotSubCommand, createSubCommand, pathToBackup, jsonFlag) + args = args.AppendLoggableKV(parallelFlag, parallelismStr) + args = args.AppendLoggableKV(progressUpdateIntervalFlag, longUpdateInterval) + + return args +} diff --git a/pkg/kopia/cmd/snapshot_delete.go b/pkg/kopia/cmd/snapshot_delete.go new file mode 100644 index 0000000000..ab4d7e2ea3 --- /dev/null +++ b/pkg/kopia/cmd/snapshot_delete.go @@ -0,0 +1,29 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// SnapshotDelete returns the kopia command for deleting a snapshot with given snapshot ID +func SnapshotDelete(encryptionKey, snapID, configFilePath, logDirectory string) []string { + return stringSliceCommand(snapshotDelete(encryptionKey, snapID, configFilePath, logDirectory)) +} + +func snapshotDelete(encryptionKey, snapID, configFilePath, logDirectory string) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(snapshotSubCommand, deleteSubCommand, snapID, unsafeIgnoreSourceFlag) + + return args +} diff --git a/pkg/kopia/cmd/snapshot_expire.go b/pkg/kopia/cmd/snapshot_expire.go new file mode 100644 index 0000000000..564b03f2e1 --- /dev/null +++ b/pkg/kopia/cmd/snapshot_expire.go @@ -0,0 +1,32 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// SnapshotExpire returns the kopia command for removing snapshots with given root ID +func SnapshotExpire(encryptionKey, rootID, configFilePath, logDirectory string, mustDelete bool) []string { + return stringSliceCommand(snapshotExpire(encryptionKey, rootID, configFilePath, logDirectory, mustDelete)) +} + +func snapshotExpire(encryptionKey, rootID, configFilePath, logDirectory string, mustDelete bool) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(snapshotSubCommand, expireSubCommand, rootID) + if mustDelete { + args = args.AppendLoggable(deleteFlag) + } + + return args +} diff --git a/pkg/kopia/cmd/snapshot_gc.go b/pkg/kopia/cmd/snapshot_gc.go new file mode 100644 index 0000000000..03c4e297ad --- /dev/null +++ b/pkg/kopia/cmd/snapshot_gc.go @@ -0,0 +1,29 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// SnapshotGC returns the kopia command for issuing kopia snapshot gc +func SnapshotGC(encryptionKey, configFilePath, logDirectory string) []string { + return stringSliceCommand(snapshotGC(encryptionKey, configFilePath, logDirectory)) +} + +func snapshotGC(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(snapshotSubCommand, gcSubCommand, deleteFlag) + + return args +} diff --git a/pkg/kopia/cmd/snapshot_list_all.go b/pkg/kopia/cmd/snapshot_list_all.go new file mode 100644 index 0000000000..7ddb0d7619 --- /dev/null +++ b/pkg/kopia/cmd/snapshot_list_all.go @@ -0,0 +1,36 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// SnapListAll returns the kopia command for listing all snapshots in the repository with their sizes +func SnapListAll(encryptionKey, configFilePath, logDirectory string) []string { + return stringSliceCommand(snapListAll(encryptionKey, configFilePath, logDirectory)) +} + +func snapListAll(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable( + snapshotSubCommand, + listSubCommand, + allFlag, + deltaFlag, + showIdenticalFlag, + jsonFlag, + ) + + return args +} diff --git a/pkg/kopia/cmd/snapshot_list_all_with_snapshot_ids.go b/pkg/kopia/cmd/snapshot_list_all_with_snapshot_ids.go new file mode 100644 index 0000000000..f44624a7c7 --- /dev/null +++ b/pkg/kopia/cmd/snapshot_list_all_with_snapshot_ids.go @@ -0,0 +1,33 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "github.com/kanisterio/kanister/pkg/kopia" + "github.com/kanisterio/kanister/pkg/logsafe" +) + +// SnapListAllWithSnapIDs returns the kopia command for listing all snapshots in the repository with snapshotIDs +func SnapListAllWithSnapIDs(encryptionKey, configFilePath, logDirectory string) []string { + return stringSliceCommand(snapListAllWithSnapIDs(encryptionKey, configFilePath, logDirectory)) +} + +func snapListAllWithSnapIDs(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(manifestSubCommand, listSubCommand, jsonFlag) + args = args.AppendLoggableKV(filterFlag, kopia.ManifestTypeSnapshotFilter) + + return args +} diff --git a/pkg/kopia/cmd/snapshot_restore.go b/pkg/kopia/cmd/snapshot_restore.go new file mode 100644 index 0000000000..93cae936a8 --- /dev/null +++ b/pkg/kopia/cmd/snapshot_restore.go @@ -0,0 +1,32 @@ +// Copyright 2022 The Kanister Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import "github.com/kanisterio/kanister/pkg/logsafe" + +// SnapshotRestore returns kopia command restoring snapshots with given snap ID +func SnapshotRestore(encryptionKey, snapID, targetPath, configFilePath, logDirectory string, sparseRestore bool) []string { + return stringSliceCommand(snapshotRestore(encryptionKey, snapID, targetPath, configFilePath, logDirectory, sparseRestore)) +} + +func snapshotRestore(encryptionKey, snapID, targetPath, configFilePath, logDirectory string, sparseRestore bool) logsafe.Cmd { + args := kopiaArgs(encryptionKey, configFilePath, logDirectory, false) + args = args.AppendLoggable(snapshotSubCommand, restoreSubCommand, snapID, targetPath) + if sparseRestore { + args = args.AppendLoggable(sparseFlag) + } + + return args +} From 878ac67682e949c1164c79d348eaec97360990c0 Mon Sep 17 00:00:00 2001 From: shlokchaudhari9 Date: Fri, 22 Jul 2022 14:27:59 +0530 Subject: [PATCH 2/7] Combine wrapper functions --- pkg/kopia/cmd/blob_list.go | 8 +----- pkg/kopia/cmd/blob_stats.go | 8 +----- pkg/kopia/cmd/maintenance_info.go | 8 +----- pkg/kopia/cmd/maintenance_owner_set.go | 8 +----- pkg/kopia/cmd/maintenance_run.go | 8 +----- pkg/kopia/cmd/policy_set_global.go | 14 ++++------ pkg/kopia/cmd/restore.go | 8 +----- pkg/kopia/cmd/server_add_user.go | 20 +------------ pkg/kopia/cmd/server_refresh.go | 24 +--------------- pkg/kopia/cmd/server_start.go | 28 +------------------ pkg/kopia/cmd/server_status.go | 22 +-------------- pkg/kopia/cmd/server_user_list.go | 16 +---------- pkg/kopia/cmd/server_user_password_set.go | 20 +------------ pkg/kopia/cmd/snapshot_create.go | 23 ++++++--------- pkg/kopia/cmd/snapshot_delete.go | 8 +----- pkg/kopia/cmd/snapshot_expire.go | 8 +----- pkg/kopia/cmd/snapshot_gc.go | 8 +----- pkg/kopia/cmd/snapshot_list_all.go | 8 +----- .../snapshot_list_all_with_snapshot_ids.go | 7 +---- pkg/kopia/cmd/snapshot_restore.go | 8 +----- 20 files changed, 33 insertions(+), 229 deletions(-) diff --git a/pkg/kopia/cmd/blob_list.go b/pkg/kopia/cmd/blob_list.go index ffb55774c4..dfc0aa16d7 100644 --- a/pkg/kopia/cmd/blob_list.go +++ b/pkg/kopia/cmd/blob_list.go @@ -14,16 +14,10 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // BlobList returns the kopia command for listing blobs in the repository with their sizes func BlobList(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(blobList(encryptionKey, configFilePath, logDirectory)) -} - -func blobList(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(blobSubCommand, listSubCommand) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/blob_stats.go b/pkg/kopia/cmd/blob_stats.go index fb2b82ef28..1fba642cf3 100644 --- a/pkg/kopia/cmd/blob_stats.go +++ b/pkg/kopia/cmd/blob_stats.go @@ -14,16 +14,10 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // BlobStats returns the kopia command to get the blob stats func BlobStats(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(blobStats(encryptionKey, configFilePath, logDirectory)) -} - -func blobStats(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(blobSubCommand, statsSubCommand, rawFlag) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/maintenance_info.go b/pkg/kopia/cmd/maintenance_info.go index ee3867b0e2..9d874b1f86 100644 --- a/pkg/kopia/cmd/maintenance_info.go +++ b/pkg/kopia/cmd/maintenance_info.go @@ -14,19 +14,13 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // MaintenanceInfoCommand returns the kopia command to get maintenance info func MaintenanceInfoCommand(encryptionKey, configFilePath, logDirectory string, getJsonOutput bool) []string { - return stringSliceCommand(maintenanceInfoCommand(encryptionKey, configFilePath, logDirectory, getJsonOutput)) -} - -func maintenanceInfoCommand(encryptionKey, configFilePath, logDirectory string, getJsonOutput bool) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(maintenanceSubCommand, infoSubCommand) if getJsonOutput { args = args.AppendLoggable(jsonFlag) } - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/maintenance_owner_set.go b/pkg/kopia/cmd/maintenance_owner_set.go index 56e8fcd205..f936111548 100644 --- a/pkg/kopia/cmd/maintenance_owner_set.go +++ b/pkg/kopia/cmd/maintenance_owner_set.go @@ -14,16 +14,10 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // MaintenanceSetOwner returns the kopia command for setting custom maintenance owner func MaintenanceSetOwner(encryptionKey, configFilePath, logDirectory, customOwner string) []string { - return stringSliceCommand(maintenanceSetOwner(encryptionKey, configFilePath, logDirectory, customOwner)) -} - -func maintenanceSetOwner(encryptionKey, configFilePath, logDirectory, customOwner string) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(maintenanceSubCommand, setSubCommand) args = args.AppendLoggableKV(ownerFlag, customOwner) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/maintenance_run.go b/pkg/kopia/cmd/maintenance_run.go index 08d8948a3d..ea97859dbd 100644 --- a/pkg/kopia/cmd/maintenance_run.go +++ b/pkg/kopia/cmd/maintenance_run.go @@ -14,16 +14,10 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // MaintenanceRunCommand returns the kopia command to run manual maintenance func MaintenanceRunCommand(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(maintenanceRunCommand(encryptionKey, configFilePath, logDirectory)) -} - -func maintenanceRunCommand(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(maintenanceSubCommand, runSubCommand) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/policy_set_global.go b/pkg/kopia/cmd/policy_set_global.go index 9705f81b42..88e2e76419 100644 --- a/pkg/kopia/cmd/policy_set_global.go +++ b/pkg/kopia/cmd/policy_set_global.go @@ -14,19 +14,17 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" +import ( + "github.com/kanisterio/kanister/pkg/kopia" +) -// policySetGlobal returns the kopia command for modifying the global policy -func policySetGlobal(encryptionKey, configFilePath, logDirectory string, modifications policyChanges) []string { - return stringSliceCommand(policySetGlobalSetup(encryptionKey, configFilePath, logDirectory, modifications)) -} - -func policySetGlobalSetup(encryptionKey, configFilePath, logDirectory string, modifications policyChanges) logsafe.Cmd { +// PolicySetGlobal returns the kopia command for modifying the global policy +func PolicySetGlobal(encryptionKey, configFilePath, logDirectory string, modifications kopia.PolicyChanges) []string { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(policySubCommand, setSubCommand, globalFlag) for field, val := range modifications { args = args.AppendLoggableKV(field, val) } - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/restore.go b/pkg/kopia/cmd/restore.go index ebfe0628b9..4e14487c53 100644 --- a/pkg/kopia/cmd/restore.go +++ b/pkg/kopia/cmd/restore.go @@ -14,16 +14,10 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // Restore returns the kopia command for restoring root of a snapshot with given root ID func Restore(encryptionKey, rootID, targetPath, configFilePath, logDirectory string) []string { - return stringSliceCommand(restore(encryptionKey, rootID, targetPath, configFilePath, logDirectory)) -} - -func restore(encryptionKey, rootID, targetPath, configFilePath, logDirectory string) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(restoreSubCommand, rootID, targetPath) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/server_add_user.go b/pkg/kopia/cmd/server_add_user.go index a07f1c2176..9f1582bce8 100644 --- a/pkg/kopia/cmd/server_add_user.go +++ b/pkg/kopia/cmd/server_add_user.go @@ -14,8 +14,6 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // ServerAddUser returns the kopia command adding a new user to the Kopia API Server func ServerAddUser( encryptionKey, @@ -24,25 +22,9 @@ func ServerAddUser( newUsername, userPassword string, ) []string { - return stringSliceCommand(serverAddUser( - encryptionKey, - configFilePath, - logDirectory, - newUsername, - userPassword, - )) -} - -func serverAddUser( - encryptionKey, - configFilePath, - logDirectory, - newUsername, - userPassword string, -) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(serverSubCommand, userSubCommand, addSubCommand, newUsername) args = args.AppendRedactedKV(userPasswordFlag, userPassword) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/server_refresh.go b/pkg/kopia/cmd/server_refresh.go index 7792f843f8..5f5b94729f 100644 --- a/pkg/kopia/cmd/server_refresh.go +++ b/pkg/kopia/cmd/server_refresh.go @@ -14,8 +14,6 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // 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( @@ -27,26 +25,6 @@ func ServerRefresh( serverPassword, fingerprint string, ) []string { - return stringSliceCommand(serverRefresh( - encryptionKey, - configFilePath, - logDirectory, - serverAddress, - serverUsername, - serverPassword, - fingerprint, - )) -} - -func serverRefresh( - encryptionKey, - configFilePath, - logDirectory, - serverAddress, - serverUsername, - serverPassword, - fingerprint string, -) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(serverSubCommand, refreshSubCommand) args = args.AppendRedactedKV(serverCertFingerprint, fingerprint) @@ -54,5 +32,5 @@ func serverRefresh( args = args.AppendLoggableKV(serverUsernameFlag, serverUsername) args = args.AppendRedactedKV(serverPasswordFlag, serverPassword) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/server_start.go b/pkg/kopia/cmd/server_start.go index 9d2eb35518..6306129ad2 100644 --- a/pkg/kopia/cmd/server_start.go +++ b/pkg/kopia/cmd/server_start.go @@ -14,8 +14,6 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // ServerStart returns the kopia command for starting the Kopia API Server func ServerStart( configFilePath, @@ -28,30 +26,6 @@ func ServerStart( autoGenerateCert, background bool, ) []string { - return bashCommand(serverStart( - configFilePath, - logDirectory, - serverAddress, - tlsCertFile, - tlsKeyFile, - serverUsername, - serverPassword, - autoGenerateCert, - background, - )) -} - -func serverStart( - configFilePath, - logDirectory, - serverAddress, - tlsCertFile, - tlsKeyFile, - serverUsername, - serverPassword string, - autoGenerateCert, - background bool, -) logsafe.Cmd { args := commonArgs("", configFilePath, logDirectory, false) if autoGenerateCert { @@ -76,5 +50,5 @@ func serverStart( args = args.AppendLoggable(redirectToDevNull, runInBackground) } - return args + return bashCommand(args) } diff --git a/pkg/kopia/cmd/server_status.go b/pkg/kopia/cmd/server_status.go index d2e0a44d77..48bf9b25bc 100644 --- a/pkg/kopia/cmd/server_status.go +++ b/pkg/kopia/cmd/server_status.go @@ -14,8 +14,6 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // ServerStatus returns the kopia command for checking status of the Kopia API Server func ServerStatus( configFilePath, @@ -25,24 +23,6 @@ func ServerStatus( serverPassword, fingerprint string, ) []string { - return stringSliceCommand(serverStatus( - configFilePath, - logDirectory, - serverAddress, - serverUsername, - serverPassword, - fingerprint, - )) -} - -func serverStatus( - configFilePath, - logDirectory, - serverAddress, - serverUsername, - serverPassword, - fingerprint string, -) logsafe.Cmd { args := commonArgs("", configFilePath, logDirectory, false) args = args.AppendLoggable(serverSubCommand, statusSubCommand) args = args.AppendLoggableKV(addressFlag, serverAddress) @@ -50,5 +30,5 @@ func serverStatus( args = args.AppendLoggableKV(serverUsernameFlag, serverUsername) args = args.AppendRedactedKV(serverPasswordFlag, serverPassword) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/server_user_list.go b/pkg/kopia/cmd/server_user_list.go index 518a8e1469..78727f6262 100644 --- a/pkg/kopia/cmd/server_user_list.go +++ b/pkg/kopia/cmd/server_user_list.go @@ -14,28 +14,14 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // ServerListUser returns the kopia command to list users from the Kopia API Server func ServerListUser( encryptionKey, configFilePath, logDirectory string, ) []string { - return stringSliceCommand(serverListUser( - encryptionKey, - configFilePath, - logDirectory, - )) -} - -func serverListUser( - encryptionKey, - configFilePath, - logDirectory string, -) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(serverSubCommand, userSubCommand, listSubCommand, jsonFlag) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/server_user_password_set.go b/pkg/kopia/cmd/server_user_password_set.go index 17e03c7bac..39315a68ad 100644 --- a/pkg/kopia/cmd/server_user_password_set.go +++ b/pkg/kopia/cmd/server_user_password_set.go @@ -14,8 +14,6 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // ServerSetUser returns the kopia command setting password for existing user for the Kopia API Server func ServerSetUser( encryptionKey, @@ -24,25 +22,9 @@ func ServerSetUser( newUsername, userPassword string, ) []string { - return stringSliceCommand(serverSetUser( - encryptionKey, - configFilePath, - logDirectory, - newUsername, - userPassword, - )) -} - -func serverSetUser( - encryptionKey, - configFilePath, - logDirectory, - newUsername, - userPassword string, -) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(serverSubCommand, userSubCommand, setSubCommand, newUsername) args = args.AppendRedactedKV(userPasswordFlag, userPassword) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/snapshot_create.go b/pkg/kopia/cmd/snapshot_create.go index 413a3e1eb4..eabecf718f 100644 --- a/pkg/kopia/cmd/snapshot_create.go +++ b/pkg/kopia/cmd/snapshot_create.go @@ -18,30 +18,25 @@ import ( "strconv" "github.com/kanisterio/kanister/pkg/kopia" - "github.com/kanisterio/kanister/pkg/logsafe" "github.com/kanisterio/kanister/pkg/utils" ) +const ( + // kube.Exec might timeout after 4h if there is no output from the command + // Setting it to 1h instead of 1000000h so that kopia logs progress once every hour + longUpdateInterval = "1h" + + requireLogLevelInfo = true +) + // SnapshotCreate returns the kopia command for creation of a snapshot // TODO: Have better mechanism to apply global flags func SnapshotCreate(encryptionKey, pathToBackup, configFilePath, logDirectory string) []string { - return stringSliceCommand(snapshotCreate(encryptionKey, pathToBackup, configFilePath, logDirectory)) -} - -func snapshotCreate(encryptionKey, pathToBackup, configFilePath, logDirectory string) logsafe.Cmd { - const ( - // kube.Exec might timeout after 4h if there is no output from the command - // Setting it to 1h instead of 1000000h so that kopia logs progress once every hour - longUpdateInterval = "1h" - - requireLogLevelInfo = true - ) - parallelismStr := strconv.Itoa(utils.GetEnvAsIntOrDefault(kopia.DataStoreParallelUploadVarName, kopia.DefaultDataStoreParallelUpload)) args := commonArgs(encryptionKey, configFilePath, logDirectory, requireLogLevelInfo) args = args.AppendLoggable(snapshotSubCommand, createSubCommand, pathToBackup, jsonFlag) args = args.AppendLoggableKV(parallelFlag, parallelismStr) args = args.AppendLoggableKV(progressUpdateIntervalFlag, longUpdateInterval) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/snapshot_delete.go b/pkg/kopia/cmd/snapshot_delete.go index 4ecfef3754..1b11594f57 100644 --- a/pkg/kopia/cmd/snapshot_delete.go +++ b/pkg/kopia/cmd/snapshot_delete.go @@ -14,16 +14,10 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // SnapshotDelete returns the kopia command for deleting a snapshot with given snapshot ID func SnapshotDelete(encryptionKey, snapID, configFilePath, logDirectory string) []string { - return stringSliceCommand(snapshotDelete(encryptionKey, snapID, configFilePath, logDirectory)) -} - -func snapshotDelete(encryptionKey, snapID, configFilePath, logDirectory string) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(snapshotSubCommand, deleteSubCommand, snapID, unsafeIgnoreSourceFlag) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/snapshot_expire.go b/pkg/kopia/cmd/snapshot_expire.go index 0effaeadc7..4362ac7c6d 100644 --- a/pkg/kopia/cmd/snapshot_expire.go +++ b/pkg/kopia/cmd/snapshot_expire.go @@ -14,19 +14,13 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // SnapshotExpire returns the kopia command for removing snapshots with given root ID func SnapshotExpire(encryptionKey, rootID, configFilePath, logDirectory string, mustDelete bool) []string { - return stringSliceCommand(snapshotExpire(encryptionKey, rootID, configFilePath, logDirectory, mustDelete)) -} - -func snapshotExpire(encryptionKey, rootID, configFilePath, logDirectory string, mustDelete bool) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(snapshotSubCommand, expireSubCommand, rootID) if mustDelete { args = args.AppendLoggable(deleteFlag) } - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/snapshot_gc.go b/pkg/kopia/cmd/snapshot_gc.go index 6f490c2d90..e954bd4e17 100644 --- a/pkg/kopia/cmd/snapshot_gc.go +++ b/pkg/kopia/cmd/snapshot_gc.go @@ -14,16 +14,10 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // SnapshotGC returns the kopia command for issuing kopia snapshot gc func SnapshotGC(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(snapshotGC(encryptionKey, configFilePath, logDirectory)) -} - -func snapshotGC(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(snapshotSubCommand, gcSubCommand, deleteFlag) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/snapshot_list_all.go b/pkg/kopia/cmd/snapshot_list_all.go index d0c3076180..2f5c30a114 100644 --- a/pkg/kopia/cmd/snapshot_list_all.go +++ b/pkg/kopia/cmd/snapshot_list_all.go @@ -14,14 +14,8 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // SnapListAll returns the kopia command for listing all snapshots in the repository with their sizes func SnapListAll(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(snapListAll(encryptionKey, configFilePath, logDirectory)) -} - -func snapListAll(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable( snapshotSubCommand, @@ -32,5 +26,5 @@ func snapListAll(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd jsonFlag, ) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/snapshot_list_all_with_snapshot_ids.go b/pkg/kopia/cmd/snapshot_list_all_with_snapshot_ids.go index bb9a68f1b5..a66b650348 100644 --- a/pkg/kopia/cmd/snapshot_list_all_with_snapshot_ids.go +++ b/pkg/kopia/cmd/snapshot_list_all_with_snapshot_ids.go @@ -16,18 +16,13 @@ package cmd import ( "github.com/kanisterio/kanister/pkg/kopia" - "github.com/kanisterio/kanister/pkg/logsafe" ) // SnapListAllWithSnapIDs returns the kopia command for listing all snapshots in the repository with snapshotIDs func SnapListAllWithSnapIDs(encryptionKey, configFilePath, logDirectory string) []string { - return stringSliceCommand(snapListAllWithSnapIDs(encryptionKey, configFilePath, logDirectory)) -} - -func snapListAllWithSnapIDs(encryptionKey, configFilePath, logDirectory string) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(manifestSubCommand, listSubCommand, jsonFlag) args = args.AppendLoggableKV(filterFlag, kopia.ManifestTypeSnapshotFilter) - return args + return stringSliceCommand(args) } diff --git a/pkg/kopia/cmd/snapshot_restore.go b/pkg/kopia/cmd/snapshot_restore.go index 2b5de26c64..58be22783e 100644 --- a/pkg/kopia/cmd/snapshot_restore.go +++ b/pkg/kopia/cmd/snapshot_restore.go @@ -14,19 +14,13 @@ package cmd -import "github.com/kanisterio/kanister/pkg/logsafe" - // SnapshotRestore returns kopia command restoring snapshots with given snap ID func SnapshotRestore(encryptionKey, snapID, targetPath, configFilePath, logDirectory string, sparseRestore bool) []string { - return stringSliceCommand(snapshotRestore(encryptionKey, snapID, targetPath, configFilePath, logDirectory, sparseRestore)) -} - -func snapshotRestore(encryptionKey, snapID, targetPath, configFilePath, logDirectory string, sparseRestore bool) logsafe.Cmd { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(snapshotSubCommand, restoreSubCommand, snapID, targetPath) if sparseRestore { args = args.AppendLoggable(sparseFlag) } - return args + return stringSliceCommand(args) } From b9bd6e7fe9943b79b30faddf6a33fb2a435da8b6 Mon Sep 17 00:00:00 2001 From: shlokchaudhari9 Date: Fri, 22 Jul 2022 15:58:56 +0530 Subject: [PATCH 3/7] Fix policyChanges in PolicySetGlobal command wrapper --- pkg/kopia/command/policy_set_global.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pkg/kopia/command/policy_set_global.go b/pkg/kopia/command/policy_set_global.go index 9635ed1b94..3d4216625c 100644 --- a/pkg/kopia/command/policy_set_global.go +++ b/pkg/kopia/command/policy_set_global.go @@ -14,12 +14,8 @@ package command -import ( - "github.com/kanisterio/kanister/pkg/kopia" -) - // PolicySetGlobal returns the kopia command for modifying the global policy -func PolicySetGlobal(encryptionKey, configFilePath, logDirectory string, modifications kopia.PolicyChanges) []string { +func PolicySetGlobal(encryptionKey, configFilePath, logDirectory string, modifications policyChanges) []string { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(policySubCommand, setSubCommand, globalFlag) for field, val := range modifications { From be51828e97f41e1ef1779971d494b6c2a8a3ee64 Mon Sep 17 00:00:00 2001 From: shlokchaudhari9 Date: Mon, 25 Jul 2022 12:26:52 +0530 Subject: [PATCH 4/7] Move command wrapper arguments --- pkg/kopia/command/kopia.go | 6 ++++++ pkg/kopia/command/restore.go | 2 +- pkg/kopia/command/snapshot_create.go | 2 +- pkg/kopia/command/snapshot_delete.go | 2 +- pkg/kopia/command/snapshot_expire.go | 2 +- pkg/kopia/command/snapshot_restore.go | 2 +- 6 files changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/kopia/command/kopia.go b/pkg/kopia/command/kopia.go index aff41b48e8..c82a6bab32 100644 --- a/pkg/kopia/command/kopia.go +++ b/pkg/kopia/command/kopia.go @@ -20,6 +20,12 @@ import ( "github.com/kanisterio/kanister/pkg/logsafe" ) +type CommonArgs struct { + encryptionKey string + configFilePath string + logDirectory string +} + func bashCommand(args logsafe.Cmd) []string { log.Info().Print("Kopia Command", field.M{"Command": args.String()}) return []string{"bash", "-o", "errexit", "-c", args.PlainText()} diff --git a/pkg/kopia/command/restore.go b/pkg/kopia/command/restore.go index 74ca0d2dc9..4e01e50fd8 100644 --- a/pkg/kopia/command/restore.go +++ b/pkg/kopia/command/restore.go @@ -15,7 +15,7 @@ package command // Restore returns the kopia command for restoring root of a snapshot with given root ID -func Restore(encryptionKey, rootID, targetPath, configFilePath, logDirectory string) []string { +func Restore(encryptionKey, configFilePath, logDirectory, rootID, targetPath string) []string { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(restoreSubCommand, rootID, targetPath) diff --git a/pkg/kopia/command/snapshot_create.go b/pkg/kopia/command/snapshot_create.go index c7d8c6acd6..ef6abc4c4e 100644 --- a/pkg/kopia/command/snapshot_create.go +++ b/pkg/kopia/command/snapshot_create.go @@ -31,7 +31,7 @@ const ( // SnapshotCreate returns the kopia command for creation of a snapshot // TODO: Have better mechanism to apply global flags -func SnapshotCreate(encryptionKey, pathToBackup, configFilePath, logDirectory string) []string { +func SnapshotCreate(encryptionKey, configFilePath, logDirectory, pathToBackup string) []string { parallelismStr := strconv.Itoa(utils.GetEnvAsIntOrDefault(kopia.DataStoreParallelUploadVarName, kopia.DefaultDataStoreParallelUpload)) args := commonArgs(encryptionKey, configFilePath, logDirectory, requireLogLevelInfo) args = args.AppendLoggable(snapshotSubCommand, createSubCommand, pathToBackup, jsonFlag) diff --git a/pkg/kopia/command/snapshot_delete.go b/pkg/kopia/command/snapshot_delete.go index ef0d2733da..2ccaa5c2de 100644 --- a/pkg/kopia/command/snapshot_delete.go +++ b/pkg/kopia/command/snapshot_delete.go @@ -15,7 +15,7 @@ package command // SnapshotDelete returns the kopia command for deleting a snapshot with given snapshot ID -func SnapshotDelete(encryptionKey, snapID, configFilePath, logDirectory string) []string { +func SnapshotDelete(encryptionKey, configFilePath, logDirectory, snapID string) []string { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(snapshotSubCommand, deleteSubCommand, snapID, unsafeIgnoreSourceFlag) diff --git a/pkg/kopia/command/snapshot_expire.go b/pkg/kopia/command/snapshot_expire.go index 8b3db20cf9..cb07cf879e 100644 --- a/pkg/kopia/command/snapshot_expire.go +++ b/pkg/kopia/command/snapshot_expire.go @@ -15,7 +15,7 @@ package command // SnapshotExpire returns the kopia command for removing snapshots with given root ID -func SnapshotExpire(encryptionKey, rootID, configFilePath, logDirectory string, mustDelete bool) []string { +func SnapshotExpire(encryptionKey, configFilePath, logDirectory, rootID string, mustDelete bool) []string { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(snapshotSubCommand, expireSubCommand, rootID) if mustDelete { diff --git a/pkg/kopia/command/snapshot_restore.go b/pkg/kopia/command/snapshot_restore.go index 80e1713528..b34e207575 100644 --- a/pkg/kopia/command/snapshot_restore.go +++ b/pkg/kopia/command/snapshot_restore.go @@ -15,7 +15,7 @@ package command // SnapshotRestore returns kopia command restoring snapshots with given snap ID -func SnapshotRestore(encryptionKey, snapID, targetPath, configFilePath, logDirectory string, sparseRestore bool) []string { +func SnapshotRestore(encryptionKey, configFilePath, logDirectory, snapID, targetPath string, sparseRestore bool) []string { args := commonArgs(encryptionKey, configFilePath, logDirectory, false) args = args.AppendLoggable(snapshotSubCommand, restoreSubCommand, snapID, targetPath) if sparseRestore { From 314f509377f63ea1c045a995ff7f8f3160300111 Mon Sep 17 00:00:00 2001 From: shlokchaudhari9 Date: Mon, 25 Jul 2022 13:37:27 +0530 Subject: [PATCH 5/7] Add CommandArgs struct to pass args to command wrappers --- pkg/kopia/command/blob_list.go | 8 +++- pkg/kopia/command/blob_stats.go | 8 +++- pkg/kopia/command/kopia.go | 2 +- pkg/kopia/command/maintenance_info.go | 13 ++++-- pkg/kopia/command/maintenance_owner_set.go | 11 +++-- pkg/kopia/command/maintenance_run.go | 8 +++- pkg/kopia/command/policy_set_global.go | 11 +++-- pkg/kopia/command/restore.go | 12 +++-- pkg/kopia/command/server_add_user.go | 20 ++++----- pkg/kopia/command/server_refresh.go | 28 ++++++------ pkg/kopia/command/server_start.go | 45 ++++++++++--------- pkg/kopia/command/server_status.go | 27 +++++------ pkg/kopia/command/server_user_list.go | 12 ++--- pkg/kopia/command/server_user_password_set.go | 19 ++++---- pkg/kopia/command/snapshot_create.go | 11 +++-- pkg/kopia/command/snapshot_delete.go | 11 +++-- pkg/kopia/command/snapshot_expire.go | 14 ++++-- pkg/kopia/command/snapshot_gc.go | 8 +++- pkg/kopia/command/snapshot_list_all.go | 8 +++- .../snapshot_list_all_with_snapshot_ids.go | 8 +++- pkg/kopia/command/snapshot_restore.go | 15 +++++-- 21 files changed, 185 insertions(+), 114 deletions(-) diff --git a/pkg/kopia/command/blob_list.go b/pkg/kopia/command/blob_list.go index 474313842a..92890e8232 100644 --- a/pkg/kopia/command/blob_list.go +++ b/pkg/kopia/command/blob_list.go @@ -14,9 +14,13 @@ package command +type BlobListCommandCommandArgs struct { + *CommandArgs +} + // BlobList returns the kopia command for listing blobs in the repository with their sizes -func BlobList(encryptionKey, configFilePath, logDirectory string) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) +func BlobList(blobListArgs BlobListCommandCommandArgs) []string { + args := commonArgs(blobListArgs.encryptionKey, blobListArgs.configFilePath, blobListArgs.logDirectory, false) args = args.AppendLoggable(blobSubCommand, listSubCommand) return stringSliceCommand(args) diff --git a/pkg/kopia/command/blob_stats.go b/pkg/kopia/command/blob_stats.go index 48bee6a7a7..d09f556a73 100644 --- a/pkg/kopia/command/blob_stats.go +++ b/pkg/kopia/command/blob_stats.go @@ -14,9 +14,13 @@ package command +type BlobStatsCommandCommandArgs struct { + *CommandArgs +} + // BlobStats returns the kopia command to get the blob stats -func BlobStats(encryptionKey, configFilePath, logDirectory string) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) +func BlobStats(blobStatsArgs BlobStatsCommandCommandArgs) []string { + args := commonArgs(blobStatsArgs.encryptionKey, blobStatsArgs.configFilePath, blobStatsArgs.logDirectory, false) args = args.AppendLoggable(blobSubCommand, statsSubCommand, rawFlag) return stringSliceCommand(args) diff --git a/pkg/kopia/command/kopia.go b/pkg/kopia/command/kopia.go index c82a6bab32..80e564aa18 100644 --- a/pkg/kopia/command/kopia.go +++ b/pkg/kopia/command/kopia.go @@ -20,7 +20,7 @@ import ( "github.com/kanisterio/kanister/pkg/logsafe" ) -type CommonArgs struct { +type CommandArgs struct { encryptionKey string configFilePath string logDirectory string diff --git a/pkg/kopia/command/maintenance_info.go b/pkg/kopia/command/maintenance_info.go index 170099a43c..2904873176 100644 --- a/pkg/kopia/command/maintenance_info.go +++ b/pkg/kopia/command/maintenance_info.go @@ -14,11 +14,16 @@ package command -// MaintenanceInfoCommand returns the kopia command to get maintenance info -func MaintenanceInfoCommand(encryptionKey, configFilePath, logDirectory string, getJsonOutput bool) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) +type MaintenanceInfoCommandArgs struct { + *CommandArgs + getJsonOutput bool +} + +// MaintenanceInfo returns the kopia command to get maintenance info +func MaintenanceInfo(maintenanceInfoArgs MaintenanceInfoCommandArgs) []string { + args := commonArgs(maintenanceInfoArgs.encryptionKey, maintenanceInfoArgs.configFilePath, maintenanceInfoArgs.logDirectory, false) args = args.AppendLoggable(maintenanceSubCommand, infoSubCommand) - if getJsonOutput { + if maintenanceInfoArgs.getJsonOutput { args = args.AppendLoggable(jsonFlag) } diff --git a/pkg/kopia/command/maintenance_owner_set.go b/pkg/kopia/command/maintenance_owner_set.go index 6b401fc747..4b92a0ead8 100644 --- a/pkg/kopia/command/maintenance_owner_set.go +++ b/pkg/kopia/command/maintenance_owner_set.go @@ -14,10 +14,15 @@ package command +type MaintenanceSetOwnerCommandArgs struct { + *CommandArgs + customOwner string +} + // MaintenanceSetOwner returns the kopia command for setting custom maintenance owner -func MaintenanceSetOwner(encryptionKey, configFilePath, logDirectory, customOwner string) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) +func MaintenanceSetOwner(maintenanceSetOwnerArgs MaintenanceSetOwnerCommandArgs) []string { + args := commonArgs(maintenanceSetOwnerArgs.encryptionKey, maintenanceSetOwnerArgs.configFilePath, maintenanceSetOwnerArgs.logDirectory, false) args = args.AppendLoggable(maintenanceSubCommand, setSubCommand) - args = args.AppendLoggableKV(ownerFlag, customOwner) + args = args.AppendLoggableKV(ownerFlag, maintenanceSetOwnerArgs.customOwner) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/maintenance_run.go b/pkg/kopia/command/maintenance_run.go index 73add66f1b..3e10c391f8 100644 --- a/pkg/kopia/command/maintenance_run.go +++ b/pkg/kopia/command/maintenance_run.go @@ -14,9 +14,13 @@ package command +type MaintenanceRunCommandCommandArgs struct { + *CommandArgs +} + // MaintenanceRunCommand returns the kopia command to run manual maintenance -func MaintenanceRunCommand(encryptionKey, configFilePath, logDirectory string) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) +func MaintenanceRunCommand(maintenanceRunArgs MaintenanceRunCommandCommandArgs) []string { + args := commonArgs(maintenanceRunArgs.encryptionKey, maintenanceRunArgs.configFilePath, maintenanceRunArgs.logDirectory, false) args = args.AppendLoggable(maintenanceSubCommand, runSubCommand) return stringSliceCommand(args) diff --git a/pkg/kopia/command/policy_set_global.go b/pkg/kopia/command/policy_set_global.go index 3d4216625c..60b8adf2ae 100644 --- a/pkg/kopia/command/policy_set_global.go +++ b/pkg/kopia/command/policy_set_global.go @@ -14,11 +14,16 @@ package command +type PolicySetGlobalCommandArgs struct { + *CommandArgs + modifications policyChanges +} + // PolicySetGlobal returns the kopia command for modifying the global policy -func PolicySetGlobal(encryptionKey, configFilePath, logDirectory string, modifications policyChanges) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) +func PolicySetGlobal(policySetGlobalArgs PolicySetGlobalCommandArgs) []string { + args := commonArgs(policySetGlobalArgs.encryptionKey, policySetGlobalArgs.configFilePath, policySetGlobalArgs.logDirectory, false) args = args.AppendLoggable(policySubCommand, setSubCommand, globalFlag) - for field, val := range modifications { + for field, val := range policySetGlobalArgs.modifications { args = args.AppendLoggableKV(field, val) } diff --git a/pkg/kopia/command/restore.go b/pkg/kopia/command/restore.go index 4e01e50fd8..30e6e28c9f 100644 --- a/pkg/kopia/command/restore.go +++ b/pkg/kopia/command/restore.go @@ -14,10 +14,16 @@ package command +type RestoreCommandArgs struct { + *CommandArgs + rootID string + targetPath string +} + // Restore returns the kopia command for restoring root of a snapshot with given root ID -func Restore(encryptionKey, configFilePath, logDirectory, rootID, targetPath string) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(restoreSubCommand, rootID, targetPath) +func Restore(restoreArgs RestoreCommandArgs) []string { + args := commonArgs(restoreArgs.encryptionKey, restoreArgs.configFilePath, restoreArgs.logDirectory, false) + args = args.AppendLoggable(restoreSubCommand, restoreArgs.rootID, restoreArgs.targetPath) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/server_add_user.go b/pkg/kopia/command/server_add_user.go index 0a47aa62f4..50fa285311 100644 --- a/pkg/kopia/command/server_add_user.go +++ b/pkg/kopia/command/server_add_user.go @@ -14,17 +14,17 @@ package command +type ServerAddUserCommandArgs struct { + *CommandArgs + newUsername string + userPassword string +} + // ServerAddUser returns the kopia command adding a new user to the Kopia API Server -func ServerAddUser( - encryptionKey, - configFilePath, - logDirectory, - newUsername, - userPassword string, -) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(serverSubCommand, userSubCommand, addSubCommand, newUsername) - args = args.AppendRedactedKV(userPasswordFlag, userPassword) +func ServerAddUser(serverAddUserArgs ServerAddUserCommandArgs) []string { + args := commonArgs(serverAddUserArgs.encryptionKey, serverAddUserArgs.configFilePath, serverAddUserArgs.logDirectory, false) + args = args.AppendLoggable(serverSubCommand, userSubCommand, addSubCommand, serverAddUserArgs.newUsername) + args = args.AppendRedactedKV(userPasswordFlag, serverAddUserArgs.userPassword) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/server_refresh.go b/pkg/kopia/command/server_refresh.go index 0afeb0ee7c..c62a351b1d 100644 --- a/pkg/kopia/command/server_refresh.go +++ b/pkg/kopia/command/server_refresh.go @@ -14,23 +14,23 @@ package command +type ServerRefreshCommandArgs struct { + *CommandArgs + serverAddress string + serverUsername string + serverPassword string + fingerprint string +} + // 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( - encryptionKey, - configFilePath, - logDirectory, - serverAddress, - serverUsername, - serverPassword, - fingerprint string, -) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) +func ServerRefresh(serverRefreshArgs ServerRefreshCommandArgs) []string { + args := commonArgs(serverRefreshArgs.encryptionKey, serverRefreshArgs.configFilePath, serverRefreshArgs.logDirectory, false) args = args.AppendLoggable(serverSubCommand, refreshSubCommand) - args = args.AppendRedactedKV(serverCertFingerprint, fingerprint) - args = args.AppendLoggableKV(addressFlag, serverAddress) - args = args.AppendLoggableKV(serverUsernameFlag, serverUsername) - args = args.AppendRedactedKV(serverPasswordFlag, serverPassword) + args = args.AppendRedactedKV(serverCertFingerprint, serverRefreshArgs.fingerprint) + args = args.AppendLoggableKV(addressFlag, serverRefreshArgs.serverAddress) + args = args.AppendLoggableKV(serverUsernameFlag, serverRefreshArgs.serverUsername) + args = args.AppendRedactedKV(serverPasswordFlag, serverRefreshArgs.serverPassword) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/server_start.go b/pkg/kopia/command/server_start.go index 4af919f556..fb79e2ece9 100644 --- a/pkg/kopia/command/server_start.go +++ b/pkg/kopia/command/server_start.go @@ -14,38 +14,39 @@ package command +type ServerStartCommandArgs struct { + *CommandArgs + serverAddress string + tlsCertFile string + tlsKeyFile string + serverUsername string + serverPassword string + autoGenerateCert bool + background bool +} + // ServerStart returns the kopia command for starting the Kopia API Server -func ServerStart( - configFilePath, - logDirectory, - serverAddress, - tlsCertFile, - tlsKeyFile, - serverUsername, - serverPassword string, - autoGenerateCert, - background bool, -) []string { - args := commonArgs("", configFilePath, logDirectory, false) - - if autoGenerateCert { +func ServerStart(serverStartArgs ServerStartCommandArgs) []string { + args := commonArgs("", serverStartArgs.configFilePath, serverStartArgs.logDirectory, false) + + if serverStartArgs.autoGenerateCert { args = args.AppendLoggable(serverSubCommand, startSubCommand, tlsGenerateCertFlag) } else { args = args.AppendLoggable(serverSubCommand, startSubCommand) } - args = args.AppendLoggableKV(addressFlag, serverAddress) - args = args.AppendLoggableKV(tlsCertFilePath, tlsCertFile) - args = args.AppendLoggableKV(tlsKeyFilePath, tlsKeyFile) - args = args.AppendLoggableKV(serverUsernameFlag, serverUsername) - args = args.AppendRedactedKV(serverPasswordFlag, serverPassword) + args = args.AppendLoggableKV(addressFlag, serverStartArgs.serverAddress) + args = args.AppendLoggableKV(tlsCertFilePath, serverStartArgs.tlsCertFile) + args = args.AppendLoggableKV(tlsKeyFilePath, serverStartArgs.tlsKeyFile) + args = args.AppendLoggableKV(serverUsernameFlag, serverStartArgs.serverUsername) + args = args.AppendRedactedKV(serverPasswordFlag, serverStartArgs.serverPassword) - args = args.AppendLoggableKV(serverControlUsernameFlag, serverUsername) - args = args.AppendRedactedKV(serverControlPasswordFlag, serverPassword) + args = args.AppendLoggableKV(serverControlUsernameFlag, serverStartArgs.serverUsername) + args = args.AppendRedactedKV(serverControlPasswordFlag, serverStartArgs.serverPassword) // TODO: Remove when GRPC support is added args = args.AppendLoggable(noGrpcFlag) - if background { + if serverStartArgs.background { // To start the server and run in the background args = args.AppendLoggable(redirectToDevNull, runInBackground) } diff --git a/pkg/kopia/command/server_status.go b/pkg/kopia/command/server_status.go index f875cb915e..e21d01e67a 100644 --- a/pkg/kopia/command/server_status.go +++ b/pkg/kopia/command/server_status.go @@ -14,21 +14,22 @@ package command +type ServerStatusCommandArgs struct { + *CommandArgs + serverAddress string + serverUsername string + serverPassword string + fingerprint string +} + // ServerStatus returns the kopia command for checking status of the Kopia API Server -func ServerStatus( - configFilePath, - logDirectory, - serverAddress, - serverUsername, - serverPassword, - fingerprint string, -) []string { - args := commonArgs("", configFilePath, logDirectory, false) +func ServerStatus(serverStatusArgs ServerStatusCommandArgs) []string { + args := commonArgs("", serverStatusArgs.configFilePath, serverStatusArgs.logDirectory, false) args = args.AppendLoggable(serverSubCommand, statusSubCommand) - args = args.AppendLoggableKV(addressFlag, serverAddress) - args = args.AppendRedactedKV(serverCertFingerprint, fingerprint) - args = args.AppendLoggableKV(serverUsernameFlag, serverUsername) - args = args.AppendRedactedKV(serverPasswordFlag, serverPassword) + args = args.AppendLoggableKV(addressFlag, serverStatusArgs.serverAddress) + args = args.AppendRedactedKV(serverCertFingerprint, serverStatusArgs.fingerprint) + args = args.AppendLoggableKV(serverUsernameFlag, serverStatusArgs.serverUsername) + args = args.AppendRedactedKV(serverPasswordFlag, serverStatusArgs.serverPassword) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/server_user_list.go b/pkg/kopia/command/server_user_list.go index 6e1a88fa34..4aac42a5af 100644 --- a/pkg/kopia/command/server_user_list.go +++ b/pkg/kopia/command/server_user_list.go @@ -14,13 +14,13 @@ package command +type ServerListUserCommmandArgs struct { + *CommandArgs +} + // ServerListUser returns the kopia command to list users from the Kopia API Server -func ServerListUser( - encryptionKey, - configFilePath, - logDirectory string, -) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) +func ServerListUser(serverListUserArgs ServerListUserCommmandArgs) []string { + args := commonArgs(serverListUserArgs.encryptionKey, serverListUserArgs.configFilePath, serverListUserArgs.logDirectory, false) args = args.AppendLoggable(serverSubCommand, userSubCommand, listSubCommand, jsonFlag) return stringSliceCommand(args) diff --git a/pkg/kopia/command/server_user_password_set.go b/pkg/kopia/command/server_user_password_set.go index 6033c69bc8..8861df2cb8 100644 --- a/pkg/kopia/command/server_user_password_set.go +++ b/pkg/kopia/command/server_user_password_set.go @@ -14,17 +14,18 @@ package command +type ServerSetUserCommandArgs struct { + *CommandArgs + newUsername string + userPassword string +} + // ServerSetUser returns the kopia command setting password for existing user for the Kopia API Server func ServerSetUser( - encryptionKey, - configFilePath, - logDirectory, - newUsername, - userPassword string, -) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(serverSubCommand, userSubCommand, setSubCommand, newUsername) - args = args.AppendRedactedKV(userPasswordFlag, userPassword) + serverSetUserArgs ServerSetUserCommandArgs) []string { + args := commonArgs(serverSetUserArgs.encryptionKey, serverSetUserArgs.configFilePath, serverSetUserArgs.logDirectory, false) + args = args.AppendLoggable(serverSubCommand, userSubCommand, setSubCommand, serverSetUserArgs.newUsername) + args = args.AppendRedactedKV(userPasswordFlag, serverSetUserArgs.userPassword) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/snapshot_create.go b/pkg/kopia/command/snapshot_create.go index ef6abc4c4e..b8700e1ef7 100644 --- a/pkg/kopia/command/snapshot_create.go +++ b/pkg/kopia/command/snapshot_create.go @@ -29,12 +29,17 @@ const ( requireLogLevelInfo = true ) +type SnapshotCreateCommandArgs struct { + *CommandArgs + pathToBackup string +} + // SnapshotCreate returns the kopia command for creation of a snapshot // TODO: Have better mechanism to apply global flags -func SnapshotCreate(encryptionKey, configFilePath, logDirectory, pathToBackup string) []string { +func SnapshotCreate(snapshotCreateArgs SnapshotCreateCommandArgs) []string { parallelismStr := strconv.Itoa(utils.GetEnvAsIntOrDefault(kopia.DataStoreParallelUploadVarName, kopia.DefaultDataStoreParallelUpload)) - args := commonArgs(encryptionKey, configFilePath, logDirectory, requireLogLevelInfo) - args = args.AppendLoggable(snapshotSubCommand, createSubCommand, pathToBackup, jsonFlag) + args := commonArgs(snapshotCreateArgs.encryptionKey, snapshotCreateArgs.configFilePath, snapshotCreateArgs.logDirectory, requireLogLevelInfo) + args = args.AppendLoggable(snapshotSubCommand, createSubCommand, snapshotCreateArgs.pathToBackup, jsonFlag) args = args.AppendLoggableKV(parallelFlag, parallelismStr) args = args.AppendLoggableKV(progressUpdateIntervalFlag, longUpdateInterval) diff --git a/pkg/kopia/command/snapshot_delete.go b/pkg/kopia/command/snapshot_delete.go index 2ccaa5c2de..08c339d31f 100644 --- a/pkg/kopia/command/snapshot_delete.go +++ b/pkg/kopia/command/snapshot_delete.go @@ -14,10 +14,15 @@ package command +type SnapshotDeleteCommandArgs struct { + *CommandArgs + snapID string +} + // SnapshotDelete returns the kopia command for deleting a snapshot with given snapshot ID -func SnapshotDelete(encryptionKey, configFilePath, logDirectory, snapID string) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(snapshotSubCommand, deleteSubCommand, snapID, unsafeIgnoreSourceFlag) +func SnapshotDelete(snapshotDeleteArgs SnapshotDeleteCommandArgs) []string { + args := commonArgs(snapshotDeleteArgs.encryptionKey, snapshotDeleteArgs.configFilePath, snapshotDeleteArgs.logDirectory, false) + args = args.AppendLoggable(snapshotSubCommand, deleteSubCommand, snapshotDeleteArgs.snapID, unsafeIgnoreSourceFlag) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/snapshot_expire.go b/pkg/kopia/command/snapshot_expire.go index cb07cf879e..b942d0d42e 100644 --- a/pkg/kopia/command/snapshot_expire.go +++ b/pkg/kopia/command/snapshot_expire.go @@ -14,11 +14,17 @@ package command +type SnapshotExpireCommandArgs struct { + *CommandArgs + rootID string + mustDelete bool +} + // SnapshotExpire returns the kopia command for removing snapshots with given root ID -func SnapshotExpire(encryptionKey, configFilePath, logDirectory, rootID string, mustDelete bool) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(snapshotSubCommand, expireSubCommand, rootID) - if mustDelete { +func SnapshotExpire(snapshotExpireArgs SnapshotExpireCommandArgs) []string { + args := commonArgs(snapshotExpireArgs.encryptionKey, snapshotExpireArgs.configFilePath, snapshotExpireArgs.logDirectory, false) + args = args.AppendLoggable(snapshotSubCommand, expireSubCommand, snapshotExpireArgs.rootID) + if snapshotExpireArgs.mustDelete { args = args.AppendLoggable(deleteFlag) } diff --git a/pkg/kopia/command/snapshot_gc.go b/pkg/kopia/command/snapshot_gc.go index 58d66da982..0afe2d95f3 100644 --- a/pkg/kopia/command/snapshot_gc.go +++ b/pkg/kopia/command/snapshot_gc.go @@ -14,9 +14,13 @@ package command +type SnapshotGCCommandArgs struct { + *CommandArgs +} + // SnapshotGC returns the kopia command for issuing kopia snapshot gc -func SnapshotGC(encryptionKey, configFilePath, logDirectory string) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) +func SnapshotGC(snapshotGCArgs SnapshotGCCommandArgs) []string { + args := commonArgs(snapshotGCArgs.encryptionKey, snapshotGCArgs.configFilePath, snapshotGCArgs.logDirectory, false) args = args.AppendLoggable(snapshotSubCommand, gcSubCommand, deleteFlag) return stringSliceCommand(args) diff --git a/pkg/kopia/command/snapshot_list_all.go b/pkg/kopia/command/snapshot_list_all.go index 25f3795a68..2ba2842b3b 100644 --- a/pkg/kopia/command/snapshot_list_all.go +++ b/pkg/kopia/command/snapshot_list_all.go @@ -14,9 +14,13 @@ package command +type SnapListAllCommandArgs struct { + *CommandArgs +} + // SnapListAll returns the kopia command for listing all snapshots in the repository with their sizes -func SnapListAll(encryptionKey, configFilePath, logDirectory string) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) +func SnapListAll(snapListAllArgs SnapListAllCommandArgs) []string { + args := commonArgs(snapListAllArgs.encryptionKey, snapListAllArgs.configFilePath, snapListAllArgs.logDirectory, false) args = args.AppendLoggable( snapshotSubCommand, listSubCommand, diff --git a/pkg/kopia/command/snapshot_list_all_with_snapshot_ids.go b/pkg/kopia/command/snapshot_list_all_with_snapshot_ids.go index aa50e377a6..df1e831e34 100644 --- a/pkg/kopia/command/snapshot_list_all_with_snapshot_ids.go +++ b/pkg/kopia/command/snapshot_list_all_with_snapshot_ids.go @@ -18,9 +18,13 @@ import ( "github.com/kanisterio/kanister/pkg/kopia" ) +type SnapListAllWithSnapIDsCommandArgs struct { + *CommandArgs +} + // SnapListAllWithSnapIDs returns the kopia command for listing all snapshots in the repository with snapshotIDs -func SnapListAllWithSnapIDs(encryptionKey, configFilePath, logDirectory string) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) +func SnapListAllWithSnapIDs(snapListAllWithSnapIDsArgs SnapListAllWithSnapIDsCommandArgs) []string { + args := commonArgs(snapListAllWithSnapIDsArgs.encryptionKey, snapListAllWithSnapIDsArgs.configFilePath, snapListAllWithSnapIDsArgs.logDirectory, false) args = args.AppendLoggable(manifestSubCommand, listSubCommand, jsonFlag) args = args.AppendLoggableKV(filterFlag, kopia.ManifestTypeSnapshotFilter) diff --git a/pkg/kopia/command/snapshot_restore.go b/pkg/kopia/command/snapshot_restore.go index b34e207575..be27bc04e8 100644 --- a/pkg/kopia/command/snapshot_restore.go +++ b/pkg/kopia/command/snapshot_restore.go @@ -14,11 +14,18 @@ package command +type SnapshotRestoreCommandArgs struct { + *CommandArgs + snapID string + targetPath string + sparseRestore bool +} + // SnapshotRestore returns kopia command restoring snapshots with given snap ID -func SnapshotRestore(encryptionKey, configFilePath, logDirectory, snapID, targetPath string, sparseRestore bool) []string { - args := commonArgs(encryptionKey, configFilePath, logDirectory, false) - args = args.AppendLoggable(snapshotSubCommand, restoreSubCommand, snapID, targetPath) - if sparseRestore { +func SnapshotRestore(snapshotRestoreArgs SnapshotRestoreCommandArgs) []string { + args := commonArgs(snapshotRestoreArgs.encryptionKey, snapshotRestoreArgs.configFilePath, snapshotRestoreArgs.logDirectory, false) + args = args.AppendLoggable(snapshotSubCommand, restoreSubCommand, snapshotRestoreArgs.snapID, snapshotRestoreArgs.targetPath) + if snapshotRestoreArgs.sparseRestore { args = args.AppendLoggable(sparseFlag) } From 7061c8984347370847330e0365620af052370b84 Mon Sep 17 00:00:00 2001 From: shlokchaudhari9 Date: Mon, 25 Jul 2022 15:31:23 +0530 Subject: [PATCH 6/7] Convert local arguments to exportable arguments --- pkg/kopia/command/blob_list.go | 2 +- pkg/kopia/command/blob_stats.go | 2 +- pkg/kopia/command/kopia.go | 6 ++-- pkg/kopia/command/maintenance_info.go | 6 ++-- pkg/kopia/command/maintenance_owner_set.go | 6 ++-- pkg/kopia/command/maintenance_run.go | 2 +- pkg/kopia/command/policy_set_global.go | 6 ++-- pkg/kopia/command/restore.go | 8 ++--- pkg/kopia/command/server_add_user.go | 10 +++--- pkg/kopia/command/server_refresh.go | 18 +++++----- pkg/kopia/command/server_start.go | 34 +++++++++---------- pkg/kopia/command/server_status.go | 18 +++++----- pkg/kopia/command/server_user_list.go | 2 +- pkg/kopia/command/server_user_password_set.go | 10 +++--- pkg/kopia/command/snapshot_create.go | 6 ++-- pkg/kopia/command/snapshot_delete.go | 6 ++-- pkg/kopia/command/snapshot_expire.go | 10 +++--- pkg/kopia/command/snapshot_gc.go | 2 +- pkg/kopia/command/snapshot_list_all.go | 2 +- .../snapshot_list_all_with_snapshot_ids.go | 2 +- pkg/kopia/command/snapshot_restore.go | 12 +++---- pkg/kopia/utils.go | 14 ++++++-- 22 files changed, 96 insertions(+), 88 deletions(-) diff --git a/pkg/kopia/command/blob_list.go b/pkg/kopia/command/blob_list.go index 92890e8232..9840e1cd7c 100644 --- a/pkg/kopia/command/blob_list.go +++ b/pkg/kopia/command/blob_list.go @@ -20,7 +20,7 @@ type BlobListCommandCommandArgs struct { // BlobList returns the kopia command for listing blobs in the repository with their sizes func BlobList(blobListArgs BlobListCommandCommandArgs) []string { - args := commonArgs(blobListArgs.encryptionKey, blobListArgs.configFilePath, blobListArgs.logDirectory, false) + args := commonArgs(blobListArgs.EncryptionKey, blobListArgs.ConfigFilePath, blobListArgs.LogDirectory, false) args = args.AppendLoggable(blobSubCommand, listSubCommand) return stringSliceCommand(args) diff --git a/pkg/kopia/command/blob_stats.go b/pkg/kopia/command/blob_stats.go index d09f556a73..a16622cc0f 100644 --- a/pkg/kopia/command/blob_stats.go +++ b/pkg/kopia/command/blob_stats.go @@ -20,7 +20,7 @@ type BlobStatsCommandCommandArgs struct { // BlobStats returns the kopia command to get the blob stats func BlobStats(blobStatsArgs BlobStatsCommandCommandArgs) []string { - args := commonArgs(blobStatsArgs.encryptionKey, blobStatsArgs.configFilePath, blobStatsArgs.logDirectory, false) + args := commonArgs(blobStatsArgs.EncryptionKey, blobStatsArgs.ConfigFilePath, blobStatsArgs.LogDirectory, false) args = args.AppendLoggable(blobSubCommand, statsSubCommand, rawFlag) return stringSliceCommand(args) diff --git a/pkg/kopia/command/kopia.go b/pkg/kopia/command/kopia.go index 80e564aa18..955112b81c 100644 --- a/pkg/kopia/command/kopia.go +++ b/pkg/kopia/command/kopia.go @@ -21,9 +21,9 @@ import ( ) type CommandArgs struct { - encryptionKey string - configFilePath string - logDirectory string + EncryptionKey string + ConfigFilePath string + LogDirectory string } func bashCommand(args logsafe.Cmd) []string { diff --git a/pkg/kopia/command/maintenance_info.go b/pkg/kopia/command/maintenance_info.go index 2904873176..1c4dd336a6 100644 --- a/pkg/kopia/command/maintenance_info.go +++ b/pkg/kopia/command/maintenance_info.go @@ -16,14 +16,14 @@ package command type MaintenanceInfoCommandArgs struct { *CommandArgs - getJsonOutput bool + GetJsonOutput bool } // MaintenanceInfo returns the kopia command to get maintenance info func MaintenanceInfo(maintenanceInfoArgs MaintenanceInfoCommandArgs) []string { - args := commonArgs(maintenanceInfoArgs.encryptionKey, maintenanceInfoArgs.configFilePath, maintenanceInfoArgs.logDirectory, false) + args := commonArgs(maintenanceInfoArgs.EncryptionKey, maintenanceInfoArgs.ConfigFilePath, maintenanceInfoArgs.LogDirectory, false) args = args.AppendLoggable(maintenanceSubCommand, infoSubCommand) - if maintenanceInfoArgs.getJsonOutput { + if maintenanceInfoArgs.GetJsonOutput { args = args.AppendLoggable(jsonFlag) } diff --git a/pkg/kopia/command/maintenance_owner_set.go b/pkg/kopia/command/maintenance_owner_set.go index 4b92a0ead8..3420dcd67c 100644 --- a/pkg/kopia/command/maintenance_owner_set.go +++ b/pkg/kopia/command/maintenance_owner_set.go @@ -16,13 +16,13 @@ package command type MaintenanceSetOwnerCommandArgs struct { *CommandArgs - customOwner string + CustomOwner string } // MaintenanceSetOwner returns the kopia command for setting custom maintenance owner func MaintenanceSetOwner(maintenanceSetOwnerArgs MaintenanceSetOwnerCommandArgs) []string { - args := commonArgs(maintenanceSetOwnerArgs.encryptionKey, maintenanceSetOwnerArgs.configFilePath, maintenanceSetOwnerArgs.logDirectory, false) + args := commonArgs(maintenanceSetOwnerArgs.EncryptionKey, maintenanceSetOwnerArgs.ConfigFilePath, maintenanceSetOwnerArgs.LogDirectory, false) args = args.AppendLoggable(maintenanceSubCommand, setSubCommand) - args = args.AppendLoggableKV(ownerFlag, maintenanceSetOwnerArgs.customOwner) + args = args.AppendLoggableKV(ownerFlag, maintenanceSetOwnerArgs.CustomOwner) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/maintenance_run.go b/pkg/kopia/command/maintenance_run.go index 3e10c391f8..7506cfdd7c 100644 --- a/pkg/kopia/command/maintenance_run.go +++ b/pkg/kopia/command/maintenance_run.go @@ -20,7 +20,7 @@ type MaintenanceRunCommandCommandArgs struct { // MaintenanceRunCommand returns the kopia command to run manual maintenance func MaintenanceRunCommand(maintenanceRunArgs MaintenanceRunCommandCommandArgs) []string { - args := commonArgs(maintenanceRunArgs.encryptionKey, maintenanceRunArgs.configFilePath, maintenanceRunArgs.logDirectory, false) + args := commonArgs(maintenanceRunArgs.EncryptionKey, maintenanceRunArgs.ConfigFilePath, maintenanceRunArgs.LogDirectory, false) args = args.AppendLoggable(maintenanceSubCommand, runSubCommand) return stringSliceCommand(args) diff --git a/pkg/kopia/command/policy_set_global.go b/pkg/kopia/command/policy_set_global.go index 60b8adf2ae..415e9965b3 100644 --- a/pkg/kopia/command/policy_set_global.go +++ b/pkg/kopia/command/policy_set_global.go @@ -16,14 +16,14 @@ package command type PolicySetGlobalCommandArgs struct { *CommandArgs - modifications policyChanges + Modifications policyChanges } // PolicySetGlobal returns the kopia command for modifying the global policy func PolicySetGlobal(policySetGlobalArgs PolicySetGlobalCommandArgs) []string { - args := commonArgs(policySetGlobalArgs.encryptionKey, policySetGlobalArgs.configFilePath, policySetGlobalArgs.logDirectory, false) + args := commonArgs(policySetGlobalArgs.EncryptionKey, policySetGlobalArgs.ConfigFilePath, policySetGlobalArgs.LogDirectory, false) args = args.AppendLoggable(policySubCommand, setSubCommand, globalFlag) - for field, val := range policySetGlobalArgs.modifications { + for field, val := range policySetGlobalArgs.Modifications { args = args.AppendLoggableKV(field, val) } diff --git a/pkg/kopia/command/restore.go b/pkg/kopia/command/restore.go index 30e6e28c9f..16e8c2eb37 100644 --- a/pkg/kopia/command/restore.go +++ b/pkg/kopia/command/restore.go @@ -16,14 +16,14 @@ package command type RestoreCommandArgs struct { *CommandArgs - rootID string - targetPath string + RootID string + TargetPath string } // Restore returns the kopia command for restoring root of a snapshot with given root ID func Restore(restoreArgs RestoreCommandArgs) []string { - args := commonArgs(restoreArgs.encryptionKey, restoreArgs.configFilePath, restoreArgs.logDirectory, false) - args = args.AppendLoggable(restoreSubCommand, restoreArgs.rootID, restoreArgs.targetPath) + args := commonArgs(restoreArgs.EncryptionKey, restoreArgs.ConfigFilePath, restoreArgs.LogDirectory, false) + args = args.AppendLoggable(restoreSubCommand, restoreArgs.RootID, restoreArgs.TargetPath) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/server_add_user.go b/pkg/kopia/command/server_add_user.go index 50fa285311..107ebac81b 100644 --- a/pkg/kopia/command/server_add_user.go +++ b/pkg/kopia/command/server_add_user.go @@ -16,15 +16,15 @@ package command type ServerAddUserCommandArgs struct { *CommandArgs - newUsername string - userPassword string + NewUsername string + UserPassword string } // ServerAddUser returns the kopia command adding a new user to the Kopia API Server func ServerAddUser(serverAddUserArgs ServerAddUserCommandArgs) []string { - args := commonArgs(serverAddUserArgs.encryptionKey, serverAddUserArgs.configFilePath, serverAddUserArgs.logDirectory, false) - args = args.AppendLoggable(serverSubCommand, userSubCommand, addSubCommand, serverAddUserArgs.newUsername) - args = args.AppendRedactedKV(userPasswordFlag, serverAddUserArgs.userPassword) + args := commonArgs(serverAddUserArgs.EncryptionKey, serverAddUserArgs.ConfigFilePath, serverAddUserArgs.LogDirectory, false) + args = args.AppendLoggable(serverSubCommand, userSubCommand, addSubCommand, serverAddUserArgs.NewUsername) + args = args.AppendRedactedKV(userPasswordFlag, serverAddUserArgs.UserPassword) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/server_refresh.go b/pkg/kopia/command/server_refresh.go index c62a351b1d..ddcae54283 100644 --- a/pkg/kopia/command/server_refresh.go +++ b/pkg/kopia/command/server_refresh.go @@ -16,21 +16,21 @@ package command type ServerRefreshCommandArgs struct { *CommandArgs - serverAddress string - serverUsername string - serverPassword string - fingerprint string + ServerAddress string + ServerUsername string + ServerPassword string + Fingerprint string } // 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(serverRefreshArgs ServerRefreshCommandArgs) []string { - args := commonArgs(serverRefreshArgs.encryptionKey, serverRefreshArgs.configFilePath, serverRefreshArgs.logDirectory, false) + args := commonArgs(serverRefreshArgs.EncryptionKey, serverRefreshArgs.ConfigFilePath, serverRefreshArgs.LogDirectory, false) args = args.AppendLoggable(serverSubCommand, refreshSubCommand) - args = args.AppendRedactedKV(serverCertFingerprint, serverRefreshArgs.fingerprint) - args = args.AppendLoggableKV(addressFlag, serverRefreshArgs.serverAddress) - args = args.AppendLoggableKV(serverUsernameFlag, serverRefreshArgs.serverUsername) - args = args.AppendRedactedKV(serverPasswordFlag, serverRefreshArgs.serverPassword) + args = args.AppendRedactedKV(serverCertFingerprint, serverRefreshArgs.Fingerprint) + args = args.AppendLoggableKV(addressFlag, serverRefreshArgs.ServerAddress) + args = args.AppendLoggableKV(serverUsernameFlag, serverRefreshArgs.ServerUsername) + args = args.AppendRedactedKV(serverPasswordFlag, serverRefreshArgs.ServerPassword) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/server_start.go b/pkg/kopia/command/server_start.go index fb79e2ece9..2332a84869 100644 --- a/pkg/kopia/command/server_start.go +++ b/pkg/kopia/command/server_start.go @@ -16,37 +16,37 @@ package command type ServerStartCommandArgs struct { *CommandArgs - serverAddress string - tlsCertFile string - tlsKeyFile string - serverUsername string - serverPassword string - autoGenerateCert bool - background bool + ServerAddress string + TLSCertFile string + TLSKeyFile string + ServerUsername string + ServerPassword string + AutoGenerateCert bool + Background bool } // ServerStart returns the kopia command for starting the Kopia API Server func ServerStart(serverStartArgs ServerStartCommandArgs) []string { - args := commonArgs("", serverStartArgs.configFilePath, serverStartArgs.logDirectory, false) + args := commonArgs("", serverStartArgs.ConfigFilePath, serverStartArgs.LogDirectory, false) - if serverStartArgs.autoGenerateCert { + if serverStartArgs.AutoGenerateCert { args = args.AppendLoggable(serverSubCommand, startSubCommand, tlsGenerateCertFlag) } else { args = args.AppendLoggable(serverSubCommand, startSubCommand) } - args = args.AppendLoggableKV(addressFlag, serverStartArgs.serverAddress) - args = args.AppendLoggableKV(tlsCertFilePath, serverStartArgs.tlsCertFile) - args = args.AppendLoggableKV(tlsKeyFilePath, serverStartArgs.tlsKeyFile) - args = args.AppendLoggableKV(serverUsernameFlag, serverStartArgs.serverUsername) - args = args.AppendRedactedKV(serverPasswordFlag, serverStartArgs.serverPassword) + args = args.AppendLoggableKV(addressFlag, serverStartArgs.ServerAddress) + args = args.AppendLoggableKV(tlsCertFilePath, serverStartArgs.TLSCertFile) + args = args.AppendLoggableKV(tlsKeyFilePath, serverStartArgs.TLSKeyFile) + args = args.AppendLoggableKV(serverUsernameFlag, serverStartArgs.ServerUsername) + args = args.AppendRedactedKV(serverPasswordFlag, serverStartArgs.ServerPassword) - args = args.AppendLoggableKV(serverControlUsernameFlag, serverStartArgs.serverUsername) - args = args.AppendRedactedKV(serverControlPasswordFlag, serverStartArgs.serverPassword) + args = args.AppendLoggableKV(serverControlUsernameFlag, serverStartArgs.ServerUsername) + args = args.AppendRedactedKV(serverControlPasswordFlag, serverStartArgs.ServerPassword) // TODO: Remove when GRPC support is added args = args.AppendLoggable(noGrpcFlag) - if serverStartArgs.background { + if serverStartArgs.Background { // To start the server and run in the background args = args.AppendLoggable(redirectToDevNull, runInBackground) } diff --git a/pkg/kopia/command/server_status.go b/pkg/kopia/command/server_status.go index e21d01e67a..115d9ce80a 100644 --- a/pkg/kopia/command/server_status.go +++ b/pkg/kopia/command/server_status.go @@ -16,20 +16,20 @@ package command type ServerStatusCommandArgs struct { *CommandArgs - serverAddress string - serverUsername string - serverPassword string - fingerprint string + ServerAddress string + ServerUsername string + ServerPassword string + Fingerprint string } // ServerStatus returns the kopia command for checking status of the Kopia API Server func ServerStatus(serverStatusArgs ServerStatusCommandArgs) []string { - args := commonArgs("", serverStatusArgs.configFilePath, serverStatusArgs.logDirectory, false) + args := commonArgs("", serverStatusArgs.ConfigFilePath, serverStatusArgs.LogDirectory, false) args = args.AppendLoggable(serverSubCommand, statusSubCommand) - args = args.AppendLoggableKV(addressFlag, serverStatusArgs.serverAddress) - args = args.AppendRedactedKV(serverCertFingerprint, serverStatusArgs.fingerprint) - args = args.AppendLoggableKV(serverUsernameFlag, serverStatusArgs.serverUsername) - args = args.AppendRedactedKV(serverPasswordFlag, serverStatusArgs.serverPassword) + args = args.AppendLoggableKV(addressFlag, serverStatusArgs.ServerAddress) + args = args.AppendRedactedKV(serverCertFingerprint, serverStatusArgs.Fingerprint) + args = args.AppendLoggableKV(serverUsernameFlag, serverStatusArgs.ServerUsername) + args = args.AppendRedactedKV(serverPasswordFlag, serverStatusArgs.ServerPassword) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/server_user_list.go b/pkg/kopia/command/server_user_list.go index 4aac42a5af..184525be47 100644 --- a/pkg/kopia/command/server_user_list.go +++ b/pkg/kopia/command/server_user_list.go @@ -20,7 +20,7 @@ type ServerListUserCommmandArgs struct { // ServerListUser returns the kopia command to list users from the Kopia API Server func ServerListUser(serverListUserArgs ServerListUserCommmandArgs) []string { - args := commonArgs(serverListUserArgs.encryptionKey, serverListUserArgs.configFilePath, serverListUserArgs.logDirectory, false) + args := commonArgs(serverListUserArgs.EncryptionKey, serverListUserArgs.ConfigFilePath, serverListUserArgs.LogDirectory, false) args = args.AppendLoggable(serverSubCommand, userSubCommand, listSubCommand, jsonFlag) return stringSliceCommand(args) diff --git a/pkg/kopia/command/server_user_password_set.go b/pkg/kopia/command/server_user_password_set.go index 8861df2cb8..3b8f4d5d46 100644 --- a/pkg/kopia/command/server_user_password_set.go +++ b/pkg/kopia/command/server_user_password_set.go @@ -16,16 +16,16 @@ package command type ServerSetUserCommandArgs struct { *CommandArgs - newUsername string - userPassword string + NewUsername string + UserPassword string } // ServerSetUser returns the kopia command setting password for existing user for the Kopia API Server func ServerSetUser( serverSetUserArgs ServerSetUserCommandArgs) []string { - args := commonArgs(serverSetUserArgs.encryptionKey, serverSetUserArgs.configFilePath, serverSetUserArgs.logDirectory, false) - args = args.AppendLoggable(serverSubCommand, userSubCommand, setSubCommand, serverSetUserArgs.newUsername) - args = args.AppendRedactedKV(userPasswordFlag, serverSetUserArgs.userPassword) + args := commonArgs(serverSetUserArgs.EncryptionKey, serverSetUserArgs.ConfigFilePath, serverSetUserArgs.LogDirectory, false) + args = args.AppendLoggable(serverSubCommand, userSubCommand, setSubCommand, serverSetUserArgs.NewUsername) + args = args.AppendRedactedKV(userPasswordFlag, serverSetUserArgs.UserPassword) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/snapshot_create.go b/pkg/kopia/command/snapshot_create.go index b8700e1ef7..6342c1ff11 100644 --- a/pkg/kopia/command/snapshot_create.go +++ b/pkg/kopia/command/snapshot_create.go @@ -31,15 +31,15 @@ const ( type SnapshotCreateCommandArgs struct { *CommandArgs - pathToBackup string + PathToBackup string } // SnapshotCreate returns the kopia command for creation of a snapshot // TODO: Have better mechanism to apply global flags func SnapshotCreate(snapshotCreateArgs SnapshotCreateCommandArgs) []string { parallelismStr := strconv.Itoa(utils.GetEnvAsIntOrDefault(kopia.DataStoreParallelUploadVarName, kopia.DefaultDataStoreParallelUpload)) - args := commonArgs(snapshotCreateArgs.encryptionKey, snapshotCreateArgs.configFilePath, snapshotCreateArgs.logDirectory, requireLogLevelInfo) - args = args.AppendLoggable(snapshotSubCommand, createSubCommand, snapshotCreateArgs.pathToBackup, jsonFlag) + args := commonArgs(snapshotCreateArgs.EncryptionKey, snapshotCreateArgs.ConfigFilePath, snapshotCreateArgs.LogDirectory, requireLogLevelInfo) + args = args.AppendLoggable(snapshotSubCommand, createSubCommand, snapshotCreateArgs.PathToBackup, jsonFlag) args = args.AppendLoggableKV(parallelFlag, parallelismStr) args = args.AppendLoggableKV(progressUpdateIntervalFlag, longUpdateInterval) diff --git a/pkg/kopia/command/snapshot_delete.go b/pkg/kopia/command/snapshot_delete.go index 08c339d31f..6b59a8fcfd 100644 --- a/pkg/kopia/command/snapshot_delete.go +++ b/pkg/kopia/command/snapshot_delete.go @@ -16,13 +16,13 @@ package command type SnapshotDeleteCommandArgs struct { *CommandArgs - snapID string + SnapID string } // SnapshotDelete returns the kopia command for deleting a snapshot with given snapshot ID func SnapshotDelete(snapshotDeleteArgs SnapshotDeleteCommandArgs) []string { - args := commonArgs(snapshotDeleteArgs.encryptionKey, snapshotDeleteArgs.configFilePath, snapshotDeleteArgs.logDirectory, false) - args = args.AppendLoggable(snapshotSubCommand, deleteSubCommand, snapshotDeleteArgs.snapID, unsafeIgnoreSourceFlag) + args := commonArgs(snapshotDeleteArgs.EncryptionKey, snapshotDeleteArgs.ConfigFilePath, snapshotDeleteArgs.LogDirectory, false) + args = args.AppendLoggable(snapshotSubCommand, deleteSubCommand, snapshotDeleteArgs.SnapID, unsafeIgnoreSourceFlag) return stringSliceCommand(args) } diff --git a/pkg/kopia/command/snapshot_expire.go b/pkg/kopia/command/snapshot_expire.go index b942d0d42e..3517a040d0 100644 --- a/pkg/kopia/command/snapshot_expire.go +++ b/pkg/kopia/command/snapshot_expire.go @@ -16,15 +16,15 @@ package command type SnapshotExpireCommandArgs struct { *CommandArgs - rootID string - mustDelete bool + RootID string + MustDelete bool } // SnapshotExpire returns the kopia command for removing snapshots with given root ID func SnapshotExpire(snapshotExpireArgs SnapshotExpireCommandArgs) []string { - args := commonArgs(snapshotExpireArgs.encryptionKey, snapshotExpireArgs.configFilePath, snapshotExpireArgs.logDirectory, false) - args = args.AppendLoggable(snapshotSubCommand, expireSubCommand, snapshotExpireArgs.rootID) - if snapshotExpireArgs.mustDelete { + args := commonArgs(snapshotExpireArgs.EncryptionKey, snapshotExpireArgs.ConfigFilePath, snapshotExpireArgs.LogDirectory, false) + args = args.AppendLoggable(snapshotSubCommand, expireSubCommand, snapshotExpireArgs.RootID) + if snapshotExpireArgs.MustDelete { args = args.AppendLoggable(deleteFlag) } diff --git a/pkg/kopia/command/snapshot_gc.go b/pkg/kopia/command/snapshot_gc.go index 0afe2d95f3..d5f9593248 100644 --- a/pkg/kopia/command/snapshot_gc.go +++ b/pkg/kopia/command/snapshot_gc.go @@ -20,7 +20,7 @@ type SnapshotGCCommandArgs struct { // SnapshotGC returns the kopia command for issuing kopia snapshot gc func SnapshotGC(snapshotGCArgs SnapshotGCCommandArgs) []string { - args := commonArgs(snapshotGCArgs.encryptionKey, snapshotGCArgs.configFilePath, snapshotGCArgs.logDirectory, false) + args := commonArgs(snapshotGCArgs.EncryptionKey, snapshotGCArgs.ConfigFilePath, snapshotGCArgs.LogDirectory, false) args = args.AppendLoggable(snapshotSubCommand, gcSubCommand, deleteFlag) return stringSliceCommand(args) diff --git a/pkg/kopia/command/snapshot_list_all.go b/pkg/kopia/command/snapshot_list_all.go index 2ba2842b3b..7838c27b83 100644 --- a/pkg/kopia/command/snapshot_list_all.go +++ b/pkg/kopia/command/snapshot_list_all.go @@ -20,7 +20,7 @@ type SnapListAllCommandArgs struct { // SnapListAll returns the kopia command for listing all snapshots in the repository with their sizes func SnapListAll(snapListAllArgs SnapListAllCommandArgs) []string { - args := commonArgs(snapListAllArgs.encryptionKey, snapListAllArgs.configFilePath, snapListAllArgs.logDirectory, false) + args := commonArgs(snapListAllArgs.EncryptionKey, snapListAllArgs.ConfigFilePath, snapListAllArgs.LogDirectory, false) args = args.AppendLoggable( snapshotSubCommand, listSubCommand, diff --git a/pkg/kopia/command/snapshot_list_all_with_snapshot_ids.go b/pkg/kopia/command/snapshot_list_all_with_snapshot_ids.go index df1e831e34..0c13f01959 100644 --- a/pkg/kopia/command/snapshot_list_all_with_snapshot_ids.go +++ b/pkg/kopia/command/snapshot_list_all_with_snapshot_ids.go @@ -24,7 +24,7 @@ type SnapListAllWithSnapIDsCommandArgs struct { // SnapListAllWithSnapIDs returns the kopia command for listing all snapshots in the repository with snapshotIDs func SnapListAllWithSnapIDs(snapListAllWithSnapIDsArgs SnapListAllWithSnapIDsCommandArgs) []string { - args := commonArgs(snapListAllWithSnapIDsArgs.encryptionKey, snapListAllWithSnapIDsArgs.configFilePath, snapListAllWithSnapIDsArgs.logDirectory, false) + args := commonArgs(snapListAllWithSnapIDsArgs.EncryptionKey, snapListAllWithSnapIDsArgs.ConfigFilePath, snapListAllWithSnapIDsArgs.LogDirectory, false) args = args.AppendLoggable(manifestSubCommand, listSubCommand, jsonFlag) args = args.AppendLoggableKV(filterFlag, kopia.ManifestTypeSnapshotFilter) diff --git a/pkg/kopia/command/snapshot_restore.go b/pkg/kopia/command/snapshot_restore.go index be27bc04e8..5355be2e96 100644 --- a/pkg/kopia/command/snapshot_restore.go +++ b/pkg/kopia/command/snapshot_restore.go @@ -16,16 +16,16 @@ package command type SnapshotRestoreCommandArgs struct { *CommandArgs - snapID string - targetPath string - sparseRestore bool + SnapID string + TargetPath string + SparseRestore bool } // SnapshotRestore returns kopia command restoring snapshots with given snap ID func SnapshotRestore(snapshotRestoreArgs SnapshotRestoreCommandArgs) []string { - args := commonArgs(snapshotRestoreArgs.encryptionKey, snapshotRestoreArgs.configFilePath, snapshotRestoreArgs.logDirectory, false) - args = args.AppendLoggable(snapshotSubCommand, restoreSubCommand, snapshotRestoreArgs.snapID, snapshotRestoreArgs.targetPath) - if snapshotRestoreArgs.sparseRestore { + args := commonArgs(snapshotRestoreArgs.EncryptionKey, snapshotRestoreArgs.ConfigFilePath, snapshotRestoreArgs.LogDirectory, false) + args = args.AppendLoggable(snapshotSubCommand, restoreSubCommand, snapshotRestoreArgs.SnapID, snapshotRestoreArgs.TargetPath) + if snapshotRestoreArgs.SparseRestore { args = args.AppendLoggable(sparseFlag) } diff --git a/pkg/kopia/utils.go b/pkg/kopia/utils.go index bda3003d32..237f68b63d 100644 --- a/pkg/kopia/utils.go +++ b/pkg/kopia/utils.go @@ -38,7 +38,7 @@ import ( "github.com/kanisterio/kanister/pkg/field" "github.com/kanisterio/kanister/pkg/format" - "github.com/kanisterio/kanister/pkg/kopia/command" + kopiacmd "github.com/kanisterio/kanister/pkg/kopia/command" "github.com/kanisterio/kanister/pkg/kube" "github.com/kanisterio/kanister/pkg/log" ) @@ -323,7 +323,7 @@ func parseSnapshotManifestList(output string) ([]*snapshot.Manifest, error) { return snapInfoList, nil } -// Duplicate of struct for Kopia user profiles since Profile struct is in internal/user package and could not be imported +// KopiaUserProfile is a duplicate of struct for Kopia user profiles since Profile struct is in internal/user package and could not be imported type KopiaUserProfile struct { ManifestID manifest.ID `json:"-"` @@ -343,7 +343,15 @@ func GetMaintenanceOwnerForConnectedRepository( configFilePath, logDirectory string, ) (string, error) { - cmd := command.MaintenanceInfoCommand(encryptionKey, configFilePath, logDirectory, false) + args := kopiacmd.MaintenanceInfoCommandArgs{ + CommandArgs: &kopiacmd.CommandArgs{ + EncryptionKey: encryptionKey, + ConfigFilePath: configFilePath, + LogDirectory: logDirectory, + }, + GetJsonOutput: false, + } + cmd := kopiacmd.MaintenanceInfo(args) stdout, stderr, err := kube.Exec(cli, namespace, pod, container, cmd, nil) format.Log(pod, container, stdout) format.Log(pod, container, stderr) From 6139f29af601eea7a95aae675a23b9034ae09473 Mon Sep 17 00:00:00 2001 From: shlokchaudhari9 Date: Thu, 28 Jul 2022 14:52:32 +0530 Subject: [PATCH 7/7] Change log level to Debug --- pkg/kopia/command/kopia.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/kopia/command/kopia.go b/pkg/kopia/command/kopia.go index 955112b81c..fc445efa87 100644 --- a/pkg/kopia/command/kopia.go +++ b/pkg/kopia/command/kopia.go @@ -27,12 +27,12 @@ type CommandArgs struct { } func bashCommand(args logsafe.Cmd) []string { - log.Info().Print("Kopia Command", field.M{"Command": args.String()}) + log.Debug().Print("Kopia Command", field.M{"Command": args.String()}) return []string{"bash", "-o", "errexit", "-c", args.PlainText()} } func stringSliceCommand(args logsafe.Cmd) []string { - log.Info().Print("Kopia Command", field.M{"Command": args.String()}) + log.Debug().Print("Kopia Command", field.M{"Command": args.String()}) return args.StringSliceCMD() }