-
Notifications
You must be signed in to change notification settings - Fork 69
/
powerstrip.go
161 lines (135 loc) · 3.99 KB
/
powerstrip.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
package daemon
import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"github.com/socketplane/socketplane/Godeps/_workspace/src/github.com/samalba/dockerclient"
)
type adapterRequest struct {
PowerstripProtocolVersion int
Type string
ClientRequest struct {
Method string
Request string
Body string
}
ServerResponse struct {
Body string
Code int
ContentType string
}
}
type adapterPreResponse struct {
PowerstripProtocolVersion int
ModifiedClientRequest struct {
Method string
Request string
Body string
}
}
type adapterPostResponse struct {
PowerstripProtocolVersion int
ModifiedServerResponse struct {
Body string
Code int
ContentType string
}
}
func psAdapterPreHook(d *Daemon, reqParams adapterRequest) (preResp *adapterPreResponse) {
preResp = &adapterPreResponse{}
preResp.PowerstripProtocolVersion = reqParams.PowerstripProtocolVersion
preResp.ModifiedClientRequest.Method = reqParams.ClientRequest.Method
preResp.ModifiedClientRequest.Request = reqParams.ClientRequest.Request
preResp.ModifiedClientRequest.Body = reqParams.ClientRequest.Body
if reqParams.ClientRequest.Body != "" {
jsonBody := &dockerclient.ContainerConfig{}
err := json.Unmarshal([]byte(reqParams.ClientRequest.Body), &jsonBody)
if err != nil {
fmt.Println("Body JSON unmarshall failed", err)
}
jsonBody.HostConfig.NetworkMode = "none"
body, _ := json.Marshal(jsonBody)
preResp.ModifiedClientRequest.Body = string(body)
}
return
}
func psAdapterPostHook(d *Daemon, reqParams adapterRequest) (postResp *adapterPostResponse) {
postResp = &adapterPostResponse{}
postResp.PowerstripProtocolVersion = reqParams.PowerstripProtocolVersion
postResp.ModifiedServerResponse.ContentType = "application/json"
postResp.ModifiedServerResponse.Body = reqParams.ServerResponse.Body
postResp.ModifiedServerResponse.Code = reqParams.ServerResponse.Code
if reqParams.ClientRequest.Method != "POST" &&
reqParams.ClientRequest.Method != "DELETE" {
fmt.Println("Invalid method: ", reqParams.ClientRequest.Method)
postResp.ModifiedServerResponse.Code = 500
return
}
if reqParams.ClientRequest.Request != "" {
// start api looks like this /<version>/containers/<cid>/start
s := regexp.MustCompile("/").Split(reqParams.ClientRequest.Request, 5)
cid := s[3]
var cfg = &Connection{}
var op = ConnectionAdd
switch reqParams.ClientRequest.Method {
case "POST":
docker, _ := dockerclient.NewDockerClient(
"unix:///var/run/docker.sock", nil)
info, err := docker.InspectContainer(cid)
if err != nil {
fmt.Println("InspectContainer failed", err)
postResp.ModifiedServerResponse.Code = 404
return
}
cfg.ContainerID = string(cid)
cfg.ContainerName = info.Name
cfg.ContainerPID = strconv.Itoa(info.State.Pid)
cfg.Network = DefaultNetworkName
for _, env := range info.Config.Env {
val := regexp.MustCompile("=").Split(env, 3)
if val[0] == "SP_NETWORK" {
cfg.Network = strings.Trim(val[1], " ")
}
}
op = ConnectionAdd
case "DELETE":
var ok bool
if cfg, ok = d.Connections[cid]; !ok {
fmt.Println("Couldn't find the connection", cid)
postResp.ModifiedServerResponse.Code = 500
return
}
op = ConnectionDelete
}
context := &ConnectionContext{
op,
cfg,
make(chan *Connection),
}
d.cC <- context
<-context.Result
}
return
}
func psAdapter(d *Daemon, w http.ResponseWriter, r *http.Request) *apiError {
var reqParams adapterRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&reqParams)
if err != nil {
fmt.Println("Error decodeing JSON", err)
//return &apiError{http.StatusInternalServerError, err.Error()}
}
var data []byte
switch reqParams.Type {
case "pre-hook":
data, _ = json.Marshal(psAdapterPreHook(d, reqParams))
case "post-hook":
data, _ = json.Marshal(psAdapterPostHook(d, reqParams))
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(data)
return nil
}