github.com/Cloud-Foundations/Dominator@v0.3.4/dom/lib/api.go (about)

     1  /*
     2  Package lib implements some of the core computations in the dominator.
     3  
     4  Package lib provides functions for computing differences between a sub and
     5  desired image to generate lists of objects for fetching and pushing and
     6  update requests. It contains some common code that both the dominator and
     7  the push-image subcommand of subtool share.
     8  */
     9  package lib
    10  
    11  import (
    12  	"errors"
    13  
    14  	"github.com/Cloud-Foundations/Dominator/lib/filesystem"
    15  	"github.com/Cloud-Foundations/Dominator/lib/filter"
    16  	"github.com/Cloud-Foundations/Dominator/lib/hash"
    17  	"github.com/Cloud-Foundations/Dominator/lib/image"
    18  	"github.com/Cloud-Foundations/Dominator/lib/log"
    19  	"github.com/Cloud-Foundations/Dominator/lib/objectcache"
    20  	"github.com/Cloud-Foundations/Dominator/lib/objectserver"
    21  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    22  	subproto "github.com/Cloud-Foundations/Dominator/proto/sub"
    23  )
    24  
    25  var (
    26  	ErrorFailedToGetObject = errors.New("get object failed")
    27  )
    28  
    29  // Sub should be initialised with data to be used in the package functions.
    30  type Sub struct {
    31  	Hostname                string
    32  	Client                  *srpc.Client
    33  	FileSystem              *filesystem.FileSystem
    34  	ComputedInodes          map[string]*filesystem.RegularInode
    35  	ObjectCache             objectcache.ObjectCache
    36  	ObjectGetter            objectserver.ObjectGetter
    37  	requiredInodeToSubInode map[uint64]uint64
    38  	inodesMapped            map[uint64]struct{} // Sub inode number.
    39  	inodesChanged           map[uint64]struct{} // Required inode number.
    40  	inodesCreated           map[uint64]string   // Required inode number.
    41  	subObjectCacheUsage     map[hash.Hash]uint64
    42  	requiredFS              *filesystem.FileSystem
    43  	filter                  *filter.Filter
    44  }
    45  
    46  // BuildMissingLists will construct lists of objects to be fetched by the sub
    47  // from an object server and the list of computed objects that should be pushed
    48  // to the sub. The lists are generated by comparing the contents of
    49  // sub.FileSystem and sub.ObjectCache with the desired image.
    50  // If pushComputedFiles is true then the list of computed files to be pushed is
    51  // generated.
    52  // If ignoreMissingComputedFiles is true then missing computed files are
    53  // ignored, otherwise these missing files lead to an error and early termination
    54  // of the function.
    55  // Computed file metadata are specified by sub.ComputedInodes.
    56  // BuildMissingLists returns a slice of objects to fetch and a map of files to
    57  // push. The map is nil if there are missing computed files.
    58  func BuildMissingLists(sub Sub, img *image.Image, pushComputedFiles bool,
    59  	ignoreMissingComputedFiles bool, logger log.Logger) (
    60  	map[hash.Hash]uint64, map[hash.Hash]struct{}) {
    61  	return sub.buildMissingLists(img, pushComputedFiles,
    62  		ignoreMissingComputedFiles, logger)
    63  }
    64  
    65  // BuildUpdateRequest will build an update request which can be sent to the sub.
    66  // If deleteMissingComputedFiles is true then missing computed files are deleted
    67  // on the sub, else missing computed files lead to the function failing.
    68  // If deleteMissingComputedFiles is false and ignoreMissingComputedFiles is true
    69  // then missing computed files are ignored.
    70  // It returns true if the function failed due to missing computed files.
    71  func BuildUpdateRequest(sub Sub, img *image.Image,
    72  	request *subproto.UpdateRequest, deleteMissingComputedFiles bool,
    73  	ignoreMissingComputedFiles bool, logger log.Logger) bool {
    74  	return sub.buildUpdateRequest(img, request, deleteMissingComputedFiles,
    75  		ignoreMissingComputedFiles, logger)
    76  }
    77  
    78  // PushObjects will push the list of files given by objectsToPush to the sub.
    79  // File data are obtained from sub.ObjectGetter.
    80  func PushObjects(sub Sub, objectsToPush map[hash.Hash]struct{},
    81  	logger log.Logger) error {
    82  	return sub.pushObjects(objectsToPush, logger)
    83  }
    84  
    85  // String returns the hostname of the sub.
    86  func (sub *Sub) String() string {
    87  	return sub.Hostname
    88  }