This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
computesservice.go
145 lines (119 loc) · 3.31 KB
/
computesservice.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package static
import (
"bytes"
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/core/infra"
"github.com/caos/orbos/internal/operator/orbiter/kinds/providers/core"
"github.com/caos/orbos/mntr"
)
type machinesService struct {
monitor mntr.Monitor
desired *DesiredV0
bootstrapKey []byte
maintenanceKey []byte
maintenanceKeyPub []byte
statusFile string
desireHostname func(machine infra.Machine, pool string) error
cache map[string]cachedMachines
}
// TODO: Dont accept the whole spec. Accept exactly the values needed (check other constructors too)
func NewMachinesService(
monitor mntr.Monitor,
desired *DesiredV0,
bootstrapKey []byte,
maintenanceKey []byte,
maintenanceKeyPub []byte,
id string,
desireHostname func(machine infra.Machine, pool string) error) core.MachinesService {
return &machinesService{
monitor,
desired,
bootstrapKey,
maintenanceKey,
maintenanceKeyPub,
filepath.Join("/var/orbiter", id),
desireHostname,
nil,
}
}
func (c *machinesService) ListPools() ([]string, error) {
pools := make([]string, 0)
for key := range c.desired.Spec.Pools {
pools = append(pools, key)
}
return pools, nil
}
func (c *machinesService) List(poolName string, active bool) (infra.Machines, error) {
pool, err := c.cachedPool(poolName)
if err != nil {
return nil, err
}
return pool.Machines(active), nil
}
func (c *machinesService) Create(poolName string) (infra.Machine, error) {
pool, err := c.cachedPool(poolName)
if err != nil {
return nil, err
}
for _, machine := range pool {
if len(c.maintenanceKeyPub) == 0 {
panic("no maintenance key")
}
if err := machine.WriteFile(c.desired.Spec.RemotePublicKeyPath, bytes.NewReader(c.maintenanceKeyPub), 600); err != nil {
return nil, err
}
if !machine.active {
if err := machine.WriteFile(c.statusFile, strings.NewReader("active"), 600); err != nil {
return nil, err
}
if err := c.desireHostname(machine, poolName); err != nil {
return nil, err
}
machine.active = true
return machine, nil
}
}
return nil, errors.New("no machines left")
}
func (c *machinesService) cachedPool(poolName string) (cachedMachines, error) {
specifiedMachines, ok := c.desired.Spec.Pools[poolName]
if !ok {
return nil, fmt.Errorf("pool %s does not exist", poolName)
}
cache, ok := c.cache[poolName]
if ok {
return cache, nil
}
newCache := make([]*machine, 0)
for _, spec := range specifiedMachines {
machine := newMachine(c.monitor, c.statusFile, c.desired.Spec.RemoteUser, &spec.ID, string(spec.IP))
if err := machine.UseKey(c.maintenanceKey, c.bootstrapKey); err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if err := machine.ReadFile(c.statusFile, buf); err != nil {
// treat as inactive
}
machine.active = strings.Contains(buf.String(), "active")
buf.Reset()
newCache = append(newCache, machine)
}
if c.cache == nil {
c.cache = make(map[string]cachedMachines)
}
c.cache[poolName] = newCache
return newCache, nil
}
type cachedMachines []*machine
func (c cachedMachines) Machines(activeOnly bool) infra.Machines {
machines := make([]infra.Machine, 0)
for _, machine := range c {
if !activeOnly || machine.active {
machines = append(machines, machine)
}
}
return machines
}