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

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/dom/lib"
     9  	"github.com/Cloud-Foundations/Dominator/lib/filesystem"
    10  	"github.com/Cloud-Foundations/Dominator/lib/filesystem/scanner"
    11  	"github.com/Cloud-Foundations/Dominator/lib/filter"
    12  	"github.com/Cloud-Foundations/Dominator/lib/json"
    13  	"github.com/Cloud-Foundations/Dominator/lib/log"
    14  	"github.com/Cloud-Foundations/Dominator/lib/objectcache"
    15  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    16  	"github.com/Cloud-Foundations/Dominator/proto/sub"
    17  	"github.com/Cloud-Foundations/Dominator/sub/client"
    18  )
    19  
    20  func showUpdateRequestSubcommand(args []string, logger log.DebugLogger) error {
    21  	srpcClient := getSubClientRetry(logger)
    22  	defer srpcClient.Close()
    23  	if err := showUpdateRequest(srpcClient, args[0]); err != nil {
    24  		return fmt.Errorf("Error showing update: %s: %s", args[0], err)
    25  	}
    26  	return nil
    27  }
    28  
    29  func showUpdateRequest(srpcClient *srpc.Client, imageName string) error {
    30  	// Start querying the imageserver for the image.
    31  	imageServerAddress := fmt.Sprintf("%s:%d",
    32  		*imageServerHostname, *imageServerPortNum)
    33  	imgChannel := getImageChannel(imageServerAddress, imageName, timeoutTime)
    34  	subObj := lib.Sub{
    35  		Hostname: *subHostname,
    36  		Client:   srpcClient,
    37  	}
    38  	pollRequest := sub.PollRequest{}
    39  	var pollReply sub.PollResponse
    40  	if err := client.CallPoll(srpcClient, pollRequest, &pollReply); err != nil {
    41  		return err
    42  	}
    43  	fs := pollReply.FileSystem
    44  	if fs == nil {
    45  		return errors.New("sub not ready")
    46  	}
    47  	if err := fs.RebuildInodePointers(); err != nil {
    48  		return err
    49  	}
    50  	fs.BuildEntryMap()
    51  	subObj.FileSystem = fs
    52  	imageResult := <-imgChannel
    53  	img := imageResult.image
    54  	if *filterFile != "" {
    55  		var err error
    56  		img.Filter, err = filter.Load(*filterFile)
    57  		if err != nil {
    58  			return err
    59  		}
    60  	}
    61  	deleteMissingComputedFiles := true
    62  	ignoreMissingComputedFiles := false
    63  	pushComputedFiles := true
    64  	if *computedFilesRoot == "" {
    65  		subObj.ObjectGetter = nullObjectGetterType{}
    66  		deleteMissingComputedFiles = false
    67  		ignoreMissingComputedFiles = true
    68  		pushComputedFiles = false
    69  	} else {
    70  		fs, err := scanner.ScanFileSystem(*computedFilesRoot, nil, nil, nil,
    71  			nil, nil)
    72  		if err != nil {
    73  			return err
    74  		}
    75  		subObj.ObjectGetter = fs
    76  		computedInodes := make(map[string]*filesystem.RegularInode)
    77  		subObj.ComputedInodes = computedInodes
    78  		for filename, inum := range fs.FilenameToInodeTable() {
    79  			if inode, ok := fs.InodeTable[inum].(*filesystem.RegularInode); ok {
    80  				computedInodes[filename] = inode
    81  			}
    82  		}
    83  	}
    84  	objectsToFetch, _ := lib.BuildMissingLists(subObj, img, pushComputedFiles,
    85  		ignoreMissingComputedFiles, logger)
    86  	subObj.ObjectCache = objectcache.ObjectMapToCache(objectsToFetch)
    87  	var updateRequest sub.UpdateRequest
    88  	if lib.BuildUpdateRequest(subObj, img, &updateRequest,
    89  		deleteMissingComputedFiles, ignoreMissingComputedFiles, logger) {
    90  		return errors.New("missing computed file(s)")
    91  	}
    92  	if err := json.WriteWithIndent(os.Stdout, "  ", updateRequest); err != nil {
    93  		return err
    94  	}
    95  	return nil
    96  }