github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/subtool/waitForImage.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/lib/log"
     9  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    10  	"github.com/Cloud-Foundations/Dominator/proto/sub"
    11  	"github.com/Cloud-Foundations/Dominator/sub/client"
    12  )
    13  
    14  func waitForImageSubcommand(args []string, logger log.DebugLogger) error {
    15  	srpcClient := getSubClientRetry(logger)
    16  	defer srpcClient.Close()
    17  	if err := waitForImage(srpcClient, args[0], logger); err != nil {
    18  		return fmt.Errorf("Error waiting for image: %s: %s", args[0], err)
    19  	}
    20  	return nil
    21  }
    22  
    23  func waitForImage(srpcClient *srpc.Client, imageName string,
    24  	logger log.DebugLogger) error {
    25  	if imageName == "" {
    26  		return errors.New("empty image name")
    27  	}
    28  	for time.Now().Before(timeoutTime) {
    29  		if waitForImageLoop(srpcClient, imageName, logger) {
    30  			return nil
    31  		}
    32  	}
    33  	return errors.New("timed out waiting for update to image")
    34  }
    35  
    36  func waitForImageLoop(srpcClient *srpc.Client, imageName string,
    37  	logger log.DebugLogger) bool {
    38  	request := sub.PollRequest{ShortPollOnly: true}
    39  	for ; time.Now().Before(timeoutTime); time.Sleep(time.Second) {
    40  		var reply sub.PollResponse
    41  		if err := client.CallPoll(srpcClient, request, &reply); err != nil {
    42  			logger.Println(err)
    43  			return false
    44  		}
    45  		if reply.LastSuccessfulImageName == imageName {
    46  			return true
    47  		}
    48  	}
    49  	return false
    50  }