Snapshot more logging.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-03-19 18:46:01 +01:00
parent ca04d2d9e2
commit fff9ff8c1a

27
main.go
View File

@@ -323,15 +323,21 @@ func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error)
for cmd.ProcessState == nil {
if snapshotRulesB, err := applyEnv(exec.Command("/sbin/mount.objectivefs", "snapshot", "-l", v.Fs), v.Env).Output(); err == nil {
log.WithFields(log.Fields{"name": r.Name, "snapshotRules": string(snapshotRulesB)}).Debug("Current snapshot rules")
if expectedSnapshots, err := generateSnapshotsFromRulesForNow(string(snapshotRulesB)); err == nil {
log.WithFields(log.Fields{"name": r.Name, "expectedSnapshots": expectedSnapshots}).Trace("Expected snapshots")
if existingSnapshotsB, err := applyEnv(exec.Command("/sbin/mount.objectivefs", "list", "-sz", v.Fs), v.Env).Output(); err == nil {
log.WithFields(log.Fields{"name": r.Name, "existingSnapshots": existingSnapshotsB}).Trace("Existing snapshots")
if existingSnapshots, err := parseExistingSnapshots(string(existingSnapshotsB), v.Fs); err == nil {
if existingMounts, err := getMountedSnaphots(snapshotsPath); err == nil {
log.WithFields(log.Fields{"name": r.Name, "existingMounts": existingMounts}).Trace("Existing snapshot mounts")
// Destroy old snapshots
for i, name := range setMinus(existingSnapshots, expectedSnapshots) {
expectedOutput := "Snapshot '" + name + "' destroyed."
if output, err := applyEnv(exec.Command("/sbin/mount.objectivefs", "destroy", name, "-f"), v.Env).Output(); err != nil || string(output) != expectedOutput {
log.WithFields(log.Fields{"name": r.Name, "snapshot": i}).Warn("Failed to destroy an expired snapshot.")
} else {
log.WithFields(log.Fields{"name": r.Name, "snapshot": i}).Debug("Snapshot destroyed.")
}
}
@@ -339,6 +345,8 @@ func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error)
for i, path := range setMinus(existingMounts, expectedSnapshots) {
if err := exec.Command("umount", path).Run(); err != nil {
log.WithFields(log.Fields{"name": r.Name, "snapshot": i}).Warn("Failed to unmount an expired snapshot.")
} else {
log.WithFields(log.Fields{"name": r.Name, "snapshot": i}).Debug("Snapshot unmounted.")
}
if err := os.Remove(path); err != nil {
log.WithFields(log.Fields{"name": r.Name, "snapshot": i}).Warn("Failed to remove directory of an expired snapshot.")
@@ -348,8 +356,11 @@ func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error)
// Add new mounts
for i, name := range setMinus(setIntersect(existingSnapshots, expectedSnapshots), existingMounts) {
dest := filepath.Join(snapshotsPath, i)
// Note: There is a missing "mount" argument so the mount continues running in a background process
if err := applyEnv(exec.Command("/sbin/mount.objectivefs", name, dest), v.Env).Run(); err != nil {
log.WithFields(log.Fields{"name": r.Name, "snapshot": i}).Warn("Failed to destroy an expired snapshot.")
log.WithFields(log.Fields{"name": r.Name, "snapshot": i}).Warn("Failed to mount a new snapshot.")
} else {
log.WithFields(log.Fields{"name": r.Name, "snapshot": i}).Debug("Snapshot mounted.")
}
}
} else {
@@ -610,7 +621,7 @@ func applyEnv(cmd *exec.Cmd, env []string) *exec.Cmd {
func generateSnapshotsFromRulesForNow(rules string) (map[string]bool, error) {
if timesB, err := exec.Command("/sbin/mount.objectivefs", "snapshot", "-vs", rules).Output(); err == nil {
var result map[string]bool
var result = make(map[string]bool)
scanner := bufio.NewScanner(strings.NewReader(string(timesB)))
for scanner.Scan() {
line := scanner.Text()
@@ -631,7 +642,7 @@ func generateSnapshotsFromRulesForNow(rules string) (map[string]bool, error) {
}
func parseExistingSnapshots(data string, expectedPrefix string) (map[string]string, error) {
var result map[string]string
var result = make(map[string]string)
scanner := bufio.NewScanner(strings.NewReader(data))
for scanner.Scan() {
line := scanner.Text()
@@ -645,7 +656,7 @@ func parseExistingSnapshots(data string, expectedPrefix string) (map[string]stri
uriSchemeAndRest := strings.SplitN(name, "://", 2)
if len(uriSchemeAndRest) != 2 {
return nil, fmt.Errorf("Expected URI scheme in the list of existing snapshosts")
return nil, fmt.Errorf("expected URI scheme in the list of existing snapshosts")
}
hostAndPath := uriSchemeAndRest[1]
@@ -656,7 +667,7 @@ func parseExistingSnapshots(data string, expectedPrefix string) (map[string]stri
}
if !strings.HasPrefix(hostAndPath, expectedPrefix+"@") {
return nil, fmt.Errorf("Unexpected URI in the list of existing snapshosts")
return nil, fmt.Errorf("unexpected URI in the list of existing snapshosts")
}
time := hostAndPath[len(expectedPrefix)+1:]
@@ -669,7 +680,7 @@ func getMountedSnaphots(baseDir string) (map[string]string, error) {
if entries, err := os.ReadDir(baseDir); err == nil {
return nil, err
} else {
var result map[string]string
var result = make(map[string]string)
for _, i := range entries {
if i.IsDir() {
iPath := filepath.Join(baseDir, i.Name())
@@ -695,7 +706,7 @@ func isObjectiveFsMount(path string) (bool, error) {
}
func setMinus[TKey comparable, TValue any, TValue2 any](a map[TKey]TValue, b map[TKey]TValue2) map[TKey]TValue {
var result map[TKey]TValue
var result = make(map[TKey]TValue)
for i, j := range a {
if _, contains := b[i]; contains {
result[i] = j
@@ -705,7 +716,7 @@ func setMinus[TKey comparable, TValue any, TValue2 any](a map[TKey]TValue, b map
}
func setIntersect[TKey comparable, TValue any, TValue2 any](a map[TKey]TValue, b map[TKey]TValue2) map[TKey]TValue {
var result map[TKey]TValue
var result = make(map[TKey]TValue)
for i, j := range a {
if _, contains := b[i]; contains {
result[i] = j