Skip to content

Commit

Permalink
Merge pull request #79 from rgooch/master
Browse files Browse the repository at this point in the history
Sync-up batch
  • Loading branch information
rgooch authored Apr 29, 2024
2 parents 30f0d23 + 7d8764a commit 668455d
Show file tree
Hide file tree
Showing 37 changed files with 511 additions and 106 deletions.
2 changes: 2 additions & 0 deletions cmd/hyper-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ Some of the sub-commands available are:
specified *Hypervisor*
- **update-network-configuration**: update the network configuration for the
local *Hypervisor*
- **watch-dhcp**: watch for DHCP messages received by the specified *Hypervisor*
and log and write packet data. This is primarily for debugging
- **write-netboot-files**: write the configuration files for installing a
machine. This is primarily for debugging

Expand Down
1 change: 1 addition & 0 deletions cmd/hyper-control/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ var subcommands = []commands.Command{
showNetworkConfigurationSubcommand},
{"update-network-configuration", "", 0, 0,
updateNetworkConfigurationSubcommand},
{"watch-dhcp", "[interface]", 0, 1, watchDhcpSubcommand},
{"write-netboot-files", "hostname dirname", 2, 2,
writeNetbootFilesSubcommand},
}
Expand Down
90 changes: 90 additions & 0 deletions cmd/hyper-control/watchDhcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"fmt"
"os"
"path/filepath"

"github.com/Cloud-Foundations/Dominator/lib/errors"
"github.com/Cloud-Foundations/Dominator/lib/fsutil"
"github.com/Cloud-Foundations/Dominator/lib/log"
"github.com/Cloud-Foundations/Dominator/lib/srpc"
proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor"
dhcp "github.com/krolaw/dhcp4"
)

func watchDhcpSubcommand(args []string, logger log.DebugLogger) error {
var interfaceName string
if len(args) > 0 {
interfaceName = args[0]
}
if err := watchDhcp(interfaceName, logger); err != nil {
return fmt.Errorf("error watching DHCP: %s", err)
}
return nil
}

