/
container_state.go
210 lines (179 loc) · 4.4 KB
/
container_state.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/lxc/lxd/lxd/db"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
)
func containerState(d *Daemon, r *http.Request) Response {
project := projectParam(r)
name := mux.Vars(r)["name"]
// Handle requests targeted to a container on a different node
response, err := ForwardedResponseIfContainerIsRemote(d, r, project, name)
if err != nil {
return SmartError(err)
}
if response != nil {
return response
}
c, err := containerLoadByProjectAndName(d.State(), project, name)
if err != nil {
return SmartError(err)
}
state, err := c.RenderState()
if err != nil {
return InternalError(err)
}
return SyncResponse(true, state)
}
func containerStatePut(d *Daemon, r *http.Request) Response {
project := projectParam(r)
name := mux.Vars(r)["name"]
// Handle requests targeted to a container on a different node
response, err := ForwardedResponseIfContainerIsRemote(d, r, project, name)
if err != nil {
return SmartError(err)
}
if response != nil {
return response
}
raw := api.ContainerStatePut{}
// We default to -1 (i.e. no timeout) here instead of 0 (instant
// timeout).
raw.Timeout = -1
if err := json.NewDecoder(r.Body).Decode(&raw); err != nil {
return BadRequest(err)
}
// Don't mess with containers while in setup mode
<-d.readyChan
c, err := containerLoadByProjectAndName(d.State(), project, name)
if err != nil {
return SmartError(err)
}
var opType db.OperationType
var do func(*operation) error
switch shared.ContainerAction(raw.Action) {
case shared.Start:
opType = db.OperationContainerStart
do = func(op *operation) error {
c.SetOperation(op)
if err = c.Start(raw.Stateful); err != nil {
return err
}
return nil
}
case shared.Stop:
opType = db.OperationContainerStop
if raw.Stateful {
do = func(op *operation) error {
c.SetOperation(op)
err := c.Stop(raw.Stateful)
if err != nil {
return err
}
return nil
}
} else if raw.Timeout == 0 || raw.Force {
do = func(op *operation) error {
c.SetOperation(op)
err = c.Stop(false)
if err != nil {
return err
}
return nil
}
} else {
do = func(op *operation) error {
c.SetOperation(op)
if c.IsFrozen() {
err := c.Unfreeze()
if err != nil {
return err
}
}
err = c.Shutdown(time.Duration(raw.Timeout) * time.Second)
if err != nil {
return err
}
return nil
}
}
case shared.Restart:
opType = db.OperationContainerRestart
do = func(op *operation) error {
c.SetOperation(op)
ephemeral := c.IsEphemeral()
if ephemeral {
// Unset ephemeral flag
args := db.ContainerArgs{
Architecture: c.Architecture(),
Config: c.LocalConfig(),
Description: c.Description(),
Devices: c.LocalDevices(),
Ephemeral: false,
Profiles: c.Profiles(),
Project: c.Project(),
}
err := c.Update(args, false)
if err != nil {
return err
}
// On function return, set the flag back on
defer func() {
args.Ephemeral = ephemeral
c.Update(args, true)
}()
}
if raw.Timeout == 0 || raw.Force {
err = c.Stop(false)
if err != nil {
return err
}
} else {
if c.IsFrozen() {
return fmt.Errorf("container is not running")
}
err = c.Shutdown(time.Duration(raw.Timeout) * time.Second)
if err != nil {
return err
}
}
err = c.Start(false)
if err != nil {
return err
}
return nil
}
case shared.Freeze:
if !d.os.CGroupFreezerController {
return BadRequest(fmt.Errorf("This system doesn't support freezing containers"))
}
opType = db.OperationContainerFreeze
do = func(op *operation) error {
c.SetOperation(op)
return c.Freeze()
}
case shared.Unfreeze:
if !d.os.CGroupFreezerController {
return BadRequest(fmt.Errorf("This system doesn't support unfreezing containers"))
}
opType = db.OperationContainerUnfreeze
do = func(op *operation) error {
c.SetOperation(op)
return c.Unfreeze()
}
default:
return BadRequest(fmt.Errorf("unknown action %s", raw.Action))
}
resources := map[string][]string{}
resources["containers"] = []string{name}
op, err := operationCreate(d.cluster, project, operationClassTask, opType, resources, nil, do, nil, nil)
if err != nil {
return InternalError(err)
}
return OperationResponse(op)
}