-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDeployRunner.cpp
More file actions
405 lines (324 loc) · 8.8 KB
/
Copy pathDeployRunner.cpp
File metadata and controls
405 lines (324 loc) · 8.8 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
*
* @file DeployRunner.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*/
#include <vix/cli/commands/deploy/DeployRunner.hpp>
#include <vix/cli/commands/deploy/DeployOutput.hpp>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#if defined(_WIN32)
#define VIX_CLI_POPEN _popen
#define VIX_CLI_PCLOSE _pclose
#else
#define VIX_CLI_POPEN popen
#define VIX_CLI_PCLOSE pclose
#endif
namespace vix::commands::deploy::runner
{
namespace
{
std::string shell_quote(const std::string &value)
{
std::string out = "'";
for (char c : value)
{
if (c == '\'')
out += "'\\''";
else
out += c;
}
out += "'";
return out;
}
bool run_cmd(
const std::string &cmd,
const DeployOptions &options)
{
output::command(std::cout, cmd);
if (options.dryRun)
return true;
return std::system(cmd.c_str()) == 0;
}
std::optional<std::string> run_capture(const std::string &cmd)
{
std::array<char, 2048> buffer{};
std::string output;
using PipeCloser = int (*)(FILE *);
std::unique_ptr<FILE, PipeCloser> pipe(
VIX_CLI_POPEN(cmd.c_str(), "r"),
VIX_CLI_PCLOSE);
if (!pipe)
return std::nullopt;
while (std::fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get()) != nullptr)
output += buffer.data();
while (!output.empty() &&
(output.back() == '\n' ||
output.back() == '\r' ||
output.back() == ' ' ||
output.back() == '\t'))
{
output.pop_back();
}
if (output.empty())
return std::nullopt;
return output;
}
struct DeployState
{
std::string previousGitRef{};
};
bool rollback_deploy(
const DeployConfig &cfg,
const DeployOptions &options,
const DeployState &state)
{
if (!cfg.rollback)
return true;
if (state.previousGitRef.empty())
{
output::warn(std::cerr, "rollback skipped: no previous git revision captured");
return false;
}
output::step(std::cout, "Rollback");
const std::string resetCmd =
"git reset --hard " +
shell_quote(state.previousGitRef);
if (!run_cmd(resetCmd, options))
return false;
if (!run_cmd(cfg.buildCommand, options))
return false;
if (!run_cmd("vix service restart", options))
return false;
if (!run_cmd("vix service status", options))
return false;
output::ok(std::cout, "rollback completed");
return true;
}
int fail_with_rollback(
const DeployConfig &cfg,
const DeployOptions &options,
const DeployState &state,
const std::string &message,
const std::string &fixMessage = {})
{
output::error(std::cerr, message);
if (!fixMessage.empty())
output::fix(std::cerr, fixMessage);
if (cfg.rollback)
{
if (!rollback_deploy(cfg, options, state))
{
output::error(std::cerr, "rollback failed");
output::fix(std::cerr, "inspect the repository and service manually");
}
}
if (cfg.logsOnFailure)
{
const std::string logsCmd =
"vix logs errors --repeated -n " +
std::to_string(cfg.logLines);
output::step(std::cout, "Failure Logs");
output::command(std::cout, logsCmd);
if (!options.dryRun)
{
const int logsExitCode = std::system(logsCmd.c_str());
(void)logsExitCode;
}
}
return 1;
}
bool pull_latest(
const DeployConfig &cfg,
const DeployOptions &options)
{
if (!cfg.pull)
return true;
output::step(std::cout, "Pull");
const std::string cmd =
"git pull origin " + shell_quote(cfg.branch);
return run_cmd(cmd, options);
}
bool build_app(
const DeployConfig &cfg,
const DeployOptions &options)
{
output::step(std::cout, "Build");
return run_cmd(cfg.buildCommand, options);
}
bool run_tests(
const DeployConfig &cfg,
const DeployOptions &options)
{
if (!cfg.tests)
return true;
output::step(std::cout, "Tests");
return run_cmd(cfg.testCommand, options);
}
bool restart_service(
const DeployConfig &cfg,
const DeployOptions &options)
{
if (cfg.serviceName.empty())
return false;
output::step(std::cout, "Restart Service");
return run_cmd("vix service restart", options);
}
bool verify_service(
const DeployConfig &cfg,
const DeployOptions &options)
{
if (cfg.serviceName.empty())
return false;
output::step(std::cout, "Service Status");
return run_cmd("vix service status", options);
}
bool health_check(
const DeployConfig &cfg,
const DeployOptions &options)
{
if (!cfg.healthLocal && !cfg.healthPublic)
return true;
output::step(std::cout, "Health Check");
return run_cmd("vix health", options);
}
bool proxy_check(
const DeployConfig &cfg,
const DeployOptions &options)
{
if (!cfg.proxyCheck && !cfg.proxyReload)
return true;
output::step(std::cout, "Proxy Check");
return run_cmd("vix proxy nginx check", options);
}
bool proxy_reload(
const DeployConfig &cfg,
const DeployOptions &options)
{
if (!cfg.proxyReload)
return true;
output::step(std::cout, "Proxy Reload");
return run_cmd("vix proxy nginx reload", options);
}
}
int run(
const DeployConfig &cfg,
const DeployOptions &options)
{
output::print_summary(std::cout, cfg, options);
DeployState state;
if (cfg.rollback && !options.dryRun)
{
const auto ref = run_capture("git rev-parse HEAD 2>/dev/null");
if (ref)
state.previousGitRef = *ref;
else
output::warn(std::cerr, "rollback enabled but current git revision could not be captured");
}
if (cfg.serviceName.empty())
{
output::error(std::cerr, "Missing deployment service name.");
output::fix(std::cerr, "add production.deploy.service to vix.json");
return 1;
}
if (!pull_latest(cfg, options))
{
return fail_with_rollback(
cfg,
options,
state,
"git pull failed",
"check repository state and branch");
}
if (cfg.pull)
output::ok(std::cout, "git pull completed");
if (!build_app(cfg, options))
{
return fail_with_rollback(
cfg,
options,
state,
"build failed",
"run the configured build command manually");
}
output::ok(std::cout, "build completed");
if (!run_tests(cfg, options))
{
return fail_with_rollback(
cfg,
options,
state,
"tests failed",
"run the configured test command manually");
}
if (cfg.tests)
output::ok(std::cout, "tests completed");
if (!restart_service(cfg, options))
{
return fail_with_rollback(
cfg,
options,
state,
"service restart failed",
"run `vix service restart`");
}
output::ok(std::cout, "service restarted");
if (!verify_service(cfg, options))
{
return fail_with_rollback(
cfg,
options,
state,
"service is not active",
"run `vix service status`");
}
output::ok(std::cout, "service is active");
if (!health_check(cfg, options))
{
return fail_with_rollback(
cfg,
options,
state,
"health check failed",
"run `vix health`");
}
if (cfg.healthLocal || cfg.healthPublic)
output::ok(std::cout, "health checks passed");
if (!proxy_check(cfg, options))
{
return fail_with_rollback(
cfg,
options,
state,
"proxy check failed",
"run `vix proxy nginx check`");
}
if (cfg.proxyCheck || cfg.proxyReload)
output::ok(std::cout, "proxy config is valid");
if (!proxy_reload(cfg, options))
{
return fail_with_rollback(
cfg,
options,
state,
"proxy reload failed",
"run `vix proxy nginx reload`");
}
if (cfg.proxyReload)
output::ok(std::cout, "proxy reloaded");
output::ok(std::cout, "deployment completed");
return 0;
}
}