-
Notifications
You must be signed in to change notification settings - Fork 14
/
resources.go
92 lines (78 loc) · 2.65 KB
/
resources.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package platform
import (
"errors"
"fmt"
"log"
"math"
"net"
"net/url"
"strings"
"github.com/bravetools/bravetools/shared"
lxd "github.com/lxc/lxd/client"
)
// CheckMemory checks if the LXD server host has sufficient RAM to deploy requested unit
func CheckMemory(lxdServer lxd.InstanceServer, ramString string) error {
// If no ram limit requested, nothing to do
if ramString == "" {
return nil
}
resources, err := lxdServer.GetServerResources()
if err != nil {
//return errors.New(" " + err.Error())
log.Printf("failed to retrieve LXD server resources: %s. However, Bravetools will continue to deploy. You can interrupt this process by pressing Cntrl+C", err.Error())
} else {
requestedMemorySize, err := shared.SizeCountToInt(ramString)
if err != nil {
return err
}
freeMemorySize := resources.Memory.Total - resources.Memory.Used
if uint64(requestedMemorySize) > freeMemorySize {
return errors.New("requested unit memory (" + ramString + ") exceeds available memory on bravetools host")
}
}
return nil
}
// CheckHostPorts ensures required forwarded ports are free by attempting to connect.
// If a connection is established the port is already taken
func CheckHostPorts(hostURL string, forwardedPorts []string) (err error) {
parsedURL, err := url.Parse(hostURL)
if err != nil {
return fmt.Errorf("failed to parse host URL %q: %s", hostURL, err)
}
host, _, err := net.SplitHostPort(parsedURL.Host)
if err != nil {
return fmt.Errorf("failed to parse host URL %q: %s", hostURL, err)
}
// Networking Checks
var hostPorts []string
if len(forwardedPorts) > 0 {
for _, p := range forwardedPorts {
ps := strings.Split(p, ":")
if len(ps) < 2 {
return errors.New("invalid port forwarding definition. Appropriate format is UNIT_PORT:HOST_PORT")
}
hostPorts = append(hostPorts, ps[1])
}
}
err = shared.TCPPortStatus(host, hostPorts)
if err != nil {
return err
}
return nil
}
func CheckStoragePoolSpace(lxdServer lxd.InstanceServer, storagePool string, requestedSpace int64) (err error) {
res, err := lxdServer.GetStoragePoolResources(storagePool)
if err != nil {
return fmt.Errorf("failed to retrieve storage information for storage pool %q from lxd server", storagePool)
}
freeSpace := res.Space.Total - res.Space.Used
if freeSpace <= uint64(requestedSpace) {
// Potential uint64 -> int64 int overflow - just setting to max for now
if freeSpace > math.MaxInt64 {
freeSpace = math.MaxInt64
}
return fmt.Errorf("requested size exceeds available space on storage pool - %q requested but %q available",
shared.FormatByteCountSI(requestedSpace), shared.FormatByteCountSI(int64(freeSpace)))
}
return nil
}