~egtann/binp

abffd380443c3d6c445e22a3816dc1ba770ce3ac — Evan Tann 5 months ago ddcce4d
rename network to net in config files
3 files changed, 25 insertions(+), 25 deletions(-)

M README.md
M binp.go
M cmd/binp/main.go
M README.md => README.md +7 -7
@@ 35,7 35,7 @@ For example, this could be our services.json file:
				"ram": "4 GB",
				"disk": "100 MB",
				"count": 4,
				"network": {
				"net": {
					"mbps": 500,
					"ports": [3000, 3001]
				}


@@ 45,7 45,7 @@ For example, this could be our services.json file:
				"ram": "2 GB",
				"disk": "50 MB",
				"count": 2,
				"network": {
				"net": {
					"mbps": 100,
					"ports": [3002]
				}


@@ 55,7 55,7 @@ For example, this could be our services.json file:
				"ram": "32 GB",
				"disk": "1 TB",
				"count": 2,
				"network": {
				"net": {
					"ports": [3002]
				}
			}


@@ 74,13 74,13 @@ For example, this could be our services.json file:
			"cpu": 12,
			"ram": "32 GB",
			"disk": "1 TB",
			"network": {"mbps": 1000}
			"net": {"mbps": 1000}
		},
		"debian": {
			"cpu": 1,
			"ram": "4 GB",
			"disk": "10 GB",
			"network": {"mbps": 1000}
			"net": {"mbps": 1000}
		}
	}
}


@@ 132,12 132,12 @@ openbsd:
	CPU:  6
	RAM:  32 GB
	Disk: 1 TB
	Network: 600 mbps
	Net:  600 mbps
debian:
	CPU:  1
	RAM:  2 GB
	Disk: 1 TB
	Network: 0 mbps
	Net:  0 mbps
```

Or the smallest CPU/RAM/Disk which will allow co-locating all services on a

M binp.go => binp.go +16 -16
@@ 19,12 19,12 @@ type Config struct {
}

type Service struct {
	CPU     int         `json:"cpu"`
	RAM     datasize    `json:"ram"`
	Disk    datasize    `json:"disk"`
	Network network     `json:"network"`
	Count   int         `json:"count"`
	Name    ServiceName `json:"-"`
	CPU   int         `json:"cpu"`
	RAM   datasize    `json:"ram"`
	Disk  datasize    `json:"disk"`
	Net   network     `json:"net"`
	Count int         `json:"count"`
	Name  ServiceName `json:"-"`
}

type network struct {


@@ 41,7 41,7 @@ type Box struct {
	CPU        int                      `json:"cpu"`
	RAM        datasize                 `json:"ram"`
	Disk       datasize                 `json:"disk"`
	Network    network                  `json:"network"`
	Net        network                  `json:"net"`
	Services   []*Service               `json:"-"`
	serviceMap map[ServiceName]struct{} `json:"-"`
	usedPorts  map[int]struct{}         `json:"-"`


@@ 100,7 100,7 @@ func (c *Config) packBox(
			CPU:        b.CPU,
			RAM:        b.RAM,
			Disk:       b.Disk,
			Network:    b.Network,
			Net:        b.Net,
			serviceMap: map[ServiceName]struct{}{},
			usedPorts:  map[int]struct{}{},
		}


@@ 164,7 164,7 @@ func (s *Service) score() int {
	if ram == 0 {
		ram = 1
	}
	mbps := s.Network.Mbps
	mbps := s.Net.Mbps
	if mbps == 0 {
		mbps = 1
	}


@@ 185,10 185,10 @@ func (s *Service) fitIn(b *Box) bool {
	if s.Disk > b.Disk {
		return false
	}
	if s.Network.Mbps > b.Network.Mbps {
	if s.Net.Mbps > b.Net.Mbps {
		return false
	}
	for _, p := range s.Network.Ports {
	for _, p := range s.Net.Ports {
		if _, exist := b.usedPorts[p]; exist {
			return false
		}


@@ 203,10 203,10 @@ func addService(box *Box, srv *Service) *Box {
	box.CPU -= srv.CPU
	box.RAM -= srv.RAM
	box.Disk -= srv.Disk
	box.Network.Mbps -= srv.Network.Mbps
	box.Net.Mbps -= srv.Net.Mbps
	box.Services = append(box.Services, srv)
	box.serviceMap[srv.Name] = struct{}{}
	for _, p := range srv.Network.Ports {
	for _, p := range srv.Net.Ports {
		box.usedPorts[p] = struct{}{}
	}
	return box


@@ 224,8 224,8 @@ func MinBox(boxes []*Service) *Box {
		if box.Disk > minBox.Disk {
			minBox.Disk = box.Disk
		}
		if box.Network.Mbps > minBox.Network.Mbps {
			minBox.Network.Mbps = box.Network.Mbps
		if box.Net.Mbps > minBox.Net.Mbps {
			minBox.Net.Mbps = box.Net.Mbps
		}
	}
	return minBox


@@ 237,7 237,7 @@ func MaxBox(boxes []*Service) *Box {
		maxBox.CPU += box.CPU
		maxBox.RAM += box.RAM
		maxBox.Disk += box.Disk
		maxBox.Network.Mbps += box.Network.Mbps
		maxBox.Net.Mbps += box.Net.Mbps
	}
	return maxBox
}

M cmd/binp/main.go => cmd/binp/main.go +2 -2
@@ 63,7 63,7 @@ func outputMinimumResources(
	fmt.Println("\tCPU: ", minBox.CPU)
	fmt.Println("\tRAM: ", minBox.RAM)
	fmt.Println("\tDisk:", minBox.Disk)
	fmt.Printf("\tNetwork: %d mbps\n", minBox.Network.Mbps)
	fmt.Printf("\tNet:  %d mbps\n", minBox.Net.Mbps)
}

func outputMaximumResources(


@@ 79,7 79,7 @@ func outputMaximumResources(
	fmt.Println("\tCPU: ", maxBox.CPU)
	fmt.Println("\tRAM: ", maxBox.RAM)
	fmt.Println("\tDisk:", maxBox.Disk)
	fmt.Printf("\tNetwork: %d mbps\n", maxBox.Network.Mbps)
	fmt.Printf("\tNet:  %d mbps\n", maxBox.Net.Mbps)
}

func outputBoxes(boxesByName map[binp.BoxName][]*binp.Box) error {