github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/dom/herd/api.go (about)

     1  package herd
     2  
     3  import (
     4  	"io"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/dom/images"
     9  	"github.com/Cloud-Foundations/Dominator/lib/cpusharer"
    10  	filegenclient "github.com/Cloud-Foundations/Dominator/lib/filegen/client"
    11  	"github.com/Cloud-Foundations/Dominator/lib/filesystem"
    12  	"github.com/Cloud-Foundations/Dominator/lib/image"
    13  	"github.com/Cloud-Foundations/Dominator/lib/log"
    14  	"github.com/Cloud-Foundations/Dominator/lib/mdb"
    15  	"github.com/Cloud-Foundations/Dominator/lib/net"
    16  	"github.com/Cloud-Foundations/Dominator/lib/objectcache"
    17  	"github.com/Cloud-Foundations/Dominator/lib/objectserver"
    18  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    19  	filegenproto "github.com/Cloud-Foundations/Dominator/proto/filegenerator"
    20  	subproto "github.com/Cloud-Foundations/Dominator/proto/sub"
    21  	"github.com/Cloud-Foundations/tricorder/go/tricorder"
    22  )
    23  
    24  type subStatus uint
    25  
    26  func (status subStatus) String() string {
    27  	return status.string()
    28  }
    29  
    30  const (
    31  	statusUnknown = iota
    32  	statusConnecting
    33  	statusDNSError
    34  	statusConnectionRefused
    35  	statusNoRouteToHost
    36  	statusConnectTimeout
    37  	statusMissingCertificate
    38  	statusBadCertificate
    39  	statusFailedToConnect
    40  	statusWaitingToPoll
    41  	statusPolling
    42  	statusPollDenied
    43  	statusFailedToPoll
    44  	statusSubNotReady
    45  	statusImageUndefined
    46  	statusImageNotReady
    47  	statusNotEnoughFreeSpace
    48  	statusFetching
    49  	statusFetchDenied
    50  	statusFailedToFetch
    51  	statusPushing
    52  	statusPushDenied
    53  	statusFailedToPush
    54  	statusFailedToGetObject
    55  	statusComputingUpdate
    56  	statusSendingUpdate
    57  	statusMissingComputedFile
    58  	statusUpdatesDisabled
    59  	statusUnsafeUpdate
    60  	statusUpdating
    61  	statusUpdateDenied
    62  	statusFailedToUpdate
    63  	statusWaitingForNextFullPoll
    64  	statusSynced
    65  )
    66  
    67  type HtmlWriter interface {
    68  	WriteHtml(writer io.Writer)
    69  }
    70  
    71  type Sub struct {
    72  	herd                         *Herd
    73  	mdb                          mdb.Machine
    74  	requiredImageName            string       // Updated only by sub goroutine.
    75  	requiredImage                *image.Image // Updated only by sub goroutine.
    76  	plannedImageName             string       // Updated only by sub goroutine.
    77  	plannedImage                 *image.Image // Updated only by sub goroutine.
    78  	clientResource               *srpc.ClientResource
    79  	computedInodes               map[string]*filesystem.RegularInode
    80  	fileUpdateChannel            <-chan []filegenproto.FileInfo
    81  	busyFlagMutex                sync.Mutex
    82  	busy                         bool
    83  	deletingFlagMutex            sync.Mutex
    84  	deleting                     bool
    85  	busyStartTime                time.Time
    86  	busyStopTime                 time.Time
    87  	cancelChannel                chan struct{}
    88  	havePlannedImage             bool
    89  	startTime                    time.Time
    90  	pollTime                     time.Time
    91  	fileSystem                   *filesystem.FileSystem
    92  	objectCache                  objectcache.ObjectCache
    93  	generationCount              uint64
    94  	freeSpaceThreshold           *uint64
    95  	computedFilesChangeTime      time.Time
    96  	scanCountAtLastUpdateEnd     uint64
    97  	isInsecure                   bool
    98  	status                       subStatus
    99  	publishedStatus              subStatus
   100  	pendingSafetyClear           bool
   101  	lastConnectionStartTime      time.Time
   102  	lastReachableTime            time.Time
   103  	lastConnectionSucceededTime  time.Time
   104  	lastConnectDuration          time.Duration
   105  	lastPollStartTime            time.Time
   106  	lastPollSucceededTime        time.Time
   107  	lastShortPollDuration        time.Duration
   108  	lastFullPollDuration         time.Duration
   109  	lastPollWasFull              bool
   110  	lastScanDuration             time.Duration
   111  	lastComputeUpdateCpuDuration time.Duration
   112  	lastUpdateTime               time.Time
   113  	lastSyncTime                 time.Time
   114  	lastSuccessfulImageName      string
   115  }
   116  
   117  func (sub *Sub) String() string {
   118  	return sub.string()
   119  }
   120  
   121  type Herd struct {
   122  	sync.RWMutex          // Protect map and slice mutations.
   123  	imageManager          *images.Manager
   124  	objectServer          objectserver.ObjectServer
   125  	computedFilesManager  *filegenclient.Manager
   126  	logger                log.DebugLogger
   127  	htmlWriters           []HtmlWriter
   128  	updatesDisabledReason string
   129  	updatesDisabledBy     string
   130  	updatesDisabledTime   time.Time
   131  	defaultImageName      string
   132  	nextDefaultImageName  string
   133  	configurationForSubs  subproto.Configuration
   134  	nextSubToPoll         uint
   135  	subsByName            map[string]*Sub
   136  	subsByIndex           []*Sub // Sorted by Sub.hostname.
   137  	pollSemaphore         chan struct{}
   138  	pushSemaphore         chan struct{}
   139  	cpuSharer             *cpusharer.FifoCpuSharer
   140  	dialer                net.Dialer
   141  	currentScanStartTime  time.Time
   142  	previousScanDuration  time.Duration
   143  }
   144  
   145  func NewHerd(imageServerAddress string, objectServer objectserver.ObjectServer,
   146  	metricsDir *tricorder.DirectorySpec, logger log.DebugLogger) *Herd {
   147  	return newHerd(imageServerAddress, objectServer, metricsDir, logger)
   148  }
   149  
   150  func (herd *Herd) AddHtmlWriter(htmlWriter HtmlWriter) {
   151  	herd.addHtmlWriter(htmlWriter)
   152  }
   153  
   154  func (herd *Herd) ClearSafetyShutoff(hostname string) error {
   155  	return herd.clearSafetyShutoff(hostname)
   156  }
   157  
   158  func (herd *Herd) ConfigureSubs(configuration subproto.Configuration) error {
   159  	return herd.configureSubs(configuration)
   160  }
   161  
   162  func (herd *Herd) DisableUpdates(username, reason string) error {
   163  	return herd.disableUpdates(username, reason)
   164  }
   165  
   166  func (herd *Herd) EnableUpdates() error {
   167  	return herd.enableUpdates()
   168  }
   169  
   170  func (herd *Herd) GetDefaultImage() string {
   171  	return herd.defaultImageName
   172  }
   173  
   174  func (herd *Herd) GetSubsConfiguration() subproto.Configuration {
   175  	return herd.getSubsConfiguration()
   176  }
   177  
   178  func (herd *Herd) LockWithTimeout(timeout time.Duration) {
   179  	herd.lockWithTimeout(timeout)
   180  }
   181  
   182  func (herd *Herd) MdbUpdate(mdb *mdb.Mdb) {
   183  	herd.mdbUpdate(mdb)
   184  }
   185  
   186  func (herd *Herd) PollNextSub() bool {
   187  	return herd.pollNextSub()
   188  }
   189  
   190  func (herd *Herd) RLockWithTimeout(timeout time.Duration) {
   191  	herd.rLockWithTimeout(timeout)
   192  }
   193  
   194  func (herd *Herd) SetDefaultImage(imageName string) error {
   195  	return herd.setDefaultImage(imageName)
   196  }
   197  
   198  func (herd *Herd) StartServer(portNum uint, daemon bool) error {
   199  	return herd.startServer(portNum, daemon)
   200  }