Skip to content

Commit

Permalink
[cain/restore] add user-group to specify files ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
maorfr committed Dec 30, 2018
1 parent c4d2c3f commit fcd8677
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Flags:
-l, --selector string selector to filter on
--src string source to restore from. Example: s3://bucket/cassandra/namespace/cluster-name
-t, --tag string tag to restore
--user-group string user and group who should own restored files (default "cassandra:cassandra")
```

#### Examples
Expand Down
3 changes: 3 additions & 0 deletions cmd/cain.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type restoreCmd struct {
container string
parallel int
bufferSize float64
userGroup string

out io.Writer
}
Expand Down Expand Up @@ -146,6 +147,7 @@ func NewRestoreCmd(out io.Writer) *cobra.Command {
Container: r.container,
Parallel: r.parallel,
BufferSize: r.bufferSize,
UserGroup: r.userGroup,
}
if err := cain.Restore(options); err != nil {
log.Fatal(err)
Expand All @@ -163,6 +165,7 @@ func NewRestoreCmd(out io.Writer) *cobra.Command {
f.StringVarP(&r.container, "container", "c", "cassandra", "container name to act on")
f.IntVarP(&r.parallel, "parallel", "p", 1, "number of files to copy in parallel. set this flag to 0 for full parallelism")
f.Float64VarP(&r.bufferSize, "buffer-size", "b", 6.75, "in memory buffer size (MB) to use for files copy (buffer per file)")
f.StringVar(&r.userGroup, "user-group", "cassandra:cassandra", "user and group who should own restored files")

return cmd
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/cain/cain.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type RestoreOptions struct {
Container string
Parallel int
BufferSize float64
UserGroup string
}

// Restore performs restore
Expand Down Expand Up @@ -144,6 +145,11 @@ func Restore(o RestoreOptions) error {
return err
}

log.Println("Changing files ownership")
if err := utils.ChangeFilesOwnership(k8sClient, existingPods, o.Namespace, o.Container, o.UserGroup); err != nil {
return err
}

log.Println("Refreshing tables")
RefreshTables(k8sClient, o.Namespace, o.Container, o.Keyspace, podsToBeRestored, tablesToRefresh)

Expand Down
16 changes: 16 additions & 0 deletions pkg/utils/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,19 @@ func PathFromSrcToK8s(k8sClient interface{}, fromPath, cassandraDataDir, srcBase

return toPath, nil
}

// ChangeFilesOwnership changes the ownership of files after restoring them
func ChangeFilesOwnership(iK8sClient interface{}, pods []string, namespace, container, userGroup string) error {
k8sClient := iK8sClient.(*skbn.K8sClient)
command := []string{"chown", "-R", userGroup, cassandraDataDir}
for _, pod := range pods {
stderr, err := skbn.Exec(*k8sClient, namespace, pod, container, command, nil, nil)
if len(stderr) != 0 {
return fmt.Errorf("STDERR: " + (string)(stderr))
}
if err != nil {
return err
}
}
return nil
}

0 comments on commit fcd8677

Please sign in to comment.