func watchDhcp(interfaceName string, logger log.DebugLogger) error {
if *hypervisorHostname == "" {
return errors.New("unspecified Hypervisor")
}
clientName := fmt.Sprintf("%s:%d", *hypervisorHostname, *hypervisorPortNum)
client, err := srpc.DialHTTP("tcp", clientName, 0)
if err != nil {
return err
}
defer client.Close()
conn, err := client.Call("Hypervisor.WatchDhcp")
if err != nil {
return err
}
defer conn.Close()
request := proto.WatchDhcpRequest{Interface: interfaceName}
if err := conn.Encode(request); err != nil {
return err
}
if err := conn.Flush(); err != nil {
return err
}
dirname, err := os.MkdirTemp("",
"hyper-control.watch-dhcp."+*hypervisorHostname+".")
if err != nil {
return err
}
logger.Printf("Results in directory: %s\n", dirname)
for counter := 0; true; counter++ {
var reply proto.WatchDhcpResponse
if err := conn.Decode(&reply); err != nil {
return err
}
if err := errors.New(reply.Error); err != nil {
return err
}
filename := fmt.Sprintf("%s.%.5d", reply.Interface, counter)
file, err := os.Create(filepath.Join(dirname, filename))
if err != nil {
return err
}
file.Write(reply.Packet)
file.Close()
packet := dhcp.Packet(reply.Packet)
options := packet.ParseOptions()
msgType := dhcp.MessageType(options[dhcp.OptionDHCPMessageType][0])
logger.Printf("Counter: %d, message type: %s, from: %s, options:\n",
counter, msgType, packet.CHAddr())
optionsDirname := filepath.Join(dirname, filename) + ".options"
os.Mkdir(optionsDirname, fsutil.DirPerms)
for code, value := range options {
logger.Printf(" Code: %s, value: %0x\n", code, value)
optionFilename := fmt.Sprintf("%s/%d_%s",
optionsDirname, code, code)
file, err := os.Create(optionFilename)
if err != nil {
return err
}
file.Write(value)
file.Close()
}
}
return nil
}
1 change: 1 addition & 0 deletions cmd/imagetool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Some of the sub-commands available are:
- **get-image-expiration**: get the expiration time for an image
- **get-image-updates**: get a stream of image updates
- **get-package-list**: get package list for an image
- **get-replication-master**: show the replication master for the imageserver
- **list**: list all images
- **listdirs**: list all directories
- **listunrefobj**: list the unreferenced objects on the server
Expand Down
2 changes: 1 addition & 1 deletion cmd/imagetool/deleteImage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func deleteImageSubcommand(args []string, logger log.DebugLogger) error {
imageSClient, _ := getClients()
imageSClient, _ := getMasterClients()
if err := client.DeleteImage(imageSClient, args[0]); err != nil {
return fmt.Errorf("error deleting image: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/imagetool/expiration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func changeImageExpirationSubcommand(args []string,
logger log.DebugLogger) error {
imageSClient, _ := getClients()
imageSClient, _ := getMasterClients()
err := changeImageExpiration(imageSClient, args[0])
if err != nil {
return fmt.Errorf("error changing image expiration: %s", err)
Expand Down
29 changes: 29 additions & 0 deletions cmd/imagetool/getReplicationMaster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"

"github.com/Cloud-Foundations/Dominator/imageserver/client"
"github.com/Cloud-Foundations/Dominator/lib/log"
"github.com/Cloud-Foundations/Dominator/lib/srpc"
)

func getReplicationMasterSubcommand(args []string,
logger log.DebugLogger) error {
imageSClient, _ := getClients()
if err := getReplicationMaster(imageSClient); err != nil {
return fmt.Errorf("error getting replication master: %s", err)
}
return nil
}

func getReplicationMaster(imageSClient *srpc.Client) error {
replicationMaster, err := client.GetReplicationMaster(imageSClient)
if err != nil {
return err
}
if len(replicationMaster) > 0 {
fmt.Println(replicationMaster)
}
return nil
}
38 changes: 29 additions & 9 deletions cmd/imagetool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ var (
"Port number of image server")
makeBootable = flag.Bool("makeBootable", true,
"If true, make raw image bootable by installing GRUB")
masterImageServerHostname = flag.String("masterImageServerHostname", "",
"Hostname of master image server (if different)")
mdbServerHostname = flag.String("mdbServerHostname", "localhost",
"Hostname of MDB server")
mdbServerPortNum = flag.Uint("mdbServerPortNum",
Expand Down Expand Up @@ -172,6 +174,7 @@ var subcommands = []commands.Command{
{"get-image-updates", "", 0, 0, getImageUpdatesSubcommand},
{"get-package-list", " name [outfile]", 1, 2,
getImagePackageListSubcommand},
{"get-replication-master", "", 0, 0, getReplicationMasterSubcommand},
{"list", "", 0, 0, listImagesSubcommand},
{"listdirs", "", 0, 0, listDirectoriesSubcommand},
{"listunrefobj", "", 0, 0, listUnreferencedObjectsSubcommand},
Expand Down Expand Up @@ -201,24 +204,41 @@ var subcommands = []commands.Command{
traceInodeHistorySubcommand},
}

var imageSrpcClient *srpc.Client
var theObjectClient *objectclient.ObjectClient
var (
imageSrpcClient *srpc.Client
masterImageSrpcClient *srpc.Client
theObjectClient *objectclient.ObjectClient
theMasterObjectClient *objectclient.ObjectClient

var listSelector filesystem.ListSelector
listSelector filesystem.ListSelector
)

func getClients() (*srpc.Client, *objectclient.ObjectClient) {
if imageSrpcClient == nil {
getPointedClients(*imageServerHostname, &imageSrpcClient, &theObjectClient)
return imageSrpcClient, theObjectClient
}

func getMasterClients() (*srpc.Client, *objectclient.ObjectClient) {
if *masterImageServerHostname == "" {
return getClients()
}
getPointedClients(*masterImageServerHostname,
&masterImageSrpcClient, &theMasterObjectClient)
return masterImageSrpcClient, theMasterObjectClient
}

func getPointedClients(hostname string, iClient **srpc.Client,
oClient **objectclient.ObjectClient) {
if *iClient == nil {
var err error
clientName := fmt.Sprintf("%s:%d",
*imageServerHostname, *imageServerPortNum)
imageSrpcClient, err = srpc.DialHTTP("tcp", clientName, 0)
clientName := fmt.Sprintf("%s:%d", hostname, *imageServerPortNum)
*iClient, err = srpc.DialHTTP("tcp", clientName, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "Error dialing: %s: %s\n", clientName, err)
os.Exit(1)
}
theObjectClient = objectclient.AttachObjectClient(imageSrpcClient)
*oClient = objectclient.AttachObjectClient(imageSrpcClient)
}
return imageSrpcClient, theObjectClient
}

func makeListSelector(arg string) filesystem.ListSelector {
Expand Down
2 changes: 1 addition & 1 deletion cmd/imagetool/makeDirectory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func makeDirectorySubcommand(args []string, logger log.DebugLogger) error {
imageSClient, _ := getClients()
imageSClient, _ := getMasterClients()
if err := client.MakeDirectory(imageSClient, args[0]); err != nil {
return fmt.Errorf("error creating directory: %s", err)
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/imaginator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ var (
"Minimum permitted expiration duration")
portNum = flag.Uint("portNum", constants.ImaginatorPortNumber,
"Port number to allocate and listen on for HTTP/RPC")
presentationImageServerHostname = flag.String(
"presentationImageServerHostname", "",
"Hostname of image server for links presentation")
slaveDriverConfigurationFile = flag.String("slaveDriverConfigurationFile",
"", "Name of configuration file for slave builders")
stateDir = flag.String("stateDir", "/var/lib/imaginator",
Expand Down Expand Up @@ -116,8 +119,10 @@ func main() {
MaximumExpirationDuration: *maximumExpirationDuration,
MaximumExpirationDurationPrivileged: *maximumExpirationDurationPrivileged,
MinimumExpirationDuration: *minimumExpirationDuration,
StateDirectory: *stateDir,
VariablesFile: *variablesFile,
PresentationImageServerAddress: fmt.Sprintf("%s:%d",
*presentationImageServerHostname, *imageServerPortNum),
StateDirectory: *stateDir,
VariablesFile: *variablesFile,
},
builder.BuilderParams{
BuildLogArchiver: buildLogArchiver,
Expand Down
16 changes: 13 additions & 3 deletions fleetmanager/hypervisors/listVMs.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ func numSpecifiedVirtualCPUs(milliCPUs, vCPUs uint) uint {
return nCpus
}

func safeDivide(numerator, denominator uint64) uint64 {
if denominator > 0 {
return numerator / denominator
}
return 0
}

func sumOwnerTotals(totalsByOwner map[string]*ownerTotalsType) ownerTotalsType {
var totals ownerTotalsType
for _, ownerTotals := range totalsByOwner {
Expand Down Expand Up @@ -221,12 +228,15 @@ func (m *Manager) listVMs(writer io.Writer, vms []*vmInfoType,
"",
"",
fmt.Sprintf("%d%%",
totals.MemoryInMiB*100/capacity.MemoryInMiB),
fmt.Sprintf("%d%%", totals.MilliCPUs/capacity.NumCPUs/10),
safeDivide(totals.MemoryInMiB*100, capacity.MemoryInMiB)),
fmt.Sprintf("%d%%",
safeDivide(uint64(totals.MilliCPUs),
uint64(capacity.NumCPUs)*10)),
"",
"",
fmt.Sprintf("%d%%",
totals.VolumeSize*100/capacity.TotalVolumeBytes),
safeDivide(totals.VolumeSize*100,
capacity.TotalVolumeBytes)),
"",
"",
"",
Expand Down
14 changes: 12 additions & 2 deletions hypervisor/dhcpd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ type DhcpServer struct {
dynamicLeases map[string]*leaseType // Key: MACaddr.
interfaceSubnets map[string][]*subnetType // Key: interface name.
ipAddrToMacAddr map[string]string // Key: IPaddr, V: MACaddr.
staticLeases map[string]leaseType // Key: MACaddr.
requestChannels map[string]chan net.IP // Key: MACaddr.
packetWatchers map[<-chan proto.WatchDhcpResponse]chan<- proto.WatchDhcpResponse
requestChannels map[string]chan net.IP // Key: MACaddr.
staticLeases map[string]leaseType // Key: MACaddr.
subnets []*subnetType
}

Expand Down Expand Up @@ -64,10 +65,19 @@ func (s *DhcpServer) AddSubnet(subnet proto.Subnet) {
s.addSubnet(subnet)
}

func (s *DhcpServer) ClosePacketWatchChannel(
channel <-chan proto.WatchDhcpResponse) {
s.closePacketWatchChannel(channel)
}

func (s *DhcpServer) MakeAcknowledgmentChannel(ipAddr net.IP) <-chan struct{} {
return s.makeAcknowledgmentChannel(ipAddr)
}

func (s *DhcpServer) MakePacketWatchChannel() <-chan proto.WatchDhcpResponse {
return s.makePacketWatchChannel()
}

func (s *DhcpServer) MakeRequestChannel(macAddr string) <-chan net.IP {
return s.makeRequestChannel(macAddr)
}
Expand Down
Loading

0 comments on commit 668455d

Please sign in to comment.