From a0954df2fb4e415512ef37ca8f0fd9877ec2a7e8 Mon Sep 17 00:00:00 2001 From: Mark de Villiers Date: Tue, 24 Nov 2015 07:33:37 -0800 Subject: [PATCH] analyze-local-images : fix race condition on intial testing --- contrib/analyze-local-images/main.go | 50 +++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/contrib/analyze-local-images/main.go b/contrib/analyze-local-images/main.go index 147dc1c5..15aa69ff 100644 --- a/contrib/analyze-local-images/main.go +++ b/contrib/analyze-local-images/main.go @@ -14,6 +14,7 @@ import ( "os/exec" "strconv" "strings" + "sync" "time" ) @@ -86,15 +87,50 @@ func main() { // Analyze layers fmt.Printf("Analyzing %d layers\n", len(layerIDs)) + for i := 0; i < len(layerIDs); i++ { - fmt.Printf("- Analyzing %s\n", layerIDs[i]) + + layerTarFile := path + "/" + layerIDs[i] + "/layer.tar" + fmt.Printf("- Analyzing %s at %s\n", layerIDs[i], layerTarFile) var err error - if i > 0 { - err = analyzeLayer(*endpoint, path+"/"+layerIDs[i]+"/layer.tar", layerIDs[i], layerIDs[i-1]) - } else { - err = analyzeLayer(*endpoint, path+"/"+layerIDs[i]+"/layer.tar", layerIDs[i], "") + var wg sync.WaitGroup + + wg.Add(1) + + go func() { + retrys := 0 + + for { + //retry until layer is unzipped + if _, err := os.Stat(layerTarFile); err == nil { + break + } + retrys++ + fmt.Printf("\nretrying : %d", retrys) + time.Sleep(1 * time.Second) + + if retrys == 10 { + err = errors.New("Time out waiting for layer being untarred.") + break + } + } + + wg.Done() + }() + + wg.Wait() + + if err != nil { + log.Fatalf("- Could not analyze layer: %s\n", err) } + + if i > 0 { + err = analyzeLayer(*endpoint, layerTarFile, layerIDs[i], layerIDs[i-1]) + } else { + err = analyzeLayer(*endpoint, layerTarFile, layerIDs[i], "") + } + if err != nil { log.Fatalf("- Could not analyze layer: %s\n", err) } @@ -143,6 +179,10 @@ func save(imageName string) (string, error) { return "", errors.New(stderr.String()) } + err = pipe.Close() + if err != nil { + return "", errors.New(stderr.String()) + } return path, nil }