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

     1  package rpcd
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  	"sync"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/hypervisor/manager"
     9  	"github.com/Cloud-Foundations/Dominator/lib/log"
    10  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    11  	proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor"
    12  )
    13  
    14  type DhcpServer interface {
    15  	AddLease(address proto.Address, hostname string) error
    16  	AddNetbootLease(address proto.Address, hostname string,
    17  		subnet *proto.Subnet) error
    18  	ClosePacketWatchChannel(channel <-chan proto.WatchDhcpResponse)
    19  	MakeAcknowledgmentChannel(ipAddr net.IP) <-chan struct{}
    20  	MakePacketWatchChannel() <-chan proto.WatchDhcpResponse
    21  	RemoveLease(ipAddr net.IP)
    22  }
    23  
    24  type ipv4Address [4]byte
    25  
    26  type srpcType struct {
    27  	dhcpServer           DhcpServer
    28  	logger               log.DebugLogger
    29  	manager              *manager.Manager
    30  	tftpbootServer       TftpbootServer
    31  	mutex                sync.Mutex             // Protect everything below.
    32  	externalLeases       map[ipv4Address]string // Value: MAC address.
    33  	manageExternalLeases bool
    34  }
    35  
    36  type TftpbootServer interface {
    37  	RegisterFiles(ipAddr net.IP, files map[string][]byte)
    38  	UnregisterFiles(ipAddr net.IP)
    39  }
    40  
    41  type htmlWriter srpcType
    42  
    43  func (hw *htmlWriter) WriteHtml(writer io.Writer) {
    44  	hw.writeHtml(writer)
    45  }
    46  
    47  func Setup(manager *manager.Manager, dhcpServer DhcpServer,
    48  	tftpbootServer TftpbootServer, logger log.DebugLogger) (
    49  	*htmlWriter, error) {
    50  	srpcObj := &srpcType{
    51  		dhcpServer:     dhcpServer,
    52  		logger:         logger,
    53  		manager:        manager,
    54  		tftpbootServer: tftpbootServer,
    55  		externalLeases: make(map[ipv4Address]string),
    56  	}
    57  	srpc.SetDefaultGrantMethod(
    58  		func(_ string, authInfo *srpc.AuthInformation) bool {
    59  			return manager.CheckOwnership(authInfo)
    60  		})
    61  	srpc.RegisterNameWithOptions("Hypervisor", srpcObj, srpc.ReceiverOptions{
    62  		PublicMethods: []string{
    63  			"AcknowledgeVm",
    64  			"AddVmVolumes",
    65  			"BecomePrimaryVmOwner",
    66  			"ChangeVmConsoleType",
    67  			"ChangeVmDestroyProtection",
    68  			"ChangeVmOwnerUsers",
    69  			"ChangeVmSize",
    70  			"ChangeVmTags",
    71  			"ChangeVmVolumeSize",
    72  			"CommitImportedVm",
    73  			"ConnectToVmConsole",
    74  			"ConnectToVmSerialPort",
    75  			"CopyVm",
    76  			"CreateVm",
    77  			"DebugVmImage",
    78  			"DeleteVmVolume",
    79  			"DestroyVm",
    80  			"DiscardVmAccessToken",
    81  			"DiscardVmOldImage",
    82  			"DiscardVmOldUserData",
    83  			"DiscardVmSnapshot",
    84  			"ExportLocalVm",
    85  			"GetCapacity",
    86  			"GetRootCookiePath",
    87  			"GetUpdates",
    88  			"GetVmAccessToken",
    89  			"GetVmInfo",
    90  			"GetVmLastPatchLog",
    91  			"GetVmUserData",
    92  			"GetVmVolume",
    93  			"ImportLocalVm",
    94  			"ListSubnets",
    95  			"ListVMs",
    96  			"ListVolumeDirectories",
    97  			"MigrateVm",
    98  			"PatchVmImage",
    99  			"ProbeVmPort",
   100  			"RebootVm",
   101  			"ReplaceVmCredentials",
   102  			"ReplaceVmImage",
   103  			"ReplaceVmUserData",
   104  			"RestoreVmFromSnapshot",
   105  			"RestoreVmImage",
   106  			"RestoreVmUserData",
   107  			"ReorderVmVolumes",
   108  			"ScanVmRoot",
   109  			"SnapshotVm",
   110  			"StartVm",
   111  			"StopVm",
   112  			"TraceVmMetadata",
   113  		}})
   114  	return (*htmlWriter)(srpcObj), nil
   115  }