-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.js
227 lines (186 loc) · 6.3 KB
/
response.js
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var fs = require('fs');
var Docker = require('dockerode');
var Step = require('step');
module.exports = function(root, verbose, body) {
var docker = new Docker({socketPath: '/var/run/docker.sock'});
var POWERSTRIP_TOKEN = "POWERSTRIP_TOKEN__";
var returnBody = {
PowerstripProtocolVersion: 1
}
//
// Helper functions
//
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function createGuid() {
return (S4() + S4() + "-" + S4() + "-4" + S4().substr(0,3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase();
}
function debug(s) {
if (verbose) {
console.log(s);
}
}
//
// Pre- and post-hook processing
//
function preHook(root, verbose, body) {
// Figure out the actual request to the Docker daemon.
var clientRequestBody = JSON.parse(body.ClientRequest.Body);
debug("\nbody.ClientRequest.Body:");
debug(JSON.stringify(clientRequestBody, null, 4));
// Create the directory for the original logs
var originalPath = root + "/original";
var token = createGuid();
var pathPrefix = originalPath + "/" + token;
if (!fs.existsSync(root)) {
var err = fs.mkdirSync(root);
if (err) throw err;
debug("Created root directory: " + root);
} else {
debug("Root directory exists: " + root);
}
if (!fs.existsSync(originalPath)) {
var err = fs.mkdirSync(originalPath);
if (err) throw err;
debug("Created original directory: " + originalPath);
} else {
debug("Original directory exists: " + originalPath);
}
if (!fs.existsSync(pathPrefix)) {
var err = fs.mkdirSync(pathPrefix);
if (err) throw err;
debug("Create prefix directory: " + pathPrefix);
} else {
debug("Prefix directory exists: " + pathPrefix);
}
// Figure out all the log file paths from the LOGS env
var envs = clientRequestBody.Env || [];
debug("Envs: " + envs);
var logs = [];
for (var i = 0; i < envs.length; i++) {
var env = envs[i];
if (env.startsWith("LOGS=")) {
var kv = env.split("=", 5);
if (kv[0] === "LOGS") {
logs = logs.concat(kv[1].split(","));
}
}
}
debug("Logs: " + logs);
// Add the token to the environment.
envs.push(POWERSTRIP_TOKEN + "=" + token);
clientRequestBody.Env = envs;
// Create the additional bind mounts for the log paths
var binds = clientRequestBody.HostConfig.Binds || [];
for (var i = 0; i < logs.length; i++) {
var hostPath = pathPrefix + logs[i];
var containerPath = logs[i];
var bind = hostPath + ":" + containerPath;
binds.push(bind);
debug("Bind: " + bind);
}
debug("Binds: " + binds);
clientRequestBody.HostConfig.Binds = binds;
// Forward the changes as the modified client request.
var modifiedClientRequestBody = JSON.stringify(clientRequestBody);
body.ClientRequest.Body = modifiedClientRequestBody;
returnBody.ModifiedClientRequest = body.ClientRequest;
}
function postHook(root, verbose, body) {
// Get the server response
debug("body.ServerResponse.Body:");
debug(body.ServerResponse.Body);
var serverResponseBody;
serverResponseBody = JSON.parse(body.ServerResponse.Body);
debug("Parsed body.ServerResponse.Body:");
debug(JSON.stringify(serverResponseBody, null, 4));
// From the server response we can get the container ID
var containerId = serverResponseBody.Id;
debug("ContainerId: " + containerId);
// Get the details for this container so we can get the
// token from the container's environment
var container = docker.getContainer(containerId);
Step(
function() {
container.inspect(this);
},
function(err, data) {
if (err) throw err;
debug(data);
// Get the environment variables and find the token
var envs = data.Config.Env
debug("Envs: " + envs)
var token = null
for (var i = 0; i < envs.length; i++) {
var env = envs[i]
if (env.startsWith(POWERSTRIP_TOKEN)) {
var kv = env.split("=", POWERSTRIP_TOKEN.length + 1)
if (kv[0] === POWERSTRIP_TOKEN) {
token = kv[1]
}
}
}
debug("Token: " + token)
// With the token, we now know the path to rename
var originalPath = root + "/original/" + token
debug("OriginalPath: " + originalPath)
var containersRoot = root + "/containers"
var containerPath = containersRoot + "/" + containerId
debug("ContainerPath: " + containerPath)
// Create the symlink
if (!fs.existsSync(containersRoot)) {
var err = fs.mkdirSync(containersRoot)
if (err) throw err;
debug("Created containers directory: " + containersRoot)
} else {
debug("Containers directory exists: " + containersRoot)
}
var err = fs.symlinkSync(originalPath, containerPath)
if (err) throw err;
debug("Linking complete: " + originalPath + " -> " + containerPath)
if (!fs.existsSync(containerPath)) {
throw "Container link doesn't exist: " + containerPath
}
});
}
//
// Main processing
//
if (body.Type == 'pre-hook'){
try {
// Pre-hook processing.
preHook(root, verbose, body);
} catch (err) {
// Just move on and pass the response back
// without processing
debug("Error during pre-hook processing, " +
"passing response without processing: " + err);
}
}
else if(body.Type == 'post-hook'){
// Check if Docker returned an error.
if (body.ServerResponse.Code != 201) {
// Just move on and pass the response back
// without processing
debug("ServerResponse.Code not 200 CREATED, but: " +
body.ServerResponse.Code);
}
else {
try {
// Post-hook processing
postHook(root, verbose, body);
} catch (err) {
// Just move on and pass the response back
// without processing
debug("Error during post-hook processing, " +
"passing response without processing: " + err);
}
}
// Just send back the incoming server response.
returnBody.ModifiedServerResponse = body.ServerResponse
} else {
return null;
}
return returnBody;
}