forked from canonical/lxd
/
container_put.go
120 lines (100 loc) · 2.58 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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"
)
type containerPutReq struct {
Architecture string `json:"architecture"`
Config map[string]string `json:"config"`
Devices shared.Devices `json:"devices"`
Ephemeral bool `json:"ephemeral"`
Profiles []string `json:"profiles"`
Restore string `json:"restore"`
}
/*
* 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 := containerLoadByName(d, name)
if err != nil {
return NotFound
}
configRaw := containerPutReq{}
if err := json.NewDecoder(r.Body).Decode(&configRaw); err != nil {
return BadRequest(err)
}
architecture, err := shared.ArchitectureId(configRaw.Architecture)
if err != nil {
architecture = 0
}
var do = func(*operation) error { return nil }
if configRaw.Restore == "" {
// Update container configuration
do = func(op *operation) error {
args := containerArgs{
Architecture: architecture,
Config: configRaw.Config,
Devices: configRaw.Devices,
Ephemeral: configRaw.Ephemeral,
Profiles: configRaw.Profiles}
// FIXME: should set to true when not migrating
err = c.Update(args, false)
if err != nil {
return err
}
return nil
}
} else {
// Snapshot Restore
do = func(op *operation) error {
return containerSnapRestore(d, name, configRaw.Restore)
}
}
resources := map[string][]string{}
resources["containers"] = []string{name}
op, err := operationCreate(operationClassTask, resources, nil, do, nil, nil)
if err != nil {
return InternalError(err)
}
return OperationResponse(op)
}
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 := containerLoadByName(d, name)
if err != nil {
shared.Log.Error(
"RESTORE => loadcontainerLXD() failed",
log.Ctx{
"container": name,
"err": err})
return err
}
source, err := containerLoadByName(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
}