forked from canonical/lxd
/
profiles_utils.go
234 lines (201 loc) · 7.14 KB
/
profiles_utils.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"fmt"
"github.com/pkg/errors"
"github.com/lxc/lxd/lxd/db"
deviceConfig "github.com/lxc/lxd/lxd/device/config"
"github.com/lxc/lxd/lxd/instance"
"github.com/lxc/lxd/lxd/instance/instancetype"
"github.com/lxc/lxd/lxd/project"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
)
func doProfileUpdate(d *Daemon, projectName string, name string, id int64, profile *api.Profile, req api.ProfilePut) error {
// Check project limits.
err := d.cluster.Transaction(func(tx *db.ClusterTx) error {
return project.AllowProfileUpdate(tx, projectName, name, req)
})
if err != nil {
return err
}
// Sanity checks.
err = instance.ValidConfig(d.os, req.Config, true, false)
if err != nil {
return err
}
// Profiles can be applied to any instance type, so just use instancetype.Any type for validation so that
// instance type specific validation checks are not performed.
err = instance.ValidDevices(d.State(), d.cluster, projectName, instancetype.Any, deviceConfig.NewDevices(req.Devices), false)
if err != nil {
return err
}
insts, err := getProfileInstancesInfo(d.cluster, projectName, name)
if err != nil {
return errors.Wrapf(err, "Failed to query instances associated with profile %q", name)
}
// Check if the root disk device's pool is supposed to be changed or removed and prevent that if there are
// instances using that root disk device.
oldProfileRootDiskDeviceKey, oldProfileRootDiskDevice, _ := shared.GetRootDiskDevice(profile.Devices)
_, newProfileRootDiskDevice, _ := shared.GetRootDiskDevice(req.Devices)
if len(insts) > 0 && oldProfileRootDiskDevice["pool"] != "" && newProfileRootDiskDevice["pool"] == "" || (oldProfileRootDiskDevice["pool"] != newProfileRootDiskDevice["pool"]) {
// Check for instances using the device.
for _, inst := range insts {
// Check if the device is locally overridden.
k, v, _ := shared.GetRootDiskDevice(inst.Devices.CloneNative())
if k != "" && v["pool"] != "" {
continue
}
// Check what profile the device comes from by working backwards along the profiles list.
for i := len(inst.Profiles) - 1; i >= 0; i-- {
_, profile, err := d.cluster.GetProfile(projectName, inst.Profiles[i])
if err != nil {
return err
}
// Check if we find a match for the device.
_, ok := profile.Devices[oldProfileRootDiskDeviceKey]
if ok {
// Found the profile.
if inst.Profiles[i] == name {
// If it's the current profile, then we can't modify that root device.
return fmt.Errorf("At least one instance relies on this profile's root disk device")
}
// If it's not, then move on to the next instance.
break
}
}
}
}
// Update the database.
err = d.cluster.Transaction(func(tx *db.ClusterTx) error {
return tx.UpdateProfile(projectName, name, db.Profile{
Project: projectName,
Name: name,
Description: req.Description,
Config: req.Config,
Devices: req.Devices,
})
})
if err != nil {
return err
}
// Update all the instances on this node using the profile. Must be done after db.TxCommit due to DB lock.
nodeName := ""
err = d.cluster.Transaction(func(tx *db.ClusterTx) error {
var err error
nodeName, err = tx.GetLocalNodeName()
return err
})
if err != nil {
return errors.Wrap(err, "failed to query local node name")
}
failures := map[*db.InstanceArgs]error{}
for _, it := range insts {
inst := it // Local var for instance pointer.
err := doProfileUpdateInstance(d, name, profile.ProfilePut, nodeName, inst)
if err != nil {
failures[&inst] = err
}
}
if len(failures) != 0 {
msg := "The following instances failed to update (profile change still saved):\n"
for inst, err := range failures {
msg += fmt.Sprintf(" - Project: %s, Instance: %s: %v\n", inst.Project, inst.Name, err)
}
return fmt.Errorf("%s", msg)
}
return nil
}
// Like doProfileUpdate but does not update the database, since it was already
// updated by doProfileUpdate itself, called on the notifying node.
func doProfileUpdateCluster(d *Daemon, projectName string, name string, old api.ProfilePut) error {
nodeName := ""
err := d.cluster.Transaction(func(tx *db.ClusterTx) error {
var err error
nodeName, err = tx.GetLocalNodeName()
return err
})
if err != nil {
return errors.Wrap(err, "Failed to query local node name")
}
insts, err := getProfileInstancesInfo(d.cluster, projectName, name)
if err != nil {
return errors.Wrapf(err, "Failed to query instances associated with profile %q", name)
}
failures := map[*db.InstanceArgs]error{}
for _, it := range insts {
inst := it // Local var for instance pointer.
err := doProfileUpdateInstance(d, name, old, nodeName, inst)
if err != nil {
failures[&inst] = err
}
}
if len(failures) != 0 {
msg := "The following instances failed to update (profile change still saved):\n"
for inst, err := range failures {
msg += fmt.Sprintf(" - Project: %s, Instance: %s: %v\n", inst.Project, inst.Name, err)
}
return fmt.Errorf("%s", msg)
}
return nil
}
// Profile update of a single instance.
func doProfileUpdateInstance(d *Daemon, name string, old api.ProfilePut, nodeName string, args db.InstanceArgs) error {
if args.Node != "" && args.Node != nodeName {
// No-op, this instance does not belong to this node.
return nil
}
profiles, err := d.cluster.GetProfiles(args.Project, args.Profiles)
if err != nil {
return err
}
for i, profileName := range args.Profiles {
if profileName == name {
// Overwrite the new config from the database with the old config and devices.
profiles[i].Config = old.Config
profiles[i].Devices = old.Devices
break
}
}
// Load the instance using the old profile config.
inst, err := instance.Load(d.State(), args, profiles)
if err != nil {
return err
}
// Update will internally load the new profile configs and detect the changes to apply.
return inst.Update(db.InstanceArgs{
Architecture: inst.Architecture(),
Config: inst.LocalConfig(),
Description: inst.Description(),
Devices: inst.LocalDevices(),
Ephemeral: inst.IsEphemeral(),
Profiles: inst.Profiles(), // List of profile names to load from DB.
Project: inst.Project(),
Type: inst.Type(),
Snapshot: inst.IsSnapshot(),
}, true)
}
// Query the db for information about instances associated with the given profile.
func getProfileInstancesInfo(cluster *db.Cluster, projectName string, profileName string) ([]db.InstanceArgs, error) {
// Query the db for information about instances associated with the given profile.
projectInstNames, err := cluster.GetInstancesWithProfile(projectName, profileName)
if err != nil {
return nil, errors.Wrapf(err, "Failed to query instances with profile %q", profileName)
}
instances := []db.InstanceArgs{}
err = cluster.Transaction(func(tx *db.ClusterTx) error {
for instProject, instNames := range projectInstNames {
for _, instName := range instNames {
inst, err := tx.GetInstance(instProject, instName)
if err != nil {
return err
}
instances = append(instances, db.InstanceToArgs(inst))
}
}
return nil
})
if err != nil {
return nil, errors.Wrapf(err, "Failed to fetch instances")
}
return instances, nil
}