~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/lxc/lxd/lxd/profiles.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package main
 
2
 
 
3
import (
 
4
        "encoding/json"
 
5
        "fmt"
 
6
        "net/http"
 
7
        "reflect"
 
8
 
 
9
        "github.com/gorilla/mux"
 
10
        _ "github.com/mattn/go-sqlite3"
 
11
 
 
12
        "github.com/lxc/lxd/shared"
 
13
 
 
14
        log "gopkg.in/inconshreveable/log15.v2"
 
15
)
 
16
 
 
17
/* This is used for both profiles post and profile put */
 
18
type profilesPostReq struct {
 
19
        Name        string            `json:"name"`
 
20
        Config      map[string]string `json:"config"`
 
21
        Description string            `json:"description"`
 
22
        Devices     shared.Devices    `json:"devices"`
 
23
}
 
24
 
 
25
func profilesGet(d *Daemon, r *http.Request) Response {
 
26
        results, err := dbProfiles(d.db)
 
27
        if err != nil {
 
28
                return SmartError(err)
 
29
        }
 
30
 
 
31
        recursion := d.isRecursionRequest(r)
 
32
 
 
33
        resultString := make([]string, len(results))
 
34
        resultMap := make([]*shared.ProfileConfig, len(results))
 
35
        i := 0
 
36
        for _, name := range results {
 
37
                if !recursion {
 
38
                        url := fmt.Sprintf("/%s/profiles/%s", shared.APIVersion, name)
 
39
                        resultString[i] = url
 
40
                } else {
 
41
                        profile, err := doProfileGet(d, name)
 
42
                        if err != nil {
 
43
                                shared.Log.Error("Failed to get profile", log.Ctx{"profile": name})
 
44
                                continue
 
45
                        }
 
46
                        resultMap[i] = profile
 
47
                }
 
48
                i++
 
49
        }
 
50
 
 
51
        if !recursion {
 
52
                return SyncResponse(true, resultString)
 
53
        }
 
54
 
 
55
        return SyncResponse(true, resultMap)
 
56
}
 
57
 
 
58
func profilesPost(d *Daemon, r *http.Request) Response {
 
59
        req := profilesPostReq{}
 
60
        if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
 
61
                return BadRequest(err)
 
62
        }
 
63
 
 
64
        // Sanity checks
 
65
        if req.Name == "" {
 
66
                return BadRequest(fmt.Errorf("No name provided"))
 
67
        }
 
68
 
 
69
        err := containerValidConfig(req.Config, true, false)
 
70
        if err != nil {
 
71
                return BadRequest(err)
 
72
        }
 
73
 
 
74
        err = containerValidDevices(req.Devices, true, false)
 
75
        if err != nil {
 
76
                return BadRequest(err)
 
77
        }
 
78
 
 
79
        // Update DB entry
 
80
        _, err = dbProfileCreate(d.db, req.Name, req.Description, req.Config, req.Devices)
 
81
        if err != nil {
 
82
                return InternalError(
 
83
                        fmt.Errorf("Error inserting %s into database: %s", req.Name, err))
 
84
        }
 
85
 
 
86
        return EmptySyncResponse
 
87
}
 
88
 
 
89
var profilesCmd = Command{
 
90
        name: "profiles",
 
91
        get:  profilesGet,
 
92
        post: profilesPost}
 
93
 
 
94
func doProfileGet(d *Daemon, name string) (*shared.ProfileConfig, error) {
 
95
        _, profile, err := dbProfileGet(d.db, name)
 
96
        return profile, err
 
97
}
 
98
 
 
99
func profileGet(d *Daemon, r *http.Request) Response {
 
100
        name := mux.Vars(r)["name"]
 
101
 
 
102
        resp, err := doProfileGet(d, name)
 
103
        if err != nil {
 
104
                return SmartError(err)
 
105
        }
 
106
 
 
107
        return SyncResponse(true, resp)
 
108
}
 
109
 
 
110
func getRunningContainersWithProfile(d *Daemon, profile string) []container {
 
111
        results := []container{}
 
112
 
 
113
        output, err := dbProfileContainersGet(d.db, profile)
 
114
        if err != nil {
 
115
                return results
 
116
        }
 
117
 
 
118
        for _, name := range output {
 
119
                c, err := containerLoadByName(d, name)
 
120
                if err != nil {
 
121
                        shared.Log.Error("Failed opening container", log.Ctx{"container": name})
 
122
                        continue
 
123
                }
 
124
                results = append(results, c)
 
125
        }
 
126
        return results
 
127
}
 
128
 
 
129
func profilePut(d *Daemon, r *http.Request) Response {
 
130
        name := mux.Vars(r)["name"]
 
131
 
 
132
        req := profilesPostReq{}
 
133
        if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
 
134
                return BadRequest(err)
 
135
        }
 
136
 
 
137
        // Sanity checks
 
