Skip to content

Commit fcd8677

Browse files
committed
[cain/restore] add user-group to specify files ownership
1 parent c4d2c3f commit fcd8677

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Flags:
113113
-l, --selector string selector to filter on
114114
--src string source to restore from. Example: s3://bucket/cassandra/namespace/cluster-name
115115
-t, --tag string tag to restore
116+
--user-group string user and group who should own restored files (default "cassandra:cassandra")
116117
```
117118

118119
#### Examples

cmd/cain.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ type restoreCmd struct {
108108
container string
109109
parallel int
110110
bufferSize float64
111+
userGroup string
111112

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

167170
return cmd
168171
}

pkg/cain/cain.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type RestoreOptions struct {
7979
Container string
8080
Parallel int
8181
BufferSize float64
82+
UserGroup string
8283
}
8384

8485
// Restore performs restore
@@ -144,6 +145,11 @@ func Restore(o RestoreOptions) error {
144145
return err
145146
}
146147

148+
log.Println("Changing files ownership")
149+
if err := utils.ChangeFilesOwnership(k8sClient, existingPods, o.Namespace, o.Container, o.UserGroup); err != nil {
150+
return err
151+
}
152+
147153
log.Println("Refreshing tables")
148154
RefreshTables(k8sClient, o.Namespace, o.Container, o.Keyspace, podsToBeRestored, tablesToRefresh)
149155

pkg/utils/path.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,19 @@ func PathFromSrcToK8s(k8sClient interface{}, fromPath, cassandraDataDir, srcBase
144144

145145
return toPath, nil
146146
}
147+
148+
// ChangeFilesOwnership changes the ownership of files after restoring them
149+
func ChangeFilesOwnership(iK8sClient interface{}, pods []string, namespace, container, userGroup string) error {
150+
k8sClient := iK8sClient.(*skbn.K8sClient)
151+
command := []string{"chown", "-R", userGroup, cassandraDataDir}
152+
for _, pod := range pods {
153+
stderr, err := skbn.Exec(*k8sClient, namespace, pod, container, command, nil, nil)
154+
if len(stderr) != 0 {
155+
return fmt.Errorf("STDERR: " + (string)(stderr))
156+
}
157+
if err != nil {
158+
return err
159+
}
160+
}
161+
return nil
162+
}

0 commit comments

Comments
 (0)