github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/prune/build_pruner.go (about)

     1  // Copyright © 2022 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package prune
    16  
    17  import (
    18  	"github.com/alibaba/sealer/common"
    19  	"github.com/alibaba/sealer/utils"
    20  	"github.com/alibaba/sealer/utils/mount"
    21  )
    22  
    23  type buildPrune struct {
    24  	pruneRootDir string
    25  }
    26  
    27  func NewBuildPrune() Pruner {
    28  	return buildPrune{
    29  		pruneRootDir: common.DefaultTmpDir,
    30  	}
    31  }
    32  
    33  func (b buildPrune) Select() ([]string, error) {
    34  	var pruneList []string
    35  	// umount all tmp dir, and delete it
    36  	pruneUnits, err := utils.GetDirNameListInDir(b.pruneRootDir, utils.FilterOptions{
    37  		All:          true,
    38  		WithFullPath: true,
    39  	})
    40  	if err != nil {
    41  		return pruneList, err
    42  	}
    43  
    44  	for _, unit := range pruneUnits {
    45  		svc := mount.NewMountServiceByTarget(unit)
    46  		if svc == nil {
    47  			pruneList = append(pruneList, unit)
    48  			continue
    49  		}
    50  
    51  		// umount tmp target
    52  		err = svc.TempUMount()
    53  		if err != nil {
    54  			return pruneList, err
    55  		}
    56  		pruneList = append(pruneList, unit)
    57  	}
    58  
    59  	return pruneList, nil
    60  }
    61  
    62  func (b buildPrune) GetSelectorMessage() string {
    63  	return BuildPruner
    64  }