github.com/Cloud-Foundations/Dominator@v0.3.4/fleetmanager/hypervisors/api.go (about)

     1  package hypervisors
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  	"sync"
     7  	"time"
     8  
     9  	"github.com/Cloud-Foundations/Dominator/fleetmanager/topology"
    10  	"github.com/Cloud-Foundations/Dominator/lib/log"
    11  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    12  	"github.com/Cloud-Foundations/Dominator/lib/tags"
    13  	fm_proto "github.com/Cloud-Foundations/Dominator/proto/fleetmanager"
    14  	hyper_proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor"
    15  )
    16  
    17  const (
    18  	probeStatusNotYetProbed probeStatus = iota
    19  	probeStatusConnected
    20  	probeStatusAccessDenied
    21  	probeStatusNoSrpc
    22  	probeStatusNoService
    23  	probeStatusConnectionRefused
    24  	probeStatusUnreachable
    25  	probeStatusOff
    26  )
    27  
    28  type hypervisorType struct {
    29  	logger               log.DebugLogger
    30  	receiveChannel       chan struct{}
    31  	mutex                sync.RWMutex // Lock everything below.
    32  	allocatedMilliCPUs   uint64
    33  	allocatedMemory      uint64
    34  	allocatedVolumeBytes uint64
    35  	cachedSerialNumber   string
    36  	closeClientChannel   chan<- struct{}
    37  	deleteScheduled      bool
    38  	disabled             bool
    39  	healthStatus         string
    40  	lastIpmiProbe        time.Time
    41  	localTags            tags.Tags
    42  	location             string
    43  	machine              *fm_proto.Machine
    44  	memoryInMiB          uint64
    45  	migratingVms         map[string]*vmInfoType // Key: VM IP address.
    46  	numCPUs              uint
    47  	ownerUsers           map[string]struct{}
    48  	probeStatus          probeStatus
    49  	serialNumber         string
    50  	subnets              []hyper_proto.Subnet
    51  	totalVolumeBytes     uint64
    52  	vms                  map[string]*vmInfoType // Key: VM IP address.
    53  }
    54  
    55  type ipStorer interface {
    56  	AddIPsForHypervisor(hypervisor net.IP, addrs []net.IP) error
    57  	CheckIpIsRegistered(addr net.IP) (bool, error)
    58  	GetHypervisorForIp(addr net.IP) (net.IP, error)
    59  	GetIPsForHypervisor(hypervisor net.IP) ([]net.IP, error)
    60  	SetIPsForHypervisor(hypervisor net.IP, addrs []net.IP) error
    61  	UnregisterHypervisor(hypervisor net.IP) error
    62  }
    63  
    64  type locationType struct {
    65  	notifiers map[<-chan fm_proto.Update]chan<- fm_proto.Update
    66  }
    67  
    68  type Manager struct {
    69  	ipmiPasswordFile string
    70  	ipmiUsername     string
    71  	logger           log.DebugLogger
    72  	storer           Storer
    73  	mutex            sync.RWMutex               // Protect everything below.
    74  	allocatingIPs    map[string]struct{}        // Key: VM IP address.
    75  	hypervisors      map[string]*hypervisorType // Key: hypervisor machine name.
    76  	locations        map[string]*locationType   // Key: location.
    77  	migratingIPs     map[string]struct{}        // Key: VM IP address.
    78  	notifiers        map[<-chan fm_proto.Update]*locationType
    79  	topology         *topology.Topology
    80  	subnets          map[string]*subnetType // Key: Gateway IP.
    81  	vms              map[string]*vmInfoType // Key: VM IP address.
    82  }
    83  
    84  type probeStatus uint
    85  
    86  type serialStorer interface {
    87  	ReadMachineSerialNumber(hypervisor net.IP) (string, error)
    88  	WriteMachineSerialNumber(hypervisor net.IP, serialNumber string) error
    89  }
    90  
    91  type StartOptions struct {
    92  	IpmiPasswordFile string
    93  	IpmiUsername     string
    94  	Logger           log.DebugLogger
    95  	Storer           Storer
    96  }
    97  
    98  type Storer interface {
    99  	ipStorer
   100  	serialStorer
   101  	tagsStorer
   102  	vmStorer
   103  }
   104  
   105  type subnetType struct {
   106  	subnet  *topology.Subnet
   107  	startIp net.IP
   108  	stopIp  net.IP
   109  	nextIp  net.IP
   110  }
   111  
   112  type tagsStorer interface {
   113  	ReadMachineTags(hypervisor net.IP) (tags.Tags, error)
   114  	WriteMachineTags(hypervisor net.IP, tgs tags.Tags) error
   115  }
   116  
   117  type vmInfoType struct {
   118  	ipAddr string
   119  	hyper_proto.VmInfo
   120  	Location   string // Used by dashboards.
   121  	hypervisor *hypervisorType
   122  }
   123  
   124  type vmStorer interface {
   125  	DeleteVm(hypervisor net.IP, ipAddr string) error
   126  	ListVMs(hypervisor net.IP) ([]string, error)
   127  	ReadVm(hypervisor net.IP, ipAddr string) (*hyper_proto.VmInfo, error)
   128  	WriteVm(hypervisor net.IP, ipAddr string, vmInfo hyper_proto.VmInfo) error
   129  }
   130  
   131  func New(startOptions StartOptions) (*Manager, error) {
   132  	return newManager(startOptions)
   133  }
   134  
   135  func (m *Manager) ChangeMachineTags(hostname string,
   136  	authInfo *srpc.AuthInformation, tgs tags.Tags) error {
   137  	return m.changeMachineTags(hostname, authInfo, tgs)
   138  }
   139  
   140  func (m *Manager) CloseUpdateChannel(channel <-chan fm_proto.Update) {
   141  	m.closeUpdateChannel(channel)
   142  }
   143  
   144  func (m *Manager) GetHypervisorForVm(ipAddr net.IP) (string, error) {
   145  	return m.getHypervisorForVm(ipAddr)
   146  }
   147  
   148  func (m *Manager) GetHypervisorsInLocation(
   149  	request fm_proto.GetHypervisorsInLocationRequest) (
   150  	fm_proto.GetHypervisorsInLocationResponse, error) {
   151  	return m.getHypervisorsInLocation(request)
   152  }
   153  
   154  func (m *Manager) GetMachineInfo(request fm_proto.GetMachineInfoRequest) (
   155  	fm_proto.Machine, error) {
   156  	return m.getMachineInfo(request)
   157  }
   158  
   159  func (m *Manager) GetTopology() (*topology.Topology, error) {
   160  	return m.getTopology()
   161  }
   162  
   163  func (m *Manager) ListHypervisorsInLocation(
   164  	request fm_proto.ListHypervisorsInLocationRequest) (
   165  	fm_proto.ListHypervisorsInLocationResponse, error) {
   166  	return m.listHypervisorsInLocation(request)
   167  }
   168  
   169  func (m *Manager) ListLocations(dirname string) ([]string, error) {
   170  	return m.listLocations(dirname)
   171  }
   172  
   173  func (m *Manager) ListVMsInLocation(request fm_proto.ListVMsInLocationRequest) (
   174  	[]net.IP, error) {
   175  	return m.listVMsInLocation(request)
   176  }
   177  
   178  func (m *Manager) MakeUpdateChannel(
   179  	request fm_proto.GetUpdatesRequest) <-chan fm_proto.Update {
   180  	return m.makeUpdateChannel(request)
   181  }
   182  
   183  func (m *Manager) MoveIpAddresses(hostname string, ipAddresses []net.IP) error {
   184  	return m.moveIpAddresses(hostname, ipAddresses)
   185  }
   186  
   187  func (m *Manager) PowerOnMachine(hostname string,
   188  	authInfo *srpc.AuthInformation) error {
   189  	return m.powerOnMachine(hostname, authInfo)
   190  }
   191  
   192  func (m *Manager) WriteHtml(writer io.Writer) {
   193  	m.writeHtml(writer)
   194  }
   195  
   196  func (m *Manager) UpdateTopology(t *topology.Topology) {
   197  	m.updateTopology(t)
   198  }