-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlogs.js
More file actions
executable file
·143 lines (127 loc) · 3.54 KB
/
logs.js
File metadata and controls
executable file
·143 lines (127 loc) · 3.54 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
#!/usr/bin/env node
//
// Logs.js
// Connect to the running containers and pipe their logs to the console or a file.
// --toFile [filepath]
//
require("dotenv").config();
const async = require("async");
const os = require("os");
const shell = require("shelljs");
const { spawn } = require("child_process");
const fs = require("fs");
const path = require("path");
// These functions are overwriten if we choose to log to file
let log = (...args) => console.log(...args);
let logError = (...args) => console.error(...args);
let end = (signal) => closeDown(signal);
if (process.argv[2] == "--toFile") {
const filepath = process.argv[3] ?? "logs/dockerLogs.txt";
const directory = path.dirname(filepath);
fs.mkdirSync(directory, { recursive: true });
const stream = fs.createWriteStream(filepath);
end = (signal) => {
stream.end();
closeDown(signal);
};
log = (text) => {
stream.write(text, (err) => {
if (err) {
console.log(err);
}
});
};
logError = (text) => log(text);
}
var stdout = null;
if (process.env.PLATFORM === "podman") {
stdout = shell.exec(
`podman ps | awk '/${process.env.STACKNAME}/ {print $1}'`
);
} else if (os.platform() == "win32") {
// windows method of gathering the service names:
stdout = shell
.exec(
`for /f "tokens=2" %a in ('docker service ls ^| findstr "${process.env.STACKNAME}_" ') do @echo %a`
)
.stdout.replace(/\r/g, "");
} else {
// common unix method of gathering the service names:
stdout = shell.exec(
`docker service ls | grep "${process.env.STACKNAME}_" | awk '{ print $2 }'`
).stdout;
}
var allServiceIDs = stdout.split("\n");
var allServices = {};
var maxIDLength = -10;
var closeDown = (signal) => {
// our process exit handler
// be sure to kill all our sub processes
allServiceIDs.forEach((id) => {
if (allServices[id]) {
log(`closing ${id} with ${signal}`);
allServices[id].kill(signal);
}
});
};
function pad(text, length) {
while (text.length < length) {
text += " ";
}
return text;
}
/**
* @param {string} id
* @param {string} text
*/
function cleanText(id, text) {
var lines = text.split("\n");
var output = [];
lines.forEach((line) => {
if (line.length > 0) {
var parts = line.split("|");
if (parts.length > 1) {
parts.shift();
}
output.push(`${pad(id, maxIDLength)} : ${parts.join("|")}`);
}
});
return output.join("\n");
}
async.eachSeries(
allServiceIDs,
(id, cb) => {
if (id == "") {
cb();
return;
}
// create a new process for logging the given service id
const command = process.env.PLATFORM === "podman" ? "podman" : "docker";
const options = ["service", "logs", "-f", "--tail", "50", id]; // `docker service logs -f ${id}`;
if (command === "podman") options.shift(); // `podman logs -f ${id}`
var logger = spawn(command, options, {
// stdio: ["ignore", "ignore", "ignore"]
});
logger.stdout.on("data", (data) => {
log(cleanText(id, data.toString()));
});
logger.stderr.on("data", (data) => {
logError(cleanText(id, data.toString()));
});
if (id.length > maxIDLength) {
maxIDLength = id.length;
}
allServices[id] = logger;
cb();
},
(err) => {
if (err) {
logError(err);
log();
end("SIGINT");
process.exit();
}
}
);
process.on("SIGINT", end);
process.on("SIGTERM", end);