github.com/Cloud-Foundations/Dominator@v0.3.4/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/log/serverlogger"
    11  	"github.com/Cloud-Foundations/Dominator/lib/objectcache"
    12  	proto "github.com/Cloud-Foundations/Dominator/proto/imageunpacker"
    13  )
    14  
    15  const (
    16  	requestAssociateWithDevice = iota
    17  	requestScan
    18  	requestUnpack
    19  	requestPrepareForCapture
    20  	requestPrepareForCopy
    21  	requestExport
    22  	requestGetRaw
    23  	requestForget
    24  )
    25  
    26  var (
    27  	stateFile = "state.json"
    28  )
    29  
    30  type deviceInfo struct {
    31  	DeviceName         string
    32  	partitionTimestamp time.Time
    33  	size               uint64
    34  	StreamName         string
    35  }
    36  
    37  type requestType struct {
    38  	request           int
    39  	desiredFS         *filesystem.FileSystem
    40  	imageName         string
    41  	deviceId          string
    42  	skipIfPrepared    bool
    43  	exportType        string
    44  	exportDestination string
    45  	errorChannel      chan<- error
    46  	readerChannel     chan<- *sizedReader
    47  }
    48  
    49  type imageStreamInfo struct {
    50  	DeviceId       string
    51  	dualLogger     log.DebugLogger
    52  	requestChannel chan<- requestType
    53  	scannedFS      *filesystem.FileSystem
    54  	status         proto.StreamStatus
    55  	streamLogger   *serverlogger.Logger
    56  }
    57  
    58  type persistentState struct {
    59  	Devices      map[string]deviceInfo       // Key: DeviceId.
    60  	ImageStreams map[string]*imageStreamInfo // Key: StreamName.
    61  }
    62  
    63  type sizedReader struct {
    64  	closeNotifier chan<- struct{}
    65  	err           error
    66  	nRead         uint64
    67  	reader        io.Reader
    68  	size          uint64
    69  }
    70  
    71  type streamManagerState struct {
    72  	unpacker    *Unpacker
    73  	streamName  string
    74  	streamInfo  *imageStreamInfo
    75  	fileSystem  *filesystem.FileSystem
    76  	objectCache objectcache.ObjectCache
    77  	rootLabel   string
    78  }
    79  
    80  type Unpacker struct {
    81  	baseDir            string
    82  	imageServerAddress string
    83  	logger             log.DebugLogger
    84  	rwMutex            sync.RWMutex // Protect below.
    85  	pState             persistentState
    86  	scannedDevices     map[string]struct{}
    87  	lastUsedTime       time.Time
    88  }
    89  
    90  func Load(baseDir string, imageServerAddress string, logger log.DebugLogger) (
    91  	*Unpacker, error) {
    92  	return load(baseDir, imageServerAddress, logger)
    93  }
    94  
    95  func (u *Unpacker) AddDevice(deviceId string) error {
    96  	return u.addDevice(deviceId)
    97  }
    98  
    99  func (u *Unpacker) AssociateStreamWithDevice(streamName string,
   100  	deviceId string) error {
   101  	return u.associateStreamWithDevice(streamName, deviceId)
   102  }
   103  
   104  func (u *Unpacker) ClaimDevice(deviceId, deviceName string) error {
   105  	return u.claimDevice(deviceId, deviceName)
   106  }
   107  
   108  func (u *Unpacker) ExportImage(streamName string, exportType string,
   109  	exportDestination string) error {
   110  	return u.exportImage(streamName, exportType, exportDestination)
   111  }
   112  
   113  func (u *Unpacker) ForgetStream(streamName string) error {
   114  	return u.forgetStream(streamName)
   115  }
   116  
   117  func (u *Unpacker) GetFileSystem(streamName string) (
   118  	*filesystem.FileSystem, error) {
   119  	return u.getFileSystem(streamName)
   120  }
   121  
   122  func (u *Unpacker) GetRaw(streamName string) (io.ReadCloser, uint64, error) {
   123  	return u.getRaw(streamName)
   124  }
   125  
   126  func (u *Unpacker) GetStatus() proto.GetStatusResponse {
   127  	return u.getStatus()
   128  }
   129  
   130  func (u *Unpacker) PrepareForCapture(streamName string) error {
   131  	return u.prepareForCapture(streamName)
   132  }
   133  
   134  func (u *Unpacker) PrepareForCopy(streamName string) error {
   135  	return u.prepareForCopy(streamName)
   136  }
   137  
   138  func (u *Unpacker) PrepareForUnpack(streamName string, skipIfPrepared bool,
   139  	doNotWaitForResult bool) error {
   140  	return u.prepareForUnpack(streamName, skipIfPrepared, doNotWaitForResult)
   141  }
   142  
   143  func (u *Unpacker) PrepareForAddDevice() error {
   144  	return u.prepareForAddDevice()
   145  }
   146  
   147  func (u *Unpacker) RemoveDevice(deviceId string) error {
   148  	return u.removeDevice(deviceId)
   149  }
   150  
   151  func (u *Unpacker) UnpackImage(streamName string, imageLeafName string) error {
   152  	return u.unpackImage(streamName, imageLeafName)
   153  }
   154  
   155  func (u *Unpacker) WriteHtml(writer io.Writer) {
   156  	u.writeHtml(writer)
   157  }
   158  
   159  func (u *Unpacker) WriteStreamHtml(writer io.Writer, streamName string) {
   160  	u.writeStreamHtml(writer, streamName)
   161  }