-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): autoclean old downloaded releases
- add autoclean option to the update command (default true) - track release recent use time with metafiles - clean releases after repo update procedure - keep actual releases - keep recently used releases Signed-off-by: Alexey Igrychev <alexey.igrychev@flant.com>
- Loading branch information
1 parent
605dc22
commit a2ea508
Showing
6 changed files
with
156 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package repo | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/werf/trdl/client/pkg/util" | ||
) | ||
|
||
var releaseMetafileExpirationPeriod = time.Hour * 24 | ||
|
||
func (c Client) CleanReleases() error { | ||
actualLocalReleases, err := c.getActualLocalReleases() | ||
if err != nil { | ||
return fmt.Errorf("unable to get actual local releases: %s", err) | ||
} | ||
|
||
allReleasesGlob := filepath.Join(c.dir, releasesDir, "*") | ||
releaseDirList, err := filepath.Glob(allReleasesGlob) | ||
if err != nil { | ||
return fmt.Errorf("unable to glob files: %s", err) | ||
} | ||
|
||
for _, releaseDir := range releaseDirList { | ||
_, releaseName := filepath.Split(releaseDir) | ||
|
||
// skip actual channel release | ||
if actualLocalReleases[releaseName] { | ||
continue | ||
} | ||
|
||
// skip recently used release | ||
{ | ||
metafile := c.releaseMetafile(releaseName) | ||
isRecentlyUsed, err := metafile.HasBeenModifiedWithinPeriod(c.locker, releaseMetafileExpirationPeriod) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if isRecentlyUsed { | ||
continue | ||
} | ||
|
||
if err := metafile.Delete(c.locker); err != nil { | ||
return fmt.Errorf("unable to remove release %q metafile: %s", releaseName, err) | ||
} | ||
} | ||
|
||
if err := os.RemoveAll(releaseDir); err != nil { | ||
return fmt.Errorf("unable to remove %q: %s", releaseDir, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c Client) getActualLocalReleases() (map[string]bool, error) { | ||
actualLocalReleases := map[string]bool{} | ||
|
||
allChannelsGlob := filepath.Join(c.dir, channelsDir, "*", "*") | ||
filePathList, err := filepath.Glob(allChannelsGlob) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to glob files: %s", err) | ||
} | ||
|
||
for _, filePath := range filePathList { | ||
exist, err := util.IsRegularFileExist(filePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to check existence of file %q: %s", filePath, err) | ||
} | ||
|
||
if !exist { | ||
continue | ||
} | ||
|
||
release, err := readChannelRelease(filePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to get channel release: %s", err) | ||
} | ||
|
||
actualLocalReleases[release] = true | ||
} | ||
|
||
return actualLocalReleases, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters