-
Notifications
You must be signed in to change notification settings - Fork 23
/
vm_agent.go
73 lines (56 loc) · 1.59 KB
/
vm_agent.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
package vm
import (
"encoding/json"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
func (vm VMImpl) ConfigureAgent(agentEnv AgentEnv) error {
bytes, err := json.Marshal(agentEnv)
if err != nil {
return bosherr.WrapError(err, "Marshalling agent env")
}
err = vm.store.Put("env.json", bytes)
if err != nil {
return bosherr.WrapError(err, "Updating agent env")
}
return nil
}
func (vm VMImpl) reconfigureAgent(hotPlug bool, agentEnvFunc func(AgentEnv) AgentEnv) error {
prevContents, err := vm.store.Get("env.json")
if err != nil {
return bosherr.WrapError(err, "Fetching agent env")
}
var prevAgentEnv AgentEnv
err = json.Unmarshal(prevContents, &prevAgentEnv)
if err != nil {
return bosherr.WrapError(err, "Unmarshalling agent env")
}
updatedAgentEnv := agentEnvFunc(prevAgentEnv)
err = vm.ConfigureAgent(updatedAgentEnv)
if err != nil {
return err
}
newContents, err := json.Marshal(updatedAgentEnv)
if err != nil {
return bosherr.WrapError(err, "Marshalling agent env")
}
err = vm.store.Put("env.json", newContents)
if err != nil {
return bosherr.WrapError(err, "Updating agent env")
}
isoBytes, err := ISO9660{FileName: "ENV", Contents: newContents}.Bytes()
if err != nil {
return bosherr.WrapError(err, "Marshaling agent env to ISO")
}
err = vm.store.Put("env.iso", isoBytes)
if err != nil {
return bosherr.WrapError(err, "Updating agent env")
}
updateFunc := func() error {
cd, err := vm.portDevices.CDROM()
if err != nil {
return err
}
return cd.Mount(vm.store.Path("env.iso"))
}
return vm.hotPlugIfNecessary(hotPlug, updateFunc)
}