forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiles.go
274 lines (224 loc) · 5.84 KB
/
profiles.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package db
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/lxc/lxd/lxd/types"
"github.com/lxc/lxd/shared/api"
)
// Profiles returns a string list of profiles.
func (n *Node) Profiles() ([]string, error) {
q := fmt.Sprintf("SELECT name FROM profiles")
inargs := []interface{}{}
var name string
outfmt := []interface{}{name}
result, err := queryScan(n.db, q, inargs, outfmt)
if err != nil {
return []string{}, err
}
response := []string{}
for _, r := range result {
response = append(response, r[0].(string))
}
return response, nil
}
func (n *Node) ProfileGet(name string) (int64, *api.Profile, error) {
id := int64(-1)
description := sql.NullString{}
q := "SELECT id, description FROM profiles WHERE name=?"
arg1 := []interface{}{name}
arg2 := []interface{}{&id, &description}
err := dbQueryRowScan(n.db, q, arg1, arg2)
if err != nil {
return -1, nil, err
}
config, err := n.ProfileConfig(name)
if err != nil {
return -1, nil, err
}
devices, err := n.Devices(name, true)
if err != nil {
return -1, nil, err
}
profile := api.Profile{
Name: name,
}
profile.Config = config
profile.Description = description.String
profile.Devices = devices
return id, &profile, nil
}
func (n *Node) ProfileCreate(profile string, description string, config map[string]string,
devices types.Devices) (int64, error) {
tx, err := begin(n.db)
if err != nil {
return -1, err
}
result, err := tx.Exec("INSERT INTO profiles (name, description) VALUES (?, ?)", profile, description)
if err != nil {
tx.Rollback()
return -1, err
}
id, err := result.LastInsertId()
if err != nil {
tx.Rollback()
return -1, err
}
err = ProfileConfigAdd(tx, id, config)
if err != nil {
tx.Rollback()
return -1, err
}
err = DevicesAdd(tx, "profile", id, devices)
if err != nil {
tx.Rollback()
return -1, err
}
err = TxCommit(tx)
if err != nil {
return -1, err
}
return id, nil
}
func (n *Node) ProfileCreateDefault() error {
id, _, _ := n.ProfileGet("default")
if id != -1 {
// default profile already exists
return nil
}
_, err := n.ProfileCreate("default", "Default LXD profile", map[string]string{}, types.Devices{})
if err != nil {
return err
}
return nil
}
// Get the profile configuration map from the DB
func (n *Node) ProfileConfig(name string) (map[string]string, error) {
var key, value string
query := `
SELECT
key, value
FROM profiles_config
JOIN profiles ON profiles_config.profile_id=profiles.id
WHERE name=?`
inargs := []interface{}{name}
outfmt := []interface{}{key, value}
results, err := queryScan(n.db, query, inargs, outfmt)
if err != nil {
return nil, fmt.Errorf("Failed to get profile '%s'", name)
}
if len(results) == 0 {
/*
* If we didn't get any rows here, let's check to make sure the
* profile really exists; if it doesn't, let's send back a 404.
*/
query := "SELECT id FROM profiles WHERE name=?"
var id int
results, err := queryScan(n.db, query, []interface{}{name}, []interface{}{id})
if err != nil {
return nil, err
}
if len(results) == 0 {
return nil, NoSuchObjectError
}
}
config := map[string]string{}
for _, r := range results {
key = r[0].(string)
value = r[1].(string)
config[key] = value
}
return config, nil
}
func (n *Node) ProfileDelete(name string) error {
id, _, err := n.ProfileGet(name)
if err != nil {
return err
}
_, err = exec(n.db, "DELETE FROM profiles WHERE id=?", id)
if err != nil {
return err
}
return nil
}
func (n *Node) ProfileUpdate(name string, newName string) error {
tx, err := begin(n.db)
if err != nil {
return err
}
_, err = tx.Exec("UPDATE profiles SET name=? WHERE name=?", newName, name)
if err != nil {
tx.Rollback()
return err
}
err = TxCommit(tx)
return err
}
func ProfileDescriptionUpdate(tx *sql.Tx, id int64, description string) error {
_, err := tx.Exec("UPDATE profiles SET description=? WHERE id=?", description, id)
return err
}
func ProfileConfigClear(tx *sql.Tx, id int64) error {
_, err := tx.Exec("DELETE FROM profiles_config WHERE profile_id=?", id)
if err != nil {
return err
}
_, err = tx.Exec(`DELETE FROM profiles_devices_config WHERE id IN
(SELECT profiles_devices_config.id
FROM profiles_devices_config JOIN profiles_devices
ON profiles_devices_config.profile_device_id=profiles_devices.id
WHERE profiles_devices.profile_id=?)`, id)
if err != nil {
return err
}
_, err = tx.Exec("DELETE FROM profiles_devices WHERE profile_id=?", id)
if err != nil {
return err
}
return nil
}
func ProfileConfigAdd(tx *sql.Tx, id int64, config map[string]string) error {
str := fmt.Sprintf("INSERT INTO profiles_config (profile_id, key, value) VALUES(?, ?, ?)")
stmt, err := tx.Prepare(str)
defer stmt.Close()
if err != nil {
return err
}
for k, v := range config {
_, err = stmt.Exec(id, k, v)
if err != nil {
return err
}
}
return nil
}
func (n *Node) ProfileContainersGet(profile string) ([]string, error) {
q := `SELECT containers.name FROM containers JOIN containers_profiles
ON containers.id == containers_profiles.container_id
JOIN profiles ON containers_profiles.profile_id == profiles.id
WHERE profiles.name == ?`
results := []string{}
inargs := []interface{}{profile}
var name string
outfmt := []interface{}{name}
output, err := queryScan(n.db, q, inargs, outfmt)
if err != nil {
return results, err
}
for _, r := range output {
results = append(results, r[0].(string))
}
return results, nil
}
func (n *Node) ProfileCleanupLeftover() error {
stmt := `
DELETE FROM profiles_config WHERE profile_id NOT IN (SELECT id FROM profiles);
DELETE FROM profiles_devices WHERE profile_id NOT IN (SELECT id FROM profiles);
DELETE FROM profiles_devices_config WHERE profile_device_id NOT IN (SELECT id FROM profiles_devices);
`
_, err := n.db.Exec(stmt)
if err != nil {
return err
}
return nil
}