@@ -571,6 +571,48 @@ func (p *Provisioner) StopAllSessions(exceptClones map[string]struct{}) error {
571
571
return nil
572
572
}
573
573
574
+ func reviewDown (repo * models.Repo , cloneDataset string ) string {
575
+ for snapshotID := range repo .Snapshots {
576
+ if strings .HasPrefix (snapshotID , cloneDataset ) {
577
+ return snapshotID
578
+ }
579
+ }
580
+
581
+ return ""
582
+ }
583
+
584
+ // CleanupCloneDataset removes a clone dataset.
585
+ func (p * Provisioner ) CleanupCloneDataset (clone * models.Clone , pool string ) error {
586
+ if clone .Snapshot == nil {
587
+ return fmt .Errorf ("clone has no snapshot, so the pool cannot be determined. Skip cleanup" )
588
+ }
589
+
590
+ fsm , err := p .pm .GetFSManager (clone .Snapshot .Pool )
591
+ if err != nil {
592
+ return fmt .Errorf ("cannot work with pool %s: %w" , pool , err )
593
+ }
594
+
595
+ repo , err := fsm .GetRepo ()
596
+ if err != nil {
597
+ return fmt .Errorf ("failed to get snapshots: %w" , err )
598
+ }
599
+
600
+ snapshotDep := reviewDown (repo , branching .CloneName (pool , clone .Branch , clone .ID , clone .Revision ))
601
+ if snapshotDep != "" {
602
+ log .Dbg (fmt .Sprintf ("Dataset has commit: %s. Skip destroying" , snapshotDep ))
603
+
604
+ return nil
605
+ }
606
+
607
+ if clone .Revision == branching .DefaultRevision && ! clone .HasDependent {
608
+ if err := fsm .DestroyDataset (branching .CloneDataset (pool , clone .Branch , clone .ID )); err != nil {
609
+ return fmt .Errorf ("failed to destroy clone dataset: %w" , err )
610
+ }
611
+ }
612
+
613
+ return nil
614
+ }
615
+
574
616
func (p * Provisioner ) stopPoolSessions (fsm pool.FSManager , exceptClones map [string ]struct {}) error {
575
617
fsPool := fsm .Pool ()
576
618
0 commit comments