func pollFetchAndPush(subObj *lib.Sub, img *image.Image, imageServerAddress string, timeoutTime time.Time, logger *log.Logger) error { var generationCount uint64 deleteEarly := *deleteBeforeFetch for ; time.Now().Before(timeoutTime); time.Sleep(time.Second) { var pollReply sub.PollResponse if err := pollAndBuildPointers(subObj.Client, &generationCount, &pollReply); err != nil { return err } if pollReply.FileSystem == nil { continue } if deleteEarly { deleteEarly = false if deleteUnneededFiles(subObj.Client, pollReply.FileSystem, img.FileSystem, logger) { continue } } subObj.FileSystem = pollReply.FileSystem subObj.ObjectCache = pollReply.ObjectCache startTime := showStart("lib.BuildMissingLists()") objectsToFetch, objectsToPush := lib.BuildMissingLists(*subObj, img, true, true, logger) showTimeTaken(startTime) if len(objectsToFetch) < 1 && len(objectsToPush) < 1 { return nil } if len(objectsToFetch) > 0 { startTime := showStart("Fetch()") err := subObj.Client.RequestReply("Subd.Fetch", sub.FetchRequest{ ServerAddress: imageServerAddress, Wait: true, Hashes: objectsToFetch}, &sub.FetchResponse{}) if err != nil { showBlankLine() logger.Printf("Error calling %s:Subd.Fetch(%s): %s\n", subObj.Hostname, imageServerAddress, err) return err } showTimeTaken(startTime) } if len(objectsToPush) > 0 { startTime := showStart("lib.PushObjects()") err := lib.PushObjects(*subObj, objectsToPush, logger) if err != nil { showBlankLine() return err } showTimeTaken(startTime) } } return errors.New("timed out fetching and pushing objects") }
func pushImage(getSubClient getSubClientFunc, imageName string) error { logger := log.New(os.Stderr, "", log.LstdFlags) computedInodes := make(map[string]*filesystem.RegularInode) // Start querying the imageserver for the image. imageServerAddress := fmt.Sprintf("%s:%d", *imageServerHostname, *imageServerPortNum) imgChannel := getImageChannel(imageServerAddress, imageName, timeoutTime) startTime := showStart("getSubClient()") srpcClient := getSubClient() showTimeTaken(startTime) subObj := lib.Sub{ Hostname: *subHostname, Client: srpcClient, ComputedInodes: computedInodes} if *computedFilesRoot == "" { subObj.ObjectGetter = nullObjectGetterType{} } else { fs, err := scanner.ScanFileSystem(*computedFilesRoot, nil, nil, nil, nil, nil) if err != nil { return err } subObj.ObjectGetter = fs for filename, inum := range fs.FilenameToInodeTable() { if inode, ok := fs.InodeTable[inum].(*filesystem.RegularInode); ok { computedInodes[filename] = inode } } } startTime = showStart("<-imgChannel") imageResult := <-imgChannel showTimeTaken(startTime) fmt.Fprintf(os.Stderr, "Background image fetch took %s\n", format.Duration(imageResult.duration)) img := imageResult.image var err error if *filterFile != "" { img.Filter, err = filter.Load(*filterFile) if err != nil { return err } } if *triggersFile != "" { img.Triggers, err = triggers.Load(*triggersFile) if err != nil { return err } } else if *triggersString != "" { img.Triggers, err = triggers.Decode([]byte(*triggersString)) if err != nil { return err } } if err := pollFetchAndPush(&subObj, img, imageServerAddress, timeoutTime, logger); err != nil { return err } var updateRequest sub.UpdateRequest var updateReply sub.UpdateResponse startTime = showStart("lib.BuildUpdateRequest()") if lib.BuildUpdateRequest(subObj, img, &updateRequest, true, logger) { showBlankLine() return errors.New("missing computed file(s)") } showTimeTaken(startTime) updateRequest.ImageName = imageName updateRequest.Wait = true startTime = showStart("Subd.Update()") err = client.CallUpdate(srpcClient, updateRequest, &updateReply) if err != nil { showBlankLine() return err } showTimeTaken(startTime) return nil }