git: add archive integrity check

This commit is contained in:
stefanprodan 2020-04-27 13:39:36 +03:00
parent 8b98573493
commit 9540efe9de
2 changed files with 20 additions and 3 deletions

View File

@ -341,8 +341,8 @@ func (r *GitRepositoryReconciler) sync(ctx context.Context, repository sourcev1.
}
defer unlock()
// archive artifact
err = r.Storage.Archive(artifact, tmpGit, "")
// archive artifact and check integrity
err = r.Storage.Archive(artifact, tmpGit, "", true)
if err != nil {
err = fmt.Errorf("storage archive error: %w", err)
return sourcev1.GitRepositoryNotReady(repository, sourcev1.StorageOperationFailedReason, err.Error()), err

View File

@ -113,7 +113,7 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool {
}
// Archive creates a tar.gz to the artifact path from the given dir excluding the provided file extensions
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes string) error {
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes string, integrityCheck bool) error {
if excludes == "" {
excludes = "jpg,jpeg,gif,png,wmv,flv,tar.gz,zip"
}
@ -129,6 +129,23 @@ func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes strin
if err != nil {
return fmt.Errorf("command '%s' failed: %w", cmd, err)
}
if integrityCheck {
cmd = fmt.Sprintf("gunzip -t %s", artifact.Path)
command = exec.CommandContext(ctx, "/bin/sh", "-c", cmd)
err = command.Run()
if err != nil {
return fmt.Errorf("gzip integrity check failed")
}
cmd = fmt.Sprintf("tar -tzf %s >/dev/null", artifact.Path)
command = exec.CommandContext(ctx, "/bin/sh", "-c", cmd)
err = command.Run()
if err != nil {
return fmt.Errorf("tar integrity check failed")
}
}
return nil
}