-
Notifications
You must be signed in to change notification settings - Fork 0
/
constraints.go
80 lines (67 loc) · 1.73 KB
/
constraints.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package cloudsigma
import (
"fmt"
"github.com/altoros/gosigma"
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs/imagemetadata"
)
type sigmaConstraints struct {
driveTemplate string
driveSize uint64
cores uint64 //number of cpu cores
power uint64 //cpu power in MHz
mem uint64 //memory size in GB
}
const (
defaultCPUPower = 2000
defaultDriveGB = 5
defaultMemoryGB = 2
)
// newConstraints creates new CloudSigma constraints from juju common constraints
func newConstraints(bootstrap bool, jc constraints.Value, img *imagemetadata.ImageMetadata) *sigmaConstraints {
var sc = sigmaConstraints{
driveTemplate: img.Id,
}
if size := jc.RootDisk; bootstrap && size == nil {
sc.driveSize = defaultDriveGB * gosigma.Gigabyte
} else if size != nil {
sc.driveSize = *size * gosigma.Megabyte
}
if c := jc.CpuCores; c != nil {
sc.cores = *c
} else {
sc.cores = 1
}
if p := jc.CpuPower; p != nil {
sc.power = *p
} else {
if sc.cores == 1 {
// The default of cpu power is 2000 Mhz
sc.power = defaultCPUPower
} else {
// The maximum amount of cpu per smp is 2300
sc.power = sc.cores * defaultCPUPower
}
}
if m := jc.Mem; m != nil {
sc.mem = *m * gosigma.Megabyte
} else {
sc.mem = defaultMemoryGB * gosigma.Gigabyte
}
return &sc
}
func (c *sigmaConstraints) String() string {
s := fmt.Sprintf("template=%s,drive=%dG", c.driveTemplate, c.driveSize/gosigma.Gigabyte)
if c.cores > 0 {
s += fmt.Sprintf(",cores=%d", c.cores)
}
if c.power > 0 {
s += fmt.Sprintf(",power=%d", c.power)
}
if c.mem > 0 {
s += fmt.Sprintf(",mem=%dG", c.mem/gosigma.Gigabyte)
}
return s
}