Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
185 lines (168 loc) · 6.09 KB
/
Copy pathmain.cpp
File metadata and controls
185 lines (168 loc) · 6.09 KB
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
#include "runtime.hpp"
#include "rootfs.hpp"
#include "oci.hpp"
#include "daemon.hpp"
#include "ipc.hpp"
#include "util.hpp"
#include <cstdlib>
#include <iostream>
#include <sstream>
using namespace minict;
static void usage() {
std::cout
<< "minict run [--memory 64m] [--cpu 50] [--name demo] [--image name] command\n"
<< "minict ps | kill NAME | stats | daemon | load-oci PATH NAME | pull-rootfs TARBALL NAME\n";
}
static bool ipc_ok(const std::string& resp, std::string& detail, std::string& body) {
if (resp.empty()) return false;
bool ok = json_get_bool(resp, "ok", false);
detail = json_get_string(resp, "detail");
body = json_get_string(resp, "body");
return ok;
}
static bool try_ipc(const std::string& req, std::string& detail, std::string& body) {
if (!socket_exists()) return false;
std::string resp = ipc_call(req);
if (resp.empty()) return false;
bool ok = ipc_ok(resp, detail, body);
return ok;
}
static std::string run_ipc_json(const Config& c) {
std::ostringstream o;
o << "{\"op\":\"run\",\"command\":\"" << json_escape(c.command) << "\"";
if (!c.name.empty()) o << ",\"name\":\"" << json_escape(c.name) << "\"";
if (!c.limits.memory.empty()) o << ",\"memory\":\"" << json_escape(c.limits.memory) << "\"";
if (c.limits.cpu > 0) o << ",\"cpu\":" << c.limits.cpu;
if (!c.rootfs.empty()) o << ",\"image\":\"" << json_escape(c.rootfs) << "\"";
o << "}";
return o.str();
}
int main(int argc, char** argv) {
if (argc < 2) {
usage();
return 1;
}
std::string op = argv[1];
if (op == "daemon") {
return run_daemon();
}
if (op == "ps") {
std::string detail, body;
if (try_ipc("{\"op\":\"ps\"}", detail, body)) {
std::cout << body;
return 0;
}
std::vector<Container> all = list_containers();
std::cout << "NAME\tSTATE\tMEMORY\tCPU\tLATENCY_MS\tCOMMAND\n";
for (size_t i = 0; i < all.size(); ++i) {
const Container& c = all[i];
std::cout << c.name << "\t" << c.state << "\t" << c.memory << "\t"
<< c.cpu << "\t" << c.latency_ms << "\t" << c.command << "\n";
}
return 0;
}
if (op == "stats") {
std::string detail, body;
if (try_ipc("{\"op\":\"stats\"}", detail, body)) {
std::cout << body << "\n";
return 0;
}
std::cout << status_json() << "\n";
return 0;
}
if (op == "kill" && argc == 3) {
std::string req = std::string("{\"op\":\"kill\",\"name\":\"") + json_escape(argv[2]) + "\"}";
std::string detail, body;
if (socket_exists()) {
std::string resp = ipc_call(req);
if (!resp.empty()) {
bool ok = ipc_ok(resp, detail, body);
std::cout << (ok ? "stopped\n" : "not found\n");
return ok ? 0 : 1;
}
}
bool ok = kill_container(argv[2]);
std::cout << (ok ? "stopped\n" : "not found\n");
return ok ? 0 : 1;
}
if (op == "pull-rootfs" && argc == 4) {
std::ostringstream req;
req << "{\"op\":\"pull-rootfs\",\"tarball\":\"" << json_escape(argv[2])
<< "\",\"name\":\"" << json_escape(argv[3]) << "\"}";
std::string detail, body;
if (socket_exists()) {
std::string resp = ipc_call(req.str());
if (!resp.empty()) {
bool ok = ipc_ok(resp, detail, body);
if (!body.empty()) std::cout << body << "\n";
else std::cout << detail << "\n";
return ok ? 0 : 1;
}
}
RootfsResult x = extract_rootfs(argv[2], argv[3], simulation_enabled());
std::cout << x.detail << ": " << x.path << "\n";
return x.ok ? 0 : 1;
}
if (op == "load-oci" && argc == 4) {
std::ostringstream req;
req << "{\"op\":\"load-oci\",\"path\":\"" << json_escape(argv[2])
<< "\",\"name\":\"" << json_escape(argv[3]) << "\"}";
std::string detail, body;
if (socket_exists()) {
std::string resp = ipc_call(req.str());
if (!resp.empty()) {
bool ok = ipc_ok(resp, detail, body);
if (!body.empty()) std::cout << body << "\n";
else std::cout << detail << "\n";
return ok ? 0 : 1;
}
}
OciResult x = load_oci(argv[2], argv[3], simulation_enabled());
std::cout << x.detail << ": " << x.path << "\n";
return x.ok ? 0 : 1;
}
if (op == "run") {
Config c;
int i = 2;
for (; i < argc; ++i) {
std::string v = argv[i];
if (v == "--memory" && i + 1 < argc) c.limits.memory = argv[++i];
else if (v == "--cpu" && i + 1 < argc) c.limits.cpu = std::atoi(argv[++i]);
else if (v == "--name" && i + 1 < argc) c.name = argv[++i];
else if (v == "--image" && i + 1 < argc) c.rootfs = argv[++i];
else break;
}
for (; i < argc; ++i) {
if (!c.command.empty()) c.command += " ";
c.command += argv[i];
}
if (c.command.empty()) {
std::cerr << "error: missing command\n";
return 1;
}
if (socket_exists()) {
std::string detail, body;
std::string resp = ipc_call(run_ipc_json(c));
if (!resp.empty() && ipc_ok(resp, detail, body)) {
std::cout << body << "\n";
return 0;
}
if (!resp.empty()) {
std::cout << "error: " << detail << "\n";
return 1;
}
}
// in-process mode: stay attached to the container (docker run semantics)
c.wait_exit = true;
RunResult x = run_container(c);
if (x.ok) {
std::cout << x.container.name << ": " << x.detail
<< " (start latency " << x.container.latency_ms << "ms)\n";
return 0;
}
std::cout << "error: " << x.detail << "\n";
return 1;
}
usage();
return 1;
}