-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
116 lines (90 loc) · 2.95 KB
/
server.js
File metadata and controls
116 lines (90 loc) · 2.95 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
import http from "http";
import { execFile } from "child_process";
import path from "path";
import fs from "fs";
const TOKEN = process.env.DEPLOY_TOKEN;
const PORT = 4040;
if (!TOKEN) {
console.error("DEPLOY_TOKEN is not set. Exiting.");
process.exit(1);
}
const running = new Set();
/**
* Simple structured logger
*/
function log(repo, message, meta = "") {
const time = new Date().toISOString();
if (meta && typeof meta === "object") {
console.log(`[${time}] [${repo}] ${message}`, JSON.stringify(meta));
} else {
console.log(`[${time}] [${repo}] ${message}`, meta);
}
}
const server = http.createServer((req, res) => {
log("system", `Incoming request ${req.method} ${req.url}`);
if (req.method !== "POST" || req.url !== "/deploy") {
res.writeHead(404).end();
return;
}
if (req.headers["x-deploy-token"] !== TOKEN) {
log("system", "Unauthorized request rejected");
res.writeHead(401).end(JSON.stringify({ ok: false, error: "Unauthorized" }));
return;
}
let body = "";
req.on("data", (chunk) => (body += chunk));
req.on("end", () => {
let payload;
try {
payload = JSON.parse(body);
} catch (err) {
log("system", "Invalid JSON received", body);
res.writeHead(400).end(JSON.stringify({ ok: false, error: "Invalid JSON" }));
return;
}
const { repo, image, deliveryId } = payload;
if (!repo || !image || !deliveryId) {
log(repo || "system", "Missing required fields", payload);
res.writeHead(400).end(JSON.stringify({ ok: false, error: "Missing fields" }));
return;
}
const script = `/home/debian/apps/${repo}/deploy.sh`;
if (!fs.existsSync(script)) {
log(repo, `Deploy script not found at ${script}`);
res.writeHead(404).end(JSON.stringify({ ok: false, error: "Deploy script not found" }));
return;
}
if (running.has(repo)) {
log(repo, "Deploy skipped (already running)", { deliveryId });
res.writeHead(409).end(JSON.stringify({ ok: false, error: "Deploy already in progress" }));
return;
}
running.add(repo);
log(repo, "Deploy accepted", { image, deliveryId, script });
res.writeHead(200).end(JSON.stringify({ ok: true }));
const child = execFile(script, [image, deliveryId], {
cwd: path.dirname(script),
});
child.stdout.on("data", (data) => {
process.stdout.write(`[${repo}] ${data}`);
});
child.stderr.on("data", (data) => {
process.stderr.write(`[${repo}] ${data}`);
});
child.on("exit", (code) => {
running.delete(repo);
if (code === 0) {
log(repo, "Deploy finished successfully", { deliveryId });
} else {
log(repo, "Deploy failed", { code, deliveryId });
}
});
child.on("error", (err) => {
running.delete(repo);
log(repo, "Failed to start deploy process", err.message);
});
});
});
server.listen(PORT, "0.0.0.0", () => {
console.log(`Deploy server listening on port ${PORT}`);
});