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