package migration import ( "context" "encoding/json" "fmt" "strings" "time" ) type ceph struct { endpoint Endpoint exec Executor native bool } func (c ceph) call(ctx context.Context, args ...string) ([]byte, error) { // A manager operation may wait for MDS capability/session recovery after a // killed client. Keep a deadline, but allow the normal recovery grace period. ctx, cancel := context.WithTimeout(ctx, 2*time.Minute) defer cancel() flags := []string{"--conf", c.endpoint.Config, "--keyring", c.endpoint.Keyring} if c.endpoint.User != "" { flags = append(flags, "--name", c.endpoint.User) } b, err := output(ctx, c.exec, "ceph", append(flags, args...)...) if err != nil { // The first three tokens identify the operation; omit names and metadata // values, which can contain confidential application information. return nil, fmt.Errorf("Ceph %s: %w", strings.Join(args[:min(3, len(args))], " "), err) } return b, nil } func (c ceph) sub(ctx context.Context, op string, extra ...string) ([]byte, error) { args := []string{"fs", "subvolume", op, c.endpoint.Filesystem, c.endpoint.Subvolume} args = append(args, extra...) if c.endpoint.Group != "" { args = append(args, "--group_name", c.endpoint.Group) } return c.call(ctx, args...) } func (c ceph) snap(ctx context.Context, op, name string, extra ...string) ([]byte, error) { args := []string{"fs", "subvolume", "snapshot", op, c.endpoint.Filesystem, c.endpoint.Subvolume} if name != "" { args = append(args, name) } args = append(args, extra...) if c.endpoint.Group != "" { args = append(args, "--group_name", c.endpoint.Group) } return c.call(ctx, args...) } func (c ceph) snapshotPath(ctx context.Context, name string) (string, error) { b, err := c.snap(ctx, "getpath", name) if err != nil { return "", err } p := strings.TrimSpace(string(b)) if c.native { return nativePath(p) } return dataPath(c.endpoint.Mount, p) } func (c ceph) names(ctx context.Context) ([]string, error) { b, err := c.snap(ctx, "ls", "") if err != nil { return nil, err } var list []struct { Name string `json:"name"` } if err = json.Unmarshal(b, &list); err != nil { return nil, err } names := make([]string, 0, len(list)) for _, s := range list { if !validName(s.Name) { return nil, fmt.Errorf("unsupported snapshot name %q", s.Name) } names = append(names, s.Name) } return names, nil } func (c ceph) metadata(ctx context.Context, snapshot string) (map[string]string, error) { args := []string{"fs", "subvolume"} if snapshot != "" { args = append(args, "snapshot") } args = append(args, "metadata", "ls", c.endpoint.Filesystem, c.endpoint.Subvolume) if snapshot != "" { args = append(args, snapshot) } if c.endpoint.Group != "" { args = append(args, "--group_name", c.endpoint.Group) } b, err := c.call(ctx, args...) if err != nil { return nil, err } var m map[string]string err = json.Unmarshal(b, &m) return m, err } func (c ceph) setMetadata(ctx context.Context, snapshot string, m map[string]string) error { // Remove obsolete keys too, including keys removed during a catch-up round. old, err := c.metadata(ctx, snapshot) if err != nil { return err } for k := range old { if _, exists := m[k]; exists { continue } if err := c.metadataCommand(ctx, snapshot, "rm", k, ""); err != nil { return err } } for k, v := range m { if ov, ok := old[k]; ok && ov == v { continue } if err := c.metadataCommand(ctx, snapshot, "set", k, v); err != nil { return err } } return nil } func (c ceph) metadataCommand(ctx context.Context, snapshot, op, key, value string) error { args := []string{"fs", "subvolume"} if snapshot != "" { args = append(args, "snapshot") } args = append(args, "metadata", op, c.endpoint.Filesystem, c.endpoint.Subvolume) if snapshot != "" { args = append(args, snapshot) } args = append(args, "--key_name="+key) if op == "set" { args = append(args, "--value="+value) } if c.endpoint.Group != "" { args = append(args, "--group_name", c.endpoint.Group) } _, err := c.call(ctx, args...) return err }