forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_bridge.go
208 lines (164 loc) · 4.69 KB
/
container_bridge.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
package server
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/pkg/httputils"
"github.com/gorilla/mux"
)
// TODO
func (s *Server) removeContainers(ctx context.Context, resp http.ResponseWriter, req *http.Request) error {
return nil
}
func (s *Server) renameContainer(ctx context.Context, resp http.ResponseWriter, req *http.Request) error {
id := mux.Vars(req)["id"]
name := req.FormValue("name")
if err := s.ContainerMgr.Rename(ctx, id, name); err != nil {
return err
}
resp.WriteHeader(http.StatusNoContent)
return nil
}
func (s *Server) createContainerExec(ctx context.Context, resp http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]
config := &types.ExecCreateConfig{}
if err := json.NewDecoder(req.Body).Decode(config); err != nil {
return err
}
if len(config.Cmd) == 0 {
return fmt.Errorf("no exec process specified")
}
id, err := s.ContainerMgr.CreateExec(ctx, name, config)
if err != nil {
return err
}
resp.WriteHeader(http.StatusCreated)
return json.NewEncoder(resp).Encode(&types.ExecCreateResponse{
ID: id,
})
}
func (s *Server) startContainerExec(ctx context.Context, resp http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]
config := &types.ExecStartConfig{}
if err := json.NewDecoder(req.Body).Decode(config); err != nil {
return err
}
var attach *types.AttachConfig
if !config.Detach {
hijacker, ok := resp.(http.Hijacker)
if !ok {
return fmt.Errorf("not a hijack connection, container: %s", name)
}
attach = &types.AttachConfig{
Hijack: hijacker,
Stdin: config.Tty,
Stdout: true,
Stderr: true,
Upgrade: true,
}
}
return s.ContainerMgr.StartExec(ctx, name, config, attach)
}
func (s *Server) createContainer(ctx context.Context, resp http.ResponseWriter, req *http.Request) error {
var config types.ContainerConfigWrapper
if err := json.NewDecoder(req.Body).Decode(&config); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}
name := req.FormValue("name")
ret, err := s.ContainerMgr.Create(ctx, name, &config)
if err != nil {
return err
}
resp.WriteHeader(http.StatusCreated)
return json.NewEncoder(resp).Encode(ret)
}
func (s *Server) startContainer(ctx context.Context, resp http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]
config := types.ContainerStartConfig{
ID: name,
DetachKeys: req.FormValue("detachKeys"),
}
if err := s.ContainerMgr.Start(ctx, config); err != nil {
return err
}
resp.WriteHeader(http.StatusOK)
return nil
}
func (s *Server) stopContainer(ctx context.Context, resp http.ResponseWriter, req *http.Request) error {
var (
t int
err error
)
if v := req.FormValue("t"); v != "" {
if t, err = strconv.Atoi(v); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}
}
name := mux.Vars(req)["name"]
if err = s.ContainerMgr.Stop(ctx, name, time.Duration(t)*time.Second); err != nil {
return err
}
resp.WriteHeader(http.StatusOK)
return nil
}
func (s *Server) attachContainer(ctx context.Context, resp http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]
_, upgrade := req.Header["Upgrade"]
hijacker, ok := resp.(http.Hijacker)
if !ok {
return fmt.Errorf("not a hijack connection, container: %s", name)
}
attach := &types.AttachConfig{
Hijack: hijacker,
Stdin: req.FormValue("stdin") == "1",
Stdout: true,
Stderr: true,
Upgrade: upgrade,
}
if err := s.ContainerMgr.Attach(ctx, name, attach); err != nil {
// TODO handle error
}
return nil
}
func (s *Server) getContainers(ctx context.Context, resp http.ResponseWriter, req *http.Request) error {
cis, err := s.ContainerMgr.List(ctx)
if err != nil {
return err
}
cs := make([]types.Container, 0, len(cis))
for _, ci := range cis {
c := types.Container{
ID: ci.ID,
Names: []string{ci.Name},
Status: ci.Status.String(),
Image: ci.Config.Image,
Command: strings.Join(ci.Config.Cmd, " "),
Created: ci.StartedAt.UnixNano(),
Labels: ci.Config.Labels,
}
cs = append(cs, c)
}
resp.WriteHeader(http.StatusOK)
return json.NewEncoder(resp).Encode(cs)
}
func (s *Server) getContainer(ctx context.Context, resp http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]
ci, err := s.ContainerMgr.Get(name)
if err != nil {
return err
}
c := types.ContainerJSON{
ID: ci.ID,
Name: ci.Name,
Image: ci.Config.Image,
Created: ci.StartedAt.Format("2006-01-02 15:04:05"),
State: &types.ContainerState{Pid: ci.Pid},
}
resp.WriteHeader(http.StatusOK)
return json.NewEncoder(resp).Encode(c)
}