forked from cqbqdd11519/DockerClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.cpp
360 lines (315 loc) · 10.6 KB
/
docker.cpp
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/****
Dependency
- libcurl
- [RapidJSON](https://github.com/Tencent/rapidjson/)
Basic Usage
#include "docker.h"
int main(){
Docker client = Docker();
JSON_DOCUMENT doc = client.list_containers(true);
std::cout << jsonToString(doc) << std::endl;
return 0;
}
Return Type for Each Methods
Object o; (rapidjson::Document)
- success [bool] : if succeeded to request
- data [Object/Array/string] : actual data by server (data type depends on API, but it would be Object if 'success' is false)
- code(optional) [int] : http status code if 'success' is false
e.g.
{
"success": false,
"code": 404,
"data": {
"message": "No such container: 5d271b3a52263330348b71948bd25cda455a49f7e7d69cfc73e6b2f3b5b41a4c"
}
}
{
"success": true ,
"data": {
"Architecture": "x86_64",
...
"SystemTime": "2018-05-23T19:26:54.357768797+09:00"
}
}
****/
#include "docker.h"
#include <utility>
#include <sstream>
/*
*
* START Docker Implementation
*
*/
JSON_DOCUMENT Docker::emptyDoc = JSON_DOCUMENT();
Docker::Docker() : host_uri("http:/v1.24"){
curl_global_init(CURL_GLOBAL_ALL);
is_remote = false;
}
Docker::Docker(std::string host) : host_uri(std::move(host)){
curl_global_init(CURL_GLOBAL_ALL);
is_remote = true;
}
Docker::~Docker(){
curl_global_cleanup();
}
/*
*
* Public Methods
*
*/
/*
* System
*/
JSON_DOCUMENT Docker::system_info(){
std::string path = "/info";
return requestAndParseJson(GET,path);
}
JSON_DOCUMENT Docker::docker_version(){
std::string path = "/version";
return requestAndParseJson(GET,path);
}
/*
* Images
*/
JSON_DOCUMENT Docker::list_images(){
std::string path = "/images/json";
return requestAndParseJson(GET,path);
}
/*
* Containers
*/
JSON_DOCUMENT Docker::list_containers(bool all, int limit, const std::string& since, const std::string& before, int size, JSON_DOCUMENT& filters){
std::string path = "/containers/json?";
path += param("all", all);
path += param("limit", limit);
path += param("since", since);
path += param("before", before);
path += param("size", size);
path += param("filters", filters);
return requestAndParseJson(GET,path);
}
JSON_DOCUMENT Docker::inspect_container(const std::string& container_id){
std::string path = "/containers/" + container_id + "/json";
return requestAndParseJson(GET,path);
}
JSON_DOCUMENT Docker::top_container(const std::string& container_id){
std::string path = "/containers/" + container_id + "/top";
return requestAndParseJson(GET,path);
}
JSON_DOCUMENT Docker::logs_container(const std::string& container_id, bool follow, bool o_stdout, bool o_stderr, bool timestamps, const std::string& tail){
std::string path = "/containers/" + container_id + "/logs?";
path += param("follow", follow);
path += param("stdout", o_stdout);
path += param("stderr", o_stderr);
path += param("timestamps", timestamps);
path += param("tail", tail);
return requestAndParse(GET,path,101);
}
JSON_DOCUMENT Docker::create_container(JSON_DOCUMENT& parameters){
std::string path = "/containers/create";
return requestAndParseJson(POST,path,201,parameters);
}
JSON_DOCUMENT Docker::start_container(const std::string& container_id){
std::string path = "/containers/" + container_id + "/start";
return requestAndParse(POST,path,204);
}
JSON_DOCUMENT Docker::get_container_changes(const std::string& container_id){
std::string path = "/containers/" + container_id + "/changes";
return requestAndParseJson(GET,path);
}
JSON_DOCUMENT Docker::stop_container(const std::string& container_id, int delay){
std::string path = "/containers/" + container_id + "/stop?";
path += param("t", delay);
return requestAndParse(POST,path,204);
}
JSON_DOCUMENT Docker::kill_container(const std::string& container_id, int signal){
std::string path = "/containers/" + container_id + "/kill?";
path += param("signal", signal);
return requestAndParse(POST,path,204);
}
JSON_DOCUMENT Docker::pause_container(const std::string& container_id){
std::string path = "/containers/" + container_id + "/pause";
return requestAndParse(POST,path,204);
}
JSON_DOCUMENT Docker::wait_container(const std::string& container_id){
std::string path = "/containers/" + container_id + "/wait";
return requestAndParseJson(POST,path);
}
JSON_DOCUMENT Docker::delete_container(const std::string& container_id, bool v, bool force){
std::string path = "/containers/" + container_id + "?";
path += param("v", v);
path += param("force", force);
return requestAndParse(DELETE,path,204);
}
JSON_DOCUMENT Docker::unpause_container(const std::string& container_id){
std::string path = "/containers/" + container_id + "/unpause?";
return requestAndParse(POST,path,204);
}
JSON_DOCUMENT Docker::restart_container(const std::string& container_id, int delay){
std::string path = "/containers/" + container_id + "/restart?";
path += param("t", delay);
return requestAndParse(POST,path,204);
}
JSON_DOCUMENT Docker::attach_to_container(const std::string& container_id, bool logs, bool stream, bool o_stdin, bool o_stdout, bool o_stderr){
std::string path = "/containers/" + container_id + "/attach?";
path += param("logs", logs);
path += param("stream", stream);
path += param("stdin", o_stdin);
path += param("stdout", o_stdout);
path += param("stderr", o_stderr);
return requestAndParse(POST,path,101);
}
//void Docker::copy_from_container(const std::string& container_id, const std::string& file_path, const std::string& dest_tar_file){}
/*
*
* Private Methods
*
*/
JSON_DOCUMENT Docker::requestAndParse(Method method, const std::string& path, unsigned success_code, JSON_DOCUMENT& param, bool isReturnJson){
std::string readBuffer;
std::string paramString;
std::string method_str;
struct curl_slist *headers = nullptr;
const char *paramChar;
switch(method){
case GET:
method_str = "GET";
break;
case POST:
method_str = "POST";
break;
case DELETE:
method_str = "DELETE";
break;
case PUT:
method_str = "PUT";
break;
default:
method_str = "GET";
}
curl = curl_easy_init();
if(!curl){
std::cout << "error while initiating curl" << std::endl;
curl_global_cleanup();
exit(1);
}
rapidjson::StringBuffer buffer;
buffer.Clear();
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
param.Accept(writer);
paramString = std::string(buffer.GetString());
paramChar = paramString.c_str();
if(isReturnJson)
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
//std::cout << "HOST_PATH : " << (host_uri + path) << std::endl;
if(!is_remote)
curl_easy_setopt(curl, CURLOPT_UNIX_SOCKET_PATH, "/var/run/docker.sock");
curl_easy_setopt(curl, CURLOPT_URL, (host_uri + path).c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method_str.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
if(method == POST){
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, paramChar);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(paramChar));
}
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
unsigned status = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &status);
curl_easy_cleanup(curl);
const char* buf = readBuffer.c_str();
JSON_DOCUMENT doc(rapidjson::kObjectType);
if(status == success_code || status == 200){
doc.AddMember("success", true, doc.GetAllocator());
JSON_VALUE dataString;
dataString.SetString(readBuffer.c_str(), doc.GetAllocator());
doc.AddMember("data", dataString, doc.GetAllocator());
}else{
JSON_DOCUMENT resp(&doc.GetAllocator());
resp.Parse(buf);
doc.AddMember("success", false, doc.GetAllocator());
doc.AddMember("code", status, doc.GetAllocator());
doc.AddMember("data", resp, doc.GetAllocator());
}
return doc;
}
JSON_DOCUMENT Docker::requestAndParseJson(Method method, const std::string& path, unsigned success_code, JSON_DOCUMENT& param){
auto result_obj = requestAndParse(method,path,success_code,param,true);
bool result = (result_obj.HasMember("success") && result_obj["success"].IsBool() && result_obj["success"].GetBool());
if(result){
JSON_DOCUMENT doc(rapidjson::kObjectType);
JSON_DOCUMENT data(&doc.GetAllocator());
data.Parse(result_obj["data"].GetString());
doc.AddMember("success", true, doc.GetAllocator());
doc.AddMember("data", data, doc.GetAllocator());
return doc;
}else{
return result_obj;
}
}
/*
*
* END Docker Implementation
*
*/
std::string param( const std::string& param_name, const std::string& param_value){
if(!param_value.empty()){
return "&" + param_name + "=" + param_value;
}
else{
return "";
}
}
std::string param( const std::string& param_name, const char* param_value){
if(param_value != nullptr){
return "&" + param_name + "=" + param_value;
}
else{
return "";
}
}
std::string param( const std::string& param_name, bool param_value){
std::string ret;
ret = "&" + param_name + "=";
if(param_value){
return ret + "true";
}
else{
return ret + "false";
}
}
std::string param( const std::string& param_name, int param_value){
if(param_value != -1){
std::ostringstream convert;
convert << param_value;
return "&" + param_name + "=" + convert.str();
}
else{
return "";
}
}
std::string param( const std::string& param_name, JSON_DOCUMENT& param_value){
if(param_value.IsObject()){
std::string paramString;
rapidjson::StringBuffer buffer;
buffer.Clear();
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
param_value.Accept(writer);
paramString = std::string(buffer.GetString());
return "&" + param_name + "=" + paramString;
}
else{
return "";
}
}
std::string jsonToString(JSON_VALUE & doc){
rapidjson::StringBuffer buffer;
buffer.Clear();
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
return std::string(buffer.GetString());
}