forked from canonical/lxd
/
container_put.go
104 lines (88 loc) · 1.96 KB
/
container_put.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/lxc/lxd/shared"
log "gopkg.in/inconshreveable/log15.v2"
)
/*
* Update configuration, or, if 'restore:snapshot-name' is present, restore
* the named snapshot
*/
func containerPut(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
c, err := containerLXDLoad(d, name)
if err != nil {
return NotFound
}
configRaw := containerConfigReq{}
if err := json.NewDecoder(r.Body).Decode(&configRaw); err != nil {
return BadRequest(err)
}
var do = func(string) error { return nil }
if configRaw.Restore == "" {
// Update container configuration
do = func(id string) error {
args := containerLXDArgs{
Config: configRaw.Config,
Devices: configRaw.Devices,
Profiles: configRaw.Profiles}
err = c.ConfigReplace(args)
if err != nil {
return err
}
return nil
}
} else {
// Snapshot Restore
do = func(id string) error {
return containerSnapRestore(d, name, configRaw.Restore)
}
}
return AsyncResponse(shared.OperationWrap(do), nil)
}
func containerSnapRestore(d *Daemon, name string, snap string) error {
// normalize snapshot name
if !shared.IsSnapshot(snap) {
snap = name + shared.SnapshotDelimiter + snap
}
shared.Log.Info(
"RESTORE => Restoring snapshot",
log.Ctx{
"snapshot": snap,
"container": name})
c, err := containerLXDLoad(d, name)
if err != nil {
shared.Log.Error(
"RESTORE => loadcontainerLXD() failed",
log.Ctx{
"container": name,
"err": err})
return err
}
source, err := containerLXDLoad(d, snap)
if err != nil {
switch err {
case sql.ErrNoRows:
return fmt.Errorf("snapshot %s does not exist", snap)
default:
return err
}
}
if err := c.Restore(source); err != nil {
return err
}
return nil
}
func emptyProfile(l []string) bool {
if len(l) == 0 {
return true
}
if len(l) == 1 && l[0] == "" {
return true
}
return false
}