github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/imageunpacker/unpacker/api.go (about) 1 package unpacker 2 3 import ( 4 "io" 5 "sync" 6 "time" 7 8 "github.com/Cloud-Foundations/Dominator/lib/filesystem" 9 "github.com/Cloud-Foundations/Dominator/lib/log" 10 "github.com/Cloud-Foundations/Dominator/lib/objectcache" 11 proto "github.com/Cloud-Foundations/Dominator/proto/imageunpacker" 12 ) 13 14 const ( 15 requestAssociateWithDevice = iota 16 requestScan 17 requestUnpack 18 requestPrepareForCapture 19 requestPrepareForCopy 20 requestExport 21 ) 22 23 var ( 24 stateFile = "state.json" 25 ) 26 27 type deviceInfo struct { 28 DeviceName string 29 partitionTimestamp time.Time 30 size uint64 31 StreamName string 32 } 33 34 type requestType struct { 35 request int 36 desiredFS *filesystem.FileSystem 37 imageName string 38 deviceId string 39 skipIfPrepared bool 40 exportType string 41 exportDestination string 42 errorChannel chan<- error 43 } 44 45 type imageStreamInfo struct { 46 DeviceId string 47 status proto.StreamStatus 48 requestChannel chan<- requestType 49 scannedFS *filesystem.FileSystem 50 } 51 52 type persistentState struct { 53 Devices map[string]deviceInfo // Key: DeviceId. 54 ImageStreams map[string]*imageStreamInfo // Key: StreamName. 55 } 56 57 type streamManagerState struct { 58 unpacker *Unpacker 59 streamName string 60 streamInfo *imageStreamInfo 61 fileSystem *filesystem.FileSystem 62 objectCache objectcache.ObjectCache 63 rootLabel string 64 } 65 66 type Unpacker struct { 67 baseDir string 68 imageServerAddress string 69 logger log.DebugLogger 70 rwMutex sync.RWMutex // Protect below. 71 pState persistentState 72 scannedDevices map[string]struct{} 73 lastUsedTime time.Time 74 } 75 76 func Load(baseDir string, imageServerAddress string, logger log.DebugLogger) ( 77 *Unpacker, error) { 78 return load(baseDir, imageServerAddress, logger) 79 } 80 81 func (u *Unpacker) AddDevice(deviceId string) error { 82 return u.addDevice(deviceId) 83 } 84 85 func (u *Unpacker) AssociateStreamWithDevice(streamName string, 86 deviceId string) error { 87 return u.associateStreamWithDevice(streamName, deviceId) 88 } 89 90 func (u *Unpacker) ExportImage(streamName string, exportType string, 91 exportDestination string) error { 92 return u.exportImage(streamName, exportType, exportDestination) 93 } 94 95 func (u *Unpacker) GetFileSystem(streamName string) ( 96 *filesystem.FileSystem, error) { 97 return u.getFileSystem(streamName) 98 } 99 100 func (u *Unpacker) GetStatus() proto.GetStatusResponse { 101 return u.getStatus() 102 } 103 104 func (u *Unpacker) PrepareForCapture(streamName string) error { 105 return u.prepareForCapture(streamName) 106 } 107 108 func (u *Unpacker) PrepareForCopy(streamName string) error { 109 return u.prepareForCopy(streamName) 110 } 111 112 func (u *Unpacker) PrepareForUnpack(streamName string, skipIfPrepared bool, 113 doNotWaitForResult bool) error { 114 return u.prepareForUnpack(streamName, skipIfPrepared, doNotWaitForResult) 115 } 116 117 func (u *Unpacker) PrepareForAddDevice() error { 118 return u.prepareForAddDevice() 119 } 120 121 func (u *Unpacker) RemoveDevice(deviceId string) error { 122 return u.removeDevice(deviceId) 123 } 124 125 func (u *Unpacker) UnpackImage(streamName string, imageLeafName string) error { 126 return u.unpackImage(streamName, imageLeafName) 127 } 128 129 func (u *Unpacker) WriteHtml(writer io.Writer) { 130 u.writeHtml(writer) 131 }