This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathfactory.go
67 lines (60 loc) · 1.85 KB
/
factory.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
// package factory defines the full function factory interface
// package base defines the base factory interface
// package cache direct and template implement base.Factory
// package single and multi implement fatory.Factory
package factory
import (
"encoding/json"
"path/filepath"
"github.com/golang/glog"
"github.com/hyperhq/runv/factory/base"
"github.com/hyperhq/runv/factory/cache"
"github.com/hyperhq/runv/factory/direct"
"github.com/hyperhq/runv/factory/multi"
"github.com/hyperhq/runv/factory/single"
"github.com/hyperhq/runv/factory/template"
"github.com/hyperhq/runv/hypervisor"
)
type Factory interface {
GetVm(cpu, mem int) (*hypervisor.Vm, error)
CloseFactory()
}
type FactoryConfig struct {
Cache int `json:"cache"`
Template bool `json:"template"`
Cpu int `json:"cpu"`
Memory int `json:"memory"`
}
func NewFromConfigs(bootConfig hypervisor.BootConfig, configs []FactoryConfig) Factory {
bases := make([]base.Factory, len(configs))
for i, c := range configs {
var b base.Factory
boot := bootConfig
boot.CPU = c.Cpu
boot.Memory = c.Memory
if c.Template {
b = template.New(filepath.Join(hypervisor.BaseDir, "template"), boot)
} else {
b = direct.New(boot)
}
bases[i] = cache.New(c.Cache, b)
}
if len(bases) == 0 {
return single.Dummy(bootConfig)
} else if len(bases) == 1 {
return single.New(bases[0])
} else {
return multi.New(bases)
}
}
// vmFactoryPolicy = [FactoryConfig,]*FactoryConfig
// FactoryConfig = {["cache":NUMBER,]["template":true|false,]"cpu":NUMBER,"memory":NUMBER}
func NewFromPolicy(bootConfig hypervisor.BootConfig, policy string) Factory {
var configs []FactoryConfig
jsonString := "[" + policy + "]"
err := json.Unmarshal([]byte(jsonString), &configs)
if err != nil && policy != "none" {
glog.Errorf("Incorrect policy: %s", policy)
}
return NewFromConfigs(bootConfig, configs)
}