138
        err := containerValidConfig(req.Config, true, false)
 
139
        if err != nil {
 
140
                return BadRequest(err)
 
141
        }
 
142
 
 
143
        err = containerValidDevices(req.Devices, true, false)
 
144
        if err != nil {
 
145
                return BadRequest(err)
 
146
        }
 
147
 
 
148
        // Get the running container list
 
149
        clist := getRunningContainersWithProfile(d, name)
 
150
        var containers []container
 
151
        for _, c := range clist {
 
152
                if !c.IsRunning() {
 
153
                        continue
 
154
                }
 
155
 
 
156
                containers = append(containers, c)
 
157
        }
 
158
 
 
159
        // Update the database
 
160
        id, profile, err := dbProfileGet(d.db, name)
 
161
        if err != nil {
 
162
                return InternalError(fmt.Errorf("Failed to retrieve profile='%s'", name))
 
163
        }
 
164
 
 
165
        tx, err := dbBegin(d.db)
 
166
        if err != nil {
 
167
                return InternalError(err)
 
168
        }
 
169
 
 
170
        if profile.Description != req.Description {
 
171
                err = dbProfileDescriptionUpdate(tx, id, req.Description)
 
172
                if err != nil {
 
173
                        tx.Rollback()
 
174
                        return InternalError(err)
 
175
                }
 
176
        }
 
177
 
 
178
        // Optimize for description-only changes
 
179
        if reflect.DeepEqual(profile.Config, req.Config) && reflect.DeepEqual(profile.Devices, req.Devices) {
 
180
                err = txCommit(tx)
 
181
                if err != nil {
 
182
                        return InternalError(err)
 
183
                }
 
184
 
 
185
                return EmptySyncResponse
 
186
        }
 
187
 
 
188
        err = dbProfileConfigClear(tx, id)
 
189
        if err != nil {
 
190
                tx.Rollback()
 
191
                return InternalError(err)
 
192
        }
 
193
 
 
194
        err = dbProfileConfigAdd(tx, id, req.Config)
 
195
        if err != nil {
 
196
                tx.Rollback()
 
197
                return SmartError(err)
 
198
        }
 
199
 
 
200
        err = dbDevicesAdd(tx, "profile", id, req.Devices)
 
201
        if err != nil {
 
202
                tx.Rollback()
 
203
                return SmartError(err)
 
204
        }
 
205
 
 
206
        err = txCommit(tx)
 
207
        if err != nil {
 
208
                return InternalError(err)
 
209
        }
 
210
 
 
211
        // Update all the containers using the profile. Must be done after txCommit due to DB lock.
 
212
        failures := map[string]error{}
 
213
        for _, c := range containers {
 
214
                err = c.Update(containerArgs{
 
215
                        Architecture: c.Architecture(),
 
216
                        Ephemeral:    c.IsEphemeral(),
 
217
                        Config:       c.LocalConfig(),
 
218
                        Devices:      c.LocalDevices(),
 
219
                        Profiles:     c.Profiles()}, true)
 
220
 
 
221
                if err != nil {
 
222
                        failures[c.Name()] = err
 
223
                }
 
224
        }
 
225
 
 
226
        if len(failures) != 0 {
 
227
                msg := "The following containers failed to update (profile change still saved):\n"
 
228
                for cname, err := range failures {
 
229
                        msg += fmt.Sprintf(" - %s: %s\n", cname, err)
 
230
                }
 
231
                return InternalError(fmt.Errorf("%s", msg))
 
232
        }
 
233
 
 
234
        return EmptySyncResponse
 
235
}
 
236
 
 
237
// The handler for the post operation.
 
238
func profilePost(d *Daemon, r *http.Request) Response {
 
239
        name := mux.Vars(r)["name"]
 
240
 
 
241
        req := profilesPostReq{}
 
242
        if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
 
243
                return BadRequest(err)
 
244
        }
 
245
 
 
246
        // Sanity checks
 
247
        if req.Name == "" {
 
248
                return BadRequest(fmt.Errorf("No name provided"))
 
249
        }
 
250
 
 
251
        err := dbProfileUpdate(d.db, name, req.Name)
 
252
        if err != nil {
 
253
                return InternalError(err)
 
254
        }
 
255
 
 
256
        return EmptySyncResponse
 
257
}
 
258
 
 
259
// The handler for the delete operation.
 
260
func profileDelete(d *Daemon, r *http.Request) Response {
 
261
        name := mux.Vars(r)["name"]
 
262
        err := dbProfileDelete(d.db, name)
 
263
 
 
264
        if err != nil {
 
265
                return InternalError(err)
 
266
        }
 
267
 
 
268
        return EmptySyncResponse
 
269
}
 
270
 
 
271
var profileCmd = Command{name: "profiles/{name}", get: profileGet, put: profilePut, delete: profileDelete, post: profilePost}