-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvxnode_client.cpp
More file actions
294 lines (244 loc) · 10.6 KB
/
Copy pathvxnode_client.cpp
File metadata and controls
294 lines (244 loc) · 10.6 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
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
#include "vxnode/vxnode_client.hpp"
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <thread>
#include <nlohmann/json.hpp>
#include <spdlog/spdlog.h>
#include "vxnode/http_client.hpp"
using json = nlohmann::json;
namespace prodxcloud::vxnode {
namespace {
std::string env_or(const char* key, const std::string& fallback) {
const char* v = std::getenv(key);
return (v && *v) ? std::string(v) : fallback;
}
bool env_flag(const char* key) {
const std::string v = env_or(key, "");
return v == "1" || v == "true" || v == "yes";
}
/// Strip a trailing '/' so base_url + path never yields a double slash.
std::string normalise_base(std::string url) {
while (!url.empty() && url.back() == '/') url.pop_back();
return url;
}
} // namespace
NodeConfig NodeConfig::from_env() {
NodeConfig c;
c.base_url = normalise_base(env_or("VXNODE_URL", "http://127.0.0.1:8744"));
c.api_key = env_or("VXNODE_API_KEY", "");
c.node_id = env_or("VXNODE_ID", "default");
c.tenant_id = env_or("VXNODE_TENANT", "");
c.dry_run = env_flag("VXNODE_DRY_RUN");
if (const std::string t = env_or("VXNODE_TIMEOUT_MS", ""); !t.empty()) {
try {
c.timeout = std::chrono::milliseconds(std::stoi(t));
} catch (const std::exception&) {
spdlog::warn("VXNODE_TIMEOUT_MS is not a number, keeping the default");
}
}
return c;
}
// ─── Payloads ───────────────────────────────────────────────────────────────
std::string ProvisionRequest::to_json() const {
json j;
j["provider"] = provider;
j["region"] = region;
j["instance_type"] = instance_type;
j["image"] = image;
j["name"] = name;
j["count"] = count;
j["dry_run"] = dry_run;
if (!ssh_key_name.empty()) j["ssh_key_name"] = ssh_key_name;
if (!purpose.empty()) j["purpose"] = purpose;
// The node tags everything it creates with its requester, so a runaway robot
// is traceable to the exact plan that asked for the instance.
j["requested_by"] = "prodxcloud-robot";
return j.dump();
}
std::string DeployRequest::to_json() const {
json j;
j["host"] = host;
j["repo_url"] = repo_url;
j["branch"] = branch;
if (!domain.empty()) j["domain"] = domain;
if (port > 0) j["port"] = port;
auto env = json::parse(env_json, nullptr, false);
j["env"] = env.is_discarded() ? json::object() : env;
j["requested_by"] = "prodxcloud-robot";
return j.dump();
}
std::string VmAction::to_json() const {
json j;
j["instance_id"] = instance_id;
j["action"] = action;
return j.dump();
}
// ─── Client ─────────────────────────────────────────────────────────────────
VxNodeClient::VxNodeClient(NodeConfig config) : config_(std::move(config)) {
config_.base_url = normalise_base(config_.base_url);
}
const std::vector<std::string>& VxNodeClient::supported_stacks() {
// Mirrors the /api/v2/infrastructure/services/<stack>/deploy routes vxnode exposes.
static const std::vector<std::string> kStacks = {
"nodejs", "python", "fastapi", "django", "flask", "golang",
"rust", "java", "springboot", "php", "laravel", "nextjs",
"reactjs", "angular", "streamlit", "staticwebsite", "cpp",
"expo",
};
return kStacks;
}
Result<std::string> VxNodeClient::post(const std::string& path, const std::string& body) const {
const std::string url = config_.base_url + path;
if (config_.dry_run) {
// Dry-run returns the exact request that *would* have gone to the node.
// A plan you can read before it touches infrastructure is worth more than
// one you can only audit afterwards.
json j;
j["dry_run"] = true;
j["method"] = "POST";
j["url"] = url;
j["node_id"] = config_.node_id;
j["payload"] = json::parse(body, nullptr, false);
spdlog::info("vxnode dry-run: POST {}", url);
return j.dump(2);
}
HttpRequest req;
req.method = "POST";
req.url = url;
req.body = body;
req.timeout = config_.timeout;
req.headers["Content-Type"] = "application/json";
if (!config_.api_key.empty()) req.headers["X-API-Key"] = config_.api_key;
if (!config_.tenant_id.empty()) req.headers["X-Tenant-ID"] = config_.tenant_id;
Error last{503, "vxnode unreachable", ""};
for (int attempt = 1; attempt <= std::max(1, config_.max_retries); ++attempt) {
const auto res = http_request(req);
if (res) {
if (res->ok()) return res->body;
// 4xx is the node telling us the request is wrong. Retrying an
// argument error just makes the same mistake more times.
if (res->status >= 400 && res->status < 500) {
return std::unexpected(Error{res->status,
"vxnode rejected the request (HTTP " +
std::to_string(res->status) + ")",
res->body});
}
last = Error{res->status,
"vxnode returned HTTP " + std::to_string(res->status),
res->body};
} else {
last = res.error();
}
if (attempt < config_.max_retries) {
const auto backoff = std::chrono::milliseconds(200 * (1 << (attempt - 1)));
spdlog::warn("vxnode POST {} failed ({}), retry {}/{} in {} ms",
path, last.message, attempt, config_.max_retries, backoff.count());
std::this_thread::sleep_for(backoff);
}
}
return std::unexpected(last);
}
Result<NodeHealth> VxNodeClient::health() const {
HttpRequest req;
req.method = "GET";
req.url = config_.base_url + "/api/v2/health";
req.timeout = config_.timeout;
if (!config_.api_key.empty()) req.headers["X-API-Key"] = config_.api_key;
const auto start = std::chrono::steady_clock::now();
const auto res = http_request(req);
const auto ms = std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - start).count();
if (!res) return std::unexpected(res.error());
NodeHealth h;
h.reachable = res->ok();
h.raw_response = res->body;
h.latency_ms = ms;
const auto j = json::parse(res->body, nullptr, false);
if (!j.is_discarded() && j.is_object()) {
h.status = j.value("status", res->ok() ? "ok" : "error");
h.version = j.value("version", "");
} else {
h.status = res->ok() ? "ok" : "error";
}
return h;
}
Result<ProvisionResult> VxNodeClient::provision_vm(const ProvisionRequest& req) const {
if (req.provider.empty()) {
return std::unexpected(Error::bad_request("provision: provider is required"));
}
if (req.count < 1 || req.count > 50) {
return std::unexpected(Error::validation("provision: count must be in [1, 50]"));
}
spdlog::info("delegating provisioning to vxnode '{}': {} x {} on {}",
config_.node_id, req.count, req.instance_type, req.provider);
ProvisionRequest r = req;
r.dry_run = r.dry_run || config_.dry_run;
const auto body = post("/api/v2/provision/vm", r.to_json());
if (!body) return std::unexpected(body.error());
ProvisionResult out;
out.raw_response = *body;
out.node_id = config_.node_id;
out.accepted = true;
const auto j = json::parse(*body, nullptr, false);
if (!j.is_discarded() && j.is_object()) {
out.request_id = j.value("request_id", j.value("id", ""));
out.status = j.value("status", "accepted");
out.message = j.value("message", "");
// The node has returned instance ids under a few different keys across
// versions; accept any of them rather than silently reporting none.
for (const char* key : {"instance_ids", "instances", "ids"}) {
if (!j.contains(key)) continue;
const auto& node = j[key];
if (!node.is_array()) continue;
for (const auto& item : node) {
if (item.is_string()) {
out.instance_ids.push_back(item.get<std::string>());
} else if (item.is_object()) {
if (item.contains("id")) out.instance_ids.push_back(item["id"].get<std::string>());
else if (item.contains("instance_id"))
out.instance_ids.push_back(item["instance_id"].get<std::string>());
}
}
break;
}
} else {
out.status = "accepted";
}
return out;
}
Result<std::string> VxNodeClient::vm_status(const std::string& instance_id) const {
if (instance_id.empty()) {
return std::unexpected(Error::bad_request("vm_status: instance_id is required"));
}
json j;
j["instance_id"] = instance_id;
return post("/api/v2/provision/vm/status", j.dump());
}
Result<std::string> VxNodeClient::vm_action(const VmAction& action) const {
static const std::vector<std::string> kActions = {"start", "stop", "restart", "status",
"terminate"};
if (std::find(kActions.begin(), kActions.end(), action.action) == kActions.end()) {
return std::unexpected(Error::validation(
"vm_action: '" + action.action +
"' is not one of start|stop|restart|status|terminate"));
}
if (action.instance_id.empty()) {
return std::unexpected(Error::bad_request("vm_action: instance_id is required"));
}
spdlog::info("vxnode action '{}' on instance {}", action.action, action.instance_id);
return post("/api/v2/provision/vm/action", action.to_json());
}
Result<std::string> VxNodeClient::deploy(const DeployRequest& req) const {
const auto& stacks = supported_stacks();
if (std::find(stacks.begin(), stacks.end(), req.stack) == stacks.end()) {
return std::unexpected(Error::validation(
"deploy: vxnode has no '" + req.stack + "' stack endpoint"));
}
if (req.host.empty()) {
return std::unexpected(Error::bad_request("deploy: host is required"));
}
spdlog::info("delegating '{}' deploy to vxnode '{}' -> {}", req.stack, config_.node_id, req.host);
return post("/api/v2/infrastructure/services/" + req.stack + "/deploy", req.to_json());
}
} // namespace prodxcloud::vxnode