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

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/Cloud-Foundations/Dominator/dom/lib"
     8  	"github.com/Cloud-Foundations/Dominator/lib/filter"
     9  	"github.com/Cloud-Foundations/Dominator/lib/log"
    10  	"github.com/Cloud-Foundations/Dominator/lib/objectcache"
    11  	objectclient "github.com/Cloud-Foundations/Dominator/lib/objectserver/client"
    12  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    13  	"github.com/Cloud-Foundations/Dominator/proto/sub"
    14  	"github.com/Cloud-Foundations/Dominator/sub/client"
    15  )
    16  
    17  func pushMissingObjectsSubcommand(args []string, logger log.DebugLogger) error {
    18  	srpcClient := getSubClientRetry(logger)
    19  	defer srpcClient.Close()
    20  	if err := pushMissingObjects(srpcClient, args[0]); err != nil {
    21  		return fmt.Errorf("Error pushing missing objects: %s: %s", args[0], err)
    22  	}
    23  	return nil
    24  }
    25  
    26  func pushMissingObjects(srpcClient *srpc.Client, imageName string) error {
    27  	// Start querying the imageserver for the image.
    28  	imageServerAddress := fmt.Sprintf("%s:%d",
    29  		*imageServerHostname, *imageServerPortNum)
    30  	imgChannel := getImageChannel(imageServerAddress, imageName, timeoutTime)
    31  	subObj := lib.Sub{
    32  		Hostname: *subHostname,
    33  		Client:   srpcClient,
    34  	}
    35  	pollRequest := sub.PollRequest{}
    36  	var pollReply sub.PollResponse
    37  	if err := client.CallPoll(srpcClient, pollRequest, &pollReply); err != nil {
    38  		return err
    39  	}
    40  	fs := pollReply.FileSystem
    41  	if fs == nil {
    42  		return errors.New("sub not ready")
    43  	}
    44  	subObj.FileSystem = fs
    45  	objSrv := objectclient.NewObjectClient(fmt.Sprintf("%s:%d",
    46  		*objectServerHostname, *objectServerPortNum))
    47  	subObjClient := objectclient.AttachObjectClient(srpcClient)
    48  	defer subObjClient.Close()
    49  	imageResult := <-imgChannel
    50  	img := imageResult.image
    51  	if *filterFile != "" {
    52  		var err error
    53  		img.Filter, err = filter.Load(*filterFile)
    54  		if err != nil {
    55  			return err
    56  		}
    57  	}
    58  	objectsToFetch, _ := lib.BuildMissingLists(subObj, img, false, true,
    59  		logger)
    60  	hashes := objectcache.ObjectMapToCache(objectsToFetch)
    61  	objectsReader, err := objSrv.GetObjects(hashes)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	defer objectsReader.Close()
    66  	for _, hashVal := range hashes {
    67  		fmt.Printf("%x\n", hashVal)
    68  		length, reader, err := objectsReader.NextObject()
    69  		if err != nil {
    70  			return err
    71  		}
    72  		_, _, err = subObjClient.AddObject(reader, length, nil)
    73  		reader.Close()
    74  		if err != nil {
    75  			return err
    76  		}
    77  	}
    78  	return nil
    79  }